Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- bank.lua
- local accounts = {}
- local function saveAccounts()
- local file = fs.open("accounts", "w")
- file.write(textutils.serialize(accounts))
- file.close()
- end
- local function loadAccounts()
- if fs.exists("accounts") then
- local file = fs.open("accounts", "r")
- accounts = textutils.unserialize(file.readAll())
- file.close()
- end
- end
- local function createAccount(name)
- if accounts[name] then
- print("Compte déjà existant!")
- return
- end
- accounts[name] = 0
- saveAccounts()
- print("Compte créé pour " .. name)
- end
- local function deposit(name, amount)
- if not accounts[name] then
- print("Compte non trouvé!")
- return
- end
- accounts[name] = accounts[name] + amount
- saveAccounts()
- print(amount .. " déposé. Nouveau solde: " .. accounts[name])
- end
- local function withdraw(name, amount)
- if not accounts[name] then
- print("Compte non trouvé!")
- return
- end
- if accounts[name] < amount then
- print("Fonds insuffisants!")
- return
- end
- accounts[name] = accounts[name] - amount
- saveAccounts()
- print(amount .. " retiré. Nouveau solde: " .. accounts[name])
- end
- local function checkBalance(name)
- if not accounts[name] then
- print("Compte non trouvé!")
- return
- end
- print("Solde de " .. name .. ": " .. accounts[name])
- end
- local function drawMenu()
- term.clear()
- term.setCursorPos(1, 1)
- print("Banque")
- print("1. Créer Compte")
- print("2. Déposer")
- print("3. Retirer")
- print("4. Consulter Solde")
- print("Selection: ")
- end
- local function main()
- loadAccounts()
- while true do
- drawMenu()
- local choice = read()
- if choice == "1" then
- print("Nom du compte: ")
- local name = read()
- createAccount(name)
- elseif choice == "2" then
- print("Nom du compte: ")
- local name = read()
- print("Montant à déposer: ")
- local amount = tonumber(read())
- deposit(name, amount)
- elseif choice == "3" then
- print("Nom du compte: ")
- local name = read()
- print("Montant à retirer: ")
- local amount = tonumber(read())
- withdraw(name, amount)
- elseif choice == "4" then
- print("Nom du compte: ")
- local name = read()
- checkBalance(name)
- else
- print("Option invalide")
- end
- sleep(2)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement