Advertisement
zaxn1234

CC: Debit card system

Apr 18th, 2023 (edited)
2,739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. creditLimit = 0
  2.  
  3. function mainLoop()
  4.     print('entering main loop')
  5.     while (not (disk.isPresent('top') and disk.hasData('top')))
  6.     do
  7.         sleep(1)
  8.     end
  9. end
  10.  
  11. function drawMainScreen(monitor)
  12.     monitor.clear()
  13.     monitor.setCursorPos(1,1)
  14.     monitor.write('Cash-o-matic 3000')
  15. end
  16.  
  17. function createDebitFile(fsroot)
  18.     local debitFile = fs.open(fsroot .. '/debit', 'w')
  19.     debitFile.writeLine('1000')
  20.     debitFile.flush()
  21.     debitFile.close()
  22.     return getDebitFile(fsroot)
  23. end
  24.  
  25. function getDebitFile(fsroot)
  26.     return fs.open(fsroot .. '/debit', 'r')
  27. end
  28.  
  29.  
  30. function drawDebitScreen(monitor)
  31.     local fsroot = disk.getMountPath('top')
  32.     if (not fs.exists(fsroot .. '/debit')) then
  33.         -- create debit file
  34.         debitFile = createDebitFile(fsroot)
  35.     else
  36.         debitFile = getDebitFile(fsroot)
  37.     end
  38.  
  39.     creditLimit = debitFile.readLine()
  40.     debitFile.close()
  41.    
  42.     -- actually draw the screen
  43.     monitor.clear()
  44.     monitor.setCursorPos(1,1)
  45.     monitor.write('Current Balance: ' .. creditLimit)
  46.  
  47.     monitor.setCursorPos(1,10)
  48.     monitor.write('Cancel')
  49.  
  50.     -- monitor.setCursorPos(1,15)
  51.     -- monitor.write('Buy')
  52.  
  53.     waiting = true
  54.     while (waiting and disk.isPresent("top"))
  55.     do
  56.         local event, button, x, y = os.pullEvent("monitor_touch")
  57.         print("Button", button, "was clicked at", x, "/", y)
  58.  
  59.         if (x >= 1 and x <= 6 and y >= 10 and y <= 12) then
  60.             disk.eject("top")
  61.             waiting = false
  62.         end
  63.     end
  64. end
  65.  
  66. local monitor = peripheral.wrap('left')
  67. monitor.setTextScale(0.5)
  68. monitor.setBackgroundColor(colors.yellow)
  69. monitor.setTextColor(colors.black)
  70.  
  71. while (true) do
  72.     drawMainScreen(monitor)
  73.     mainLoop()
  74.    
  75.     drawDebitScreen(monitor)
  76. end
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement