CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

Run-outs, odd coins and new states.

Step 5: edge cases, tests and change requests

Edge cases:

Case Behaviour
Unknown product code Exception naming the slot
Sold out Message, money kept, user may choose again or cancel
Exact payment Dispensed with empty change
Overpayment with no coins to return Sale refused, refund on cancel
A coin that the machine does not accept The bank refuses it with an exception, and no state change happens
Cancel with no money "nothing to return"
Two users at once One lock, one action at a time

Tests to write first: each row of the run table; change for amounts that greedy gets wrong; a refund that returns the same coins; and a refused sale that leaves stock and bank unchanged. Check the bank counts before and after.

One real flaw is worth naming. If the user inserts a coin of an unknown size, the bank's add throws in the middle of accept. As written, accept calls bank.add first, which throws before the balance changes, so nothing is left half done. If the order were reversed, the balance would grow with no coin in the bank. Order the steps so that the one that can fail comes first.

Change requests and the part that changes:

  • "Add an out-of-service state." A new OutOfService class with the three methods. The other states stay as they are, and an admin action switches to it.
  • "Add a dispensing state with a delay." A new state for the time the motor runs. All actions return "please wait".
  • "Take card payment." A PaymentMethod interface, with coins and cards as two classes. The balance comes from the method (Level 4, module 1).
  • "Prices change by time of day." A PriceStrategy.
  • "Staff refill." An admin interface that calls inventory.load and bank.add.
  • "Report sales." An observer that receives a sale event.
  • "The machine must remember its state after a power cut." Save balance, inserted coins, stock and state. This is persistence, and it would need the state to be stored as a name and rebuilt on start.

Say why two states are enough here. A third state could be added with no change to the others, which is the main benefit of the pattern.

Common mistake

Using a long if on a state code in every method, or assuming that change is always available. Both are easy to catch in a round when the interviewer asks "what if the machine has no coins".

Interview angle

Answer in your head first, then open it.

Which pattern fits a vending machine?

State, since each action's behaviour depends on the current state. Each state is a class.

How do you make change?

Search the coin stock from the largest coin, and backtrack. Say why greedy can fail with limited coins.

What if change cannot be made?

Refuse the sale before changing anything, and let the user cancel to get the same coins back.

How do you add a new state?

One new class that implements the interface, plus the transition into it.

Check yourself: 1. Why must the unknown-coin check come before the balance changes? 2. Which change request adds a class and edits no other state? 3. How does the design keep two users from mixing up their coins?

Words you will meet

Word Meaning
State A condition of the machine that decides what each action does
Transition A move from one state to another
Balance The money inserted and not yet spent
Change The coins returned after a sale
Greedy Always taking the largest option that fits