Four states, four buttons, sixteen answers
transcript
Four states. Four buttons. Sixteen answers. Write them as ifs, and every method grows a ladder.
Take one event. A coin goes in. What happens? Idle? Add it to the balance. Move to has money. Has money? Add it, and stay. Dispensing? The motor's running. Coin straight back. Sold out? Back it goes too. That's one event. Three more to go.
So draw the diagram first. A coin moves idle to has money. Paid, and change possible? Only then, dispensing. Item drops. Back to idle, or sold out if everything's empty.
The machine holds the data. Slots, cash box, escrow, and one lock. It never asks which state am I in. It just forwards. Each state is a class. It decides what an event means, and what's next. And the base state refuses everything. New states handle only what they care about.
Here's the trap. Where does your inserted money live? Not in has money. Switch state, and you'd lose it. It's the machine's escrow.
Two states, two events? An enum is clearer. Four by four, with more modes coming? That's when State pays.
Full breakdown in the lecture.