The complete library, one file
Both of Asha's loans are due on the same day; Stream.sorted is stable for ordered streams, so they keep the order in which they were borrowed. Asha returns Dune 5 days late: 5 x 10 = 50.
Build a small library system step by step: enums for fixed rules, records for data, a sealed interface for results, collections for state, Optional for lookups, exceptions for errors, java.time for due dates and fines, and streams for reports, ending in one runnable file you can extend.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Extension 1, reservations: when no copy is left, put the member in a first-come, first-served queue for that ISBN; on return, hand the copy to the first member waiting instead of putting it back on the shelf.
Extension 2, rules: add a GUEST member type (1 book for 7 days) and cap any single fine at 200. Print the due date for each member type and the fine for 0, 3 and 30 days late.
Extension 3, report: return the top N most borrowed titles from a borrow history, highest count first, ties broken alphabetically.
Extension 4, concurrency: 100 tasks on an 8-thread pool all try to borrow the last copy of one book. Make sure exactly one succeeds.
Explain it without notes
Why are Book, Member and Loan records, while Library is a normal class?
Why does borrow return a sealed result for a refused borrow but throw an exception for an unknown ISBN?
Why does the program pass dates in instead of calling LocalDate.now()?
How would you make this library safe for many threads borrowing at once?
Which collections did you choose for the catalog, copy counts and loans, and why?
Expected output
borrowed Dune until 2026-03-15
rejected: no copies of Dune left
borrowed Cosmos until 2026-03-15
rejected: Asha already has 2 books
borrowed Cosmos until 2026-03-31
error: no book with ISBN 999
overdue on 2026-03-20: [Asha/Dune, Asha/Cosmos]
Asha returns Dune, fine 50
borrowed Dune until 2026-04-17
Ravi returns Cosmos, fine 0
borrow counts: {Cosmos=2, Dune=2}
find 333: SPQR
find 444: none