Step 1: Requirements and rules
In this project you design a small bank account system, a classic because the rules are strict and mistakes are costly. You will represent money safely, protect an account's rules (it can never go below zero), make transfers all-or-nothing, handle many threads using accounts at once without losing a cent, design the database with transactions, and write tests. The core is Java; every Java example was compiled and run for real, with its real output.
Start by writing the rules (invariants) that must always be true:
| Rule | Why it matters |
|---|---|
| Money is exact, never rounded | A bank cannot lose a penny to arithmetic |
| A balance is never negative | Overdrafts are refused |
| Deposits and withdrawals are positive amounts | Otherwise a negative deposit steals money |
| A transfer moves money or does nothing | Never lose or create money half-way |
| The history cannot be edited | It is the audit trail |
| Concurrent use cannot corrupt a balance | Two people use the account at once |
public class Main {
public static void main(String[] args) {
String[] rules = {
"money is exact",
"balance never negative",
"amounts must be positive",
"transfers are all-or-nothing",
"history is append-only",
"safe under concurrent use",
};
for (int i = 0; i < rules.length; i++) System.out.println((i + 1) + ". " + rules[i]);
}
}1. money is exact 2. balance never negative 3. amounts must be positive 4. transfers are all-or-nothing 5. history is append-only 6. safe under concurrent use
Each remaining step enforces one or two of these rules, and the tests in step 8 check all of them.
What is an invariant?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.