Skip to content
Codewilds

Lesson 34 of 34 · Collections & Exceptions

Final Boss: Java Library Registry with Maps, Sets and Exceptions

Boss battlehard100 XP2 min read

Valdris the Bytecode Lich blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.

At the summit of the Bytecode Highlands stands the Great Archive, and Valdris the Bytecode Lich has cursed it: books vanish, loans are forgotten, members are counted twice. Only a flawless registry can break the curse. This is the final battle — everything you have learned, in one class.

Battle plan

The archive needs to answer questions fast, so pick the right collection for each job:

  • a HashSet of member names — registering the same member twice must fail,
  • a HashMap from ISBN to Book (a record) and another from ISBN to the member who borrowed it,
  • a custom checked exception for the one failure a caller is expected to handle — a book that is already on loan,
  • IllegalArgumentException for programming mistakes like unknown members,
  • a TreeMap and streams for sorted reports.

Book and BookUnavailableException are already written for you. Validate the input first, and change the library's state only when everything is valid — a failed call must leave the archive exactly as it was.

Assignment

Complete the class Library:

Method Behaviour
void registerMember(String name) Adds a member. If the name is already registered, throw IllegalArgumentException("member already registered: " + name).
void addBook(Book book) Adds a book. If a book with the same ISBN exists, throw IllegalArgumentException("duplicate isbn: " + isbn).
void borrow(String member, String isbn) throws BookUnavailableException Lends the book to the member. Check in this order: unknown member → IllegalArgumentException("unknown member: " + member); unknown ISBN → IllegalArgumentException("unknown book: " + isbn); book already on loan → BookUnavailableException(title + " is already borrowed"). Every successful borrow adds 1 to the member's loan count.
void returnBook(String isbn) Ends the loan. If the ISBN is not on loan, throw IllegalArgumentException("book is not borrowed: " + isbn).
boolean isAvailable(String isbn) true if the library owns the book and it is not on loan; false otherwise (also for unknown ISBNs).
Set<String> borrowedBy(String member) Titles of the books the member currently has, sorted alphabetically (empty set if none).
Map<String, Integer> booksPerAuthor() Number of books per author, with the authors in alphabetical order.
String topBorrower() The member with the most successful borrows in total (returning a book does not lower the count). Ties go to the alphabetically first name; null if nobody has borrowed anything yet.
List<String> availableTitles() Titles of all books that are not on loan, sorted alphabetically.

Example:

Library archive = new Library();
archive.registerMember("Ann");
archive.addBook(new Book("111", "Dune", "Herbert"));
archive.borrow("Ann", "111");
archive.isAvailable("111");     // false
archive.borrow("Ann", "111");   // throws BookUnavailableException: "Dune is already borrowed"

Every passing test strikes Valdris. Make all ten pass to free the Archive — and to finish the Bytecode Highlands!

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.

Lesson last updated on .

Write code ↓
Java+100 XP

Lesson 34 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.