Project: The Plan
What we will build.
Let's finish the course by building a real program, a to do list for the terminal. It adds tasks, marks them done, shows them, and saves them to a file.
- A to-do list
- Add, complete, list
- Saved to a file
The finished program
Here's the main method of the finished program. It loads saved tasks, runs a menu, and saves when we quit. Let's watch it work, before we build it. Run it. We add a task, and mark task five as done. Then we list all six tasks, and quit. The tasks are saved.
public class Main {
public static void main(String[] args) throws Exception {
TaskStore store = new TaskStore("tasks.txt");
TaskList list = new TaskList();
store.load().forEach(list::restore);
Menu.run(list);
store.save(list.all());
System.out.println("Saved " + list.all().size() + " tasks. Bye!");
}
}add, done, list or quit: add Title: Buy milk add, done, list or quit: done Id: 5 add, done, list or quit: list 1 [ ] !!! Pay rent 2 [x] ! Water plants 3 [ ] !! Book dentist; maybe Friday 4 [x] !! Email Sam 5 [x] !!! Finish report 6 [ ] !! Buy milk add, done, list or quit: quit Saved 6 tasks. Bye!
Also uses Task.java
public record Task(int id, String title, int priority, boolean done) {
public Task {
if (title.isBlank()) throw new IllegalArgumentException("Title can't be empty");
if (priority < 1 || priority > 3) throw new IllegalArgumentException("1-3 only");
}
public Task complete() {
return new Task(id, title, priority, true);
}
@Override
public String toString() {
String box = done ? "[x]" : "[ ]";
return String.format("%2d %s %-3s %s", id, box, "!".repeat(priority), title);
}
}Also uses TaskList.java
import java.util.*;
public class TaskList {
private final List<Task> tasks = new ArrayList<>();
private int nextId = 1;
public Task add(String title, int priority) {
Task t = new Task(nextId++, title, priority, false);
tasks.add(t);
return t;
}
public void complete(int id) {
tasks.replaceAll(t -> t.id() == id ? t.complete() : t);
}
public boolean remove(int id) { return tasks.removeIf(t -> t.id() == id); }
public List<Task> all() { return List.copyOf(tasks); }
public void restore(Task t) { tasks.add(t); nextId = Math.max(nextId, t.id() + 1); }
}Also uses TaskStore.java
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
public class TaskStore {
private final Path file;
public TaskStore(String name) { file = Path.of(name); }
public void save(List<Task> tasks) throws IOException {
List<String> lines = new ArrayList<>();
for (Task t : tasks) {
lines.add(t.id() + ";" + t.priority() + ";" + t.done() + ";" + t.title());
}
Files.write(file, lines);
}
public List<Task> load() throws IOException {
List<Task> out = new ArrayList<>();
if (!Files.exists(file)) return out;
for (String line : Files.readAllLines(file)) {
String[] p = line.split(";", 4);
int id = Integer.parseInt(p[0]), prio = Integer.parseInt(p[1]);
out.add(new Task(id, p[3], prio, Boolean.parseBoolean(p[2])));
}
return out;
}
}Also uses Menu.java
import java.util.Scanner;
public class Menu {
private static final Scanner in = new Scanner(System.in);
public static void run(TaskList list) {
while (true) {
String cmd = ask("add, done, list or quit: ");
if (cmd.equals("quit")) return;
switch (cmd) {
case "add" -> list.add(ask("Title: "), 2);
case "done" -> list.complete(Integer.parseInt(ask("Id: ")));
case "list" -> list.all().forEach(System.out::println);
default -> System.out.println("Unknown command: " + cmd);
}
}
}
static String ask(String q) { System.out.print(q); return in.nextLine(); }
}Also uses tasks.txt
1;3;false;Pay rent
2;1;true;Water plants
3;2;false;Book dentist; maybe Friday
4;2;true;Email Sam
5;3;false;Finish reportThe four classes
The program is split into four small classes. Each one has a single job. This makes every piece easy to understand, and to test.
| Class | Job |
|---|---|
| Task | one task: id, title, priority, done |
| TaskList | the tasks in memory |
| TaskStore | save to and load from a file |
| Menu | talk to the user |
How the pieces connect
Main creates the store, and the list. The store loads saved tasks into the list. The menu changes the list, based on what the user types. And at the end, the store saves the list again.
- Main
- TaskStore.load
- TaskList
- Menu
- TaskStore.save
Summary
We'll build a to do list with four classes, Task, TaskList, TaskStore, and Menu. Next, the Task record.
Key takeaways- Task
- TaskList
- TaskStore + Menu