Python, with its simplicity and readability, provides an ideal platform for beginners and professionals alike to develop robust applications swiftly. One such application is the Bank Management System. In this blog post, we'll delve into constructing a simple bank management system using Python, breaking down each segment and explaining its functionality.
System Overview
Our Bank Management System will offer:
- Creating a new bank account.
- Checking an account's balance.
- Depositing money.
- Withdrawing money.
- Displaying all accounts.
- Fetching transaction history for an account.
- Account deletion.
- Interest calculations.
- Transferring money between accounts.
Complete Source Code with Output
class Account:
def __init__(self, account_number, holder_name, balance=0.0):
self.account_number = account_number
self.holder_name = holder_name
self.balance = balance
self.transactions = []
def deposit(self, amount):
self.balance += amount
self.transactions.append(f"Deposited: ${amount:.2f}")
return self.balance
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
else:
self.balance -= amount
self.transactions.append(f"Withdrew: ${amount:.2f}")
return self.balance
def transfer(self, other_account, amount):
if amount > self.balance:
print("Insufficient funds!")
return
self.balance -= amount
other_account.balance += amount
self.transactions.append(f"Transferred: ${amount:.2f} to Account: {other_account.account_number}")
other_account.transactions.append(f"Received: ${amount:.2f} from Account: {self.account_number}")
def display_transactions(self):
print(f"\nTransaction history for Account: {self.account_number}")
for t in self.transactions:
print(t)
def __str__(self):
return f"AccountNumber: {self.account_number}, Holder: {self.holder_name}, Balance: ${self.balance:.2f}"
class Bank:
def __init__(self):
self.accounts = {}
def create_account(self, account_number, holder_name, initial_balance=0.0):
if account_number in self.accounts:
print("Account already exists!")
return
self.accounts[account_number] = Account(account_number, holder_name, initial_balance)
def delete_account(self, account_number):
if account_number not in self.accounts:
print("Account does not exist!")
return
del self.accounts[account_number]
print(f"Account {account_number} deleted successfully.")
def get_account(self, account_number):
return self.accounts.get(account_number, None)
def list_accounts(self):
for account in self.accounts.values():
print(account)
def main():
my_bank = Bank()
# Create accounts
my_bank.create_account(101, "John Doe", 1000.50)
my_bank.create_account(102, "Jane Smith", 500.75)
# Display accounts
print("\nListing all accounts:")
my_bank.list_accounts()
# Deposit money
john_account = my_bank.get_account(101)
john_account.deposit(200)
print(f"\nJohn's updated balance after depositing $200: ${john_account.balance:.2f}")
# Withdraw money
john_account.withdraw(50)
print(f"John's updated balance after withdrawing $50: ${john_account.balance:.2f}")
# Transferring money between accounts
jane_account = my_bank.get_account(102)
print("\nTransferring $300 from John to Jane:")
john_account.transfer(jane_account, 300)
print(f"John's balance: ${john_account.balance:.2f}")
print(f"Jane's balance: ${jane_account.balance:.2f}")
# Display transactions
john_account.display_transactions()
jane_account.display_transactions()
# Deleting an account
my_bank.delete_account(102)
print("\nListing all accounts after deleting Jane's account:")
my_bank.list_accounts()
if __name__ == "__main__":
main()
Output:
Listing all accounts:
AccountNumber: 101, Holder: John Doe, Balance: $1000.50
AccountNumber: 102, Holder: Jane Smith, Balance: $500.75
John's updated balance after depositing $200: $1200.50
John's updated balance after withdrawing $50: $1150.50
Transferring $300 from John to Jane:
John's balance: $850.50
Jane's balance: $800.75
Transaction history for Account: 101
Deposited: $200.00
Withdrew: $50.00
Transferred: $300.00 to Account: 102
Transaction history for Account: 102
Received: $300.00 from Account: 101
Account 102 deleted successfully.
Listing all accounts after deleting Jane's account:
AccountNumber: 101, Holder: John Doe, Balance: $850.50
Conclusion
Our Bank Management System in Python showcases how simple it is to build and understand the application. We've utilized Python's class system to define bank accounts and their associated behaviors. This project serves as a foundation, and one can integrate more advanced features such as transaction history, interest calculations, or even database connectivity for a more robust system.