Menu
Coddy logo textTech

Book and User Classes

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

challenge icon

Challenge

Easy

Let's continue building the Library Management System by expanding the Book and User classes from the previous lesson. Now you'll add the ability for users to borrow books, creating a real connection between these two entities.

You'll organize your code across three files:

  • Book.java: Enhance your Book class from the previous challenge. Keep all existing functionality (isbn, title, author, isAvailable with getters/setters, and getDetails()). Now add a private field borrowedBy of type String that stores the ID of the user who borrowed the book (or null if not borrowed). Add a getter for borrowedBy and a setter that also updates isAvailable accordingly—when a book is borrowed (borrowedBy is set to a non-null value), it becomes unavailable; when returned (set to null), it becomes available again.
  • User.java: Expand your User class to track borrowed books. Keep the existing id, name, constructor, getters, and toString(). Add a private ArrayList<Book> field called borrowedBooks (initialize it in the constructor). Create a borrowBook(Book book) method that adds the book to the user's list and sets the book's borrowedBy to this user's ID. Create a returnBook(Book book) method that removes the book from the list and sets borrowedBy to null. Add a getBorrowedCount() method that returns how many books the user currently has.
  • Main.java: Demonstrate the borrowing system in action. You'll receive five inputs: ISBN, title, author for a book, then user ID and name.

    Create a Book and a User with these inputs. Print the book's details and availability status. Then have the user borrow the book. Print the book's availability again, followed by who borrowed it in the format Borrowed by: [userId]. Print how many books the user has in the format [name] has [count] book(s). Finally, have the user return the book and print the availability one more time.

You will receive five inputs in order: ISBN (String), title (String), author (String), user ID (String), and user name (String).

For example, with inputs 978-0-13-468599-1, Effective Java, Joshua Bloch, U001, and Alice, your output would be:

[978-0-13-468599-1] Effective Java by Joshua Bloch
Available: true
Available: false
Borrowed by: U001
Alice has 1 book(s)
Available: true

This challenge builds directly on your previous work, adding the crucial relationship between books and users that makes a library system functional. You'll use composition (User "has" Books) and see how changes to one object can affect another through well-designed methods!

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read book information
        String isbn = scanner.nextLine();
        String title = scanner.nextLine();
        String author = scanner.nextLine();
        
        // Read user information
        String userId = scanner.nextLine();
        String userName = scanner.nextLine();
        
        // Create a Book object with isbn, title, and author
        Book book = new Book(isbn, title, author);
        
        // Create a User object with userId and userName
        User user = new User(userId, userName);
        
        // Print the book's details using getDetails()
        System.out.println(book.getDetails());
        
        // Print whether the book is available (format: "Available: true" or "Available: false")
        System.out.println("Available: " + book.isAvailable());
        
        // Have the user borrow the book
        user.borrowBook(book);
        
        // Print the book's availability again
        System.out.println("Available: " + book.isAvailable());
        
        // Print who borrowed it
        System.out.println("Borrowed by: " + book.getBorrowedBy());
        
        // Print how many books the user has
        System.out.println(user.getName() + " has " + user.getBorrowedCount() + " book(s)");
        
        // Have the user return the book
        user.returnBook(book);
        
        // Print the availability one more time
        System.out.println("Available: " + book.isAvailable());
    }
}

All lessons in Object Oriented Programming