Borrowing System
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 79 of 87.
Challenge
EasyLet'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 centralLibraryclass that manages everything. Your library should maintain two ArrayLists—one forBookobjects and one forUserobjects. 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, ornullif not found.findUserById(String id)- searches through the users and returns the User with the matching ID, ornullif 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, printBook not available. If user or book not found, printInvalid 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, printInvalid 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:isbnorreturn: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: BorrowedThis 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
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