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 Idle and ejects the card.
  • Extensions: deposits (count and hold until verified), fund transfers, receipts, and multiple card types.

Real-world reading

Check your understanding

Question 1 of 3

0 correct so far

Why is the State pattern a good fit for an ATM?

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.