The model: loans and sealed states
The service is a book-loan desk for a small library. A loan names a member, a title and the day the book is due, and it may have a return day. From those two dates a loan is in one of four states, and the state is what the service answers. This page is the model alone: it builds and runs without a server, a socket or a JSON library.
The domain
Four loans are enough to show every state. One is due in five days, one in one day, one was due six days ago and one has been returned. The date of the demonstration is fixed at 20 November 2026, so the output of this page is the same on every run and a reader can check every number by hand.
The loan record
Loan is a record of five components: the id, the member, the title, the due date and the return date, which is null while the book is out. A record gives the constructor, the accessors, equals, hashCode and toString, and the two methods below are the only code the type needs. The file is the whole program of this page, and it grows in the pages that follow:
package library;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
public class Desk {
record Loan(int id, String member, String title, LocalDate due, LocalDate returned) {
State state(LocalDate today) {
if (returned != null) {
return new Returned(returned);
}
long days = ChronoUnit.DAYS.between(due, today);
if (days > 0) {
return new Overdue(days);
}
if (days >= -2) {
return new DueSoon(-days);
}
return new OnLoan(-days);
}
}
sealed interface State permits OnLoan, DueSoon, Overdue, Returned {
}
record OnLoan(long daysLeft) implements State {
}
record DueSoon(long daysLeft) implements State {
}
record Overdue(long daysLate) implements State {
}
record Returned(LocalDate on) implements State {
}
static final LocalDate TODAY = LocalDate.of(2026, 11, 20);
static final List<Loan> LOANS = List.of(
new Loan(1, "Ada", "Dune", LocalDate.of(2026, 11, 25), null),
new Loan(2, "Grace", "The Dispossessed", LocalDate.of(2026, 11, 21), null),
new Loan(3, "Alan", "Snow Crash", LocalDate.of(2026, 11, 14), null),
new Loan(4, "Edsger", "Programming Pearls", LocalDate.of(2026, 11, 10), LocalDate.of(2026, 11, 18)));
static String describe(State state) {
return switch (state) {
case OnLoan onLoan -> "on_loan " + onLoan.daysLeft();
case DueSoon dueSoon -> "due_soon " + dueSoon.daysLeft();
case Overdue overdue -> "overdue " + overdue.daysLate();
case Returned returned -> "returned " + returned.on();
};
}
public static void main(String[] args) {
for (Loan loan : LOANS) {
System.out.println(loan.id() + " | " + loan.title() + " | " + describe(loan.state(TODAY)));
}
}
}
The sealed state
State is a sealed interface with exactly four permitted records. The set is closed on purpose: a switch over a State is exhaustive, so a method that names the four forms cannot forget one, and a fifth form would stop every such switch from compiling until it is handled. That is the property the JSON and the routes of the next pages rely on.
The four records carry only what the form needs. OnLoan and DueSoon carry the days left, Overdue the days late, and Returned the day the book came back. A boolean and a number would have been shorter and would have lost the difference between a book that is out and a book that is back.
The rule of the date
Loan.state(today) takes the date instead of reading the clock. The rule is four lines: a returned loan is returned; otherwise the signed number of days between the due date and today is positive for late, zero or one or two days before for due soon, and anything earlier for on loan. Taking the date as a parameter is what makes the model testable and the page reproducible; a service that called LocalDate.now() here would answer differently tomorrow.
The switch in describe has no default. It is the exhaustive form of Java 21, and it is checked against the permits clause: if a fifth record were added, this file would not compile until the new case was written. The same shape returns in the JSON writer and in the tests.
The run
The class is compiled into out with the package layout and run with the package name. The four lines are one per loan, in the order of the list:
javac -Xlint:all -d out src/library/Desk.java
java -cp out library.Desk
1 | Dune | on_loan 5
2 | The Dispossessed | due_soon 1
3 | Snow Crash | overdue 6
4 | Programming Pearls | returned 2026-11-18
Why records and a sealed interface
A record is the right shape for a value that is read and never changed: a loan is replaced when it is returned, not mutated. The sealed interface is the right shape for a closed set of forms. Together they give the compiler enough information to check every function that looks at a loan, which is the same design capstone 1 used for a JSON value, applied here to a domain that a person can read without a specification.
The alternative, a class with a boolean returned and a mutable dueDate, compiles too and is what most loan systems looked like before records. It loses the exhaustiveness, it lets a returned loan have no return date, and it makes the state method a set of if statements that a new field can quietly invalidate.
The next page
The next page adds the JSON: the same file gains a writer that turns a loan and its state into text, with no library and no reflection. The Java course covers records and sealed types in lesson 061 and the date arithmetic in lesson 110.
The code on this page was compiled with OpenJDK 21.0.13 (javac) and run on Alpine Linux in a Docker container. The date of the demonstration is fixed, so the output is the same on every run.
Article author: Arthur Isaev