0% found this document useful (0 votes)
8 views

Programming Assignment Unit 2

The document describes a Java program that simulates a library system, allowing users to manage book inventory through a menu-driven interface. It features a LibrarySystem class with methods for adding, borrowing, and returning books, utilizing a HashMap for efficient data storage. Basic error handling is included to ensure user-friendly interaction with the system.

Uploaded by

falakfraidoon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming Assignment Unit 2

The document describes a Java program that simulates a library system, allowing users to manage book inventory through a menu-driven interface. It features a LibrarySystem class with methods for adding, borrowing, and returning books, utilizing a HashMap for efficient data storage. Basic error handling is included to ensure user-friendly interaction with the system.

Uploaded by

falakfraidoon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

Here is the source code:


import java.util.HashMap;
import java.util.Scanner;

public class Main {

// Book class to store details of each book


static class Book {
String title;
String author;
int quantity;

Book(String title, String author, int quantity) {


this.title = title;
this.author = author;
this.quantity = quantity;
}
}
// HashMap to store books with title as the key
static HashMap<String, Book> library = new HashMap<>();

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int choice;

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.");
}
}
}

// Method to add books to the library


public static void addBooks(Scanner scanner) {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author name: ");
String author = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline

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.");
}
}

// Method to borrow books from the library


public static void borrowBooks(Scanner scanner) {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter quantity to borrow: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline

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.");
}
}

// Method to return books to the library


public static void returnBooks(Scanner scanner) {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter quantity to return: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline

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/

You might also like