ATM machine
A machine that behaves differently in each state: the State pattern, card and PIN checks, and cash dispensing with limits.
On the roadmap:Software Design · step 4: Low-level design: start with objects
Lesson 6 of 16 · 10 min read · quiz · exercise
The problem
An ATM accepts a card, checks the PIN, then lets the customer check balance, withdraw cash or deposit, and returns the card. It talks to a bank to authorise operations and holds a limited amount of cash.
States
Figure SDA-LLD-06-1 · ATM states · © forcsguy.com
In Idle, "enter PIN" makes no sense; in Authenticated, "insert card" makes no sense. The State pattern puts those rules inside state objects instead of if (state === ...) chains.
interface AtmState {
insertCard(atm: Atm, card: Card): void;
enterPin(atm: Atm, pin: string): void;
withdraw(atm: Atm, cents: number): void;
}
class Idle implements AtmState {
insertCard(atm, card) { atm.card = card; atm.setState(new HasCard()); }
enterPin() { throw new Error("insert a card first"); }
withdraw() { throw new Error("insert a card first"); }
}
class HasCard implements AtmState {
tries = 0;
insertCard() { throw new Error("card already inserted"); }
enterPin(atm, pin) {
if (atm.bank.checkPin(atm.card!, pin)) atm.setState(new Authenticated());
else if (++this.tries >= 3) { atm.retainOrEject(); atm.setState(new Idle()); }
}
withdraw() { throw new Error("enter the PIN first"); }
}
Who is responsible for what
Figure SDA-LLD-06-2 · Withdrawal sequence · © forcsguy.com
- Bank owns the balance and account rules. ATM owns hardware, cash on hand and the session.
- Cash dispenser holds counts per denomination and decides which notes to give (greedy from the largest, with a fallback).
- Every step is logged so a failure between "dispensed" and "confirmed" can be reconciled later (a mismatch report, or an automatic reversal).
Design notes
- Treat PINs as secrets: never store or log them; the bank verifies, the ATM only forwards an encrypted PIN block.
- Idempotency: a retried authorise call must not debit twice; use a transaction id.
- Timeouts: an idle session returns to
Idleand ejects the card. - Extensions: deposits (count and hold until verified), fund transfers, receipts, and multiple card types.
Real-world reading
Related reading
- Apple Machine Learning BlogApple engineers · AppleBlogs
- AWS Machine Learning BlogAmazon Web Services engineers · Amazon Web ServicesBlogs
- Optimizing Payments with Machine Learning at DropboxDropbox engineers · DropboxCase Studies
- Machine Learning Platform at YelpYelp engineers · YelpCase Studies
Show 6 moreShow fewer
- PyKrylov: Accelerating Machine Learning Research at eBayeBay engineers · eBayCase Studies
- Machine Learning (2 parts) at Condé NastCondé Nast engineers · Condé NastCase Studies
- Machine Learning Platform at ZalandoZalando engineers · ZalandoCase Studies
- Secret Detector: Preventing Secrets in Source Code at YelpYelp engineers · YelpCase Studies
- Machine Learning at Jane StreetJane Street engineers · Jane StreetCase Studies
- Waymo BlogWaymo engineers · WaymoBlogs
Check your understanding
Question 1 of 3
0 correct so far
Why is the State pattern a good fit for an ATM?
Practise it: URL shortener
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Read-heavy, a few links are very popular.
Add or remove parts, move traffic up and down, and break something to see what gives way first.
Try it yourself
Add a feature: cash is dispensed in the fewest notes possible, and if the machine lacks a denomination it chooses another combination. How would you model it?
Click what a strong design here would need:
More in Design practice
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.