Advertisement
KingAesthetic

Python Example 1

Aug 12th, 2024 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 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. class Account:
  5.     def __init__(self, acc_number, acc_holder, initial_balance):
  6.         self.account_number = acc_number
  7.         self.account_holder = acc_holder
  8.         self.balance = initial_balance
  9.  
  10.     # function to find an already existing account number
  11.     def get_account_number(self):
  12.         return self.account_number
  13.  
  14.     # function to deposit money into the account
  15.     def deposit(self, amount):
  16.         if amount > 0:
  17.             self.balance += amount
  18.             print(f"Deposited: {amount}")
  19.         else:
  20.             print("Invalid deposit amount.")
  21.  
  22.     # function to withdraw money from the account
  23.     def withdraw(self, amount):
  24.         if amount > 0 and amount <= self.balance:
  25.             self.balance -= amount
  26.             print(f"Withdrew: {amount}")
  27.         else:
  28.             print("Invalid withdrawal amount or insufficient funds.")
  29.  
  30.     # function to check the account balance
  31.     def get_balance(self):
  32.         return self.balance
  33.  
  34.     # function to display account details
  35.     def display(self):
  36.         print(f"Account Number: {self.account_number}")
  37.         print(f"Account Holder: {self.account_holder}")
  38.         print(f"Balance: {self.balance}")
  39.  
  40. # public Class to manage multiple accounts
  41. class Bank:
  42.     def __init__(self):
  43.         self.accounts = []
  44.  
  45.     # function to create a new account
  46.     def create_account(self, acc_number, acc_holder, initial_balance):
  47.         self.accounts.append(Account(acc_number, acc_holder, initial_balance))
  48.         print("Account created successfully.")
  49.  
  50.     # function to find an account by account number
  51.     def find_account(self, acc_number):
  52.         for account in self.accounts:
  53.             if account.get_account_number() == acc_number:
  54.                 return account
  55.         return None
  56.  
  57.     # function to display all accounts
  58.     def display_all_accounts(self):
  59.         for account in self.accounts:
  60.             account.display()
  61.             print("-------------------")
  62.  
  63. def main():
  64.     bank = Bank()
  65.     while True:
  66.         print("1. Create Account")
  67.         print("2. Deposit")
  68.         print("3. Withdraw")
  69.         print("4. Check Balance")
  70.         print("5. Display All Accounts")
  71.         print("6. Exit")
  72.         choice = input("Enter your choice: ")
  73.  
  74.         if choice == '1':
  75.             acc_number = input("Enter account number: ")
  76.             acc_holder = input("Enter account holder name: ")
  77.             amount = float(input("Enter initial balance: "))
  78.             bank.create_account(acc_number, acc_holder, amount)
  79.         elif choice == '2':
  80.             acc_number = input("Enter account number: ")
  81.             account = bank.find_account(acc_number)
  82.             if account:
  83.                 amount = float(input("Enter amount to deposit: "))
  84.                 account.deposit(amount)
  85.             else:
  86.                 print("Account not found.")
  87.         elif choice == '3':
  88.             acc_number = input("Enter account number: ")
  89.             account = bank.find_account(acc_number)
  90.             if account:
  91.                 amount = float(input("Enter amount to withdraw: "))
  92.                 account.withdraw(amount)
  93.             else:
  94.                 print("Account not found.")
  95.         elif choice == '4':
  96.             acc_number = input("Enter account number: ")
  97.             account = bank.find_account(acc_number)
  98.             if account:
  99.                 print(f"Balance: {account.get_balance()}")
  100.             else:
  101.                 print("Account not found.")
  102.         elif choice == '5':
  103.             bank.display_all_accounts()
  104.         elif choice == '6':
  105.             print("Exiting...")
  106.             break
  107.         else:
  108.             print("Invalid choice. Please try again.")
  109.  
  110. if __name__ == "__main__":
  111.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement