Return after two or three days, then again a week later. Try the tasks from memory before reopening the earlier source. Delayed retrieval distinguishes an idea you can use independently from one you merely recognize while reading. This review is extra time beyond Day 7's main work.
Understand the contract
Start by explaining the contracts aloud: normalized values, explicit outcomes, staged state, deterministic reports and independent tests. For each, name a plausible defect and a test that distinguishes it. You do not need to remember every collector overload; you should know what behavior to ask for and how to verify it.
The first trace checks decimal representation, not calculation accuracy. (Its setScale(2) would throw, not round, if a value had a third decimal place.) The second checks collection ownership, not value equality. Predict both before running, then state which earlier lesson resolves each surprise. If you need a hint, record that fact and repeat just that skill on your next review date.
Choose manageable exercises rather than rebuilding the whole project every time. Fifteen focused minutes spent reproducing a rollback test can teach more than rereading every source file without making a prediction. Your completion checkmark records a visit, not a proof of mastery or a backup of edited code.
Before running, write the output you expect and explain which rule produces each line. Each editor is an independent program.
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("3.0");
BigDecimal b = new BigDecimal("3.00");
System.out.println(a.compareTo(b) == 0);
System.out.println(a.equals(b));
System.out.println(a.setScale(2).equals(b));
}
}Check your prediction
true
false
trueTrace a different case
A snapshot can be unmodifiable and independent while still sharing element references. With immutable string elements that is enough. If the elements had setters, this would protect membership only. Trace which list receives each operation and avoid describing every copy as “deep.”
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> source = new ArrayList<>(List.of("A"));
List<String> snapshot = List.copyOf(source);
source.add("B");
System.out.println(snapshot);
System.out.println(source);
}
}Check your prediction
[A]
[A, B]Build it yourself
Come Back Later: a staged batch from memory
This task combines the course's core ideas in one method: normalized decimals, a staged copy, replay versus conflict, and a sealed outcome. Try it without reopening Days 2 to 4.
Write stage(Map<String, BigDecimal> current, List<Row> batch). Each Row has an
ID and an amount as text. Normalize every amount to two places without rounding.
An absent ID inserts; an equal normalized amount is a replay; a different
amount is a conflict. Return Committed(next, inserted, replayed) on success
or Refused(row, reason) at the first problem, with rows numbered from 1. The
caller's current must never change.
Batch, from {A=2.00} | Expected line |
|---|---|
| B "1.5", A "2.0" | Committed {A=2.00, B=1.50}: 1 inserted, 1 replayed |
| C "4", A "9.00" | Refused at row 2: conflicting id A |
| D "1.005" | Refused at row 1: more than two decimal places |
| empty | Committed {A=2.00}: 0 inserted, 0 replayed |
The workspace prints Current: last, which must still be {A=2.00}.
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, BigDecimal> current = new LinkedHashMap<>();
current.put("A", new BigDecimal("2.00"));
System.out.println(display(stage(current, List.of(new Row("B", "1.5"), new Row("A", "2.0")))));
System.out.println(display(stage(current, List.of(new Row("C", "4"), new Row("A", "9.00")))));
System.out.println(display(stage(current, List.of(new Row("D", "1.005")))));
System.out.println(display(stage(current, List.of())));
System.out.println("Current: " + current);
}
static Outcome stage(Map<String, BigDecimal> current, List<Row> batch) {
// Stage on a copy; normalize each amount; return Refused at the first problem.
return new Committed(current, 0, 0);
}
static String display(Outcome outcome) {
return switch (outcome) {
case Committed committed -> "Committed " + committed.next() + ": "
+ committed.inserted() + " inserted, " + committed.replayed() + " replayed";
case Refused refused -> "Refused at row " + refused.row() + ": " + refused.reason();
};
}
}
record Row(String id, String amount) {}
sealed interface Outcome permits Committed, Refused {}
record Committed(Map<String, BigDecimal> next, int inserted, int replayed) implements Outcome {
Committed {
next = Collections.unmodifiableMap(new LinkedHashMap<>(next));
}
}
record Refused(int row, String reason) implements Outcome {}Hint 1: the idea
Three earlier lessons meet here. The second row is a late conflict: C is already staged when A fails, so the staging must happen on a copy. And "2.0" must count as a replay of 2.00, so normalize before comparing.
Hint 2: a first step
Stage on a copy of current and loop over the rows by index, so a refusal can
name its row. Get the replay and conflict rules right with amounts already
written to two places first; then decide how a third decimal place should be
refused without rounding it away, and which exception that produces.
Show one solution
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, BigDecimal> current = new LinkedHashMap<>();
current.put("A", new BigDecimal("2.00"));
System.out.println(display(stage(current, List.of(new Row("B", "1.5"), new Row("A", "2.0")))));
System.out.println(display(stage(current, List.of(new Row("C", "4"), new Row("A", "9.00")))));
System.out.println(display(stage(current, List.of(new Row("D", "1.005")))));
System.out.println(display(stage(current, List.of())));
System.out.println("Current: " + current);
}
static Outcome stage(Map<String, BigDecimal> current, List<Row> batch) {
Map<String, BigDecimal> candidate = new LinkedHashMap<>(current);
int inserted = 0;
int replayed = 0;
for (int index = 0; index < batch.size(); index++) {
Row row = batch.get(index);
BigDecimal amount;
try {
amount = new BigDecimal(row.amount()).setScale(2, RoundingMode.UNNECESSARY);
} catch (ArithmeticException error) {
return new Refused(index + 1, "more than two decimal places");
}
BigDecimal old = candidate.get(row.id());
if (old == null) {
candidate.put(row.id(), amount);
inserted++;
} else if (old.equals(amount)) {
replayed++;
} else {
return new Refused(index + 1, "conflicting id " + row.id());
}
}
return new Committed(candidate, inserted, replayed);
}
static String display(Outcome outcome) {
return switch (outcome) {
case Committed committed -> "Committed " + committed.next() + ": "
+ committed.inserted() + " inserted, " + committed.replayed() + " replayed";
case Refused refused -> "Refused at row " + refused.row() + ": " + refused.reason();
};
}
}
record Row(String id, String amount) {}
sealed interface Outcome permits Committed, Refused {}
record Committed(Map<String, BigDecimal> next, int inserted, int replayed) implements Outcome {
Committed {
next = Collections.unmodifiableMap(new LinkedHashMap<>(next));
}
}
record Refused(int row, String reason) implements Outcome {}Expected output:
Committed {A=2.00, B=1.50}: 1 inserted, 1 replayed
Refused at row 2: conflicting id A
Refused at row 1: more than two decimal places
Committed {A=2.00}: 0 inserted, 0 replayed
Current: {A=2.00}All work happens on candidate, so returning Refused at row 2 simply abandons
the staged C, and current prints {A=2.00} at the end. Normalizing before
the comparison is what makes "2.0" a replay: without setScale, equals would see
scale 1 against scale 2 and report a conflict. UNNECESSARY turns 1.005 into a
refusal instead of silently storing 1.01. The Committed record copies its map
into an unmodifiable LinkedHashMap, so callers can neither change it nor lose its
order; Map.copyOf would not promise any order.
Change it and test again
Choose two additional retrieval tasks: rebuild an Optional lookup; add a third
outcome, Empty, to the staged batch and let the compiler show you every switch
to update; compare a decimal report with a loop oracle; or write a generic
distinctCopy(List<T>) that keeps first-seen nonnull values in order. Add one deliberately broken version and prove your check catches it. Then revisit the ledger extension without copying your earlier solution.
Explain before moving on
Record what you completed unaided, which hint you used and a specific next practice date. For local study afterward, choose one focused subject—packages and build tools, JUnit, real file adapters, concurrency or persistence—and preserve this course's contract-testing habits as the tools become larger.
Completion is saved in this browser only. It does not measure mastery.