This document provides a quick reference for all methods, data structures, and usage patterns in the Library Management System.
- Core Methods
- Data Structures
- Usage Examples
- Constants and Configuration
- Error Codes
- State Management
public static void register()Description: Handles user registration with interactive input.
Behavior:
- Prompts for username and password via console
- Validates username uniqueness
- Loops until valid unique username is provided
- Stores credentials in
registeredAcc
Side Effects: Modifies global registeredAcc hashtable
Example Flow:
Input: username = "newuser", password = "mypass"
Result: registeredAcc.put("newuser", "mypass")
public static String checkAccount(String username, String password)Description: Validates user login credentials.
Parameters:
| Parameter | Type | Description |
|---|---|---|
username |
String | User's login identifier |
password |
String | User's password |
Returns:
| Return Value | Description |
|---|---|
null |
Valid credentials |
"Username doesn't exist!" |
Username not found |
"Wrong password!" |
Invalid password |
Example:
// Valid login
String result = checkAccount("carlo", "morva");
// result = null
// Invalid username
String result = checkAccount("nonexistent", "password");
// result = "Username doesn't exist!"
// Wrong password
String result = checkAccount("carlo", "wrongpass");
// result = "Wrong password!"public static boolean borrowersHandler(String username)Description: Checks if a user has any borrowed books.
Parameters:
| Parameter | Type | Description |
|---|---|---|
username |
String | Username to check |
Returns:
| Return Value | Description |
|---|---|
true |
User has borrowed books |
false |
User has no borrowed books |
Algorithm:
- Iterates through
borrowersTreeMap keys - Splits each key by comma:
"username, date, (count)" - Checks if any key contains the target username
Example:
// Assuming borrowers contains: "john, 2024-01-15, (1)" → "Java Book"
boolean hasBorrowed = borrowersHandler("john");
// hasBorrowed = true
boolean hasBorrowed = borrowersHandler("mary");
// hasBorrowed = false (if mary has no borrowed books)Purpose: Store user account credentials
Structure:
Key (Username) → Value (Password)
"carlo" → "morva"
"john" → "password123"
Operations:
// Add user
registeredAcc.put(username, password);
// Check user exists
if (registeredAcc.get(username) != null) { }
// Validate password
if (password.equals(registeredAcc.get(username))) { }Purpose: Track borrowed books with timestamps and borrowing counts
Key Format: "username, date, (borrowCount)"
Value: Book title
Structure:
Key → Value
"john, 2024-01-15, (1)" → "Java Programming"
"john, 2024-01-15, (2)" → "Web Development"
"mary, 2024-01-16, (1)" → "Data Structures"
Key Components:
- Username: Borrower's username
- Date: Borrowing date (LocalDate format)
- Count: Sequential borrowing number (1 or 2)
Operations:
// Add borrowed book
borrowers.put(username + ", " + date + ", " + "(" + borrowCount + ")", bookName);
// Find user's borrowed books
for (String key : borrowers.keySet()) {
if (key.contains(username)) {
String borrowedBook = borrowers.get(key);
}
}
// Remove returned book
borrowers.remove(key);Purpose: Store available books with their access links
Structure:
Key (Book Title) → Value (Book Link)
"Java" → "https://pdfdrive.com/Java"
"JavaScript" → "https://pdfdrive.com/JavaScript"
Operations:
// Add book
books.put(bookTitle, bookLink);
// Remove book
books.remove(bookTitle);
// Check book exists
if (books.get(bookTitle) != null) { }
// Get book link
String link = books.get(bookTitle);
// List all books
for (String title : books.keySet()) {
System.out.println(title + " - " + books.get(title));
}Purpose: Store unique book suggestions from users
Structure:
Set of book titles (duplicates automatically removed)
{"Advanced Java", "Python Basics", "Database Design"}
Operations:
// Add suggestion
suggestedBooks.add(bookTitle);
// Check if suggestion exists
if (suggestedBooks.contains(bookTitle)) { }
// Remove suggestion (when added to library)
suggestedBooks.remove(bookTitle);
// List all suggestions
for (String suggestion : suggestedBooks) {
System.out.println(suggestion);
}// 1. User chooses to register
System.out.print("Do you have an account?[y/n]: ");
String response = input.nextLine(); // "n"
// 2. Registration process
if (response.equals("n")) {
register(); // Handles input and validation
}// 1. User provides credentials
System.out.print("Enter username: ");
String username = input.nextLine();
System.out.print("Enter password: ");
String password = input.nextLine();
// 2. Validate credentials
String validation = checkAccount(username, password);
// 3. Route based on validation and role
if (validation == null) {
if (username.equals("carlo") && password.equals("morva")) {
// Admin access
handleAdminDashboard();
} else {
// Guest access
handleGuestDashboard();
}
} else {
System.out.println(validation); // Error message
}// 1. Check borrowing eligibility
if (borrowCount < 2) {
// 2. Display available books
for (String book : books.keySet()) {
System.out.println(book);
}
// 3. User selects book
String selectedBook = input.nextLine();
// 4. Validate book exists
if (books.get(selectedBook) != null) {
// 5. Update borrowing records
borrowCount++;
String key = username + ", " + date + ", (" + borrowCount + ")";
borrowers.put(key, selectedBook);
}
}// Admin account
Username: "carlo"
Password: "morva"
// Pre-registered guest accounts
"carlo2" → "morva2"
"carlo3" → "morva3"
"carlo32" → "morva""Java" → "https://pdfdrive.com/Java"
"JavaScript" → "https://pdfdrive.com/JavaScript"MAX_BORROW_LIMIT = 2; // Maximum books per user
BORROW_DURATION_DAYS = 30; // Maximum borrowing period// Admin menu
String[] adminCommands = {
"Add a new book",
"Remove a book",
"See list of borrowers",
"View suggestions",
"Log-out"
};
// Guest menu
String[] guestCommands = {
"Browse a book",
"Borrow a book",
"Return a book",
"Suggest a book",
"Log-out"
};| Error Message | Cause | Solution |
|---|---|---|
"Username already taken!" |
Registration with existing username | Choose different username |
"Username doesn't exist!" |
Login with non-existent username | Register first or check spelling |
"Wrong password!" |
Incorrect password | Enter correct password |
| Error Message | Cause | Solution |
|---|---|---|
"You already borrowed 2 books!" |
Borrowing limit reached | Return a book first |
"You already borrowed this book!" |
Duplicate borrowing attempt | Choose different book |
"No book found!" |
Invalid book title | Check available books list |
"You have not borrowed any books." |
Return attempt with no borrowed books | Borrow books first |
| Message | Context | Meaning |
|---|---|---|
"No books available at the moment" |
Empty library | Admin needs to add books |
"No guests borrowed for a moment.." |
Empty borrowers list | No current borrowers |
"No user-suggestions at the moment..." |
Empty suggestions | No pending suggestions |
static Hashtable<String, String> registeredAcc = new Hashtable<>();
static TreeMap<String, String> borrowers = new TreeMap<String, String>();
static Hashtable<String, String> books = new Hashtable<>();
static Set<String> keys = registeredAcc.keySet();LocalDate date = LocalDate.now(); // Current date
HashSet<String> suggestedBooks = new HashSet<>(); // User suggestions
ArrayList<String> borrowCountHistory = new ArrayList<>(); // Borrow count tracking
int borrowCount = 0; // Current user's borrow countApplication Start → User Choice (y/n for account)
↓
Registration/Login → Credential Validation
↓
Role Detection → Admin Dashboard / Guest Dashboard
↓
Menu Selection → Feature Execution → Back to Menu
↓
Logout → Back to Main Menu
Version: 1.0
Last Updated: [Current Date]
Compatible with: Java 8+