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

Programming Assignment 2 Assignment 2 Assi

The document describes a Java-based library system that allows users to add, borrow, and return books through a console menu. It includes a 'Book' class for book details and a 'LibrarySystem' class that manages the collection using a HashMap. The system provides user feedback on operations and continues running until the user decides to exit.

Uploaded by

mdsnxlpz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Assignment 2 Assignment 2 Assi

The document describes a Java-based library system that allows users to add, borrow, and return books through a console menu. It includes a 'Book' class for book details and a 'LibrarySystem' class that manages the collection using a HashMap. The system provides user feedback on operations and continues running until the user decides to exit.

Uploaded by

mdsnxlpz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Assignment 2

University of the People

CS 1102 Programming 1

Nnamdi Nwosu, Instructor

September 19, 2024


PROGRAM CODE

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 class LibrarySystem {


private Map<String, Book> library;

public LibrarySystem() {
library = new HashMap<>();
}

public void addBook(String title, String author, int quantity) {


if (library.containsKey(title)) {
Book existingBook = library.get(title);
existingBook.quantity += quantity;
System.out.println("Updated quantity of " + title + " to " + existingBook.quantity);
} else {
library.put(title, new Book(title, author, quantity));
System.out.println("Added new book: " + title);
}
}

public void borrowBook(String title, int quantity) {


if (library.containsKey(title)) {
Book book = library.get(title);
if (book.quantity >= quantity) {
book.quantity -= quantity;
System.out.println("Successfully borrowed " + quantity + " copies of " + title);
} else {
System.out.println("Error: Not enough copies of " + title + " available.");
}
} else {
System.out.println("Error: Book " + title + " not found in the library.");
}
}

public void returnBook(String title, int quantity) {


if (library.containsKey(title)) {
Book book = library.get(title);
book.quantity += quantity;
System.out.println("Successfully returned " + quantity + " copies of " + title);
} else {
System.out.println("Error: Book " + title + " not found in the library.");
}
}

public void showMenu() {


Scanner scanner = new Scanner(System.in);
while (true) {
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("Choose an option: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume the newline

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

public static void main(String[] args) {


LibrarySystem librarySystem = new LibrarySystem();
librarySystem.showMenu();
}
}

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

You might also like