Programming Assignment 2 Assignment 2 Assi
Programming Assignment 2 Assignment 2 Assi
CS 1102 Programming 1
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Book {
String title;
String author;
int quantity;
public LibrarySystem() {
library = new HashMap<>();
}
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantityToAdd = scanner.nextInt();
addBook(title, author, quantityToAdd);
break;
case 2:
System.out.print("Enter book title to borrow: ");
String borrowTitle = scanner.nextLine();
System.out.print("Enter quantity to borrow: ");
int quantityToBorrow = scanner.nextInt();
borrowBook(borrowTitle, quantityToBorrow);
break;
case 3:
System.out.print("Enter book title to return: ");
String returnTitle = scanner.nextLine();
System.out.print("Enter quantity to return: ");
int quantityToReturn = scanner.nextInt();
returnBook(returnTitle, quantityToReturn);
break;
case 4:
System.out.println("Exiting the library system. Goodbye!");
return;
default:
System.out.println("Invalid option. Please choose again.");
}
}
}
EXPLANATION
I developed a basic library system with my Java software that allows users to add, borrow, and
return books. The two primary components of the system that I built are the ‘Book’ class and the
‘LibrarySystem’ class. I defined the title, author, and quantity of copies of each book in the ‘Book’ class. I
can simply build new books with these details by using a constructor.
The ‘LibrarySystem’ class, which is the major component of the application, stores all of the
books in a ‘HashMap’. Because each book is uniquely recognized by its title, I can easily determine
whether or not it is already in the system by looking at this map. The book addition process determines
whether a book already exists and, if so, adds more of it. It adds a new entry for the book if it doesn't
already exist.
I also included a system for borrowing. Upon a user's request, the ‘borrowBook’ method
determines if there are sufficient copies of the book available for loan. If so, the quantity is decreased by
the loan amount. However, users can return books by using the ‘returnBook’ method, which adds the
returned copies back into the system.
Using the ‘showMenu’ technique, I developed a menu to enable interactive features on the
system. Users can select to add books, borrow books, return books, or log out of the system. Until the
user chooses to end it, it continues indefinitely. To allow users to enter their selections and the book
details, I utilized the ‘Scanner’ class.
In conclusion, this application emulates a basic library system where users may efficiently handle
books. A ‘HashMap’ makes it possible to quickly retrieve books by title, and the system gives the user
immediate feedback on things like how well the borrowing or returning procedure went.
OUTPUT SCREENSHOT