0% found this document useful (0 votes)
4 views

library

The document presents a Java implementation of a Library Management System that allows users to add, remove, search, and display books. It utilizes a HashMap to store book IDs and names, providing methods for each functionality. The main method includes a user interface for interacting with the system through a console menu.

Uploaded by

hari1972003
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)
4 views

library

The document presents a Java implementation of a Library Management System that allows users to add, remove, search, and display books. It utilizes a HashMap to store book IDs and names, providing methods for each functionality. The main method includes a user interface for interacting with the system through a console menu.

Uploaded by

hari1972003
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/ 2

import java.util.

*;

class LibraryManagementSystem {
private Map<Integer, String> bookCollection = new HashMap<>();

// Add a book to the library


public void addBook(int bookId, String bookName) {
if (!bookCollection.containsKey(bookId)) {
bookCollection.put(bookId, bookName);
System.out.println("Book added successfully!");
} else {
System.out.println("Book ID already exists!");
}
}

// Remove a book from the library


public void removeBook(int bookId) {
if (bookCollection.containsKey(bookId)) {
bookCollection.remove(bookId);
System.out.println("Book removed successfully!");
} else {
System.out.println("Book ID not found!");
}
}

// Search for a book in the library


public void searchBook(int bookId) {
if (bookCollection.containsKey(bookId)) {
System.out.println("Book Found: " + bookCollection.get(bookId));
} else {
System.out.println("Book not found!");
}
}

// Display all books


public void displayBooks() {
if (bookCollection.isEmpty()) {
System.out.println("No books in the library.");
} else {
System.out.println("Books in Library:");
for (Map.Entry<Integer, String> entry : bookCollection.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Name: " +
entry.getValue());
}
}
}

public static void main(String[] args) {


LibraryManagementSystem library = new LibraryManagementSystem();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add Book");
System.out.println("2. Remove Book");
System.out.println("3. Search Book");
System.out.println("4. Display Books");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter Book ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter Book Name: ");
String name = scanner.nextLine();
library.addBook(id, name);
break;
case 2:
System.out.print("Enter Book ID to remove: ");
int removeId = scanner.nextInt();
library.removeBook(removeId);
break;
case 3:
System.out.print("Enter Book ID to search: ");
int searchId = scanner.nextInt();
library.searchBook(searchId);
break;
case 4:
library.displayBooks();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}

You might also like