Write a class that represents a Food Item

"""
TODO: Write a class that represents a Food item:
 + Constructor takes two parameters (name and calories). Calories may or may not be
present when created...(set to 100 if not present - default value)
 + Has one accessor function (getCalories) and one mutator function (you decide what to mutate)
 + All attributes must be private
 + When you print an instantiated object of this Food class, it must be in the form:
"FoodName (# calories)"
"""

class Recipe():
    def __init__(self, name):
        self.__ingredients = []
        self.__amounts = []
        self.__name = name

    def __iter__(self):
        self.iterIndex = -1
        return self

    def addIngredient(self,food,amount=1):
        self.__ingredients.append(food)
        self.__amounts.append(amount)

    def __next__(self):
        # TODO: step through the amount and ingredients to
        # return a dictionary of ingredient (type:Food) and amount (type:float)
        # Which is comprised of elements of the list attributes
        # stop when there are no more ingredients
        pass

    def getCalories(self):
        cals = 0
        for grp in self:
            cals+=grp['ingredient'].getCalories()*grp['amount']
        return cals

    def __str__(self):
        txt = self.__name+":"
        for i in self:
            txt += "\n\t- {}".format(i['ingredient'])
        return txt


if __name__=="__name__":
    m = Recipe("Mirepoix")
    celery = Food("Celery",10)
    m.addIngredient(Food("Carrot"))
    m.addIngredient(celery,10)
    m.addIngredient(Food("Onion",30),2)
    print(m)
    print("\nRecipe calories",m.getCalories())

    # TODO: Create a recipe for Peanut Butter Cookies, Contains
    # 1 cup peanut butter
    # ½ cup sugar
    # 1 egg
    # 1 cup of peanut butter is 1500 calories
    # 1 cup of sugar is 800
    # 12 eggs is 900
    # Print the recipe.

"""
Expected Output:
Mirepoix:
    - Carrot (100 calories)
    - Celery (10 calories)
    - Onion (30 calories)
   
Recipe calories 260

Peanut Butter Cookies:
    - Peanut Butter (1500 calories)
    - Sugar (800 calories)
    - Egg (75 calories)
"""

Need a custom answer at your budget?

This assignment has been answered 3 times in private sessions.

© 2024 Codify Tutor. All rights reserved