Learn Java · lesson 46 of 61 · about 8 minutes
Capstone: Planning and Modelling
Plan the closing duty board and model duties with a record and a DutyBoard class.
What you'll learn
- Turn an idea into features
- Model data with a record
- Wrap a list in a class
The duty board
When the last film ends, the staff work through the closing duties: clean the screens, count the till, restock the popcorn. The capstone is a duty board for them. A Duty is a record with a title, a priority from 1 (do it now) to 3 (if there's time) and whether it's done, and its compact constructor refuses any other priority. A DutyBoard keeps every duty in a private list, so nothing outside can change it except through the board's own methods. Duties are typed as 1 Clean screen 2, a priority and then the title, until done.
The program
Your capstone is a task manager:
1. Add tasks with a title and a priority from 1 (urgent) to 3 (whenever)
2. Mark tasks done
3. List what's left, most urgent first
4. Save to a file and load it back
Records
A record is a compact class for plain data. record Task(String title, int priority, boolean done) {} gives you a constructor, getters named title(), priority() and done(), plus equals and a readable toString, all automatically.
Records can't be changed after creation. To "change" one, make a new one: new Task(t.title(), t.priority(), true).
The model
import java.util.ArrayList;
import java.util.List;
record Task(String title, int priority, boolean done) {
Task {
if (priority < 1 || priority > 3) throw new IllegalArgumentException("priority must be 1-3");
}
Task markDone() {
return new Task(title, priority, true);
}
}
class TaskList {
private final List<Task> tasks = new ArrayList<>();
void add(String title, int priority) {
tasks.add(new Task(title, priority, false));
}
int size() {
return tasks.size();
}
Task get(int i) {
return tasks.get(i);
}
}
public class Main {
public static void main(String[] args) {
TaskList list = new TaskList();
list.add("Feed Phoebe", 1);
list.add("Write capstone", 2);
System.out.println(list.size() + " tasks");
System.out.println(list.get(0));
System.out.println(list.get(0).markDone().done());
}
}Output
2 tasks
Task[title=Feed Phoebe, priority=1, done=false]
trueTip
The block Task { ... } is a compact constructor: it runs checks before the record's fields are set.
A priority, then the title
line.split(" ", 2) cuts a line at its first space only, so 1 Clean screen 2 becomes 1 and Clean screen 2, and the title keeps its own spaces. Integer.parseInt turns the first part into a number, and throws a NumberFormatException if it isn't one. That's a kind of IllegalArgumentException, so it's caught first: a line that isn't a duty at all and a duty with a priority of 5 get different messages.
Two kinds of bad line
public class Main {
public static void main(String[] args) {
for (String line : new String[] {"1 Clean screen 2", "x Mop", "5 Polish the sign"}) {
String[] parts = line.split(" ", 2);
try {
int priority = Integer.parseInt(parts[0]);
if (priority < 1 || priority > 3) {
throw new IllegalArgumentException("priority must be 1 to 3");
}
System.out.println("P" + priority + ": " + parts[1]);
} catch (NumberFormatException e) {
System.out.println("Not a duty: " + line);
} catch (IllegalArgumentException e) {
System.out.println("Skipped " + parts[1] + ": " + e.getMessage());
}
}
}
}Output
P1: Clean screen 2
Not a duty: x Mop
Skipped Polish the sign: priority must be 1 to 3Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.