Programming Assignment Unit 2
Programming Assignment Unit 2
The Java program simulates a straightforward library system where users can manage book
inventory through various operations. Central to the program is the LibrarySystem class, which
includes a nested Book class to encapsulate details such as title, author, and quantity for each
book. Utilizing a HashMap named library, the program efficiently stores and retrieves book
information based on the book title.
When the application runs, users are greeted with a menu-driven interface with choices to add
books, borrow books, return books, and log out of the system. A distinct method in the
LibrarySystem class is associated with each menu option: addBooks, borrowBooks, and
returnBooks.
Users can update the quantity of an existing book or input details about a new one using the
addBooks method. It verifies if the book already exists in the library; if so, it changes the
amount; otherwise, it adds the book as a new entry.
When borrowing books, users are prompted by the borrowBooks method to indicate the title of
the book and the quantity of copies they want to check out. It checks the library to see if the
requested quantity is available before updating the quantity to account for the books that have
been borrowed. Conversely, the returnBooks method handles the return process, requiring users
to submit the book title and the quantity of copies they are returning. It modifies the number of
books that have been returned to the library.
Basic error handling is built into the application to handle situations like trying to borrow more
books than are available or entering erroneous information. With its concise instructions and
educational information, it guarantees that users can engage with the system without difficulty.
To exit the software, simply choose the relevant menu item and make sure that all resources have
been closed correctly.
while (true) {
// Display menu options
System.out.println("\nLibrary System Menu:");
System.out.println("1. Add Books");
System.out.println("2. Borrow Books");
System.out.println("3. Return Books");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addBooks(scanner);
break;
case 2:
borrowBooks(scanner);
break;
case 3:
returnBooks(scanner);
break;
case 4:
System.out.println("Exiting the system. Goodbye!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
if (library.containsKey(title)) {
Book existingBook = library.get(title);
existingBook.quantity += quantity;
System.out.println("Book already exists. Updated the quantity.");
} else {
library.put(title, new Book(title, author, quantity));
System.out.println("New book added to the library.");
}
}
if (library.containsKey(title)) {
Book book = library.get(title);
if (book.quantity >= quantity) {
book.quantity -= quantity;
System.out.println("You have successfully borrowed " +
quantity + " copy/copies of \"" + title + "\".");
} else {
System.out.println("Not enough copies available. Available
quantity: " + book.quantity);
}
} else {
System.out.println("Book not found in the library.");
}
}
if (library.containsKey(title)) {
Book book = library.get(title);
book.quantity += quantity;
System.out.println("You have successfully returned " + quantity +
" copy/copies of \"" + title + "\".");
} else {
System.out.println("Book not recognized by the library.");
}
}
}
Output screenshot:
The screenshots of code running on IDE:
References:
Eck, D. J. (2022). Introduction to programming using Java: Version 9, Swing
edition. Hobart and William Smith Colleges.
W3Schools. (n.d.). Java tutorial. W3Schools. https://www.w3schools.com/java/