Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a Bank Account That Delegates to a Transaction History

Design a BankAccount class that uses composition: delegate transaction recording to a TransactionHistory class. Implement safe deposit/withdraw behavior and a helper to simulate scenarios.

Python practice16 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

Create two classes: TransactionHistory and BankAccount. TransactionHistory should store a chronological list of transactions (type and amount) and expose a method to retrieve all recorded entries. BankAccount should use a TransactionHistory instance (composition) to record deposits and withdrawals. Implement the following behaviors: - BankAccount(initial_balance=0) initializes with an integer balance and an internal TransactionHistory. - deposit(amount): if amount is a positive integer, add to balance, record the transaction via TransactionHistory, and return the new balance. Non-positive amounts should be ignored and not recorded. - withdraw(amount): if amount is a positive integer and less than or equal to the current balance, subtract from balance, record the transaction, and return True. If insufficient funds or non-positive amount, do not change balance, do not record, and return False. - get_balance(): return the current integer balance. - TransactionHistory.record(kind, amount): record a transaction as a tuple like (kind, amount) where kind is 'deposit' or 'withdraw'. - TransactionHistory.all(): return the list of recorded transactions (in insertion order). Also implement a convenience function simulate_transactions(initial, operations) that builds a BankAccount with the given initial balance, applies each operation from the list operations (each operation is a tuple (kind, amount) where kind is 'deposit' or 'withdraw'), and returns the final balance (an int). Use the public BankAccount methods to perform operations so tests also validate delegation.

Task

Practice composition by separating responsibilities: BankAccount manages balance and business rules while TransactionHistory stores transaction records. Implement methods that validate inputs and delegate recording.

Examples

Basic deposit and withdraw

Input

simulate_transactions(100, [('deposit', 50), ('withdraw', 30)])

Output

120

Start at 100, deposit 50 -> 150, withdraw 30 -> 120. All valid operations are recorded in TransactionHistory by the BankAccount.

Input format

An integer initial balance and a list of operations where each operation is a tuple (kind, amount). Kind is 'deposit' or 'withdraw'. Example: (100, [('deposit', 20), ('withdraw', 10)])

Output format

An integer representing the final account balance after applying the operations.

Constraints

Do not use external libraries. Amounts should be treated as integers. Deposits or withdrawals with non-positive amounts are ignored. Withdrawals that exceed the current balance should fail and not be recorded. TransactionHistory must be used by BankAccount (composition) to record transactions.

Samples

Sample 1

Input

simulate_transactions(0, [('deposit', 10), ('deposit', 5), ('withdraw', 3)])

Output

12

0 + 10 + 5 - 3 = 12