Capstone 2: a task model

The second project is a task list: a task with an id, a title, a status and a priority, a store that keeps them in memory, and the operations a command line will call in lesson 133. It is the shape of a small business program, and every part of it has appeared in the course already.

The status enum

A task is open, done or dropped, and that is a closed list of three, which is an enum of lesson 044. The status is part of the value and not a boolean, because dropped is not the same as done and a program that models both as false loses the difference.

The task record

The record has four components and a compact constructor that refuses an empty title and a priority outside 1 to 5. Its withStatus returns a new record, which is the immutable update of lesson 042: the store replaces the element instead of changing it, and a task that somebody holds a reference to keeps the status it was read with.

import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Tasks { enum Status { OPEN, DONE, DROPPED } record Task(int id, String title, Status status, int priority) { Task { if (title.isBlank()) { throw new IllegalArgumentException("empty title"); } if (priority < 1 || priority > 5) { throw new IllegalArgumentException("priority " + priority + " outside 1..5"); } } Task withStatus(Status next) { return new Task(id, title, next, priority); } } static class Store { private final List<Task> tasks = new ArrayList<>(); private int nextId = 1; Task add(String title, int priority) { Task task = new Task(nextId++, title, Status.OPEN, priority); tasks.add(task); return task; } boolean complete(int id) { for (int i = 0; i < tasks.size(); i++) { if (tasks.get(i).id() == id) { tasks.set(i, tasks.get(i).withStatus(Status.DONE)); return true; } } return false; } List<Task> byStatus(Status status) { List<Task> result = new ArrayList<>(); for (Task task : tasks) { if (task.status() == status) { result.add(task); } } return result; } List<Task> byPriority() { List<Task> sorted = new ArrayList<>(tasks); sorted.sort(Comparator.comparingInt(Task::priority).thenComparing(Task::title)); return sorted; } int size() { return tasks.size(); } } public static void main(String[] args) { Store store = new Store(); store.add("write the report", 2); store.add("read the spec", 1); store.add("answer mail", 3); System.out.println("after add: " + store.size() + " tasks, " + store.byStatus(Status.OPEN).size() + " open"); System.out.println("complete 2: " + store.complete(2)); System.out.println("complete 9: " + store.complete(9)); System.out.println("open: " + store.byStatus(Status.OPEN)); System.out.println("done: " + store.byStatus(Status.DONE)); for (Task task : store.byPriority()) { System.out.println(task.priority() + " " + task.id() + " " + task.title() + " " + task.status()); } try { store.add(" ", 3); } catch (IllegalArgumentException e) { System.out.println("rejected: " + e.getMessage()); } try { store.add("bad priority", 9); } catch (IllegalArgumentException e) { System.out.println("rejected: " + e.getMessage()); } } }

javac -Xlint:all Tasks.java
java Tasks

after add: 3 tasks, 3 open
complete 2: true
complete 9: false
open: [Task[id=1, title=write the report, status=OPEN, priority=2], Task[id=3, title=answer mail, status=OPEN, priority=3]]
done: [Task[id=2, title=read the spec, status=DONE, priority=1]]
1 2 read the spec DONE
2 1 write the report OPEN
3 3 answer mail OPEN
rejected: empty title
rejected: priority 9 outside 1..5

The store

The store holds a list and the next id, and it is the only class that may change the list. add builds the task and answers it, which is what a caller wants to print; complete answers a boolean, because an id that is not there is an expected answer and not an error, which is the rule of lesson 096.

Two queries complete the set: byStatus filters and byPriority sorts a copy. The sort copies first because the store keeps its own order, which is the order of insertion and the order a file will preserve in the next lesson.

The operations

The first three lines are the state after the three adds: three tasks, all open, and the second one completed. Completing an id that does not exist answers false and changes nothing, which is the complete 9: false line.

The two lists are the query by status: the open ones keep the insertion order and the done one holds the task that was completed. The three lines after them are the sort by priority: read the spec is first with priority 1, then write the report with 2, then answer mail with 3, which is the order of the numbers and not of the ids.

The rejected tasks

The last two lines are the compact constructor doing its job: a title of three spaces is blank and is refused, and a priority of 9 is outside the range. The message names the value and the range, which is the exception design of lesson 099, and the store's next id did not move for either, because the exception was thrown before the task was added.

A program that reads these from a file or a command line in the next lessons catches the exception and reports the line, which is the shape of the parser of lesson 103. The model is where the rule lives, so every caller obeys it without repeating the check.

Exercises

  1. Add a drop(int id) that sets the status to DROPPED and a query that lists the dropped tasks.
  2. Add a rename(int id, String title) that returns the new task or null.
  3. Sort by status and then by priority and print the result.
  4. Make add refuse a duplicate title and print the message.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why is the status an enum and not a boolean?

    Answer

    There are three states, and dropped is not the same as done

    A closed list of states is what an enum is for, and two of the three are not the negation of each other.

  2. What does withStatus return?

    Answer

    A new task with the same fields and the other status

    A record is immutable, so the update is a new value and the store replaces the element.

  3. Why does complete answer a boolean?

    Answer

    An id that is not there is an expected answer, not an error

    A missing id is a case the caller handles, which is the rule of the exception design lesson.

  4. What does this program print? import java.util.List; public class Q1 { record P(int n) { } static int sum(List<P> values) { int total = 0; for (P p : values) { total += p.n(); } return total; } public static void main(String[] args) { System.out.println(sum(List.of(new P(1), new P(2), new P(3)))); System.out.println(new P(1).equals(new P(1))); } }

    Answer

    6 / true

    The three components sum to six, and the records with the same value are equal.

  5. Why does byPriority sort a copy?

    Answer

    The store keeps the insertion order, which the file of the next lesson preserves

    A query returns a new list, and the store is not reordered by asking a question.

  6. What does a blank title do in the compact constructor?

    Answer

    It throws IllegalArgumentException with the message empty title

    isBlank covers spaces and tabs, and the message names the rule that failed.

  7. What happens to the next id when an add is refused?

    Answer

    It does not move, because the exception came before the add

    The record is built before it is added, so a refusal leaves the store as it was.

  8. What does this program print? public class Q2 { enum S { A, B } record T(S s, int n) { } public static void main(String[] args) { T first = new T(S.A, 1); T second = new T(S.A, 1); System.out.println(first.equals(second)); System.out.println(first.s() == S.A); } }

    Answer

    true / true

    The records are equal and the enum constant is the same object, so both are true.

  9. Which class may change the list of tasks?

    Answer

    The store, which owns the list and offers the operations

    The list is private and the mutations go through the store, which is encapsulation in a small program.

  10. What does Comparator.comparingInt(Task::priority).thenComparing(Task::title) order by?

    Answer

    The priority, and the title when two are equal

    The first comparison decides, and thenComparing is the tie-break of lesson 076.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev

Banner image