Advertisement
123pandaman

Computer Craft - ATM

Mar 22nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1.  
  2. local adminPass = "ENTER_PASSWORD_HERE"
  3. local currencyName = "CURRENCY_NAME"
  4.  
  5. -- Function splitString writen by user973713 on stackoverflow.com
  6. function splitString(inputstr, sep)
  7.         if sep == nil then
  8.                 sep = "%s"
  9.         end
  10.         local t={} ; i=1
  11.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  12.                 t[i] = str
  13.                 i = i + 1
  14.         end
  15.         return t
  16. end
  17.  
  18. function login(username)
  19.         shell.run("clear")
  20.         print("Welcome to the bank " .. username .. ".")
  21.  
  22.         local file = fs.open(username, "r")
  23.         local dataRaw = file.readLine()
  24.         local data = splitString(dataRaw, " ")
  25.         local money = data[2]
  26.  
  27.         print("Your current account balance is " .. money .. " " .. currencyName .. "(s).")
  28.  
  29.         local command;
  30.         repeat
  31.             term.write(">")
  32.             command = read()
  33.  
  34.             if(command == "transfer") then
  35.                 print("Enter account name to continue:")
  36.                 local transferAccount = read()
  37.                 local transferAccountFile = fs.open(transferAccount, "r")
  38.                 if(transferAccountFile == nil) then
  39.                     print("Specified user does not exist.")
  40.                 else
  41.                     local transferAccountDataRaw = transferAccountFile.readLine()
  42.                     local transferAccountData = splitString(transferAccountDataRaw, " ")
  43.  
  44.                     if(transferAccountFile == nil) then
  45.                         print("No account exists with this account name.")
  46.                     else
  47.                         print("How much money would you like to transfer?")
  48.                         local transferAmmount = tonumber(read())
  49.                         if(transferAmmount == nil) then
  50.                             print("That is not a valid amount.")                       
  51.                         else
  52.                             local accountFile = fs.open(username, "r")
  53.                             local accountDataRaw = accountFile.readLine()
  54.                             local accountData = splitString(accountDataRaw, " ")
  55.                             local accountMoney = tonumber(data[2])
  56.                             if(transferAmmount < 0) then
  57.                                 print("You can not transfer a negative amount of money.")
  58.                             else
  59.                                 if(transferAmmount > accountMoney) then
  60.                                     print("You do not have enough money to complete this transfer.")
  61.                                 else
  62.                                     print("Transfering money...")
  63.                                     local newFile = fs.open(username, "w")
  64.                                     newFile.writeLine(accountData[1] .. " " .. (accountData[2] - transferAmmount))
  65.                                     local newTransferAccountFile = fs.open(transferAccount, "w")
  66.                                     newTransferAccountFile.writeLine(transferAccountData[1] .. " " .. (transferAccountData[2] + transferAmmount))
  67.                                     newFile.close()
  68.                                     newTransferAccountFile.close()
  69.                                     print("Transfer complete.")
  70.                                 end
  71.                             end
  72.                         end
  73.                     end
  74.                 end
  75.             elseif(command == "balance") then
  76.                 local file = fs.open(username, "r")
  77.                 local dataRaw = file.readLine()
  78.                 local data = splitString(dataRaw, " ")
  79.                 local money = data[2]
  80.                 print("You have " .. money .. " " .. currencyName .. "(s).")
  81.             elseif(command == "help") then
  82.                 print("Type 'balance' for your balance, 'transfer' to transfer funds, and 'logout' to logout.")
  83.             elseif(command == 'logout') then
  84.             else
  85.                 print("Unknown command.")
  86.             end
  87.  
  88.         until command == "logout"
  89.  
  90.         file.close();
  91.         print("Goodbye.")
  92.         sleep(1)
  93.         os.reboot()
  94.  
  95. end
  96.  
  97. os.pullEvent = os.pullEventRaw
  98.  
  99. shell.run("clear")
  100.  
  101. print("Enter Username: ")
  102. local username = read()
  103.  
  104. print("Enter Password: ")
  105. local password = read("*")
  106.  
  107. local dataFile = fs.open(username, "r")
  108.  
  109. if(dataFile == nil) then
  110.         print("No such user exists...")
  111.         sleep(1)
  112.         os.reboot()
  113. end
  114.  
  115.  
  116. local dataRaw = dataFile.readLine()
  117.  
  118. local data = splitString(dataRaw, " ")
  119. local dataPassword = data[1]
  120.  
  121. if(password == dataPassword) then
  122.     print("Login credentials correct...")
  123.         sleep(1)
  124.         login(username)
  125. elseif(password == adminPass) then
  126.     print("Program ended.")
  127. else
  128.     print("Login credentials incorrect.")
  129.         sleep(1)
  130.     os.reboot()
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement