ForeverDev

Database / Store Testing

Oct 8th, 2014
276
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.append = 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. }
  40.  
  41. store.register = function(customer) {
  42.     store.inventory[customer] = {}
  43. }
  44.  
  45. store.getItems = function(customer) {
  46.     for (belonging in store.inventory[customer]) {
  47.         console.log(store.inventory[customer][belonging])
  48.     }
  49. }
  50.  
  51. store.purchase = function(customer, buy) {
  52.     if (database.amount.money(customer) >= store.items[buy].price) {
  53.         database.money.withdraw(customer, store.items[buy].price)
  54.         store.inventory[customer][Object.size(store.inventory[customer])] = buy
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment