Admin Interface
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 81 of 87.
Challenge
EasyLet'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 methodremoveBook(String isbn)that finds and removes a book from the collection by its ISBN. The method should returntrueif the book was found and removed,falseotherwise. A book can only be removed if it's currently available (not borrowed).LibraryAdmin.java: Create an interface calledLibraryAdminthat defines the contract for administrative operations. Your interface should declare three methods:addBook(String isbn, String title, String author)- returns a boolean indicating successremoveBook(String isbn)- returns a boolean indicating successregisterUser(String id, String name)- returns a boolean indicating success
Then create a class called
AdminServicethat 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:authorREMOVE_BOOK:isbnREGISTER_USER:id:nameBORROW: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, orCannot 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 - BorrowedNotice 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
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting10Exception Handling in OOP
Exception Class HierarchyCustom ExceptionsChecked vs Unchecked ErrorsTry With Resources PatternRecap - Validated User13Project: Library Management
Project Overview & UML DesignBook and User Classes2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System