Codiqo

Learn Java · lesson 49 of 61 · about 10 minutes

Capstone: Polishing

Format the closing report, add a summary, and review the design.

What you'll learn

  • Format a report with printf
  • Summarise with streams
  • Review code for clarity

The closing report

The last touch is a report the manager reads before locking up. Every duty goes in one table, sorted with a Comparator that puts the ones still to do first (false comes before true), then the most urgent, then by title. printf lines it up with %-20s and %-8s, which pad on the right, and a stream does the counting: filter(Duty::done).count() gives how many are done, printed with a percentage, using %% for the percent sign.

Streams for summaries

A stream processes a collection in steps:

tasks.stream().filter(Task::done).count() counts the done tasks.

map, filter, sorted and count cover most everyday summaries.

The final report

import java.util.Comparator;
import java.util.List;

record Task(String title, int priority, boolean done) {}

public class Main {
    public static void main(String[] args) {
        List<Task> tasks = List.of(
            new Task("Tidy desk", 3, false),
            new Task("Feed Phoebe", 1, true),
            new Task("Write capstone", 2, false),
            new Task("Fix bug #12", 1, false)
        );
        System.out.printf("%-16s %-8s %s%n", "Task", "Priority", "Status");
        tasks.stream()
            .sorted(Comparator.comparing(Task::done).thenComparingInt(Task::priority))
            .forEach(t -> System.out.printf("%-16s %-8s %s%n", t.title(), "P" + t.priority(), t.done() ? "done" : "todo"));
        long done = tasks.stream().filter(Task::done).count();
        System.out.printf("%d of %d done (%d%%)%n", done, tasks.size(), done * 100 / tasks.size());
    }
}

Output

Task             Priority Status
Fix bug #12      P1       todo
Write capstone   P2       todo
Tidy desk        P3       todo
Feed Phoebe      P1       done
1 of 4 done (25%)

Multiply first

doneCount * 100 / everything.size() multiplies before it divides. Both numbers are whole numbers, so dividing first, 1 / 4, would already be 0 before the 100 came in. The report also checks for an empty board first, because dividing a whole number by 0 throws an ArithmeticException.

The order of the sums

public class Main {
    public static void main(String[] args) {
        int done = 3;
        int all = 4;
        System.out.println(done * 100 / all + "% " + done / all * 100 + "%");
    }
}

Output

75% 0%

A final review

Before calling it done:

  • Does each class have one clear job?
  • Are fields private, and data immutable where possible (records)?
  • Are bad inputs rejected with clear exceptions?
  • Do the checks still pass?

Tip

You've built a real Java program with records, classes, collections, exceptions, files, tests and streams. Try adding due dates with java.time.LocalDate next.

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