Program 1
# Library Book Management System
class Book:
def __init__(self, id, title, author):
self.id = id
self.title = title
self.author = author
self.prev = None
self.next = None
class Library:
def __init__(self):
self.head = None
def add_book(self, id, title, author):
if self.book_exists(id):
print(f"Book with ID {id} already exists.")
return
new_book = Book(id, title, author)
if self.head is None:
self.head = new_book
else:
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_book
new_book.prev = temp
print("Book added successfully.")
def book_exists(self, id):
temp = self.head
while temp:
if temp.id == id:
return True
temp = temp.next
return False
def display_books(self):
if not self.head:
print("Library is empty.")
return
print("\nBooks in Library:")
temp = self.head
while temp:
print(f"ID: {temp.id}, Title: {temp.title}, Author: {temp.author}")
temp = temp.next
def search_by_id(self, id):
temp = self.head
while temp:
if temp.id == id:
print(f"Book found: ID = {temp.id}, Title = {temp.title}, Author = {temp.author}")
return
temp = temp.next
print(f"Book not found with ID {id}")
def search_by_title(self, title):
temp = self.head
while temp:
if temp.title.lower() == title.lower():
print(f"Book found: ID = {temp.id}, Author = {temp.author}")
return
temp = temp.next
print(f"Book not found with title '{title}'")
def search_by_author(self, author):
temp = self.head
found = False
while temp:
if temp.author.lower() == author.lower():
print(f"Book found: ID = {temp.id}, Title = {temp.title}, Author = {temp.author}")
found = True
temp = temp.next
if not found:
print(f"No books found by author '{author}'")
def delete_book(self, id):
temp = self.head
while temp and temp.id != id:
temp = temp.next
if not temp:
print("Book not found.")
return
print(f"Book found: ID = {temp.id}, Title = {temp.title}, Author = {temp.author}")
choice = input("Are you sure you want to delete this book? (yes/no): ").strip().lower()
if choice == 'yes':
if temp.prev:
temp.prev.next = temp.next
else:
self.head = temp.next
if temp.next:
temp.next.prev = temp.prev
print("Book deleted successfully.")
def update_book(self, id):
temp = self.head
while temp:
if temp.id == id:
temp.title = input("Enter new title: ").strip()
temp.author = input("Enter new author: ").strip()
print("Book updated successfully.")
return
temp = temp.next
print("Book not found.")
def main():
library = Library()
while True:
print("\n------------- Library Menu -----------")
print("1. Add Book")
print("2. Display Books")
print("3. Search Book by Title")
print("4. Delete Book by ID")
print("5. Update Book by ID")
print("6. Search Book by ID")
print("7. Search Book by Author")
print("8. Exit")
print("--------------------------------------")
try:
choice = int(input("Enter your choice: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == 1:
try:
id = int(input("Enter Book ID: "))
title = input("Enter Title: ").strip()
author = input("Enter Author: ").strip()
library.add_book(id, title, author)
except ValueError:
print("Invalid ID. Must be a number.")
elif choice == 2:
library.display_books()
elif choice == 3:
title = input("Enter title to search: ").strip()
library.search_by_title(title)
elif choice == 4:
try:
id = int(input("Enter Book ID to delete: "))
library.delete_book(id)
except ValueError:
print("Invalid ID.")
elif choice == 5:
try:
id = int(input("Enter Book ID to update: "))
library.update_book(id)
except ValueError:
print("Invalid ID.")
elif choice == 6:
try:
id = int(input("Enter Book ID to search: "))
library.search_by_id(id)
except ValueError:
print("Invalid ID.")
elif choice == 7:
author = input("Enter author name to search: ").strip()
library.search_by_author(author)
elif choice == 8:
print("Exiting program.")
break
else:
print("Invalid choice! Please choose a valid option.")
if __name__ == "__main__":
main()