// Import Scanner class for user input
import java.util.Scanner;
// Create a Book class to store book information
class Book {
// Declare instance variables
private String title;
private String author;
private int quantity;
// Constructor
public Book(String title, String author, int quantity) {
this.title = title;
this.author = author;
this.quantity = quantity;
}
// Getter methods
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getQuantity() {
return quantity;
}
// Setter methods
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// Override equals method to compare books by title and author
public boolean equals(Object obj) {
if (obj instanceof Book) {
Book other = (Book) obj;
return this.title.equalsIgnoreCase(other.title) && this.author.equalsIgnoreCase(other.author);
}
return false;
}
// Override toString method to display book information
public String toString() {
return "Title: " + title + ", Author: " + author + ", Quantity: " + quantity;
}
}
// Create a Library class to store and manage books
class Library {
// Declare instance variables
private Book[] books; // Array of books
private int size; // Number of books in the library
private final int CAPACITY = 100; // Maximum capacity of the library
// Constructor
public Library() {
books = new Book[CAPACITY];
size = 0;
}
// Method to add a book to the library
public void addBook(Book book) {
// Check if the book is null
if (book == null) {
System.out.println("Invalid book.");
return;
}
// Check if the library is full
if (size == CAPACITY) {
System.out.println("The library is full.");
return;
}
// Check if the book already exists in the library
int index = findBook(book);
if (index != -1) {
// Update the quantity of the existing book
books[index].setQuantity(books[index].getQuantity() + book.getQuantity());
System.out.println("The book " + book.getTitle() + " by " + book.getAuthor() + " has been updated.");
} else {
// Add the book to the next available position
books[size] = book;
size++;
System.out.println("The book " + book.getTitle() + " by " + book.getAuthor() + " has been added.");
}
}
// Method to borrow a book from the library
public void borrowBook(Book book, int num) {
// Check if the book is null
if (book == null) {
System.out.println("Invalid book.");
return;
}
// Check if the number of books to borrow is positive
if (num <= 0) {
System.out.println("Invalid number of books.");
return;
}
// Check if the book exists in the library
int index = findBook(book);
if (index != -1) {
// Check if the requested number of books is available
if (books[index].getQuantity() >= num) {
// Update the quantity of the book
books[index].setQuantity(books[index].getQuantity() - num);
System.out.println("You have borrowed " + num + " copies of " + book.getTitle() + " by " + book.getAuthor() + ".");
} else {
// Display an error message
System.out.println("Sorry, there are only " + books[index].getQuantity() + " copies of " + book.getTitle() + " by " + book.getAuthor() + " available.");
}
} else {
// Display an error message
System.out.println("Sorry, the book " + book.getTitle() + " by " + book.getAuthor() + " is not in the library.");
}
}
// Method to return a book to the library
public void returnBook(Book book, int num) {
// Check if the book is null
if (book == null) {
System.out.println("Invalid book.");
return;
}
// Check if the number of books to return is positive
if (num <= 0) {
System.out.println("Invalid number of books.");
return;
}
// Check if the book belongs to the library system
int index = findBook(book);
if (index != -1) {
// Update the quantity of the book
books[index].setQuantity(books[index].getQuantity() + num);
System.out.println("You have returned " + num + " copies of " + book.getTitle() + " by " + book.getAuthor() + ".");
} else {
// Display an error message
System.out.println("Sorry, the book " + book.getTitle() + " by " + book.getAuthor() + " does not belong to the library system.");
}
}
// Method to find the index of a book in the library
// Return -1 if the book is not found
public int findBook(Book book) {
// Loop through the books array
for (int i = 0; i < size; i++) {
// Compare the book with the current element
if (books[i].equals(book)) {
// Return the index if found
return i;
}
}
// Return -1 if not found
return -1;
}
// Method to display the books in the library
public void displayBooks() {
// Check if the library is empty
if (size == 0) {
System.out.println("The library is empty.");
return;
}
// Loop through the books array
for (int i = 0; i < size; i++) {
// Print the book information
System.out.println(books[i]);
}
}
}
// Create a Test class to test the library system
public class Test {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner input = new Scanner(System.in);
// Create a Library object
Library library = new Library();
// Declare variables for user input
String title, author;
int quantity, num, choice;
// Display a welcome message
System.out.println("Welcome to the library system.");
// Create a loop for the menu
do {
// Display the menu options
System.out.println("Please choose an option:");
System.out.println("1. Add Books");
System.out.println("2. Borrow Books");
System.out.println("3. Return Books");
System.out.println("4. Display Books");
System.out.println("5. Exit");
// Prompt the user to enter a choice
System.out.print("Enter your choice: ");
choice = input.nextInt();
// Handle the choice using a switch statement
switch (choice) {
case 1: // Add Books
// Prompt the user to enter the book title, author, and quantity
System.out.print("Enter the book title: ");
input.nextLine(); // Consume the newline character
title = input.nextLine();
System.out.print("Enter the book author: ");
author = input.nextLine();
System.out.print("Enter the book quantity: ");
quantity = input.nextInt();
// Create a Book object with the user input
Book book = new Book(title, author, quantity);
// Add the book to the library
library.addBook(book);
// Display a confirmation message
System.out.println("The book has been added successfully.");
break;
case 2: // Borrow Books
// Prompt the user to enter the book title and the number of books to borrow
System.out.print("Enter the book title: ");
input.nextLine(); // Consume the newline character
title = input.nextLine();
System.out.print("Enter the number of books to borrow: ");
num = input.nextInt();
// Create a Book object with the user input
book = new Book(title, "", 0); // The author and quantity are not relevant for borrowing
// Borrow the book from the library
library.borrowBook(book, num);
break;
case 3: // Return Books
// Prompt the user to enter the book title and the number of books to return
System.out.print("Enter the book title: ");
input.nextLine(); // Consume the newline character
title = input.nextLine();
System.out.print("Enter the number of books to return: ");
num = input.nextInt();
// Create a Book object with the user input
book = new Book(title, "", 0); // The author and quantity are not relevant for returning
// Return the book to the library
library.returnBook(book, num);
break;
case 4: // Display Books
// Display the books in the library
library.displayBooks();
break;
case 5: // Exit
// Display a farewell message
System.out.println("Thank you for using the library system. Goodbye.");
break;
default: // Invalid choice
// Display an error message
System.out.println(“Invalid choice. Please try again.”);
break;
}
} while (choice != 5);
input.close(); // Close the Scanner
}
}
}