ASSIGNMENT-2 IMPLEMENTING A BASIC LIBRARY SYSTEM IN JAVA
import [Link];
import [Link];
import [Link];
public class LibrarySystem {
private Map<String, Integer> books = new HashMap<>();
private Scanner scanner = new Scanner([Link]);
public static void main(String[] args) {
LibrarySystem librarySystem = new LibrarySystem();
[Link]();
}
public void runLibrarySystem() {
boolean exit = false;
while (!exit) {
[Link]("Options:");
[Link]("1. Add Books");
[Link]("2. Borrow Books");
[Link]("3. Return Books");
[Link]("4. Exit");
int option = [Link]();
[Link](); // Consume newline character
switch (option) {
case 1:
addBooks();
break;
case 2:
borrowBooks();
break;
case 3:
returnBooks();
break;
case 4:
exit = true;
break;
default:
[Link]("Invalid option. Please try again.");
}
}
[Link]();
}
public void addBooks() {
[Link]("Enter book title: ");
String title = [Link]();
[Link]("Enter author: ");
String author = [Link]();
[Link]("Enter quantity: ");
int quantity = [Link]();
[Link](); // Consume newline character
if ([Link](title)) {
[Link](title, [Link](title) + quantity);
} else {
[Link](title, quantity);
}
}
public void borrowBooks() {
[Link]("Enter book title: ");
String title = [Link]();
[Link]("Enter number of books to borrow: ");
int quantityToBorrow = [Link]();
[Link](); // Consume newline character
if ([Link](title)) {
int availableQuantity = [Link](title);
if (availableQuantity >= quantityToBorrow) {
[Link](title, availableQuantity - quantityToBorrow);
[Link]("Books successfully borrowed.");
} else {
[Link]("Requested quantity not available in the library.");
}
} else {
[Link]("Book not found in the library.");
}
}
public void returnBooks() {
[Link]("Enter book title: ");
String title = [Link]();
[Link]("Enter number of books to return: ");
int quantityToReturn = [Link]();
[Link](); // Consume newline character
if ([Link](title)) {
int existingQuantity = [Link](title);
[Link](title, existingQuantity + quantityToReturn);
[Link]("Books successfully returned.");
} else {
[Link]("Cannot return books that do not belong to the library.");
}
}
}