Library Management
Library Management
package LibraryManagement;
import java.util.*;
// Class-Level List
public List<MainClass> books;
while (iterator.hasNext()) {
MainClass book = iterator.next();
if(book.bookID == bookID) {
iterator.remove();
System.out.println("Book with ID " + bookID + " has been
Removed.");
removed = true;
break;
}
}
if(!removed) {
System.out.println("No Book found with ID: " + bookID);
}
}
Main Class:
package LibraryManagement;
import java.util.Scanner;
int bookID;
String bookName;
double price;
try {
while(true) {
System.out.println("\nLibrary Management System Menu:");
System.out.println("1. Add Book");
System.out.println("2. Display All Books");
System.out.println("3. Search Book by ID");
System.out.println("4. Remove Book by ID");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scn.nextInt();
scn.nextLine();
switch(choice) {
case 1:
System.out.print("Enter Book ID: ");
int bookID = scn.nextInt();
scn.nextLine();
System.out.print("Enter Book Name: ");
String bookName = scn.nextLine();
System.out.print("Enter Book Price: ");
double price = scn.nextDouble();
case 2:
lib.displayBooks();
break;
case 3:
System.out.print("Enter Book ID to search: ");
int searchId = scn.nextInt();
lib.searchBookbyId(searchId);
break;
case 4:
System.out.print("Enter Book ID to remove: ");
int removeId = scn.nextInt();
lib.removeBookbyId(removeId);
break;
case 5:
System.out.println("Exiting...");
scn.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
} catch(Exception e) {
System.out.println(e);
}