Exercise: State
This exercise puts the State pattern lesson into code.
Scenario
Model a subway turnstile as a State machine. The turnstile starts in the Locked state and
accepts two events: coin and push. Each event is processed by the current state object,
which returns the output message and the next state. The Turnstile context delegates each
event to its current state and swaps in whatever state the handler returns.
Commands
Each input row contains exactly one command string. Produce one output line per command.
| Current state | Event | Output | Next state |
|---|---|---|---|
| Locked | coin | Unlocked | Unlocked |
| Locked | push | Locked: pay first | Locked |
| Unlocked | coin | Already unlocked | Unlocked |
| Unlocked | push | Walked through | Locked |
7 coin push push coin coin push push
Unlocked Walked through Locked: pay first Unlocked Already unlocked Walked through Locked: pay first
Your task
Create LockedState and UnlockedState (each implements TurnstileState) so the commands produce the output described above. The starter keeps TurnstileState and the Turnstile context, and marks where to add the two state classes with a TODO comment listing each method's signature and exact return value.