Advertisement
KingAesthetic

LuaExample1

Sep 29th, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. -- Kaizer here. I created a banking system, which allows you to create your own account, withdraw money, input account numbers, initial balance, and so on and so forth.
  2.  
  3. -- class to represent a bank account
  4. local Account = {}
  5. Account.__index = Account
  6.  
  7. -- constructor for initializing account details
  8. function Account.new(accNumber, accHolder, initialBalance)
  9.     local self = setmetatable({}, Account)
  10.     self.accountNumber = accNumber
  11.     self.accountHolder = accHolder
  12.     self.balance = initialBalance
  13.     return self
  14. end
  15.  
  16. -- function to find an already existing account number
  17. function Account:getAccountNumber()
  18.     return self.accountNumber
  19. end
  20.  
  21. -- function to deposit money into the account
  22. function Account:deposit(amount)
  23.     if amount > 0 then
  24.         self.balance = self.balance + amount
  25.         print("Deposited: " .. amount)
  26.     else
  27.         print("Invalid deposit amount.")
  28.     end
  29. end
  30.  
  31. -- function to withdraw money from the account
  32. function Account:withdraw(amount)
  33.     if amount > 0 and amount <= self.balance then
  34.         self.balance = self.balance - amount
  35.         print("Withdrew: " .. amount)
  36.     else
  37.         print("Invalid withdrawal amount or insufficient funds.")
  38.     end
  39. end
  40.  
  41. -- function to check the account balance
  42. function Account:getBalance()
  43.     return self.balance
  44. end
  45.  
  46. -- function to display account details
  47. function Account:display()
  48.     print("Account Number: " .. self.accountNumber)
  49.     print("Account Holder: " .. self.accountHolder)
  50.     print("Balance: " .. self.balance)
  51. end
  52.  
  53. -- Class to manage multiple accounts
  54. local Bank = {}
  55. Bank.__index = Bank
  56.  
  57. function Bank.new()
  58.     local self = setmetatable({}, Bank)
  59.     self.accounts = {}
  60.     return self
  61. end
  62.  
  63. -- function to create a new account
  64. function Bank:createAccount(accNumber, accHolder, initialBalance)
  65.     table.insert(self.accounts, Account.new(accNumber, accHolder, initialBalance))
  66.     print("Account created successfully.")
  67. end
  68.  
  69. -- function to find an account by account number
  70. function Bank:findAccount(accNumber)
  71.     for _, account in ipairs(self.accounts) do
  72.         if account:getAccountNumber() == accNumber then
  73.             return account
  74.         end
  75.     end
  76.     return nil
  77. end
  78.  
  79. -- function to display all accounts
  80. function Bank:displayAllAccounts()
  81.     for _, account in ipairs(self.accounts) do
  82.         account:display()
  83.         print("-------------------")
  84.     end
  85. end
  86.  
  87. local function main()
  88.     local bank = Bank.new()
  89.     local choice
  90.     local accNumber, accHolder, amount
  91.  
  92.     repeat
  93.         print("1. Create Account")
  94.         print("2. Deposit")
  95.         print("3. Withdraw")
  96.         print("4. Check Balance")
  97.         print("5. Display All Accounts")
  98.         print("6. Exit")
  99.         io.write("Enter your choice: ")
  100.         choice = tonumber(io.read())
  101.  
  102.         if choice == 1 then
  103.             io.write("Enter account number: ")
  104.             accNumber = io.read()
  105.             io.write("Enter account holder name: ")
  106.             accHolder = io.read()
  107.             io.write("Enter initial balance: ")
  108.             amount = tonumber(io.read())
  109.             bank:createAccount(accNumber, accHolder, amount)
  110.         elseif choice == 2 then
  111.             io.write("Enter account number: ")
  112.             accNumber = io.read()
  113.             local account = bank:findAccount(accNumber)
  114.             if account then
  115.                 io.write("Enter amount to deposit: ")
  116.                 amount = tonumber(io.read())
  117.                 account:deposit(amount)
  118.             else
  119.                 print("Account not found.")
  120.             end
  121.         elseif choice == 3 then
  122.             io.write("Enter account number: ")
  123.             accNumber = io.read()
  124.             local account = bank:findAccount(accNumber)
  125.             if account then
  126.                 io.write("Enter amount to withdraw: ")
  127.                 amount = tonumber(io.read())
  128.                 account:withdraw(amount)
  129.             else
  130.                 print("Account not found.")
  131.             end
  132.         elseif choice == 4 then
  133.             io.write("Enter account number: ")
  134.             accNumber = io.read()
  135.             local account = bank:findAccount(accNumber)
  136.             if account then
  137.                 print("Balance: " .. account:getBalance())
  138.             else
  139.                 print("Account not found.")
  140.             end
  141.         elseif choice == 5 then
  142.             bank:displayAllAccounts()
  143.         elseif choice == 6 then
  144.             print("Exiting...")
  145.         else
  146.             print("Invalid choice. Please try again.")
  147.         end
  148.     until choice == 6
  149. end
  150.  
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement