Introduction
Banks never just change a balance; they record entries in a ledger and derive balances from them. This project follows that discipline and uses a small hierarchy of checked exceptions so the caller can react precisely to 'insufficient funds' versus 'account frozen' versus 'no such account'.
Problem statement
Objectives
- Design a hierarchy of checked custom exceptions
- Keep money movements in an append-only ledger
- Validate everything before changing state (atomic operations)
- Use an enum to vary rules by account type
Features
- Two account types with different rules
- Overdraft-aware withdrawals and transfers
- Interest, freezing, statements and audit
- Declines that report the exact shortfall
Concepts demonstrated
- Exception hierarchies
- Ledger / audit trail
- Atomicity
- Encapsulation
- Business rules
System architecture
BankException is a checked exception with three nested subclasses: InsufficientFunds (carrying the shortfall), AccountFrozen and NoSuchAccount. Bank stores accounts in insertion order and a list of Entry records; the private post method is the only place a balance changes, and it always writes an entry. transfer validates both accounts and the funds before posting the debit and the credit. audit compares the sum of ledger amounts with the sum of balances. Main catches InsufficientFunds before the general BankException to print the shortfall.
Folder structure
Complete source code
src/Bank.java
What each file does
- src/BankException.java
- The checked exception hierarchy used for every banking rule.
- src/Bank.java
- Accounts, the ledger, deposits, withdrawals, transfers, interest, freezing and the audit.
- src/Main.java
- Command parsing and specific handling of each exception type.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
open Asha savings 25000 open Ravi current 8000 open Meera savings 500 open John fixed 9000 deposit AC0001 5000 withdraw AC0001 40000 withdraw AC0002 15000 withdraw AC0002 5000 transfer AC0001 AC0002 12000 transfer AC0001 AC0001 10 transfer AC0001 AC0099 10 deposit AC0001 -5 deposit AC0001 ten freeze AC0002 deposit AC0002 100 interest unfreeze AC0002 interest statement AC0001 statement AC0002 accounts audit quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Ravi's current account goes to −7,000 using the overdraft; a further 5,000 is declined, short by 2,000.
- Deposits to a frozen account are refused; interest skips current accounts.
- The final audit confirms the ledger matches the balances.
Common errors
A transfer debits one account but the credit fails
Validate both sides first, then post both entries; never post before all checks pass.
unreported exception BankException; must be caught or declared
Checked exceptions must be handled or declared with throws — that is the point of making them checked.
Possible improvements
- Add scheduled transfers and standing instructions
- Charge a fee for using the overdraft
- Export statements to CSV
Additional challenges
- 1Reverse a transaction by posting compensating entries.
- 2Add fixed deposits that pay interest at maturity.
- 3Make the bank thread-safe for concurrent transfers.
Built and tested it yourself? Mark it to update your dashboard.