Skip to content

Instantly share code, notes, and snippets.

@TheTrueShell
Last active December 27, 2023 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheTrueShell/f5f108b984af7d419ee58640cf9f42dc to your computer and use it in GitHub Desktop.
Save TheTrueShell/f5f108b984af7d419ee58640cf9f42dc to your computer and use it in GitHub Desktop.
cc atm
-- ATM Script for Computercraft
-- Open modem
local modem = peripheral.find("modem")
if not modem then
print("No modem attached")
return
end
modem.open(2) -- Open on channel 2, for example
-- Function to send a request to the bank server and wait for a response
local function sendBankRequest(request)
modem.transmit(1, 2, request) -- Transmit on bank's channel (1), reply on ATM's channel (2)
while true do
local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
if senderChannel == 2 then
return message
end
end
end
-- Function to handle user interactions
local function handleUser()
print("Welcome to the ATM")
print("Please enter your account ID:")
local accountID = read()
print("Choose an action:\n1. Check Balance\n2. Deposit\n3. Withdraw")
local action = read()
local request = {accountID = accountID}
if action == "1" then
-- Check balance
request.action = "balance"
local response = sendBankRequest(request)
print("Your balance is: " .. response)
elseif action == "2" then
-- Deposit
print("Enter deposit amount:")
local amount = tonumber(read())
request.action = "deposit"
request.amount = amount
local response = sendBankRequest(request)
print(response)
elseif action == "3" then
-- Withdraw
print("Enter withdrawal amount:")
local amount = tonumber(read())
request.action = "withdraw"
request.amount = amount
local response = sendBankRequest(request)
print(response)
else
print("Invalid action selected.")
end
end
-- Main loop
while true do
handleUser()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment