This project is a simple banking system implemented in Python to demonstrate core Object-Oriented Programming (OOP) concepts.
It includes basic bank account functionality, savings accounts with interest, and calculations using data structures.
Represents a standard bank account.
Attributes:
owner_name: Name of the account owner.account_number: Unique account number.balance: Current balance.total_accounts(Class Variable): Tracks total number of accounts.
Methods:
deposit(amount): Add money to the account.withdraw(amount): Withdraw money from the account.show_balance(Property): Returns the current balance.__str__(): Returns a readable string representation.__add__(other): Returns the sum of two account balances.__lt__(other): Compares two accounts by balance.__getitem__(key): Access account info by key (owner_name,account_number,balance).get_count()(Class Method): Returns the total number of accounts.
Represents a savings account with interest.
Additional Attributes:
interest_rate: Interest rate in percentage.
Additional Methods:
add_interest(): Calculates interest and adds it to the balance.withdraw(amount): Overrides the parentwithdrawmethod (Polymorphism) to show a different message.
Calculates the average balance of multiple accounts using a list (array).
Attributes:
accounts: List to store all account objects.
Methods:
add_account(account): Adds an account to the list.average_balance(): Calculates and prints the average balance of all accounts.
- Basic Banking Operations: Deposit, Withdraw, Show Balance.
- Savings Account Functionality: Add interest automatically.
- Magic Methods:
__str__,__add__,__lt__,__getitem__. - Class Variables & Class Methods: Track total accounts.
- Property Decorators: Access balance safely.
- Data Structure Integration: Calculate average balances of multiple accounts.
- Polymorphism:
withdrawmethod behaves differently inSavingsAccount.
from account import Account, SavingsAccount, Average
# Create accounts
acc1 = Account("Ezz", "100", 54000)
acc2 = Account("Ali", "112", 35000)
sav1 = SavingsAccount("Mostafa", "287", 12000, 8)
sav2 = SavingsAccount("Adham", "265", 23000, 5)
# Transactions
acc1.deposit(10000)
acc2.withdraw(300)
sav1.deposit(450)
sav2.withdraw(1000)
sav1.add_interest()
sav2.add_interest()
# Magic Methods
print(acc1) # __str__
print(acc1 + acc2) # __add__
print(acc1 < acc2) # __lt__
print(acc1["owner_name"]) # __getitem__
# Show balances
print(acc1.show_balance)
print(sav1.show_balance)
# Average balance calculation
avg_calc = Average()
avg_calc.add_account(acc1)
avg_calc.add_account(acc2)
avg_calc.add_account(sav1)
avg_calc.add_account(sav2)
avg_calc.average_balance()
# Total accounts
print(Account.get_count())