Content Beyond Syllabus
Implement classes for account management, transactions, and interest
Program
class Account:
def __init__(self, account_number, balance, interest_rate):
self.account_number = account_number
self.balance = balance
self.interest_rate = interest_rate
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print("Insufficient funds")
def calculate_interest(self):
interest = self.balance * (self.interest_rate / 100)
self.balance += interest
return interest
class Transaction:
def __init__(self, transaction_type, amount, date,
account_number):
self.transaction_type = transaction_type # "Deposit" or
"Withdrawal"
self.amount = amount
self.date = date
self.account_number = account_number
def display_transaction(self):
print(f"Transaction Type: {self.transaction_type}, Amount:
{self.amount}, Date: {self.date}, Account: {self.account_number}")
class Bank:
def __init__(self):
self.accounts = {} # Dictionary to store accounts by account
number
def create_account(self, account_number, initial_balance,
interest_rate):
new_account = Account(account_number, initial_balance,
interest_rate)
self.accounts[account_number] = new_account
def get_account(self, account_number):
return self.accounts.get(account_number)
def make_deposit(self, account_number, amount):
account = self.get_account(account_number)
if account:
account.deposit(amount)
# Add transaction logging here
def make_withdrawal(self, account_number, amount):
account = self.get_account(account_number)
if account:
account.withdraw(amount)
# Add transaction logging here
def calculate_interest_all(self):
for account in self.accounts.values():
account.calculate_interest()
Explanation:
Account Class:
Stores essential account details like account number, balance, and interest rate.
Methods for deposit, withdrawal, and calculating interest are defined here.
Transaction Class:
Represents a single transaction with details like type (deposit/withdrawal), amount, date, and
associated account number.
display_transaction method to neatly present transaction information.
Bank Class:
Manages a collection of accounts using a dictionary where the key is the account number.
Methods to create new accounts, retrieve an existing account, process deposits and withdrawals,
and calculate interest across all accounts.
Key points:
Encapsulation:
Each class encapsulates its own data and functionality, promoting modularity.
Data Integrity:
Input validation (like checking for sufficient funds during withdrawal) can be added to ensure
data consistency.
Transaction Logging:
To maintain a complete record of transactions, you would typically implement a mechanism to
store new Transaction objects in a list within the Bank class whenever a deposit or withdrawal
occurs.
Error Handling:
Consider adding error handling for scenarios like invalid account numbers or attempting to
withdraw more than the available balance.
Example Usage:
Python
my_bank = Bank()
my_bank.create_account("1234", 1000, 5)
my_bank.make_deposit("1234", 500)
my_bank.make_withdrawal("1234", 200)
my_bank.calculate_interest_all()
This creates a bank object, opens an account with number "1234", makes a deposit, a
withdrawal, and then calculates interest for all accounts.