Lesson 25 of 30

In progress

Course progress

Python

Project: a bank account with a history

Put classes, validation and custom exceptions together in a small banking project that keeps an honest transaction history.

A project is where separate ideas have to agree. This one combines a class, private state, validation, a custom exception and a formatted statement, and it stays small enough to read in one sitting.

What you will learn

  • Model an account whose balance can only change through methods.
  • Record every transaction with a reason and a running balance.
  • Format money without floating-point drift.

Design the rules first

  1. An account has an owner and a balance in paise, stored as an integer.
  2. deposit and withdraw accept rupees, convert to paise, and validate.
  3. Every successful change appends a transaction: kind, amount, balance after.
  4. A withdrawal larger than the balance raises a custom InsufficientFunds.
  5. A statement prints one line per transaction plus the closing balance.

Storing paise avoids the 0.1 + 0.2 problem from lesson 4. Convert at the edge, calculate in integers, format at the end.

The class

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The public balance is a read-only property in rupees, while _paise holds the exact integer. _record is a private helper: one place to append transactions means no method can forget to log.

A statement that adds up

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The after value is captured at the time of the transaction, so the statement remains truthful even if the balance changes later. Recomputing history from the current balance would produce a fictional past.

Extending without copying

A savings account is an account with interest. Inheritance fits because the relationship is genuinely "is a kind of".

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

add_interest uses the same private state and the same recording helper. The subclass adds behaviour without duplicating deposit and withdrawal rules.

Test the hard paths

Run the failure cases on purpose. A bank account that crashes on an overdraft is not finished.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

attempt takes a function and calls it, which keeps the error handling in one place. The lambda is a small unnamed function; if you have not met them yet, read them as "a function that does this one thing when called".

Keep the rules testable

Because every change goes through a method, the rules can be tested without a database or a browser. A small function that creates an account, performs three operations and checks the balance catches most regressions. Check the history length as well: a balance can be correct while a transaction was silently not recorded, and that bug only shows up in the statement.

The statement function stays outside the class on purpose. Formatting is a presentation concern, and a separate function is easier to replace when the same data needs to appear on a web page instead of a terminal.

What this project teaches

The pattern generalises: private storage, validated methods, an append-only history, and formatting at the edge. Swap the account for inventory, a booking or a subscription and the shape stays the same. That transferable structure is the real output of this lesson, more than the banking domain itself.

Using floats for money accumulates errors: fifty deposits of 0.10 can end at 4.999999 rather than 5.00. Store integer paise as this project does, convert with round(value * 100) at the boundary, and divide by 100 only for display.

Add a transfer(amount, target) method that withdraws from one account and deposits into another. If the deposit fails, the withdrawal must not stand. Test a successful transfer and one that overdraws.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The withdrawal happens first, so a failed deposit is rolled back before the error is re-raised. The second attempt fails before any money moves, because withdraw checks the balance first.

Recap

  • Keep exact money in integer paise and convert only for display.
  • Route every balance change through methods that validate and record.
  • A subclass reuses the rules; a custom exception makes the failure catchable by name.

You have finished the object-oriented module. Next, you will work with files, JSON and the command line, where programs start to outlive a single run.

Why does the statement use the stored 'after' value?
Because a statement is a record of what happened, not a recalculation. Storing the balance after each transaction keeps the history correct even when later transactions change the current balance.
Should history be a list of dictionaries or a small class?
Either works. A dictionary is fine while the entries are only printed; a `Transaction` class becomes worthwhile when entries need their own formatting or rules.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.