Mental Model: TUI Word Processor in Java

A word processor sounds big, but your terminal version is just “text editor plus a few document operations.”

Mental Model in Layers

Think about this tool in layers, not as one giant program.

  1. Document layer: list of lines and text operations.
  2. Metrics layer: derive stats like word count from current lines.
  3. CLI layer: map commands to document methods.

When stuck, ask: which layer is this bug in?

Document Is the Product

In Java, the Document class is your product core.

  • append, insert, edit, delete are editing primitives.
  • stats is read-only computation over current state.
  • dirty tracks unsaved edits.

Everything else is just delivery plumbing.

Command Loop as a Router

The main loop should route commands, not do business logic.

  • Parse command and line number safely.
  • Delegate to Document.
  • Print user feedback.

If the loop becomes huge, move logic down into methods.

Java-Specific Insight

Java nudges you toward explicit method boundaries. Use that to your advantage.

  • Keep mutation methods tiny and predictable.
  • Keep file I/O in load and save only.
  • Keep input parsing (parseLine) isolated.

This gives you code you can reason about with confidence.

Practical Build Sequence

  1. Start with append and show.
  2. Add insert, edit, and delete with bounds checks.
  3. Add stats.
  4. Add save, load, and unsaved-quit warning.

That sequence mirrors how real text tools evolve.

Signs Your Model Is Working

  • You can explain where data lives in one sentence.
  • You can add a new command without touching everything.
  • You can debug by tracing state before and after one command.

That’s a strong beginner architecture.