Program 1
// Library Book Management System
import java.util.Scanner;
class Book {
int id;
String title;
String author;
Book prev;
Book next;
Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
this.prev = null;
this.next = null;
}
}
class Library {
private Book head;
public void addBook(int id, String title, String author) {
Book newBook = new Book(id, title, author);
if (head == null) {
head = newBook;
} else {
Book temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newBook;
newBook.prev = temp;
}
System.out.println("Book added successfully.");
}
public void displayBooks() {
if (head == null) {
System.out.println("Library is empty.");
return;
}
Book temp = head;
System.out.println("\nBooks in Library:");
while (temp != null) {
System.out.println("ID: " + temp.id + ", Title: " + temp.title + ", Author: " + temp.author);
temp = temp.next;
}
}
public void searchBookById(int id)
{
Book temp=head;
while(temp!=null)
{
if(temp.id==id)
{
System.out.println("Book found: ID = " + temp.id + ", Author = " + temp.author +",Title = " + temp.title);
return;
}
temp=temp.next;
}
System.out.println("Book not found with "+id +" id");
}
public void searchBook(String title) {
Book temp = head;
while (temp != null)
{
if (temp.title.equalsIgnoreCase(title)) {
System.out.println("Book found: ID = " + temp.id + ", Author = " + temp.author);
return;
}
temp = temp.next;
}
System.out.println("Book not found with "+title +" title");
}
public void searchBookByAuthor(String author) {
Book temp = head;
boolean flag=true;
while (temp != null)
{
if (temp.author.equalsIgnoreCase(author)) {
System.out.println("Book found: ID = " + temp.id + ", Title = " + temp.title +", Author = " + temp.author);
flag=false;
}
temp = temp.next;
}
if(flag==true)
System.out.println("Book not found with "+author +" Author");
}
public void deleteBook(int id) {
Book temp = head;
while (temp != null && temp.id != id) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Book not found.");
return;
}
System.out.println("Book found: ID = " + temp.id + ", Title = " + temp.title +", Author = " + temp.author);
String choice;
Scanner scan = new Scanner(System.in);
System.out.println("Are you sure want to delete(yes/no)");
choice=scan.next();
if(choice.toUpperCase().equalsIgnoreCase("YES"))
{
if (temp.prev != null)
temp.prev.next = temp.next;
else
head = temp.next;
if (temp.next != null)
temp.next.prev = temp.prev;
System.out.println("Book deleted successfully.");
}
}
public void updateBook(int id, Scanner sc) {
Book temp = head;
while (temp != null) {
if (temp.id == id) {
System.out.print("Enter new title: ");
sc.nextLine(); // clear buffer
temp.title = sc.nextLine();
System.out.print("Enter new author: ");
temp.author = sc.nextLine();
System.out.println("Book updated successfully.");
return;
}
temp = temp.next;
}
System.out.println("Book not found.");
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
Library lib = new Library();
Scanner sc = new Scanner(System.in);
int choice, id;
String title, author;
do {
System.out.println("\n------------- Library Menu-----------");
System.out.println("1. Add Book");
System.out.println("2. Display Books");
System.out.println("3. Search Book by Title");
System.out.println("4. Delete Book by ID");
System.out.println("5. Update Book by ID");
System.out.println("6. Search Book by ID");
System.out.println("7. Search Book by Author");
System.out.println("8. Exit");
System.out.println("---------------------------------------------");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Book ID: ");
id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Title: ");
title = sc.nextLine();
System.out.print("Enter Author: ");
author = sc.nextLine();
lib.addBook(id, title, author);
break;
case 2:
lib.displayBooks();
break;
case 3:
sc.nextLine(); // clear buffer
System.out.print("Enter title to search: ");
title = sc.nextLine();
lib.searchBook(title);
break;
case 4:
System.out.print("Enter Book ID to delete: ");
id = sc.nextInt();
lib.deleteBook(id);
break;
case 5:
System.out.print("Enter Book ID to update: ");
id = sc.nextInt();
lib.updateBook(id, sc);
break;
case 6:
System.out.print("Enter Book ID to Search: ");
id = sc.nextInt();
lib.searchBookById(id);
break;
case 7:
System.out.print("Enter author name to Search: ");
author=sc.next();
lib.searchBookByAuthor(author);
break;
case 8:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 8);
sc.close();
}
}