lvs

Bank

lvs
Jan 10th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. -- Bank class
  2. -- notice NO module(package.seecrap)
  3.  
  4. local _M = {}  -- class table, which is returned at the end
  5.  
  6.  
  7. -- properties
  8. _M.gold = 100    -- initial capital
  9. _M.silver = 50
  10.  
  11.  
  12. -- methods
  13. -- notice NO "local" and colons (:)
  14. -- those functions are methods of table _M and they use "self"
  15.  
  16. function _M:withdraw(account, amount)
  17.     self[account] = self[account] - amount
  18.     return amount
  19.     -- this is a faulty bank code, don't use it in real life
  20. end
  21.  
  22. function _M:deposit(account, amount)
  23.     self[account] = self[account] + amount
  24. end
  25.  
  26.  
  27.  
  28. -- you can also use this as an exchange library
  29. -- notice NO "local" and a DOT (.) and no use of "self"
  30.  
  31. function _M.gold2silver (amount)
  32.     return amount * 2
  33. end
  34.  
  35.  
  36. -- when you require this thing you do    bank = require("bank") and _M comes into "bank" variable
  37. return _M
  38.  
  39.  
  40. -- After you require you can use  bank:deposite('silver', bank.gold2silver(bank.gold))  - which will convert all gold into silver inside the bank. Deposite is a method and gold2silver is a simple conversion function (supplimentary).
Add Comment
Please, Sign In to add comment