Vending machine
Java- Problem
- Behaviour depends on whether money is in, customers and operators have different rights, and money can arrive in more than one way. Done naively, every method becomes an if-chain over the current mode.
- My part
- Requirements, class design and implementation, in plain Java with no framework.
- Design
- State pattern for idle and has-money; payment behind an injected interface; separate customer and operator interfaces, so misuse fails to compile. Singleton considered and rejected to keep the machine testable.
- Trade-off
- Money is held as integer paise: no floating-point error, at the cost of converting at the edges.
Outcome · change test: add card payments
Press run to see what the new feature touched.