Question

Python Classes - Write a class to determine the price of a salad and its ingredients....

Python Classes - Write a class to determine the price of a salad and its ingredients.

Write a Salad class. Your class will have two attributes: • name, which should be a string containing the name of the person who ordered the salad. • ingredients, which should be a set of strings where each string is an ingredient in the salad.

__init__() The Salad class should have an __init__() method with four parameters, in the following order: • self • the name of the person • a set of ingredients to add to the base ingredients of the salad (including things from the protein, extra toppings, and dressings categories above). This should be an optional parameter with a default value of None. A value of None means not to add any additional ingredients. • a set of base ingredients to omit from the salad. This should be an optional parameter with a default value of None. A value of None means not to omit any ingredients (in other words, to include all the base ingredients). The __init__() method should set the name attribute to the name specified in the second parameter. It should set the ingredients attribute to contain exactly the ingredients that should go in the salad (including the nonomitted base ingredients). For example, if the user says to add "falafel", "apples", "cashews", and "citrus" and omit "onions" and "carrots", the value of ingredients should be {"greens", "tomatoes", "cucumbers", "falafel", "apples", "cashews", "citrus"} (the base ingredients the user didn't omit, plus the additional ingredients the user added). It's possible that users will provide invalid values in either the set of ingredients to add or the set of ingredients to omit. You do not need to validate the values of these sets in the __init__() method. • If the user says to omit things that are not base ingredients (including other ingredients on the menu or things that are not on the menu), ignore those things; only omit actual base ingredients. For example, if the set of things to omit includes "corn", ignore it because corn is not a base ingredient. • If the user says to omit an ingredient and they also say to add the same ingredient, include the ingredient in the value of ingredients. • If the user says to add things that are not ingredients on the menu, go ahead and add those things to the value of the ingredients attribute. For example, if the set of things to add includes "walnuts", then the value of ingredients should include "walnuts". Hint: the ingredients attribute is a set. So are the values of the parameters for ingredients to add to the salad and base ingredients to omit (unless they are None, in which case you can convert them to empty sets). Set operations can help you build up the ingredients attribute quickly and efficiently. Think about which operation can help you omit items from a set, and which operation can help you combine two sets.

get_price() The Salad class should have a get_price() method that calculates the price of the order. An order with no ingredients should have a price of 0 dollars. Most other orders will have a price of 8 dollars. Add one dollar each for "shrimp" and "cashews", if they are in ingredients. Return the price in dollars as an integer (e.g., 8).

is_vegan() The Salad class should have an is_vegan() method that returns False if the ingredients attribute contains non-vegan ingredients, and True otherwise. As a reminder, non-vegan ingredients are "chicken", "shrimp", "feta", and "ranch".

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 class Salad: def __init__(self, name, adds=None, omits=None): self.name = name self.ingredient = set() self.ingredient.difference_update(set(omits)) self.ingredient.update(set(adds)) def get_price(self): price=8 for item in self.ingredient: if item in ["shrimp","cashews"]: price = price + 1 else: price=0 return price def is_vegan(self): flag = true for item in self.ingredient: if item in ["chicken","shrimp","feta","ranch"]: flag = false break return flag
Add a comment
Know the answer?
Add Answer to:
Python Classes - Write a class to determine the price of a salad and its ingredients....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in...

    python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict’s values as keys, associated to a set of the bidict’s keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • IN C++ PLEASE Write a class Food that has a string attribute for name and a...

    IN C++ PLEASE Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false add a method spoil() which sets isSpoiled to true Add a method eat() If hasBeenEaten is false, if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true if isSpoiled is true, return a string that says "I wouldn't...

  • Need help asap with basic c++ question Write a class Food that has a string attribute...

    Need help asap with basic c++ question Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false add a method spoil() which sets isSpoiled to true Add a method eat() If hasBeenEaten is false, if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true if isSpoiled is true, return a string...

  • Language is Python 3.6 Function name (7): class writing Description: Write a class called "Burrito". A...

    Language is Python 3.6 Function name (7): class writing Description: Write a class called "Burrito". A Burrito should have the following attributes (instance variables): meat, to_go, rice, beans, extra meat (default: False), guacamole (default: False), cheese (default: False), pico (default: False), corn (default: False), Add a method called "get_cost" to the Burrito class. It should accept zero arguments (except for "self, of course) and it will return a float. Here's how the cost should be computed: - The base cost...

  • ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...

    ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes, where the Media superclass has the artistName and the mediaName as the common attributes. Create a subclass called CDMedia that has the additional arrayList of String objects containing the songs per album. Create another subclass called DVDMedia that has the additional year the movie came out, and an arrayList of co-stars for the movie. 1.) The superclass Media will implement Comparable, and will define...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Pls help me write this function in python. (pls include commented explanation) Function name (7): class...

    Pls help me write this function in python. (pls include commented explanation) Function name (7): class writing Description: Write a class called "Burrito". A Burrito should have the following attributes (instance variables): meat, to_go, rice, beans, extra_meat (default: False), guacamole (default: False), cheese (default: False), pico (default: False), corn (default: False), Add a method called "get_cost" to the Burrito class. It should accept zero arguments (except for "self", of course) and it will return a float. Here's how the cost...

  • Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price)....

    Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price). An instance of SodaMachine has access to three methods, purchase, deposit and restock that will describe its interaction with the user returning strings. Tip: use the string format method Here is the base code===== class SodaMachine: ''' Creates instances of the class SodaMachine. Takes a string and an integer ''' def __init__(self, product, price): #-- start code here --- #-- ends here --- def...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT