AI Hints
Home›Java Tutorial›Project: The Plan, The Task Record and The TaskList Class
Java · Lesson 112 of 120

Project: The Plan

What we will build.

⏱ 2 min read▶ Video at 2:22:55🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

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.

Main.javajava
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!");
    }
}
Input typed: add · Buy milk · done · 5 · list · quit
Output
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 report

The 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.

ClassJob
Taskone task: id, title, priority, done
TaskListthe tasks in memory
TaskStoresave to and load from a file
Menutalk 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.

  1. Main
  2. TaskStore.load
  3. TaskList
  4. Menu
  5. 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
Next up: Project: The Task Record — one task