0% found this document useful (0 votes)
16 views5 pages

22F-3842 Lab 7 Ai

The programing solved assignment included template and function,file handling

Uploaded by

hamidfarooq.ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

22F-3842 Lab 7 Ai

The programing solved assignment included template and function,file handling

Uploaded by

hamidfarooq.ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy