CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 1 of 6 · 1 min read

Why this page comes fourth in Level 6

In this module you will

  • Apply the five-step method to a machine with states.
  • Draw a state table and turn it into the state pattern.
  • Keep stock and coins in classes that guard their own rules.
  • Make change from a limited coin stock, and see why a greedy loop can fail.
  • Handle refunds, sold-out items and a machine that cannot give change.

The vending machine is the classic test of the state pattern. What a button does depends on what the machine is doing. It also hides a small algorithm, making change, which catches candidates who assume coins are unlimited. This page uses the five steps of Level 0, module 4, and the state pattern of Level 4, module 3. No book was supplied for this track, so the content is common practice, not from the books. The Java code was compiled and run before it was written here. Numbers marked "made up" only show the idea.