Library Management System

 class Book:

def __init__(self, title, author, genre):
self.title = title
self.author = author
self.genre = genre

class Library:
def __init__(self):
self.books = []

def add_book(self, book):
self.books.append(book)

def search_book(self, title):
for book in self.books:
if book.title.lower() == title.lower():
return book
return None

def display_books(self):
if not self.books:
print("No books in the library.")
else:
print("Library Inventory:")
for book in self.books:
print(f"Title: {book.title}, Author: {book.author}, Genre: {book.genre}")


def main():
library = Library()

while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. Search Book")
print("3. Display Books")
print("4. Exit")

choice = input("Enter your choice (1-4): ")

if choice == "1":
title = input("Enter the title of the book: ")
author = input("Enter the author of the book: ")
genre = input("Enter the genre of the book: ")

book = Book(title, author, genre)
library.add_book(book)
print("Book added successfully.")

elif choice == "2":
title = input("Enter the title of the book to search: ")
found_book = library.search_book(title)
if found_book:
print(f"Book found - Title: {found_book.title}, Author: {found_book.author}, Genre: {found_book.genre}")
else:
print("Book not found.")

elif choice == "3":
library.display_books()

elif choice == "4":
print("Exiting the program...")
break

else:
print("Invalid choice. Please enter a number from 1 to 4.")


if __name__ == '__main__':
main()

Comments

Popular Posts