Menu
Coddy logo textTech

Testing and Integration

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

challenge icon

Challenge

Easy

Congratulations on building your Library Management System! It's time to bring everything together and verify that all components work correctly as an integrated system. You'll create a comprehensive test scenario that exercises all the features you've built throughout this project.

You'll work with all seven files from your completed system:

  • Book.java: Your Book class with isbn, title, author, isAvailable, borrowedBy, and all associated methods including getDetails().
  • User.java: Your User class with id, name, borrowedBooks list, and methods for borrowing/returning books.
  • LibraryException.java: Your base custom exception class.
  • BookNotAvailableException.java: Your exception for unavailable books.
  • LibraryAdmin.java: Your admin interface and AdminService implementation.
  • Library.java: Your complete Library class with all methods—adding/removing books, registering users, borrowing/returning, searching, and exception handling.
  • Main.java: Your integration test that will demonstrate the entire system working together.

In your Main class, you'll process a series of commands that test every major feature of your library system. The commands come as a single comma-separated string with these formats:

  • ADD_BOOK:isbn:title:author - Add a book via AdminService
  • REGISTER_USER:id:name - Register a user via AdminService
  • BORROW:userId:isbn - Borrow a book (with exception handling)
  • RETURN:userId:isbn - Return a book
  • SEARCH_TITLE:keyword - Search books by title
  • SEARCH_AUTHOR:keyword - Search books by author
  • REMOVE_BOOK:isbn - Remove a book via AdminService

Process each command and print the appropriate output:

  • ADD_BOOK: Added: [title]
  • REGISTER_USER: Registered: [name]
  • BORROW: Print the success message or Error: [exception message]
  • RETURN: [userName] returned [bookTitle]
  • SEARCH_TITLE: Print Search '[keyword]': followed by each matching book's details, or No results
  • SEARCH_AUTHOR: Same format as title search
  • REMOVE_BOOK: Removed: [isbn] or Cannot remove: [isbn]

After all commands, print a final summary:

  • --- Final Status ---
  • Books: [count]
  • Users: [count]
  • For each user, print [name]: [borrowedCount] book(s)

You will receive one input: the comma-separated command string.

For example, with input ADD_BOOK:ISBN-001:Clean Code:Robert Martin,ADD_BOOK:ISBN-002:Effective Java:Joshua Bloch,REGISTER_USER:U001:Alice,REGISTER_USER:U002:Bob,BORROW:U001:ISBN-001,BORROW:U002:ISBN-001,SEARCH_TITLE:java,RETURN:U001:ISBN-001,REMOVE_BOOK:ISBN-001, your output would be:

Added: Clean Code
Added: Effective Java
Registered: Alice
Registered: Bob
Alice borrowed Clean Code
Error: Book not available: ISBN-001
Search 'java':
[ISBN-002] Effective Java by Joshua Bloch
Alice returned Clean Code
Removed: ISBN-001
--- Final Status ---
Books: 1
Users: 2
Alice: 0 book(s)
Bob: 0 book(s)

This final challenge validates that all your components—classes, interfaces, exceptions, and the library logic—work together seamlessly. You've built a complete, robust Library Management 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 the comma-separated commands
        String input = scanner.nextLine();
        String[] commands = input.split(",");
        
        // Create Library and AdminService
        Library library = new Library();
        AdminService adminService = new AdminService(library);
        
        // Process each command
        for (String command : commands) {
            String[] parts = command.split(":");
            String operation = parts[0];
            
            if (operation.equals("ADD_BOOK")) {
                String isbn = parts[1];
                String title = parts[2];
                String author = parts[3];
                if (adminService.addBook(isbn, title, author)) {
                    System.out.println("Added: " + title);
                }
            } else if (operation.equals("REMOVE_BOOK")) {
                String isbn = parts[1];
                if (adminService.removeBook(isbn)) {
                    System.out.println("Removed: " + isbn);
                } else {
                    System.out.println("Cannot remove: " + isbn);
                }
            } else if (operation.equals("REGISTER_USER")) {
                String id = parts[1];
                String name = parts[2];
                if (adminService.registerUser(id, name)) {
                    System.out.println("Registered: " + name);
                }
            } else if (operation.equals("BORROW")) {
                String userId = parts[1];
                String isbn = parts[2];
                try {
                    String result = library.borrowBook(userId, isbn);
                    System.out.println(result);
                } catch (LibraryException e) {
                    System.out.println("Error: " + e.getMessage());
                }
            }
            // TODO: Add handling for RETURN command
            // TODO: Add handling for SEARCH_TITLE command
            // TODO: Add handling for SEARCH_AUTHOR command
        }
        
        // TODO: Print final status
        // --- Final Status ---
        // Books: [count]
        // Users: [count]
        // For each user: [name]: [borrowedCount] book(s)
        System.out.println("Operations completed");
    }
}

All lessons in Object Oriented Programming