oblinger

Bank

Jun 3rd, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. require "iuplua"
  2.  
  3. bankAccounts = {}
  4. bankAccounts.__index = bankAccounts
  5. accounts = {}
  6. try = 0
  7.  
  8. function loadAccounts()
  9.     file = io.open("accounts/dir.txt","r")
  10.     for i in file:lines() do
  11.         table.insert(accounts,i)
  12.     end
  13.  
  14.     for i = 1,#accounts do
  15.         file = io.open("accounts/"..accounts[i],"r")
  16.         for i in file:lines() do
  17.             if try == 0 then
  18.                 name = i
  19.                 try = try + 1
  20.             elseif try == 1 then
  21.                 code = i
  22.                 try = try + 1
  23.             elseif try == 2 then
  24.                 balance = i
  25.             end
  26.         end
  27.         accounts[name] = createAccount( name, balance, code )
  28.     end
  29. end
  30.  
  31. function createAccount( name, balance, code )
  32.     account = {}
  33.     setmetatable(account,bankAccounts)
  34.     account.name = name
  35.     account.balance = balance
  36.     account.code = code
  37.     updateUserInfo( name, balance,code )
  38.     return account
  39. end
  40.  
  41. function updateUserInfo( name,balance,code )
  42.     file = io.open("accounts/"..name,"w")
  43.     file:write(name)
  44.     file:write("\n"..code)
  45.     file:write("\n"..balance)
  46.     file:close()
  47. end
  48.  
  49. function bankAccounts:withdrawFunds( code, amount )
  50.     if self.code ~= nil then
  51.         if code == self.code then
  52.             if tonumber(self.balance) >= tonumber(amount) then
  53.                 self.balance = tonumber(self.balance) - amount
  54.                 updateUserInfo( self.name , self.balance, code)
  55.                 transaction = self.balance
  56.             else
  57.                 transaction = "Withdraw To Large", self.balance
  58.             end
  59.         else
  60.             transaction = "Invalid Code"
  61.         end
  62.     else
  63.         transaction = "No Such Account"
  64.     end
  65.     return transaction
  66. end
  67.  
  68. function bankAccounts:addFunds( code, amount )
  69.     if self.code ~= nil then
  70.         if code == self.code then
  71.             self.balance = tonumber(self.balance) + amount
  72.             updateUserInfo( self.name, self.balance, code)
  73.             transaction = self.balance
  74.         else
  75.             transaction = "Invalid Code"
  76.         end
  77.     else
  78.         transaction = "No Such Account"
  79.     end
  80.     return transaction
  81. end
  82.  
  83. function bankAccounts:deleteAccount()
  84.     os.remove("accounts/"..self.name..".txt")
  85.     self.name = nil
  86.     self.balance = nil
  87.     self.code = nil
  88.     return true
  89. end
  90.  
  91. function loadInterface()
  92.  
  93.     --Elements
  94.  
  95.     withdraw = iup.button {
  96.         title = "Withdraw Funds",
  97.         padding = "25x5",
  98.         action = function() loadWithdraw() window:hide() end
  99.     }
  100.  
  101.     create = iup.button {
  102.         title = "Create Account",
  103.         padding = "27x5"
  104.     }
  105.  
  106.     add = iup.button {
  107.         title = "Add Funds",
  108.         padding = "38x5"
  109.     }
  110.  
  111.     delete = iup.button {
  112.         title = "Delete Account",
  113.         padding = "26x5"
  114.     }
  115.  
  116.     --Dialog
  117.  
  118.     window = iup.dialog {
  119.         iup.vbox {
  120.             iup.hbox {
  121.                 iup.vbox {
  122.                     iup.hbox {
  123.                         withdraw,
  124.                         add,
  125.                     },
  126.  
  127.                     iup.hbox {
  128.                         create,
  129.                         delete,
  130.                     },
  131.                 },
  132.             },
  133.         },
  134.         title = "Banker",
  135.         size = "200x55"
  136.     }
  137.  
  138.  
  139.     window:show()
  140. end
  141.  
  142. function loadWithdraw()
  143.  
  144.     --Elements
  145.  
  146.     title = iup.label {title = "Withdraw Funds", padding = "95x0"}
  147.  
  148.     five = iup.button {
  149.         title = "$5",
  150.         padding = "50x5",
  151.  
  152.     }
  153.  
  154.     ten = iup.button {
  155.         title = "$10",
  156.         padding = "49x5",
  157.     }
  158.  
  159.     twenty = iup.button {
  160.         title = "$25",
  161.         padding = "49x5",
  162.     }
  163.  
  164.     fifty = iup.button {
  165.         title = "$50",
  166.         padding = "49x5",
  167.     }
  168.  
  169.     --Dialog
  170.  
  171.     withdrawWindow = iup.dialog {
  172.         iup.vbox {
  173.             iup.hbox {
  174.                 five,
  175.                 ten
  176.             },
  177.  
  178.             iup.hbox{
  179.                 twenty,
  180.                 fifty
  181.             },
  182.         },
  183.         title = "Banker",
  184.         size = "200x55"
  185.     }
  186.  
  187.  
  188.     withdrawWindow:show()
  189. end
  190.  
  191. loadInterface()
  192.  
  193. if (iup.MainLoopLevel() == 0) then
  194.   iup.MainLoop()
  195. end
Add Comment
Please, Sign In to add comment