Mental Model: Pomodoro Timer TUI in Java
Timers are state machines with a clock. That’s the mental model.
State Machine First
This Pomodoro has a small set of states:
- phase: focus or break
- running: true or false
- remaining seconds
- completed focus sessions
Every command and every tick should update these predictably.
Two Loops in One Program
The app alternates between:
- command mode (paused): read and handle user input
- tick mode (running): sleep 1 second, decrement, maybe transition
If you keep these modes clear, the code stays easy to reason about.
Transition Logic Is the Heart
tick() is where correctness lives.
- decrement remaining time
- when it hits zero, switch phase
- reset remaining seconds for the new phase
- increment session count after focus completes
This is classic finite-state-machine behavior.
Java-Specific Insight
Java’s explicit fields make timer state very legible.
- you can inspect all important fields quickly
- helper methods (
resetCurrentPhase,statusLine) isolate concerns Thread.sleep+ interrupt handling teaches real runtime basics
It’s simple, but it’s real systems thinking.
Debugging by Invariants
Keep a few invariants in your head:
- remaining seconds should never go negative for long
- phase changes only at zero
- session count increases only after focus phase
When behavior is weird, check invariants first.
Next Feature Ideas
- auto-start next phase toggle
- long break every 4 sessions
- daily history saved to file
Add features without breaking the state machine model, and you’ll build robust timer apps.