0% found this document useful (0 votes)
7 views3 pages

Library Management

The document contains a Java implementation of a Library Management System with two main classes: OperationClass and MainClass. OperationClass manages a list of books, allowing users to add, display, search, and remove books by ID. MainClass serves as the entry point for the application, providing a menu-driven interface for user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Library Management

The document contains a Java implementation of a Library Management System with two main classes: OperationClass and MainClass. OperationClass manages a list of books, allowing users to add, display, search, and remove books by ID. MainClass serves as the entry point for the application, providing a menu-driven interface for user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Operation Class:

package LibraryManagement;
import java.util.*;

public class OperationClass {

// Class-Level List
public List<MainClass> books;

//Constructor for initializing "books" List


public OperationClass() {
books = new ArrayList<>();
}

// Add Book Class


public void addBook(int bookID, String bookName, double price) {
MainClass book = new MainClass(bookID, bookName, price);
books.add(book);
}

// Display Books Class


public void displayBooks() {
if(books.isEmpty()) {
System.out.println("No Books Available!");
} else {
for (MainClass book: books) {
book.displayBookInfo();
}
}
}

// Search Book by ID Class


public void searchBookbyId(int bookID) {
boolean found = false;
for(MainClass book: books) {
if(book.bookID == bookID) {
book.displayBookInfo();
found = true;
break;
}
}
if(!found) {
System.out.println("Book not found with ID: " + bookID);
}
}

// Remove Book by ID Class


public void removeBookbyId(int bookID) {
Iterator<MainClass> iterator = books.iterator();
boolean removed = false;

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;

public class MainClass {

int bookID;
String bookName;
double price;

public MainClass(int bookID, String bookName, double price){


this.bookID = bookID;
this.bookName = bookName;
this.price = price;
}

public void displayBookInfo() {


System.out.println("Book ID: " + bookID);
System.out.println("Book Name: " + bookName);
System.out.println("Price: " + price);
System.out.println("----------------------------");
}

public static void main(String[] args) throws Exception{


Scanner scn = new Scanner(System.in);
OperationClass lib = new OperationClass();

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();

lib.addBook(bookID, bookName, price);


break;

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

You might also like