Design a Vending Machine
Design a vending machine with the State pattern, an inventory of slots, safe change-making and clear handling of every wrong action, using the seven-step method.
A vending machine is the textbook example of an object that behaves differently depending on its situation. If you press a product button before inserting any money, nothing happens, or the machine asks you to insert money. If you press the same button after inserting enough money, you get a snack and your change. If the machine is busy dropping your drink, pressing a button does nothing. The same action means different things at different times, and this is exactly the problem the State pattern from Module 6 solves.
In this lesson we design a vending machine with the seven-step method. The most interesting parts are the state design and the change-making, which is a small algorithm hidden inside the problem.
01The problem
Design a vending machine. Customers insert money, choose a product by its code, and receive the product and any change. They can cancel and get their money back. An operator can fill the machine with products.
Loading the rest of the lesson…