Lesson 9 of 10
Mini Project: Library System - Part 4 (Core API)
Enhance the Library System with proper type handling using enums for BookStatus and Genre, Optional-returning search methods, date tracking for borrows/returns, and formatted reports using printf and DateTimeFormatter.
Enhancements Applied
- BookStatus enum replaces string status - type-safe, descriptive
- Genre enum categorises books - enables genre-based filtering
- BorrowRecord tracks who borrowed what and when, with due date calculation
- Due date = borrow date + 14 days (LocalDate.plusDays)
- Late fee calculation using ChronoUnit.DAYS.between(dueDate, returnDate)
- Formatted report using printf with aligned columns and DateTimeFormatter
BorrowRecord & Formatted Report
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class BorrowRecord {
private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
private static final int LOAN_DAYS = 14;
private final Book book;
private final String memberId;
private final LocalDate borrowDate;
private final LocalDate dueDate;
private LocalDate returnDate;
public BorrowRecord(Book book, String memberId) {
this.book = book;
this.memberId = memberId;
this.borrowDate = LocalDate.now();
this.dueDate = borrowDate.plusDays(LOAN_DAYS);
}
public void returnBook(LocalDate returnDate) {
this.returnDate = returnDate;
book.returnItem();
}
public double calculateLateFee() {
if (returnDate == null) {
LocalDate ref = LocalDate.now();
long overdue = ChronoUnit.DAYS.between(dueDate, ref);
return Math.max(0, overdue) * book.calculateLateFee(1);
}
long overdue = ChronoUnit.DAYS.between(dueDate, returnDate);
return Math.max(0, overdue) * book.calculateLateFee(1);
}
public boolean isOverdue() {
LocalDate ref = returnDate != null ? returnDate : LocalDate.now();
return ref.isAfter(dueDate);
}
public void printReceipt() {
System.out.println("╔══════════════════════════════════════╗");
System.out.printf("║ BORROW RECEIPT ║%n");
System.out.printf("║ Book : %-26s║%n", book.getTitle());
System.out.printf("║ Member : %-26s║%n", memberId);
System.out.printf("║ Borrowed: %-26s║%n", borrowDate.format(DATE_FMT));
System.out.printf("║ Due : %-26s║%n", dueDate.format(DATE_FMT));
if (returnDate != null) {
System.out.printf("║ Returned: %-26s║%n", returnDate.format(DATE_FMT));
System.out.printf("║ Late Fee: $%-25.2f║%n", calculateLateFee());
}
System.out.println("╚══════════════════════════════════════╝");
}
}Quick Quiz
Tip
Tip
Practice Mini Project Library System Part 4 Core API 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 4 (Core API)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Library System Part 4 Core API 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 4 Core API 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
- Enhance the Library System with proper type handling using enums for BookStatus and Genre, Optional-returning search methods, date tracking for borrows/returns, and formatted reports using printf and DateTimeFormatter.
- BookStatus enum replaces string status - type-safe, descriptive
- Genre enum categorises books - enables genre-based filtering
- BorrowRecord tracks who borrowed what and when, with due date calculation
Finished this lesson?