Skip to main content

Practice ยท 2 of 3

Milestone 2: The Repository

Add persistence behind an interface in Solution: - interface ExpenseStore { void save(Solution.Expense e); Optional<Solution.Expense> find(String id); List<Solution.Expense> all(); } (referencing the nested record as Solution.Expense โ€” inside Solution you can write Expense directly) - static class InMemoryExpenseStore implements ExpenseStore (insertion order preserved) - static class DuplicateExpenseException extends RuntimeException - ExpenseStore.save throws DuplicateExpenseException when a DIFFERENT expense already occupies the id (idempotent identical re-save is fine); note: category may legitimately change on update โ€” id is the only conflict dimension. Two saves with the same id but different cents = update, allowed. Same id, different category = conflict, throw.

Difficulty: intermediate

Back to lesson: Practice: Capstone Milestones