Coffee Vending Machine

Recipes, stock, coins and a state machine

Design a machine that sells drinks made from a stock of ingredients: pick a drink, pay, get the drink and the change, and never keep money for a drink it did not make.

Overview

A coffee vending machine holds a few ingredients (water, beans, milk, cocoa), knows a handful of recipes, and has three buttons a user can press: choose a drink, insert money, cancel. It must refuse a drink it cannot make, keep count of what it has poured, give change, and refund every cent when a sale does not finish.

The problem looks small and is a favorite because it tests three things at once. Can you model a device whose valid actions depend on what it is doing right now? Can you add a new drink without touching the code that sells drinks? And can you make the one dangerous step, checking the stock and then taking from it, safe when two requests arrive together?

A strong answer names a state machine in the first minute, says that recipes are data, and points at the check-and-consume step as the place that needs a lock. The code below does all three.

Scoping the problem

Before drawing anything, ask these six questions, because each answer removes a branch from the design. Each comes with the answer used below.

  • Which drinks, and can new ones be added later? This decides whether a drink is a class or a row of data. Recipes are data: a name, a price and the amounts it needs.
  • How is money handled: coins, notes, card? Coins and notes need a balance and change; a card is one call. Coins in cents here; card payment is a follow-up question.
  • Can a user cancel, and when? Cancel must refund, and during dispensing it must be refused. Yes, until the price is covered.
  • What happens if an ingredient runs out mid-sale? The machine must not keep the money. It gives a full refund and goes back to idle.
  • How does maintenance learn stock is low? This is polling versus a notification. The inventory calls listeners when a level crosses a threshold.
  • One user at a time, or many requests at once? This decides where the locks go. One user at the panel, but the inventory is shared with a maintenance thread.

Functional requirements

  • select(name) accepts a drink only when it exists and every ingredient it needs is in stock
  • insert(cents) adds to the balance; once the price is covered the machine dispenses and returns the change
  • cancel() refunds the balance while the machine is waiting for payment and is refused while dispensing
  • Money inserted when no drink is selected goes straight back to the user
  • Dispensing takes all of a recipe's ingredients or none; if any is short the whole payment is refunded
  • refill(ingredient) restores a container to capacity; a listener is told when a level falls to 20 percent or less
  • New drinks are added as recipe rows, without changing the machine or state code

Non-functional requirements

  • Every button press is atomic: the state, selection and balance change together or not at all
  • Stock never goes negative, even when a maintenance thread and a sale touch the inventory at once
  • No money is kept for a drink that was not made
  • Prices and balances are integers in cents, never floats
  • Small, testable classes: each state is a class with three methods and no shared conditionals

Interview questions

  • Why use the State pattern here instead of an if-else on a status field?
  • How would you add card payments without touching the states?
  • The machine has a limited number of each coin. How do you give change?
  • What if the power fails while a drink is being made?
  • Where exactly is the race, and why is one lock in the inventory enough?
  • How does maintenance learn that milk is low, and why not just poll?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio