ForeverDev

Untitled

Oct 9th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object.size = function(object) {
  2.     var size = 0
  3.     for (child in object) {
  4.         if (object.hasOwnProperty(child)) {
  5.             size++
  6.         }
  7.     }
  8.     return size
  9. }
  10.  
  11. var database = {}
  12. var store = {}
  13.  
  14. database.people = {}
  15.  
  16. database.amount = {
  17.     money : function(name) {
  18.         return database.people[name].money || 0
  19.     }
  20. }
  21.  
  22. database.register = function(name, money) {
  23.     database.people[name] = { name : name, money : money }
  24. }
  25.  
  26. database.money = {
  27.     deposit : function(name, amount) {
  28.         database.people[name].money += amount
  29.     },
  30.     withdraw : function(name, amount) {
  31.         database.people[name].money -= amount
  32.     }
  33. }
  34.  
  35. store.inventory = {}
  36.  
  37. store.items = {
  38.     shirt : { price : 50 },
  39.     pants : { price : 35 },
  40.     tie : { price : 20 }
  41. }
  42.  
  43. store.register = function(customer) {
  44.     store.inventory[customer] = {}
  45. }
  46.  
  47. store.getItems = function(customer) {
  48.     var output = ""
  49.     for (belonging in store.inventory[customer]) {
  50.         output = output + store.inventory[customer][belonging] + ", "
  51.     }
  52.     console.log(output)
  53. }
  54.  
  55. store.purchase = function(customer, buy, quantity) {
  56.     for (i = 0; i < quantity; i++) {
  57.         if (database.amount.money(customer) >= store.items[buy].price) {
  58.             database.money.withdraw(customer, store.items[buy].price)
  59.             store.inventory[customer][Object.size(store.inventory[customer]) + 1] = buy || "unknown"
  60.         } else {
  61.             console.log("You can't afford that " + buy + "!")
  62.         }
  63.     }
  64. }
  65.  
  66. database.register("David Wilson", 0)
  67. store.register("David Wilson")
  68.  
  69. database.money.deposit("David Wilson", 150)
  70.  
  71. store.purchase("David Wilson", "shirt", 2)
  72. store.purchase("David Wilson", "tie", 4)
Advertisement
Add Comment
Please, Sign In to add comment