Advertisement
CelticCoder

CS 405 HW1

Sep 15th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw -- eliminates ability to kill program with keybind
  2. serverID = 0 -- computer id of server unit that holds employee data (practically the same thing as an ip in computercraft)
  3. insuranceserverID = 1 -- computer id of insurance server to access data from (for this demonstration only one will be used to avoid confusion)
  4. adminusername = "temp"
  5. adminpassword = "password"
  6.  
  7. local function displayTextLogin()
  8.     print("Enter username: ")
  9.     username = read()
  10.     print("Enter password: ")
  11.     password = read()
  12.     return username, password
  13. end
  14.  
  15.  
  16. local function displayTextMenu()
  17.     print("Please Choose an Option")
  18.     print("1: Calculate/Display Medical Information")
  19.     print("2: Calculate/Display Retirement Information")
  20.     print("0: Log Out")
  21.     return tonumber(read())
  22. end
  23.  
  24. local function getBalances()
  25.     message = "balances " .. username .. " " .. password -- string request to be processed by server
  26.     rednet.send(serverID, message)  -- sends user info to company server to get any medical info
  27.     id, balances = rednet.receive(nil, 3) -- listens if the server found user, if not in 3 seconds will return nil (ie empty variable)
  28.     return balances
  29. end
  30.  
  31. local function getInfo(info)
  32.     message = info .. " " .. username .. " " .. password -- string request to be processed by server
  33.     rednet.send(insuranceserverID, message) -- sends user info to insurance server to get any medical info
  34.     id, info = rednet.receive(nil, 3) -- listens if the server found user, if not in 3 seconds will return nil (ie empty variable)
  35.     return info
  36. end
  37.  
  38. local function authorization()
  39.     while true do
  40.         print("Do you authorize this program to access your insurance information on your behalf? (yes, or no)")
  41.         choice = read()
  42.         if choice == "yes" then
  43.             return true
  44.         end
  45.         if choice ~= "no" then
  46.             return false
  47.         end
  48.         print("incorrect input, try again (its case sensitive)")
  49.     end
  50. end
  51.  
  52. function checkCredentials(username, password)
  53.     message = "search " .. username .. " " .. password -- string request to be processed by server
  54.     rednet.send(serverID, message) -- sends user info to company server to get any medical info
  55.     id, value = rednet.receive(nil, 3) -- listens if the server found user, if not in 3 seconds will return nil (ie empty variable)
  56.     os.sleep(1)
  57.     return value
  58. end
  59.  
  60. function medimath(balance, insurance)
  61.     crazycoolmaththings = balance * insurance --temp code as all of this is made up for demonstation purposes
  62.     return crazycoolmaththings
  63. end
  64. function retiremath(balance, insurance)
  65.     crazycoolmaththings = balance * insurance --temp code as all of this is made up for demonstation purposes
  66.     return crazycoolmaththings
  67. end
  68.  
  69. function twoInputStrip(str)
  70.     first, second = str:match("(%S+)%s+(%S+)")
  71.     return first, second
  72. end
  73.  
  74. while true do
  75.     username, password = displayTextLogin()
  76.     if username == adminusername and password == adminpassword then
  77.         return 1 -- breaks out of loop if admin login is detected to edit this script
  78.     end
  79.     confirm = checkCredentials(username, password)
  80.     if confirm ~= nil then
  81.         if authorization() then
  82.             while true do
  83.                 choice = displayTextMenu()
  84.                 --unfortunately switch statements are not in lua to use :(
  85.                 if choice == 0 then
  86.                     break
  87.                 end
  88.                 if choice == 1 then
  89.                     info = getInfo("medicalinfo")
  90.                     if info == nil then
  91.                         print("error getting insurance info")
  92.                         break
  93.                     end
  94.                     balances = getBalances()
  95.                     if balances == nil then
  96.                         print("error getting balance info")
  97.                         break
  98.                     end
  99.                     balance1 = twoInputStrip(balances) --assuming that both retirement and medical balances are returned separated by a space
  100.                     finaltotal = medimath(balance1, info)
  101.                     print("Medical balance = " .. balance1)
  102.                     print("Medical Insurance Info = " .. info)
  103.                     print("Final Calculated Total " .. finaltotal)
  104.                     print("Press enter to return to menu")
  105.                     read()
  106.                 end
  107.                 if choice == 2 then
  108.                     info = getInfo("retirementinfo")
  109.                     if info == nil then
  110.                         print("error getting insurance info")
  111.                         break
  112.                     end
  113.                     balances = getBalances()
  114.                     if balances == nil then
  115.                         print("error getting balance info")
  116.                         break
  117.                     end
  118.                     balance1, balance2 = twoInputStrip(balances)  --assuming that both retirement and medical balances are returned separated by a space
  119.                     finaltotal = retiremath(balance2, insurance)
  120.                     print("Retirement balance = " .. balance2)
  121.                     print("Retirement Insurance Info = " .. info)
  122.                     print("Final Calculated Total " .. finaltotal)
  123.                     print("Press enter to return to menu")
  124.                     read()
  125.                 end
  126.             end
  127.         end
  128.     end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement