Step 1: Creating the Java Program
creating a LibrarySystem class with methods to add books, borrow books, return books, and exit the program.
using a HashMap to store book details.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Book {
String title;
String author;
int quantity;
public Book(String title, String author, int quantity) {
this.title = title;
this.author = author;
this.quantity = quantity;
public void addQuantity(int quantity) {
this.quantity += quantity;
}
public boolean borrowBook(int quantity) {
if (this.quantity >= quantity) {
this.quantity -= quantity;
return true;
} else {
return false;
public void returnBook(int quantity) {
this.quantity += quantity;
public class LibrarySystem {
private static final Map<String, Book> library = new HashMap<>();
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\nLibrary System Menu:");
System.out.println("1. Add Book");
System.out.println("2. Borrow Book");
System.out.println("3. Return Book");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addBook();
break;
case 2:
borrowBook();
break;
case 3:
returnBook();
break;
case 4:
System.out.println("Exiting the program. Goodbye!");
return;
default:
System.out.println("Invalid choice! Please enter a number between 1 and 4.");
private static void addBook() {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (library.containsKey(title)) {
library.get(title).addQuantity(quantity);
System.out.println("Updated the quantity of existing book: " + title);
} else {
library.put(title, new Book(title, author, quantity));
System.out.println("Added new book: " + title);
}
private static void borrowBook() {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
if (!library.containsKey(title)) {
System.out.println("Error: Book not found in library.");
return;
System.out.print("Enter quantity to borrow: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (library.get(title).borrowBook(quantity)) {
System.out.println("You have borrowed " + quantity + " copies of '" + title + "'.");
} else {
System.out.println("Error: Not enough copies available.");
}
private static void returnBook() {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
if (!library.containsKey(title)) {
System.out.println("Error: This book does not belong to our library.");
return;
System.out.print("Enter quantity to return: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline
library.get(title).returnBook(quantity);
System.out.println("You have returned " + quantity + " copies of '" + title + "'.");
Step 2: Explanation of the Code
Book Class: Represents a book with attributes (title, author, quantity) and methods to modify the quantity.
Library System: Stores books using a HashMap<String, Book>, where the key is the book title.
Menu Options:
Add Book: If the book exists, it updates the quantity; otherwise, it adds a new book.
Borrow Book: Checks if the requested number of books is available before borrowing.
Return Book: Ensures the book belongs to the library before accepting a return.
Exit: Ends the program.
Input Handling: Prevents incorrect input formats using scanner.nextLine() adjustments.
Step 3: Sample Output
Adding Books:
Borrowing a Book:
Returning a Book:
Borrowing More Than Available:
Returning a Non-Existing Book:
Exiting:
References: