Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 9 of 10
Mini Project: Library System - Part 2 (Book Class)
Replace the parallel arrays from Module 1 with a proper Book class. Apply encapsulation, constructors, equals/hashCode, and toString. The catalog is now a List of Book objects with proper search and validation.
Improvements over Module 1
- Book is now an encapsulated class - no exposed fields
- Constructor validates ISBN and price - fail fast on bad data
- equals/hashCode by ISBN - duplicates detected correctly
- toString gives clean output for logging/debugging
- Search method on the catalog class - methods on objects, not static procedures
Library Part 2 - OOP Catalog
package com.priygop.library.model;
import java.util.Objects;
public class Book {
private final String isbn;
private String title;
private String author;
private double price;
private boolean available;
public Book(String isbn, String title, String author, double price) {
if (isbn == null || isbn.isBlank()) throw new IllegalArgumentException("ISBN required");
if (price < 0) throw new IllegalArgumentException("Price negative: " + price);
this.isbn = isbn;
this.title = title.trim();
this.author = author.trim();
this.price = price;
this.available = true;
}
public boolean borrow() {
if (!available) return false;
available = false;
return true;
}
public void returnBook() { available = true; }
// Getters
public String getIsbn() { return isbn; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public double getPrice() { return price; }
public boolean isAvailable() { return available; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Book b)) return false; // pattern matching instanceof (Java 16+)
return Objects.equals(isbn, b.isbn);
}
@Override public int hashCode() { return Objects.hashCode(isbn); }
@Override
public String toString() {
return String.format("[%s] "%s" by %s - $%.2f (%s)",
isbn, title, author, price, available ? "Available" : "Borrowed");
}
}
// Catalog:
package com.priygop.library.service;
import com.priygop.library.model.Book;
import java.util.*;
public class LibraryCatalog {
private final List<Book> books = new ArrayList<>();
public void addBook(Book book) {
if (books.contains(book)) throw new IllegalArgumentException("Duplicate ISBN: " + book.getIsbn());
books.add(book);
}
public Optional<Book> findByIsbn(String isbn) {
return books.stream().filter(b -> b.getIsbn().equals(isbn)).findFirst();
}
public List<Book> findByAuthor(String author) {
return books.stream()
.filter(b -> b.getAuthor().equalsIgnoreCase(author))
.toList(); // Java 16+
}
public void printAll() { books.forEach(System.out::println); }
}Quick Quiz
Tip
Tip
Practice Mini Project Library System Part 2 Book Class 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 2 (Book Class)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Library System Part 2 Book Class 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 2 Book Class 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
- Replace the parallel arrays from Module 1 with a proper Book class.
- Book is now an encapsulated class - no exposed fields
- Constructor validates ISBN and price - fail fast on bad data
- equals/hashCode by ISBN - duplicates detected correctly
Finished this lesson?