Programming Assingment Unit. 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CODE:

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

// Define a Book class to represent a book with title, author, and quantity
class Book {
private String title;
private String author;
private int quantity;

// Constructor to initialize the Book object


public Book(String title, String author, int quantity) {
this.title = title;
this.author = author;
this.quantity = quantity;
}

// Getter method for retrieving the title of the book


public String getTitle() {
return title;
}

// Getter method for retrieving the author of the book


public String getAuthor() {
return author;
}

// Getter method for retrieving the quantity of the book available in the
library
public int getQuantity() {
return quantity;
}

// Setter method for updating the quantity of the book


public void setQuantity(int quantity) {
this.quantity = quantity;
}
}

// Main class that implements the library system functionalities


public class LibrarySystem {
private static Map<String, Book> library = new HashMap<>(); // HashMap to
store books in the library
private static Scanner scanner = new Scanner(System.in); // Scanner object
for user input

public static void main(String[] args) {


boolean exit = false;

// Main menu will loop to interact with the user until the "Exit"
option is chosen
while (!exit) {
System.out.println("Library 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("Select an option: ");

int choice = scanner.nextInt(); // Read user's choice


scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
addBooks();
break;
case 2:
borrowBooks();
break;
case 3:
returnBooks();
break;
case 4:
exit = true; // Set exit flag to true to terminate the
program
System.out.println("Exiting Library System. Goodbye!");
break;
default:
System.out.println("Invalid option. Please try again.");
break;
}
}

scanner.close(); // Close the scanner


}

// Method to add books to the library


private static void addBooks() {
System.out.println("Add Books to the Library:");
System.out.print("Enter book title: ");
String title = scanner.nextLine(); // Read book title
System.out.print("Enter author: ");
String author = scanner.nextLine(); // Read book author
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt(); // Read book quantity
Book newBook = new Book(title, author, quantity); // Create a new Book
object

// Check if the book already exists in the library


if (library.containsKey(title)) {
// Book exists, update the quantity
Book existingBook = library.get(title);
existingBook.setQuantity(existingBook.getQuantity() + quantity);
System.out.println("Quantity updated for '" + title + "'. Total
quantity: " + existingBook.getQuantity());
} else {
// Book is new, add it to the library
library.put(title, newBook);
System.out.println("Book '" + title + "' added to the library.");
}
}

// Method to borrow books from the library


private static void borrowBooks() {
System.out.println("Borrow Books from the Library:");
System.out.print("Enter book title: ");
String title = scanner.nextLine(); // Read book title
System.out.print("Enter number of books to borrow: ");
int borrowQuantity = scanner.nextInt(); // Read number of books to
borrow

// Check if the book exists in the library


if (library.containsKey(title)) {
Book book = library.get(title);
// Check if requested quantity is available
if (book.getQuantity() >= borrowQuantity) {
book.setQuantity(book.getQuantity() - borrowQuantity); //
Update quantity after borrowing
System.out.println("Successfully borrowed " + borrowQuantity +
" book(s) of '" + title + "'.");
} else {
System.out.println("Sorry, only " + book.getQuantity() + "
book(s) of '" + title + "' available.");
}
} else {
System.out.println("Book '" + title + "' not found in the
library.");
}
}

// Method to return books to the library


private static void returnBooks() {
System.out.println("Return Books to the Library:");
System.out.print("Enter book title: ");
String title = scanner.nextLine(); // Read book title
System.out.print("Enter number of books to return: ");
int returnQuantity = scanner.nextInt(); // Read number of books to
return

// Check if the book exists in the library


if (library.containsKey(title)) {
Book book = library.get(title);
book.setQuantity(book.getQuantity() + returnQuantity); // Update
quantity after returning
System.out.println("Successfully returned " + returnQuantity + "
book(s) of '" + title + "'.");
} else {
System.out.println("Book '" + title + "' not found in the library
system.");
}
}
}

OUTPUT:

You might also like