Description
CS 115 – Introduction to Programming in Python
Lab Guide 07B
Lab Objectives: Classes
a) Create a class, MenuItem, with the following data members and methods: Data Members:
• itemName: stores the string name of the item
• itemDescription: stores the string description of the menu item.
• ingredients: stores the number of ingredients.
• itemCalories: stores the number of calories in the item.
• itemCost: stores the cost of the menu item.
Methods:
• __init__(): initializes the data members to the values passed as parameters. The calories and cost should be set using the set method.
• Get and set methods for all data members. The set for itemCalories and itemCost should only be set if the values passed as a parameters are greater than zero.
• __repr__(): returns a string representation of a MenuItem object formatted as shown in the sample run.
• getAverageCostPerIngredient (): calculates and returns the average cost per ingredient of the menu item.
b) Write a script MenuApp with the following functionality:
• readMenuItems(): takes a string filename as a parameter. The function reads the MenuItem data from the file and adds the items in the file to an list of MenuItems. The first value in the file indicates the number of MenuItems to be added to the list. The method should return the filled list of MenuItems.
• The script should do the following: o Add the MenuItems in the file items.txt to a list using the read method. o Display the list of MenuItems. o Display all MenuItems with calories less than 1000 containing meat in the description.
o Display the average cost per ingredient for all MenuItems (see sample run).
Sample Run:
Menu Items:
MenuItem [itemDescription=Spaghetti with an italian meat sauce, itemName=Spaghetti bolognese, calories=750, ingredients=8, itemCost=6.5]
MenuItem [itemDescription=Mixed kebab meat plate with bulgur, itemName=Mixed Grill, calories=1050, ingredients=5, itemCost=12.5]
MenuItem [itemDescription=Doner meat with bread, yogurt and special sauce, itemName=Iskender Kebap, calories=800, ingredients=6, itemCost=10.5]
MenuItem [itemDescription=Mixed greens with cheese and a balsamic sauce, itemName=Seasonal Salad, calories=400, ingredients=10, itemCost=8.5]
MenuItem [itemDescription=Pastry with cream and chocolate sauce, itemName=Profiterol, calories=700, ingredients=12, itemCost=6.5]
All Menu Items with calories less than 1000 containing meat:
MenuItem [itemDescription=Spaghetti with an italian meat sauce, itemName=Spaghetti bolognese, calories=750, ingredients=8, itemCost=6.5]
MenuItem [itemDescription=Doner meat with bread, yogurt and special sauce, itemName=Iskender Kebap, calories=800, ingredients=6, itemCost=10.5]
Average Cost Per Ingredient of All Menu Items: 1.29
Reviews
There are no reviews yet.