diff --git a/objects.py b/objects.py index 85e37a1..397fcc0 100644 --- a/objects.py +++ b/objects.py @@ -5,40 +5,50 @@ class Dessert(object): - - def __init__(): + def __init__(self,price,calories=None): # Edit me! # You need to be able to initialize a Dessert object with arguments: # price - required # calories - optional - + self.price = price + self.calories = calories # This should set the object's price and calories, accessible by # .price and .calories respectively. - pass # Add a calories_per_dollar method that returns the calories per dollar # for the dessert. - + def calories_per_dollar(self): + if self.calories == None: + return None + else: + ratio = self.calories/self.price + return ratio # Define a method is_a_cake on Dessert that returns False + def is_a_cake(self): + return False + -class Cake(Dessert): - def __init__(): +class Cake(Dessert): + def __init__(self,kind): # Edit me! # Cakes all cost the same amount and have the same calories, so their # price and calories can be set at the class-level, not during init. # However, we need to be able to tell cakes apart. Accept argument: # kind - required - - pass - + self.kind = kind + price = 5 + calories = 200 + super(Cake,self).__init__(price,calories) # Define a method is_a_cake on Cake that returns True # (This will override the one on Dessert) + def is_a_cake(self): + return True -class Menu(object): +class Menu(object): def __init__(self, items): self.items = items @@ -46,6 +56,14 @@ def desserts(self): # Return only the items in self.items which are desserts desserts = [] for item in self.items: - if isinstance(item, Dessert): + if isinstance(item, Dessert): ## I don't really understand what's going on here, Jennie desserts.append(item) return desserts + + def cakes(self): + # Return only the items in self.items which are cakes + cakes = [] + for item in self.items: + if isinstance(item, Cake): + cakes.append(item) + return cakes diff --git a/test_objects.py b/test_objects.py index 9ae71f2..35cc76c 100644 --- a/test_objects.py +++ b/test_objects.py @@ -79,9 +79,17 @@ def test_object_relationships(): # NOTE: To test that it really works, you probably want to create a Menu # with a list that includes things that *aren't* desserts, like integers. - assert False # Take this line out, it forces the test to fail + dessert1x = Dessert(price=10) + dessert2x = Dessert(price=12) + dessert3x = Dessert(price=13) + cake1x = 12 + cake2x = 13 + my_dessertsx = Menu([dessert1x, dessert2x, dessert3x, cake1x, cake2x]) + my_menux = my_dessertsx.desserts() + assert len(my_menux) == 3 # Create a cakes() method that does the same thing. + # This code is the test for cakes(): dessert1 = Dessert(price=10)