diff --git a/objects.py b/objects.py index 85e37a1..90ea58b 100644 --- a/objects.py +++ b/objects.py @@ -5,8 +5,12 @@ class Dessert(object): - - def __init__(): + """Makes a new dessert""" + def __init__(self, price, calories=None): + if calories is None: + self.calories = None + self.price = price + self.calories = calories # Edit me! # You need to be able to initialize a Dessert object with arguments: # price - required @@ -14,28 +18,43 @@ def __init__(): # This should set the object's price and calories, accessible by # .price and .calories respectively. - pass + # pass # Add a calories_per_dollar method that returns the calories per dollar # for the dessert. + def calories_per_dollar(self): + if self.calories is None: + return None + return self.calories/self.price + # Define a method is_a_cake on Dessert that returns False + def is_a_cake(self): + return False + +# new_dessert = Dessert(10) + class Cake(Dessert): - def __init__(): + def __init__(self, kind): + self.kind = 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 - + # pass + price = 5 + calories = 200 # 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): @@ -49,3 +68,23 @@ def desserts(self): if isinstance(item, Dessert): desserts.append(item) return desserts + + def cakes(self): + cakes = [] + for item in self.items: + if isinstance(item, Cake): + cakes.append(item) + return cakes + + +# dessert1 = Dessert(price=10, calories=5) +# print dessert1.calories + +# cake = Cake(kind="chocolate") +# print cake.kind + +# my_desserts = [dessert1, cake] + +# my_menu = Menu(my_desserts) + +# cakes = my_menu.cakes() diff --git a/test_objects.py b/test_objects.py index 9ae71f2..347700e 100644 --- a/test_objects.py +++ b/test_objects.py @@ -79,7 +79,7 @@ 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 + # assert False # Take this line out, it forces the test to fail # Create a cakes() method that does the same thing. # This code is the test for cakes():