Part 5 of 6 · 3 min read
Edge cases and changes
Faults, races and new features.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Wrong PIN three times | Card blocked and ejected, with a message to keep it |
| Blocked card inserted later | Refused at once |
| Withdraw before signing in | IllegalStateException ("not signed in") |
| Amount not a multiple of 100, zero or negative | Refused |
| Dispenser cannot make the amount | Refused before any charge |
| Jam while dispensing | Account charge undone |
| Two machines debit one account together | Account.debit is synchronized, so the second is refused instead of overdrawing |
| Insert a card while one is in | IllegalStateException |
A gap worth naming: if the machine loses power between the debit and the dispense, the account is charged and no cash came out. The undo code does not run. Real systems solve this with a record of each step that is checked on restart, and a reconciliation of the machine's cash against the bank's ledger. Naming this is a good answer, and building it is not expected.
Also name the security basics, which are common practice and not part of the core design. The machine should not log or store the PIN. The bank should store a hash of the PIN, never the PIN itself, and should compare in constant time. The card number should be masked in logs.
Tests to write first: each row of the run table; undo on a jam, checking the balance and the daily total; the search that picks three 200-notes over a 500-note; a blocked card; and two threads withdrawing from one account with total demand above the balance.
Change requests and the part that changes:
- "Add deposits." A
depositmethod that credits the account after the cash is counted. The order is reversed: count first, credit second, and a fault before the credit means returning the cash. - "Add a transfer." A bank operation, as the machine does not hold both accounts.
- "Different limits per card type." A
LimitPolicyreturned by the bank, soAtmstops holding constants. - "More note sizes." A new value in the note list. The search stays.
- "Show a receipt." An observer that receives the transaction event.
- "Run a mini statement." A bank method, and a view.
- "A new state, such as maintenance." Add a mode value, and a guard in each public method. If states keep growing, move to the state pattern of module 4.
Common mistake
Debiting the account before checking that the cash can be paid out, or dispensing before the debit succeeds. In the first order a customer can be charged for nothing. In the second the machine can give cash that cannot be charged for.
Interview angle
Answer in your head first, then open it.
What is the order of steps in a withdrawal?
Validate, plan the payout, debit the account, dispense, and undo the debit if dispensing fails.
What if the machine jams after the debit?
Run a compensating action that credits the account back, and say what happens on a power cut.
Who checks the PIN?
The bank, which also counts wrong tries and blocks the card.
How do you pay out 600 with a few notes?
Search from the largest note and backtrack. Greedy can get stuck.
Check yourself: 1. Why is a power cut between debit and dispense a gap? 2. Which class changes when limits depend on card type? 3. How does a deposit reverse the order of steps?
Words you will meet
| Word | Meaning |
|---|---|
| Session | The time from card insertion to card ejection |
| Compensating action | A step that undoes the effect of an earlier step |
| Payout plan | The notes chosen for an amount, before anything is dispensed |
| Daily limit | The most a card may withdraw in one day |
| Reconciliation | Comparing two records of the same money to find differences |