In: Computer Science
Python 2. Please ensure you run the tests in idle
Write a function orderPizza that allows the user input to build a pizza. It then prints a thank you message, the cost of the pizza and then returns the Pizza that was built.
>>> orderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): M
Type topping to add (or Enter to quit): mushroom
Type topping to add (or Enter to quit): onion
Type topping to add (or Enter to quit): garlic
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $14.299999999999999
Pizza('M',{'mushroom', 'onion', 'garlic'})
>>> orderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): L
Type topping to add (or Enter to quit): calamari
Type topping to add (or Enter to quit): garlic
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $16.65
Pizza('L',{'garlic', 'calamari'})
>>> p=orderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): S
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $6.25
>>> p
Pizza('S',set())
>>>
please run this tests in idle to ensure it is correct
##### orderPizza #####
>>> orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'})
Welcome to Python Pizza!
What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering!
Your pizza costs $14.299999999999999
True
>>> orderPizza()==Pizza('L',{'calamari', 'garlic'})
Welcome to Python Pizza!
What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering!
Your pizza costs $16.65
True
>>> orderPizza()==Pizza('S',set())
Welcome to Python Pizza!
What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Thanks for ordering!
Your pizza costs $6.25
True
##### stdin back #####
put stdin back to original, again, shouldnt cause error
>>> sys.stdin = si # return stdin
class Pizza:
def __init__(self, builder):
self.builder = builder
self.pizzaObj = self.__ComputeCost__(builder)
def getPizzaObj(self):
return self.pizzaObj
def __ComputeCost__(self, builder):
cost = builder.pizzaBase[1]
items = []
if builder.mushroom[0]:
cost = cost + builder.mushroom[1]
items.append("mushroom")
if builder.garlic[0]:
cost = cost + builder.garlic[1]
items.append("garlic")
if builder.onion[0]:
cost = cost + builder.onion[1]
items.append("onion")
if builder.calamari[0]:
cost = cost + builder.calamari[1]
items.append("calamari")
return (cost, items)
class PizzaBuilder:
def __init__(self, size, basePrice):
self.pizzaBase = (size, basePrice) # 10 is the base cost
self.mushroom, self.onion = (False, 0), (False, 0)
self.garlic, self.calamari = (False, 0), (False, 0)
def AddMushroom(self):
self.mushroom = (True, 2)
return self
def AddGarlic(self):
self.garlic = (True, 2)
return self
def AddOnion(self):
self.onion = (True, 1)
return self
def AddCalamari(self):
self.calamari = (True, 3)
return self
def Build(self):
return Pizza(self)
if __name__ == '__main__':
pizzaSize = 'start'
while pizzaSize != "q":
print("Welcome To Python Pizza")
pizzaSize = input("Please Select the Size of the Pizza (S, M, L):").strip()
if pizzaSize == 'S' or pizzaSize == 's':
cost = 10
elif pizzaSize == 'M' or pizzaSize=='m':
cost = 15
elif pizzaSize == 'L' or pizzaSize == 'l':
cost = 20
else:
print("Invalid Choice", pizzaSize, " " , "(Available Sizes are: S, M, L) Try Again. To Quit (Type q to quit" )
continue
pizzaObj = PizzaBuilder(pizzaSize, cost)
topping = -1
while topping !=0:
topping = int(input("Please Choose the Topping Available (Mushroom(1), Garlic(2), Onion(3), Calamari(4), To Place Order(0): ").strip())
if topping == 1:
pizzaObj = pizzaObj.AddMushroom()
elif topping == 2:
pizzaObj = pizzaObj.AddGarlic()
elif topping == 3:
pizzaObj = pizzaObj.AddOnion()
elif topping == 4:
pizzaObj = pizzaObj.AddCalamari()
pizza = pizzaObj.Build()
obj = pizza.getPizzaObj()
print("Your Pizza Costs: ", obj[0] )
print(f'Your Pizza({pizzaSize, obj[1]})')