The last train at Northgate

Design the classes for a vending machine

MediumAbout 40 minJava or TypeScript

Concepts this problem usesHidden, since spotting them is part of the practice.Show themHide
The machine · Friday, 11:48 pm · last train at 11:52Screen: INSERT COINS
A1Pretzels$1.25 · 4 leftA2ChipsEMPTYA3Trail mix$1.50 · 6 leftB1Granola bar$0.90 · 2 leftB2Gum$0.45 · 9 leftB3Cookies$1.25 · 3 leftC1Water$1.00 · 5 leftC2Juice$1.75 · 1 leftC3CrackersEMPTYINSERT COINSCOINSCANCEL

The platform

One machine, one last train

Dev Okafor manages Northgate Station. On platform 2 stands one snack machine, and at 11:48 on a Friday night it is the only food for twenty miles of track. People feed it coins with one eye on the departure board.

The machine's software is older than the station's escalators. It swallows coins when a slot is empty, and nobody can say what it will do after a cancel. Dev wants new software in it before the timetable changes next month.

What Dev wants

Coins in, a snack and change out

The machine takes coins and keeps a running balance. A customer picks a slot by its code: if the slot is stocked and the balance covers the price, the machine sells the snack and gives back the change. With too little money in, or an empty slot, it refuses and keeps the balance for another pick. Cancel gives back everything put in.

Platform 2 · Snacks

5¢ 10¢ 25¢ $1 coins accepted

  • Change comes back in coins
  • No bills, no cards (yet)
  • Cancel returns everything inserted

A few of the slots

PriceLeft
A1 Pretzels$1.254
A2 Chips$1.000
C2 Juice$1.751

Last Friday

Two receipts from 11:48 pm

One commuter bought pretzels with a quarter to spare. Another tried the empty chips slot, then gave up and pressed cancel before the doors closed.

Receipt #81Sale
Coins
$1 + 25¢ + 25¢
Picked
A1 Pretzels
Price
$1.25
$1.50 − $1.2525¢ change
Receipt #82Cancel
Coins
$1 + 10¢
Picked
A2 Chips: empty
Then
Cancel
Everything back$1.10 refund

Next month

Out of service, without a rewrite

Cleaning nights need an out-of-service mode, and card readers come next year. Neither should mean rewriting the machine.Dev Okafor

A technician refills the slots by hand, so there is no restocking app to build, and nothing has to be saved anywhere: when the power goes, the coins drop into the cash box.

Design the classes for the platform 2 machine.

Start building