Worksheet 9 - OOP Class BankAccount example
Jump to navigation
Jump to search
Bank Account Class
Here’s another simple object-oriented programming (OOP) example to help students practice. This one involves creating a Bank Account class, which simulates the basic functionality of a bank account. Like the previous example, it's designed for easy expansion.
# Simple Bank Account Class Example
class BankAccount:
def __init__(self, account_holder, balance=0):
"""Initialize the account holder's name and balance."""
self.account_holder = account_holder # Account holder's name
self.balance = balance # Initialize with an optional balance (default is 0)
def deposit(self, amount):
"""Deposit money into the account."""
if amount > 0:
self.balance += amount
print(f"Deposited ${amount}. New balance: ${self.balance}.")
else:
print("Deposit amount must be positive!")
def withdraw(self, amount):
"""Withdraw money from the account."""
if amount > 0 and amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount}. Remaining balance: ${self.balance}.")
elif amount > self.balance:
print("Insufficient funds!")
else:
print("Withdrawal amount must be positive!")
def display_balance(self):
"""Display the current balance."""
print(f"Account balance: ${self.balance}")
if __name__ == '__main__':
# Create an instance of the BankAccount class
my_account = BankAccount('John Doe', 100) # Initial deposit of $100
# Display the current balance
my_account.display_balance()
# Perform a deposit
my_account.deposit(50) # Deposit $50
# Perform a withdrawal
my_account.withdraw(30) # Withdraw $30
# Attempt to withdraw more than the balance
my_account.withdraw(150) # Insufficient funds example
# Display the final balance
my_account.display_balance()
Explanation of the Code
Class Definition: BankAccount
- The class is defined to simulate a simple bank account.
- The __init__ method initializes the account holder’s name and an optional balance (default is 0).
Methods:
- deposit(self, amount): Adds money to the account, ensuring the amount is positive.
- withdraw(self, amount): Withdraws money from the account, ensuring the amount does not exceed the balance.
- display_balance(self): Prints the current account balance.
Main Script:
- An instance of the BankAccount class is created with an initial deposit.
- The program demonstrates depositing and withdrawing money and checks for insufficient funds.
- It prints the balance after each transaction.
Expanding the Program
Students can expand this example in several ways:
- Add Interest: Implement a method to apply interest to the account balance.
- Overdraft Protection: Add an overdraft limit and handle what happens when a withdrawal exceeds the overdraft limit.
- Multiple Accounts: Create a system to manage multiple bank accounts (e.g., savings and checking).
- Transaction History: Add a feature that tracks and prints all deposits and withdrawals.
This example builds upon the basics of OOP and encourages students to experiment with new features and functionalities.