Java Banking Ledger
In plain English
A bank account ledger built in Java that counts money in whole pence instead of rounded decimals so balances and transfers stay exact.
A Java 21 OOP banking domain library demonstrating template methods, strategy interfaces, immutable record statements, and rollback-safe transfers.
1. Why I built it
Built to apply inheritance, composition, exact integer arithmetic, and rollback semantics cleanly to a precision-sensitive financial domain.
2. How it works
Implemented `Account`, `CheckingAccount`, `SavingsAccount`, `FeePolicy`, and immutable `TransactionEntry` records in `agency.shss7.clearledger.model` paired with `LedgerService`.
Representing balances in `double` introduces binary rounding drift after repeated arithmetic. Java Banking Ledger stores every balance, overdraft limit, fee, and minimum reserve as 64-bit integer pence (`long`), expressing savings yields in integer basis points (`100 bps = 1.00%`).
An abstract `Account` base class encapsulates balance mutations behind template deposit/withdraw methods, enforcing `requireValidTxId` and `normalizeNote` prior to altering state. `CheckingAccount` and `SavingsAccount` override `canWithdraw()` while delegating variable surcharges to a `@FunctionalInterface` `FeePolicy`.
Across accounts, `LedgerService.transfer()` guards blank references upfront and performs a compensating source reversal if crediting the destination account throws an error.
3. Output & Source Specimen
Account Domain Model & Invariants
Run checks: mvn test -B4. Design Decisions & Challenges
Key Design Decisions
- 01.Enforced `requireValidTxId` and `normalizeNote` before modifying balance or ledger history.
- 02.Stored all balances solely as `long` pence and exposed audit logs exclusively via `Collections.unmodifiableList()`.
- 03.Added compensating reversal in `LedgerService.transfer()` when destination deposit raises an exception.
Edge Cases Handled
- Keeping `CheckingAccount` overdraft checks coherent when custom `FeePolicy` lambdas add flat or tiered charges onto withdrawals.
5. Testing & Limits
Automated Checks
Verified with 9 JUnit 5 methods in `LedgerServiceTest.java` covering overdraft limits, minimum reserves, fee policies, overflow-safe BigInteger interest, compensating rollback, RFC 4180 CSV export, `getStatementByType()`, and `getTotalFeesChargedPence()`.
Current Limits & Next Steps
- Account state lives in memory; adding relational persistence with optimistic locking would extend this to multi-process workloads.
6. Key Takeaways
What building Java Banking Ledger reinforced
Pairing inheritance for solvency rules with functional interfaces for fee calculation keeps domain rules extensible without rigid class hierarchies.
- — 9 JUnit 5 tests pass under `mvn test -B` in CI.
- — Source hosted at github.com/16lv/java-banking-ledger.
Recent Activity
Latest repository updates
feat: add entry-type ledger filtering and total fee accumulation
Added LedgerService.getStatementByType() and getTotalFeesChargedPence() backed by immutable TransactionEntry records.
feat: add RFC 4180 CSV statement export and remove unused Account debit helper
Implemented LedgerService.exportStatementCsv(accountId) with quote/comma escaping and removed the unused Account.debitInternal() helper.
Use exact overflow guards on total holdings/interest rollups and update README
Aligned LedgerService rollups with Account overflow protections and updated build & limitation documentation.