· 8 years ago · Mar 25, 2018, 01:58 AM
1-- *Bank API - by Wes (eastly878)*
2-- Filename: bankAPI
3function handleError (errorMsg)
4 term.clear()
5 term.setCursorPos(1,1)
6 errorMsg = errorMsg or "Unknown error occurred"
7 print("ERROR:", errorMsg, "... rebooting.")
8 sleep(5)
9 os.reboot()
10end
11
12function continueCheck ()
13 while true do
14 local sEvent, param = os.pullEvent("key")
15 if(sEvent == "key") then
16 if param == 28 then
17 break
18 end
19 end
20 end
21end
22
23function checkAccount (userName, userPassword)
24 if fs.exists(userName) then
25 local file = fs.open (userName, "r") -- needs to be adjusted to propper file location
26 local fileData = {} -- stores file data to table
27 local line = file.readLine() -- ticking this up to read different lines
28 repeat
29 table.insert(fileData, line) -- add this line's string to table
30 line = file.readLine()
31 until line == nil -- when nil, there's nothing left
32 file.close()
33
34 local aPassword = fileData[1]
35 local aBalance = fileData[2]
36 if aPassword == userPassword then -- if password correct, return balance
37 return aBalance
38 else -- if password incorrect, return nil
39 return nil
40 end
41 end
42end
43
44function createAccount (aName, aPassword)
45 -- init.
46 if fs.exists("bank/accounts") == false then -- check for accounts folder
47 fs.makeDir("bank/accounts") -- create it if not
48 end
49 if fs.exists("bank/accounts/"..aName) then
50 handleError("account already exists")
51 end
52
53 -- creation
54 local file = fs.open("bank/accounts/"..aName, "a") -- create file in append mode
55 file.writeLine(aPassword) -- write password to file on line 1
56 file.writeLine("0") -- create balance and place on line 2
57 file.close() -- save file
58end