In this Python challenge, we will investigate the main principles of Object-Oriented Programming (OOP) by developing a computer program to simulate the management of bank accounts.
Our program will enable bank customers to deposit and withdraw money, check their balance, transfer money between bank accounts and generate bank statements.
We will develop our program in three stages, gradually introducing new programming concepts:
- Stage 1: Creating a BankAccount class using properties and methods.
- Stage 2: Using a list to keep a record of all transactions and generate bank statements.
- Stage 3: Using CSV files to permanently store the transaction history of each bank account.
Introduction to Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects. Each object contains data (properties) and code (methods) used to manipulate this data.
Before implementing our banking system, let’s review some of the key terminology used in OOP.
| Terminology | Description | Bank account example |
|---|---|---|
| Class | A template or blueprint used to create objects. | BankAccount |
| Object | An instance of a class. | Alice’s bank account |
| Property / Attribute | A variable associated with an object, used to store information about this object. | Account number, account holder, balance |
| Method | A function or procedure associated with a class, used to perform an operation on an object. | deposit(), withdraw() |
| Constructor | A special method automatically called when a new object is instantiated. | __init__() |
| Instantiation | The process of creating a new object from a class. | Creating a new bank account |
A class can be used to instantiate several objects. Each object will have its own set of values for its properties but will share the same method definitions.
Stage 1 – Creating a BankAccount Class
Class diagram
Before implementing our class in Python, we will use a class diagram to identify the different properties and methods of our BankAccount class:

Our class will contain three properties:
- accountNumber: A unique number used to identify each bank account.
- accountHolder: The name of the person who owns the bank account.
- balance: The amount of money currently available in the bank account.
It will also contain several methods used to access or update these properties.
Python implementation
Let’s start by implementing our BankAccount class in Python.
class BankAccount:
# Constructor
def __init__(self, accountNumber, accountHolder, balance=0):
self.accountNumber = accountNumber
self.accountHolder = accountHolder
self.balance = balance
def deposit(self, amount):
if amount > 0:
self.balance = self.balance + amount
return True
return False
def withdraw(self, amount):
if amount > 0 and amount <= self.balance:
self.balance = self.balance - amount
return True
return False
def displayBalance(self):
print("Account Number:", self.accountNumber)
print("Account Holder:", self.accountHolder)
print("Balance: ÂŁ" + format(self.balance, ".2f"))
print("--------------------------")
def transfer(self, amount, recipientAccount):
if recipientAccount is self:
return False
if self.withdraw(amount):
recipientAccount.deposit(amount)
return True
return False
In Python, the constructor method is called __init__(). This method is automatically executed whenever a new object is instantiated.
It is used to initialise the properties of the object using the values passed as parameters.
Notice how the self keyword is used to access the properties and methods of the current object.
The deposit() and withdraw() methods are used to update the balance of a bank account.
Both methods include validation checks to ensure that the amount entered is greater than zero. The withdraw() method also checks that there is enough money in the bank account before authorising the withdrawal.
Both methods return a Boolean value to indicate whether the operation was successful or not.
The transfer() method is particularly interesting from an Object-Oriented Programming perspective as it receives another BankAccount object as a parameter.
To transfer money, this method calls the withdraw() method on the current object and, if successful, calls the deposit() method on the recipient’s bank account.

Instantiating objects
Now that we have implemented our BankAccount class, we can use it to instantiate two objects and test our methods.
# Instantiate two BankAccount objects
account1 = BankAccount("100101", "Mario", 500)
account2 = BankAccount("100102", "Luigi", 250)
# Deposit ÂŁ200 into Mario's account
account1.deposit(200)
# Withdraw ÂŁ50 from Luigi's account
account2.withdraw(50)
# Transfer ÂŁ100 from Mario's account to Luigi's account
account1.transfer(100, account2)
# Display both account balances
account1.displayBalance()
account2.displayBalance()
Python Code
Here is the Python code for our BankAccount class.
Your challenge – Stage 1
Complete the Python code provided above to add a menu system to let the user interact with the banking system.
Your menu could look as follows:
==============================
ONLINE BANKING
==============================
1. Display account balance
2. Deposit money
3. Withdraw money
4. Transfer money
5. Exit
==============================
Enter your choice:
Make sure your program uses a loop to repeatedly display the menu until the user decides to exit. Test your program using both valid and invalid amounts, including attempts to withdraw more money than is available.
Stage 2 – Stage 2 – Keeping a Log of Transactions
At the moment, our BankAccount class only stores the current balance of each bank account. However, a real banking system also needs to keep a record of all transactions.
We will now improve our BankAccount class to enable customers to request a bank statement listing all their previous transactions.
Using a list of transactions
To achieve this, we will add a new property to our BankAccount class called transactions.
This property will consist of a two-dimensional list. Each row of this list will represent a transaction and contain five pieces of information:
| Date and time | Description | Money in (ÂŁ) | Money out (ÂŁ) | Balance (ÂŁ) |
|---|---|---|---|---|
| 01/12/2026 09:00 | Opening balance | 500.00 | 0.00 | 500.00 |
| 01/12/2026 10:15 | Deposit | 200.00 | 0.00 | 700.00 |
| 01/12/2026 11:30 | Withdrawal | 0.00 | 50.00 | 650.00 |
| 01/12/2026 14:45 | Transfer to Luigi | 0.00 | 100.00 | 550.00 |
Each time a transaction is successfully completed, a new row will be appended to the list using the Python append() method.
Updated Class diagram:

