Created
January 26, 2012 01:58
-
-
Save kevinmeziere/1680441 to your computer and use it in GitHub Desktop.
Homer JSON Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person1 = { | |
firstName: 'Homer', | |
lastName: 'Simpson', | |
cash : 55 | |
}; | |
var shoppingCart = { | |
dougnuts: 12, | |
beer: 24, | |
cigs: 1 | |
}; | |
var storeInventory = { | |
dougnuts: 100, | |
beer: 100, | |
cigs: 100, | |
candy: 100 | |
}; | |
var storePrices = { | |
dougnuts: 0.5, | |
beer: 8, | |
cigs: 12, | |
candy: 1 | |
}; | |
function checkOut(customer, customerShoppingCart){ | |
var total = 0; | |
customer.posessions = {}; | |
if(customerShoppingCart.dougnuts > 0){ | |
total += customerShoppingCart.dougnuts * storePrices.dougnuts; | |
transferGoods(customer, customerShoppingCart, 'dougnuts'); | |
} | |
if(customerShoppingCart.beer > 0){ | |
total += customerShoppingCart.beer * storePrices.beer; | |
transferGoods(customer, customerShoppingCart, 'beer'); | |
} | |
if(customerShoppingCart.cigs > 0){ | |
total += customerShoppingCart.cigs * storePrices.cigs; | |
transferGoods(customer, customerShoppingCart, 'cigs'); | |
} | |
if(customerShoppingCart.candy > 0){ | |
total += customerShoppingCart.candy * storePrices.candy; | |
transferGoods(customer, customerShoppingCart, 'candy'); | |
} | |
customer.cash = customer.cash - total; | |
} | |
function transferGoods(customer, customerShoppingCart, item){ | |
storeInventory[item] = storeInventory[item] - customerShoppingCart[item]; | |
customer.posessions[item] = customerShoppingCart[item]; | |
customerShoppingCart[item] = 0; | |
} | |
console.log("Going to checkout"); | |
checkOut(person1, shoppingCart); | |
console.log("Checked Out"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment