Advertisement
PaymentOption

PaymentCalculator

Apr 8th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local firstInput = 0
  2. local secondInput = 0
  3. local operationInput = ""
  4. local screenWidth, screenHeight = term.getSize()
  5. local username = ""
  6. local selection = 1
  7.  
  8. if os.getComputerLabel() == nil then username = "New User"
  9. else username = os.getComputerLabel()
  10.  
  11. sp = textutils.slowPrint
  12.  
  13. local function cPrint(height, string)
  14.     xPos = screenWidht/2 - string.len(string)/2
  15.     term.setCursorPos(xPos, height)
  16.     term.write(string)
  17. end
  18.  
  19. local function rPrint(heightm string)
  20.     xPos = screenWidht - string.len(string)
  21.     term.setCursorPos(xPos, height)
  22.     term.write(string)
  23. end
  24.  
  25. function getInput()
  26.     cPrint(6, "First number: ")
  27.     firstInput = read()
  28.    
  29.     cPrint(7, "Operation: ")
  30.     operationInput = read()
  31.    
  32.     cPrint(8, "Second number: ")
  33.     secondInput = read()
  34. end
  35.  
  36. function printWelcome()
  37.     cPrint(7, "")
  38.     sp("Welcome ".. username)
  39.     sleep(2)
  40.     term.clear()
  41.     term.setCursorPos(1,1)
  42. end
  43.  
  44. function printMenu()
  45.     if selection == 1 then cPrint(6, "[ Calculate ]")
  46.     else                   cPrint(6, "  Calculate  ") end
  47.    
  48.     if selection == 2 then cPrint(7, "[ Exit ]")
  49.     else                   cPrint(7, "  Exit  ") end
  50. end
  51.  
  52. function determineOperation(operationInput, answer)
  53.     local operations = {'+', '-', '*', '/', '%'}
  54.     local operation = 1
  55.    
  56.     operationInput = tostring(operationInput)
  57.    
  58.     for i=1, #operations do
  59.         if operationInput == operations[i] then
  60.             if operations[i] = 1 then operation = 1
  61.             elseif operations[i] == 2 then operation = 2
  62.             elseif operations[i] == 3 then operation = 3
  63.             elseif operations[i] == 4 then operation = 4
  64.             elseif operations[i] == 5 then operation = 5
  65.             end
  66.         else
  67.             operation = 0
  68.         end
  69.     end
  70.    
  71.     if operation == 0 then return answer = nil
  72.     elseif operation == 1 then return answer = firstInput+secondInput
  73.     elseif operation == 2 then return answer = firstInput-secondInput
  74.     elseif operation == 3 then return answer = firstInput*secondInput
  75.     elseif operation == 4 then return answer = firstInput/secondInput
  76.     elseif operation == 5 then return answer = firstInput%secondInput end
  77. end
  78.  
  79. while true do
  80.     local answer = 0
  81.     term.clear()
  82.     term.setCursorPos(1,1)
  83.    
  84.     printWelcome()
  85.     printMenu()
  86.         event, key = os.pullEvent("key")
  87.        
  88.         if key == 200 and selection > 1 then selection = selection - 1
  89.         elseif key == 208 and selection < 2 then selection = selection+1
  90.         end
  91.        
  92.         if selection == 1 and key == 28 then
  93.             getInput()
  94.             if determineOperation(operationInput, answer) ==  nil then
  95.                 cPrint(10, "Not a valid operation.")
  96.                 sleep(2)
  97.             else
  98.                 cPrint(10, "The answer is " ..answer)
  99.                 sleep(2)
  100.             end
  101.         elseif selection == 2 and key == 28 then break
  102.         end
  103.     -- End menu operations
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement