Name: Hamid
Roll No: 22f-3842
class Book:
def __init__(self, title, author, isbn, year, price):
self.title = title
self.author = author
self.isbn = isbn
self.year = year
self.price = price
def __str__(self):
return f"{self.title} by {self.author} ({self.year})"
def __eq__(self, other):
return (self.title == other.title and
self.author == other.author and
self.year == other.year and
self.price == other.price)
class LibraryCatalog:
def __init__(self):
self.books = []
def add_book(self, book):
if not any(b == book for b in self.books):
self.books.append(book)
else:
print("This book already exists in the catalog.")
def remove_book(self, book):
if book in self.books:
Name: Hamid
Roll No: 22f-3842
self.books.remove(book)
def __add__(self, other):
new_catalog = LibraryCatalog()
new_catalog.books = self.books + other.books
return new_catalog
def __sub__(self, other):
new_catalog = LibraryCatalog()
new_catalog.books = [book for book in self.books if book not in other.books]
return new_catalog
def __eq__(self, other):
return sorted(self.books, key=lambda x: (x.title, x.author, x.isbn, x.year)) == \
sorted(other.books, key=lambda x: (x.title, x.author, x.isbn, x.year))
def __len__(self):
return len(self.books)
def __str__(self):
catalog_str = "Library Catalog:\n"
for i, book in enumerate(self.books, 1):
catalog_str += f"{i}. {str(book)}\n"
return catalog_str
def create_book():
title = input("Enter book title: ")
author = input("Enter author: ")
isbn = input("Enter ISBN: ")
Name: Hamid
Roll No: 22f-3842
year = int(input("Enter publication year: "))
price = float(input("Enter price: "))
return Book(title, author, isbn, year, price)
catalog1 = LibraryCatalog()
catalog2 = LibraryCatalog()
while True:
print("\nMenu:")
print("1. Add a book to Catalog 1")
print("2. Add a book to Catalog 2")
print("3. Merge catalogs")
print("4. Subtract catalogs")
print("5. Compare catalogs for equality")
print("6. Show the catalog")
print("7. Quit")
choice = input("\nEnter your choice: ")
if choice == '1':
book = create_book()
catalog1.add_book(book)
print("\nBook added to Catalog 1.")
elif choice == '2':
book = create_book()
catalog2.add_book(book)
print("\nBook added to Catalog 2.")
Name: Hamid
Roll No: 22f-3842
elif choice == '3':
merged_catalog = catalog1 + catalog2
print("\nMerged Catalog:")
print(merged_catalog)
elif choice == '4':
subtracted_catalog = catalog1 - catalog2
print("\nSubtracted Catalog:")
print(subtracted_catalog)
elif choice == '5':
print("\nCatalogs are equal:", catalog1 == catalog2)
elif choice == '6':
catalog_choice = input("Enter the catalog (1 or 2): ")
if catalog_choice == '1':
print("\nCatalog 1:")
print(catalog1)
elif catalog_choice == '2':
print("\nCatalog 2:")
print(catalog2)
else:
print("\nInvalid catalog choice.")
elif choice == '7':
break
else:
Name: Hamid
Roll No: 22f-3842
print("\nInvalid choice. Please select a valid option.")