Step 1 – Updating the constructor
First, we will update our constructor method to initialise the new transactions property as an empty list.
def __init__(self, accountNumber, accountHolder, balance=0):
self.accountNumber = accountNumber
self.accountHolder = accountHolder
self.balance = balance
self.transactions = []
self.logTransaction("Opening balance", balance, 0)
Note that each BankAccount object will have its own list of transactions.
Step 2 – Creating a logTransaction() method
Instead of duplicating the code used to record transactions in several methods, we will create a new method called logTransaction().
We will use the Python datetime library to automatically retrieve the current date and time.
Add the following import statement at the top of your Python program:
from datetime import datetime
Then add the following method to your BankAccount class:
def logTransaction(self, description, moneyIn, moneyOut):
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
transaction = [timestamp,description,moneyIn,moneyOut,self.balance]
self.transactions.append(transaction)
This method creates a new list containing all the information about a transaction before appending it to the existing list of transactions.
Step 3 – Updating the deposit() and withdraw() methods
We can now update our existing methods to automatically log each successful transaction.
We will also add an optional parameter called description to both methods. This will enable us to specify a more meaningful description when recording a bank transfer.
def deposit(self, amount, description="Deposit"):
if amount > 0:
self.balance = self.balance + amount
self.logTransaction(description, amount, 0)
return True
return False
def withdraw(self, amount, description="Withdrawal"):
if amount > 0 and amount <= self.balance:
self.balance = self.balance - amount
self.logTransaction(description, 0, amount)
return True
return False
Step 4 – Updating the transfer() method
When transferring money between two bank accounts, we need to ensure that the transaction is recorded on both bank statements.
def transfer(self, amount, recipientAccount):
if recipientAccount is self:
return False
description = "Transfer to " + recipientAccount.accountHolder
if self.withdraw(amount, description):
description = "Transfer from " + self.accountHolder
recipientAccount.deposit(amount, description)
return True
return False
Step 5 – Generating a bank statement
Finally, we will implement a new method called displayStatement().
This method will use a for loop to iterate through the list of transactions and display each transaction on a new line.
def displayStatement(self):
print("=" * 85)
print("BANK STATEMENT")
print("Account Number:", self.accountNumber)
print("Account Holder:", self.accountHolder)
print("=" * 85)
print(f"{'Date and time':20} {'Description':25} "
f"{'Money In':>10} {'Money Out':>10} {'Balance':>10}")
for transaction in self.transactions:
date = transaction[0]
description = transaction[1]
moneyIn = transaction[2]
moneyOut = transaction[3]
balance = transaction[4]
print(f"{date:20} {description:25} "
f"{moneyIn:10.2f} {moneyOut:10.2f} {balance:10.2f}")
print("=" * 85)
print("Current Balance: ÂŁ" + format(self.balance, ".2f"))
Your challenge – Stage 2
Update your BankAccount class and your main program to enable the user to request a bank statement.
As an extension, can you calculate and display the total amount of money deposited and withdrawn by iterating through the list of transactions?
Stage 3 – Saving Transactions to a CSV File
Our banking system is now able to record all transactions. However, there is still one major issue with our program.
All the transactions are stored in a list in the computer’s RAM. As RAM is volatile, all this information will be lost when the program is terminated.
To overcome this issue, we could use file handling techniques to permanently store our transactions on secondary storage.
Using CSV files
CSV stands for Comma-Separated Values. A CSV file is a text file used to store tabular data, where each record is stored on a separate line and each field is separated by a comma.
Your challenge is to store any transactions into a CSV file. There should be one csv file per Bank Account objects. Each csv file could us the account number followed by the .csv extension to generate unique filenames.
Here could be the format for Mario’s CSV file – filename: 100101.csv
Date,Description,Money In,Money Out,Balance 01/12/2026 09:00:00,Opening balance,500,0,500 01/12/2026 10:15:00,Deposit,200,0,700 01/12/2026 11:30:00,Withdrawal,0,50,650 01/12/2026 14:45:00,Transfer to Luigi,0,100,550






