Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron and Library classes, and the three classes that inherit

You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron and Library classes, and the three classes that inherit from LibraryItem (Book, Album and Movie). All data members of each class should be marked as private and the classes should have any get or set methods that will be needed to access them.

I have been trying to write this code for the past few days and I can't figure out what I'm doing wrong! Can you please help me?

Here is my code:

You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron and Library classes, and the three classes that inherit from LibraryItem (Book, Album and Movie). All data members of each class should be marked as private and the classes should have any get or set methods that will be needed to access them.

class InvalidCheckoutError(Exception): pass class Product: """ Represents a product """ def __init__(self, _id_code, _title, _description, _price, _quantity_available): self._id_code = _id_code self._title = _title self._description = _description self._price = _price self._quantity_available = _quantity_available def get_id_code(self): return self._id_code def get_title(self): return self._title def get_description(self): return self._description def get_price(self): return self._price def get_quantity_available(self): return self._quantity_available def decrease_quantity(self): self._quantity_available -= 1 class Customer: """ Represents a customer with a name and account ID. Customers must be members of the Store to make a purchase. Premium members get free shipping """ def __init__(self, _name, _account_id, _premium_member): self._name = _name self._account_id = _account_id self._premium_member = _premium_member self._cart = [] def add_product_to_cart(self, product): self._cart.append(product) def empty_cart(self): self._cart.clear() def get_name(self): return self._name def get_account_id(self): return self._account_id def is_premium_member(self): return self._premium_member def get_cart(self): return self._cart class Store: """ Represents a store, which has some number of products in its inventory and some number of customers as members """ def __init__(self): self._members = [] self._inventory = [] def add_product(self, product): self._inventory.append(product) def add_member(self, customer): self._members.append(customer) def get_product_from_ID(self, product_id): for product in self._inventory: if product.get_id_code() == product_id: return product return None def get_member_from_ID(self, customer_id): for customer in self._members: if customer.get_account_id() == customer_id: return customer return None def product_search(self, string): # create a list to store all matching products matched = [] # iterate over all products in store for product in self._inventory: found = False # check if string is part of description or title of product by convertig both in same case alphabet if string.lower() in product.get_title().lower(): found = True elif string.lower() in product.get_description().lower(): found = True # if product matches for search then add it into the list if found: matched.append(product) for product in matched: return ("ID: " + str(product.get_id_code()) + ", Title: " + str(product.get_title()) + ", Description: " + str(product.get_description()) def add_product_to_member_cart(self, product_id, customer_id): product = self.get_product_from_ID(product_id) if product is None: return "product ID not found" customer = self.get_member_from_ID(customer_id) if customer is None: return "member ID not found" if product.get_quantity_available() > 0: customer.add_product_to_cart(product) return "product added to cart" else: return "product out of stock" def check_out_member(self, customer_id): customer = self.get_member_from_ID(customer_id) if customer is None: return "Customer not found." if len(customer.get_cart()) == 0: return "There are no items in the cart." total = 0 stotal = 0 ship = 0 for product in customer.get_cart(): if product.get_quantity_available() == 0: return "Sorry, product is not available." else: stotal += product.get_price() product.decrease_quantity() if not customer.is_premium_member(): ship = stotal*0.07 total = stotal+ship def main(): try: TatooineCantina = Store() p1 = Product(1111, "milk", "yummy", 2.00, 5) TatooineCantina.add_product(p1) p2 = Product(2222, "death sticks", "umm yummy", 23.50, 3) TatooineCantina.add_product(p2) c1 = Customer("Han Solo", 382, True) TatooineCantina.add_member(c1) c2 = Customer("Obi-Wan Kenobi", 157, False) TatooineCantina.add_member(c2) c3 = Customer("Chewbacca", 890, False) print(TatooineCantina.product_search('yum')) print(TatooineCantina.get_member_from_ID(382).get_account_id()) print(TatooineCantina.add_product_to_member_cart(1111, 382)) print(TatooineCantina.add_product_to_member_cart(2222, 382)) print(TatooineCantina.check_out_member(382)) print(TatooineCantina.product_search('yum')) print(TatooineCantina.get_member_from_ID(157).get_account_id()) print(TatooineCantina.add_product_to_member_cart(1111, 157)) print(TatooineCantina.add_product_to_member_cart(2222, 157)) print(TatooineCantina.check_out_member(157)) print(TatooineCantina.get_member_from_ID(890).get_account_id()) print(TatooineCantina.add_product_to_member_cart(1111, 890)) print(TatooineCantina.add_product_to_member_cart(2222, 890)) print(TatooineCantina.check_out_member(890)) except InvalidCheckoutError: print("Member ID not found.") if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions