Menu
Coddy logo textTech

Admin Interface

Part of the Object Oriented Programming section of Coddy's Java journey. Lesson 81 of 87.

challenge icon

Challenge

Easy

Let's expand your Library Management System with an Admin Interface! A real library needs administrative capabilities—adding new books to the collection, removing outdated ones, and registering new members. You'll create an interface that defines these administrative operations and implement them in a dedicated admin class.

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

  • Book.java: Keep your Book class with all existing functionality—isbn, title, author, isAvailable, borrowedBy, getDetails(), and the appropriate getters/setters.
  • User.java: Keep your User class unchanged with id, name, the borrowedBooks ArrayList, borrowBook(), returnBook(), getBorrowedCount(), and toString().
  • Library.java: Keep your Library class with all existing methods. Add a new method removeBook(String isbn) that finds and removes a book from the collection by its ISBN. The method should return true if the book was found and removed, false otherwise. A book can only be removed if it's currently available (not borrowed).
  • LibraryAdmin.java: Create an interface called LibraryAdmin that defines the contract for administrative operations. Your interface should declare three methods:
    • addBook(String isbn, String title, String author) - returns a boolean indicating success
    • removeBook(String isbn) - returns a boolean indicating success
    • registerUser(String id, String name) - returns a boolean indicating success

    Then create a class called AdminService that implements this interface. The AdminService should hold a reference to a Library (passed through its constructor) and implement all three methods by delegating to the Library while adding appropriate feedback. When adding a book, create a new Book and add it to the library. When registering a user, create a new User and register them.

  • Main.java: Demonstrate your admin system! You'll receive a series of admin commands as a single comma-separated string. Each command follows one of these formats:
    • ADD_BOOK:isbn:title:author
    • REMOVE_BOOK:isbn
    • REGISTER_USER:id:name
    • BORROW:userId:isbn

    Create a Library and an AdminService. Process each command and print the result:

    • For ADD_BOOK: print Added: [title] on success
    • For REMOVE_BOOK: print Removed: [isbn] on success, or Cannot remove: [isbn] if the book doesn't exist or is borrowed
    • For REGISTER_USER: print Registered: [name]
    • For BORROW: use the existing library borrow functionality

    After processing all commands, print Library status: followed by each book's details and availability status in the format [details] - [Available/Borrowed].

You will receive one input: a comma-separated string of commands.

For example, with input REGISTER_USER:U001:Alice,ADD_BOOK:ISBN-001:Clean Code:Robert Martin,ADD_BOOK:ISBN-002:Design Patterns:Gang of Four,BORROW:U001:ISBN-001,REMOVE_BOOK:ISBN-001,REMOVE_BOOK:ISBN-002, your output would be:

Registered: Alice
Added: Clean Code
Added: Design Patterns
Alice borrowed Clean Code
Cannot remove: ISBN-001
Removed: ISBN-002
Library status:
[ISBN-001] Clean Code by Robert Martin - Borrowed

Notice how the admin interface provides a clean contract for administrative operations, while the AdminService implementation handles the actual logic. The system correctly prevents removing a borrowed book while allowing removal of available ones. This separation of interface and implementation is a key OOP principle that makes your code more maintainable and testable!

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];
                library.borrowBook(userId, isbn);
            }
        }
        
        // Print library status
        System.out.println("Library status:");
        ArrayList<Book> allBooks = library.getAllBooks();
        for (Book book : allBooks) {
            String status = book.isAvailable() ? "Available" : "Borrowed";
            System.out.println(book.getDetails() + " - " + status);
        }
    }
}

All lessons in Object Oriented Programming

Practice on your own: Online Java compiler