Menu
Coddy logo textTech

Borrowing System

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

challenge icon

Challenge

Easy

Let's continue building the Library Management System by creating a proper borrowing system with a Library class that manages the collection of books and users. This is where your system starts to feel like a real library—tracking who has what and enforcing borrowing rules!

You'll organize your code across four files:

  • Book.java: Keep your Book class from the previous challenge with all its functionality—isbn, title, author, isAvailable, borrowedBy, getDetails(), and the appropriate getters/setters.
  • User.java: Keep your User class with id, name, the borrowedBooks ArrayList, borrowBook(), returnBook(), getBorrowedCount(), and toString().
  • Library.java: Create the central Library class that manages everything. Your library should maintain two ArrayLists—one for Book objects and one for User objects. Include these methods:

    addBook(Book book) - adds a book to the library's collection.

    registerUser(User user) - adds a user to the library's registered users.

    findBookByIsbn(String isbn) - searches through the books and returns the Book with the matching ISBN, or null if not found.

    findUserById(String id) - searches through the users and returns the User with the matching ID, or null if not found.

    borrowBook(String userId, String isbn) - finds the user and book, then if both exist and the book is available, has the user borrow the book and prints [userName] borrowed [bookTitle]. If the book isn't available, print Book not available. If user or book not found, print Invalid user or book.

    returnBook(String userId, String isbn) - finds the user and book, then has the user return the book and prints [userName] returned [bookTitle]. If user or book not found, print Invalid user or book.

  • Main.java: Bring your library system together! You'll receive inputs for two books (ISBN, title, author each), one user (ID, name), and then a series of operations.

    Create a Library, add both books, and register the user. Then process the operations—you'll receive a comma-separated string where each operation is formatted as borrow:userId:isbn or return:userId:isbn.

    After processing all operations, print a summary showing each book's status in the format [isbn]: [Available/Borrowed].

You will receive inputs in this order: book1 ISBN, book1 title, book1 author, book2 ISBN, book2 title, book2 author, user ID, user name, and finally the operations string.

For example, with inputs ISBN-001, Clean Code, Robert Martin, ISBN-002, Design Patterns, Gang of Four, U001, Alice, and borrow:U001:ISBN-001,borrow:U001:ISBN-002,return:U001:ISBN-001, your output would be:

Alice borrowed Clean Code
Alice borrowed Design Patterns
Alice returned Clean Code
ISBN-001: Available
ISBN-002: Borrowed

This challenge brings together composition, encapsulation, and method design to create a cohesive system. The Library class acts as a coordinator, delegating actual borrowing logic to the User and Book classes while managing the overall collection!

Try it yourself

import java.util.Scanner;

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 user information
        String userId = scanner.nextLine();
        String userName = scanner.nextLine();
        
        // Read operations string (comma-separated, format: borrow:userId:isbn or return:userId:isbn)
        String operationsStr = scanner.nextLine();
        
        // TODO: Create Book objects for book1 and book2
        
        // TODO: Create a User object
        
        // TODO: Create a Library, add both books, and register the user
        
        // TODO: Split operationsStr by "," and process each operation
        //       For each operation, split by ":" to get action, userId, isbn
        //       Call library.borrowBook() or library.returnBook() accordingly
        
        // TODO: Print summary for each book in format "[isbn]: [Available/Borrowed]"
    }
}

All lessons in Object Oriented Programming