Testing and Integration
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 83 of 87.
Challenge
EasyCongratulations 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 includinggetDetails().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 AdminServiceREGISTER_USER:id:name- Register a user via AdminServiceBORROW:userId:isbn- Borrow a book (with exception handling)RETURN:userId:isbn- Return a bookSEARCH_TITLE:keyword- Search books by titleSEARCH_AUTHOR:keyword- Search books by authorREMOVE_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, orNo results - SEARCH_AUTHOR: Same format as title search
- REMOVE_BOOK:
Removed: [isbn]orCannot 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
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