Practice ยท 1 of 3
In-Memory Expense Repository
Implement in Solution:
- record Expense(String id, String category, int cents)
- static class ExpenseRepository with:
- Expense save(Expense e) (upsert by id)
- Optional<Expense> findById(String id)
- List<Expense> findByCategory(String category) (insertion order)
- boolean deleteById(String id)
- List<Expense> findAll() (insertion order)
- int totalCents()
- static class DuplicateExpenseException extends RuntimeException.
save acts as a category-guarded upsert: same id + same category
replaces the entry (updates allowed); same id + DIFFERENT category
throws DuplicateExpenseException (an id is bound to one category).
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Persistence Lab