Transfer the ideas to a different domain without copying the task tracker. Build an in-memory room-slot reservation book that can also import reservations from text. No times, networking or database are required.
Spend about 30 minutes designing, coding and checking before opening hints. Nothing on this page shows how to write the class; the two warm-ups below only check two library facts you will need.
The brief
A slot name (such as Morning) maps to at most one owner. Write a Reservations class:
reserve(slot, owner)trims both, rejects null or blank values with IllegalArgumentException, returns false if the slot is already taken (the first owner stays), and true otherwise;cancel(slot)returns whether there was a reservation to remove;snapshot()returns a copy the caller may change freely;importFrom(BufferedReader reader)readsslot;ownerlines until EOF, skips blank lines, reserves each good line, and returns a list of problems, one per bad line, written asline N: reasonwith physical line numbers. A bad line never stops the import. The caller owns the reader, and importFrom declares IOException rather than hiding it.
For the input
Morning;Ada
(blank line)
Evening
Morning;Grace
Noon;
Noon ; Lin main prints each problem, then the snapshot:
line 3: expected slot;owner
line 4: Morning already reserved
line 5: nonblank value required
{Morning=Ada, Noon=Lin}Warm-ups: two library facts
split(";", -1) keeps empty fields, so you can count fields reliably. Predict the three lengths:
public class Main {
public static void main(String[] args) {
System.out.println("Morning;Ada".split(";", -1).length);
System.out.println("Noon;".split(";", -1).length);
System.out.println("Evening".split(";", -1).length);
}
}Check your prediction
2
2
1Map.put on a key that is already present does not refuse: it replaces the value and returns the old one. Your reserve must not behave like this.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Map<String, String> slots = new LinkedHashMap<>();
System.out.println(slots.put("Morning", "Ada"));
System.out.println(slots.put("Morning", "Grace"));
System.out.println(slots);
}
}Check your prediction
null
Ada
{Morning=Grace}Acceptance cases
Turn each row into a labelled check in main, and finish with Reservation checks passed.
| Case | Expected |
|---|---|
| import the sample input | the three problems and snapshot shown above |
reserve("Morning", "Grace") afterwards | false; Morning is still Ada |
| clear a snapshot, then take a new one | still 2 entries |
reserve("Evening", " ") | IllegalArgumentException; still 2 entries |
cancel(" Noon "), then cancel("Noon") | true, then false |
reserve("Noon", "Grace") after cancelling | true |
Check your work
Before you open the hints or the solution, score your version honestly:
- Does every acceptance row have its own labelled check, and does the success line print only after all of them?
- After each rejected call, do you read the book again to prove nothing changed?
- Does importFrom report the right physical line numbers when there are blank lines?
- Could a caller damage the book through snapshot(), or through the reader?
- Is each rule written in exactly one place (for example, trimming only inside one helper)?
Any "no" is the next thing to fix.
Your turn
On Your Own: Reservations
Implement the brief, print the sample output, and check every acceptance case.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String text = "Morning;Ada\n\nEvening\nMorning;Grace\nNoon; \n Noon ; Lin \n";
System.out.println("Your reservation book");
}
}Hint 1: the idea
Build it in two layers. reserve, cancel and snapshot are the model and know nothing about text lines. importFrom is a boundary: it turns each line into one reserve call and turns every way that can go wrong into a problem message.
Hint 2: a first step
Write reserve and its first two acceptance checks before anything else. Then
write importFrom with the read loop from Day 6 and a line counter that
increases before the blank check. Note that reserve gives two different
signals: a false result and an exception. Each needs its own message.
Show one solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Reservations book = new Reservations();
String text = "Morning;Ada\n\nEvening\nMorning;Grace\nNoon; \n Noon ; Lin \n";
List<String> problems;
try (BufferedReader reader = new BufferedReader(new StringReader(text))) {
problems = book.importFrom(reader);
} catch (IOException error) {
throw new IllegalStateException("reading reservations failed", error);
}
for (String problem : problems) {
System.out.println(problem);
}
System.out.println(book.snapshot());
check("occupied keeps owner", !book.reserve("Morning", "Grace"));
check("owner unchanged", book.snapshot().get("Morning").equals("Ada"));
book.snapshot().clear();
check("snapshot isolated", book.snapshot().size() == 2);
check("blank rejected", rejects(() -> book.reserve("Evening", " ")));
check("rejection changes nothing", book.snapshot().size() == 2);
check("cancel existing", book.cancel(" Noon "));
check("cancel again", !book.cancel("Noon"));
check("slot free again", book.reserve("Noon", "Grace"));
System.out.println("Reservation checks passed");
}
static boolean rejects(Runnable action) {
try {
action.run();
return false;
} catch (IllegalArgumentException expected) {
return true;
}
}
static void check(String label, boolean condition) {
if (!condition) {
throw new AssertionError(label);
}
}
}
final class Reservations {
private final Map<String, String> entries = new LinkedHashMap<>();
boolean reserve(String slot, String owner) {
String key = required(slot);
String name = required(owner);
if (entries.containsKey(key)) {
return false;
}
entries.put(key, name);
return true;
}
boolean cancel(String slot) {
return entries.remove(required(slot)) != null;
}
Map<String, String> snapshot() {
return new LinkedHashMap<>(entries);
}
/** Reads slot;owner lines. Keeps going after a bad line; the caller owns the reader. */
List<String> importFrom(BufferedReader reader) throws IOException {
List<String> problems = new ArrayList<>();
int lineNumber = 0;
String line = reader.readLine();
while (line != null) {
lineNumber++;
if (!line.trim().isEmpty()) {
String[] fields = line.split(";", -1);
try {
if (fields.length != 2) {
throw new IllegalArgumentException("expected slot;owner");
}
if (!reserve(fields[0], fields[1])) {
problems.add("line " + lineNumber + ": " + fields[0].trim() + " already reserved");
}
} catch (IllegalArgumentException error) {
problems.add("line " + lineNumber + ": " + error.getMessage());
}
}
line = reader.readLine();
}
return problems;
}
private String required(String value) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException("nonblank value required");
}
return value.trim();
}
}Expected output:
line 3: expected slot;owner
line 4: Morning already reserved
line 5: nonblank value required
{Morning=Ada, Noon=Lin}
Reservation checks passedThe containsKey check before put is what keeps Ada: a plain put would have
replaced her, as the warm-up showed. importFrom treats the two kinds of failure
differently on purpose: an occupied slot is an ordinary false and becomes
"already reserved", while malformed text throws and the per-line catch turns it
into a message. Line 6 is accepted because reserve trims, which is why the
snapshot shows Noon=Lin. The rejects helper takes a Runnable, Java's
functional interface for an action with no parameters and no result, so a
lambda such as () -> book.reserve("Evening", " ") can be passed in and run
inside the try.
Change it and test again
Choose one extension after the core checks pass: an owner-filtered read-only report built with a stream, or a configurable reservation policy through an interface (for example, a limit on slots per owner). Write acceptance cases first. Keep the original book's behavior intact while adding the new feature.
Explain before moving on
Compare this design with the task tracker: which ideas transferred, and which task-specific rules did you deliberately leave behind? Identify one decision you can now explain without opening an earlier solution.
Completion is saved in this browser only. It does not measure mastery.