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

Project: The TaskList Class

Many tasks.

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

TaskList keeps all the tasks in memory, and hands out ids. Other code never touches the list directly. It only calls TaskList's methods.

  • Holds the tasks
  • Hands out ids
  • Hides the list

TaskList.java

The list, and the next id, are private. Add creates a task with the next id, and stores it. Complete replaces the matching task with its done copy, using replace all. Remove uses remove if. And all returns a copy of the list, that can't be changed. So nobody can change our tasks behind our back.

TaskList.javajava
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); }
}

Trying it out

We add three tasks, complete number two, and remove number three. Then we print them all. Run it. Two tasks remain. Water plants is done.

Main.javajava
public class Main {
    public static void main(String[] args) {
        TaskList list = new TaskList();
        list.add("Pay rent", 3);
        list.add("Water plants", 1);
        list.add("Email Sam", 2);
        list.complete(2);
        list.remove(3);
        list.all().forEach(System.out::println);
        System.out.println(list.all().size() + " tasks");
    }
}
Output
 1 [ ] !!! Pay rent
 2 [x] !   Water plants
2 tasks
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); }
}

Summary

TaskList holds the tasks privately, hands out ids, and offers add, complete, remove, and all. Next, saving tasks to a file.

Key takeaways
  • private list
  • add, complete, remove
  • all() returns a copy
Next up: Project: Saving Tasks — write them to disk