I have file dessert.py which has the methods can_combine and combine. I need to apply those two
Fantastic news! We've Found the answer you've been seeking!
Question:
I have file dessert.py which has the methods can_combine and combine. I need to apply those two methods in my dessertshop.py file inside the __add__ method in my Order class to combine all like-items such as my my_cookie = Cookie("oatmeal raisin", 2, 3.45) and my_cookie = Cookie("oatmeal raisin", 4, 3.45) and only add the combined dessert to the list self.order. So those two Cookie objects should only be one my_cookie = Cookie("oatmeal raisin", 6, 3.45), the rest of the items should be added to the list since they don't have a similar object. Please let me know if I need to add more information.
dessertshop.py class Order(): def __init__(self): """default constructor that takes no arguments and instantiates the order instance variable to a new list of DessertItem.""" self.order = [] self.payment_method = PayType.CASH def add(self, dessert): """takes a single DessertItem and adds it to the empty list 'order'.""" self.dessert = dessert self.order.append(self.dessert) def main_menu(): """Displays options for the User to choose""" my_order = Order() my_cookie = Cookie("oatmeal raisin", 4, 3.45) my_order.add(my_cookie) my_candy = Candy('candy corn', 1.50, .25) my_order.add(my_candy) my_candy = Candy('gummy bears', .25, .35) my_order.add(my_candy) my_cookie = Cookie("chocolate chip", 6, 3.99) my_order.add(my_cookie) my_ice_cream = IceCream("Pistachio", 2, .79) my_order.add(my_ice_cream) my_sundae = Sundae("Vanilla", 3, .69, "Hot Fudge", 1.29) my_order.add(my_sundae) my_cookie = Cookie("oatmeal raisin", 2, 3.45) my_order.add(my_cookie)
dessert.py class Candy(DessertItem): """Child class that will simulate a Candy sold by pounds.""" # Constructor with enough parameters to initialize all the # attributes of the object, including the superclass attribute def __init__(self, name="", candy_weight=0.5, price_per_pound=1.5, packaging='Bag'): super().__init__(name) self.candy_weight = candy_weight self.price_per_pound = price_per_pound self.packaging = packaging # add a method calculate_cost() that overrides the superclass # method and returns the correct cost for the item def calculate_cost(self): return (self.candy_weight * (self.price_per_pound * 100)) / 100 def __str__(self): return f"{self.name.title()} ({self.packaging})\n {self.candy_weight}lbs. @ ${self.price_per_pound}/lb.: ${round(self.calculate_cost(), 2):.2f} [Tax: ${round(self.calculate_tax(), 2):.2f}]" def can_combine(self, other: "Candy") -> bool: """Checks if two desserts are combinable and returns True. Otherwise returns False.""" return isinstance(other, Candy) and \ other.price_per_pound == self.price_per_pound and \ other.name == self.name def combine(self, other: "Candy") -> "Candy": """Combines the weight of two candies into one dessert.""" self.candy_weight = self.candy_weight + other.candy_weight return self
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date: