Lesson 8 of 9
Mini Project: Library System - Part 3 (Type Hierarchy)
Refactor the Library System to use a proper inheritance hierarchy with an abstract LibraryItem, concrete Book/EBook/AudioBook subclasses, Searchable and Rateable interfaces, and polymorphic checkout and reporting.
Architecture Applied
- Abstract LibraryItem - shared borrow/return/id/title + abstract getDisplaySummary, calculateLateFee, getCategory
- Book extends LibraryItem, implements Searchable + Rateable
- EBook extends LibraryItem - overrides borrow() for concurrent readers
- AudioBook extends LibraryItem - different late fee
- LibraryCatalog holds List<LibraryItem> - polymorphic search and report
- Sealed LibraryItemType for type-safe categorisation
Updated Checkout Service
import java.util.*;
public class CheckoutService {
private final List<LibraryItem> catalog;
private final Map<String, List<LibraryItem>> memberBorrowings = new HashMap<>();
public CheckoutService(List<LibraryItem> catalog) {
this.catalog = catalog;
}
public boolean checkout(String itemId, String memberId) {
LibraryItem item = findById(itemId)
.orElseThrow(() -> new NoSuchElementException("Item not found: " + itemId));
if (!item.borrow()) {
System.out.println("UNAVAILABLE: " + item.getTitle());
return false;
}
memberBorrowings.computeIfAbsent(memberId, k -> new ArrayList<>()).add(item);
System.out.printf("CHECKED OUT: '%s' → %s%n", item.getTitle(), memberId);
return true;
}
public void returnItem(String itemId, String memberId) {
List<LibraryItem> borrowed = memberBorrowings.getOrDefault(memberId, List.of());
borrowed.stream()
.filter(i -> i.getId().equals(itemId))
.findFirst()
.ifPresent(item -> {
item.returnItem();
borrowed.remove(item);
System.out.printf("RETURNED: '%s' by %s%n", item.getTitle(), memberId);
});
}
public void printReport() {
System.out.println("\n=== LIBRARY REPORT ===");
for (LibraryItem item : catalog) {
System.out.println(item.getDisplaySummary());
double lateFee = item.calculateLateFee(7);
System.out.printf(" 7-day late fee: $%.2f%n", lateFee);
}
}
private Optional<LibraryItem> findById(String id) {
return catalog.stream().filter(i -> i.getId().equals(id)).findFirst();
}
}Quick Quiz
Tip
Tip
Practice Mini Project Library System Part 3 Type Hierarchy in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mini Project: Library System - Part 3 (Type Hierarchy)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Library System Part 3 Type Hierarchy from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Mini Project Library System Part 3 Type Hierarchy is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Refactor the Library System to use a proper inheritance hierarchy with an abstract LibraryItem, concrete Book/EBook/AudioBook subclasses, Searchable and Rateable interfaces, and polymorphic checkout and reporting.
- Abstract LibraryItem - shared borrow/return/id/title + abstract getDisplaySummary, calculateLateFee, getCategory
- Book extends LibraryItem, implements Searchable + Rateable
- EBook extends LibraryItem - overrides borrow() for concurrent readers
Finished this lesson?