Menu
Coddy logo textTech

Search Functionality

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 80 of 87.

challenge icon

Challenge

Easy

Let's enhance your Library Management System with powerful search capabilities! A library isn't very useful if users can't find the books they're looking for. You'll add methods to search by title and author, making your library truly functional.

You'll continue building on your existing code across four files:

  • Book.java: Keep your Book class with all existing functionality—isbn, title, author, isAvailable, borrowedBy, getDetails(), and the appropriate getters/setters. Make sure you have getter methods for title and author if you haven't added them already.
  • User.java: Keep your User class unchanged with id, name, the borrowedBooks ArrayList, borrowBook(), returnBook(), getBorrowedCount(), and toString().
  • Library.java: Expand your Library class with search functionality. Keep all existing methods (addBook, registerUser, findBookByIsbn, findUserById, borrowBook, returnBook) and add these new search methods:

    searchByTitle(String keyword) - returns an ArrayList<Book> containing all books whose title contains the keyword (case-insensitive). Use toLowerCase() on both the title and keyword for comparison.

    searchByAuthor(String keyword) - returns an ArrayList<Book> containing all books whose author name contains the keyword (case-insensitive).

    getAvailableBooks() - returns an ArrayList<Book> containing only the books that are currently available for borrowing.

  • Main.java: Demonstrate your search system! You'll receive inputs for three books (ISBN, title, author for each), then a user (ID, name), followed by a borrow operation and two search queries.

    Create a Library, add all three books, and register the user. Process the borrow operation (formatted as userId:isbn). Then perform the two searches—the first input is a title keyword, the second is an author keyword.

    Print the results in this order:

    • After borrowing, print the borrow confirmation
    • Print Title search '[keyword]': followed by each matching book's details on separate lines (using getDetails()). If no matches, print No books found
    • Print Author search '[keyword]': followed by each matching book's details. If no matches, print No books found
    • Print Available books: followed by each available book's details

You will receive inputs in this order: book1 ISBN, book1 title, book1 author, book2 ISBN, book2 title, book2 author, book3 ISBN, book3 title, book3 author, user ID, user name, borrow operation, title search keyword, author search keyword.

For example, with inputs ISBN-001, Clean Code, Robert Martin, ISBN-002, The Clean Coder, Robert Martin, ISBN-003, Design Patterns, Gang of Four, U001, Alice, U001:ISBN-001, clean, martin, your output would be:

Alice borrowed Clean Code
Title search 'clean':
[ISBN-001] Clean Code by Robert Martin
[ISBN-002] The Clean Coder by Robert Martin
Author search 'martin':
[ISBN-001] Clean Code by Robert Martin
[ISBN-002] The Clean Coder by Robert Martin
Available books:
[ISBN-002] The Clean Coder by Robert Martin
[ISBN-003] Design Patterns by Gang of Four

Notice how the search methods use case-insensitive matching—searching for "clean" finds both "Clean Code" and "The Clean Coder". The available books list correctly excludes the borrowed book. These search capabilities transform your library from a simple collection into a truly usable system!

Try it yourself

import java.util.Scanner;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read book 1 information
        String isbn1 = scanner.nextLine();
        String title1 = scanner.nextLine();
        String author1 = scanner.nextLine();
        
        // Read book 2 information
        String isbn2 = scanner.nextLine();
        String title2 = scanner.nextLine();
        String author2 = scanner.nextLine();
        
        // Read book 3 information
        String isbn3 = scanner.nextLine();
        String title3 = scanner.nextLine();
        String author3 = scanner.nextLine();
        
        // Read user information
        String userId = scanner.nextLine();
        String userName = scanner.nextLine();
        
        // Read borrow operation
        String borrowOp = scanner.nextLine();
        
        // Read search keywords
        String titleKeyword = scanner.nextLine();
        String authorKeyword = scanner.nextLine();
        
        // Create Book objects
        Book book1 = new Book(isbn1, title1, author1);
        Book book2 = new Book(isbn2, title2, author2);
        Book book3 = new Book(isbn3, title3, author3);
        
        // Create User object
        User user = new User(userId, userName);
        
        // Create Library and add books and user
        Library library = new Library();
        library.addBook(book1);
        library.addBook(book2);
        library.addBook(book3);
        library.registerUser(user);
        
        // Process borrow operation
        String[] parts = borrowOp.split(":");
        String opUserId = parts[0];
        String opIsbn = parts[1];
        library.borrowBook(opUserId, opIsbn);
        
        // TODO: Title search
        // Print "Title search '[titleKeyword]':"
        // Use library.searchByTitle(titleKeyword)
        // Print each book's getDetails(), or "No books found" if empty
        
        // TODO: Author search
        // Print "Author search '[authorKeyword]':"
        // Use library.searchByAuthor(authorKeyword)
        // Print each book's getDetails(), or "No books found" if empty
        
        // TODO: Available books
        // Print "Available books:"
        // Use library.getAvailableBooks()
        // Print each book's getDetails()
    }
}

All lessons in Object Oriented Programming