Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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.
- -- class to represent a bank account
- local Account = {}
- Account.__index = Account
- -- constructor for initializing account details
- function Account.new(accNumber, accHolder, initialBalance)
- local self = setmetatable({}, Account)
- self.accountNumber = accNumber
- self.accountHolder = accHolder
- self.balance = initialBalance
- return self
- end
- -- function to find an already existing account number
- function Account:getAccountNumber()
- return self.accountNumber
- end
- -- function to deposit money into the account
- function Account:deposit(amount)
- if amount > 0 then
- self.balance = self.balance + amount
- print("Deposited: " .. amount)
- else
- print("Invalid deposit amount.")
- end
- end
- -- function to withdraw money from the account
- function Account:withdraw(amount)
- if amount > 0 and amount <= self.balance then
- self.balance = self.balance - amount
- print("Withdrew: " .. amount)
- else
- print("Invalid withdrawal amount or insufficient funds.")
- end
- end
- -- function to check the account balance
- function Account:getBalance()
- return self.balance
- end
- -- function to display account details
- function Account:display()
- print("Account Number: " .. self.accountNumber)
- print("Account Holder: " .. self.accountHolder)
- print("Balance: " .. self.balance)
- end
- -- Class to manage multiple accounts
- local Bank = {}
- Bank.__index = Bank
- function Bank.new()
- local self = setmetatable({}, Bank)
- self.accounts = {}
- return self
- end
- -- function to create a new account
- function Bank:createAccount(accNumber, accHolder, initialBalance)
- table.insert(self.accounts, Account.new(accNumber, accHolder, initialBalance))
- print("Account created successfully.")
- end
- -- function to find an account by account number
- function Bank:findAccount(accNumber)
- for _, account in ipairs(self.accounts) do
- if account:getAccountNumber() == accNumber then
- return account
- end
- end
- return nil
- end
- -- function to display all accounts
- function Bank:displayAllAccounts()
- for _, account in ipairs(self.accounts) do
- account:display()
- print("-------------------")
- end
- end
- local function main()
- local bank = Bank.new()
- local choice
- local accNumber, accHolder, amount
- repeat
- print("1. Create Account")
- print("2. Deposit")
- print("3. Withdraw")
- print("4. Check Balance")
- print("5. Display All Accounts")
- print("6. Exit")
- io.write("Enter your choice: ")
- choice = tonumber(io.read())
- if choice == 1 then
- io.write("Enter account number: ")
- accNumber = io.read()
- io.write("Enter account holder name: ")
- accHolder = io.read()
- io.write("Enter initial balance: ")
- amount = tonumber(io.read())
- bank:createAccount(accNumber, accHolder, amount)
- elseif choice == 2 then
- io.write("Enter account number: ")
- accNumber = io.read()
- local account = bank:findAccount(accNumber)
- if account then
- io.write("Enter amount to deposit: ")
- amount = tonumber(io.read())
- account:deposit(amount)
- else
- print("Account not found.")
- end
- elseif choice == 3 then
- io.write("Enter account number: ")
- accNumber = io.read()
- local account = bank:findAccount(accNumber)
- if account then
- io.write("Enter amount to withdraw: ")
- amount = tonumber(io.read())
- account:withdraw(amount)
- else
- print("Account not found.")
- end
- elseif choice == 4 then
- io.write("Enter account number: ")
- accNumber = io.read()
- local account = bank:findAccount(accNumber)
- if account then
- print("Balance: " .. account:getBalance())
- else
- print("Account not found.")
- end
- elseif choice == 5 then
- bank:displayAllAccounts()
- elseif choice == 6 then
- print("Exiting...")
- else
- print("Invalid choice. Please try again.")
- end
- until choice == 6
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement