96.9k views
1 vote
Q1) Write a full code that will contain all the following measures. Suppose, you are running a book shop. You can have a maximum of 100 books there. Design a structure name Book_info to store all the

User Coby
by
7.6k points

1 Answer

5 votes

Answer:

python

Copy code

class Book_info:

def __init__(self, title, author, price, quantity):

self.title = title

self.author = author

self.price = price

self.quantity = quantity

def display_info(self):

print(f"Title: {self.title}")

print(f"Author: {self.author}")

print(f"Price: ${self.price}")

print(f"Quantity: {self.quantity}")

print("-----------------------")

class BookShop:

def __init__(self):

self.books = []

def add_book(self, book):

if len(self.books) >= 100:

print("Book shop is full. Cannot add more books.")

else:

self.books.append(book)

print(f"{book.title} has been added to the book shop.")

def search_book(self, title):

for book in self.books:

if book.title == title:

return book

return None

def sell_book(self, title, quantity):

book = self.search_book(title)

if book:

if book.quantity >= quantity:

book.quantity -= quantity

print(f"{quantity} copies of {book.title} have been sold.")

if book.quantity == 0:

self.books.remove(book)

print(f"{book.title} is out of stock and has been removed from the book shop.")

else:

print(f"Insufficient quantity. Only {book.quantity} copies of {book.title} are available.")

else:

print(f"{title} is not available in the book shop.")

def display_books(self):

if not self.books:

print("No books available in the book shop.")

else:

print("Books in the book shop:")

for book in self.books:

book.display_info()

# Example usage of the BookShop class

# Create book instances

book1 = Book_info("The Great Gatsby", "F. Scott Fitzgerald", 10.99, 20)

book2 = Book_info("To Kill a Mockingbird", "Harper Lee", 8.99, 15)

book3 = Book_info("1984", "George Orwell", 7.99, 10)

# Create book shop

book_shop = BookShop()

# Add books to the book shop

book_shop.add_book(book1)

book_shop.add_book(book2)

book_shop.add_book(book3)

# Display books in the book shop

book_shop.display_books()

# Sell a book

book_shop.sell_book("The Great Gatsby", 5)

# Display books after selling

book_shop.display_books()

User Habibi
by
7.8k points

No related questions found