Question

(Composition) Write a Pizza class so that this client code works. Please note that it is...

  1. (Composition) Write a Pizza class so that this client code works. Please note that it is ok if the toppings are listed in a different order.

>>> pie = Pizza()

>>> pie

Pizza('M',set())

>>> pie.setSize('L')

>>> pie.getSize()

'L'

>>> pie.addTopping('pepperoni')

>>> pie.addTopping('anchovies')

>>> pie.addTopping('mushrooms')

>>> pie

Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'})

>>> pie.addTopping('pepperoni')

>>> pie

Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'})

>>> pie.removeTopping('anchovies')

>>> pie

Pizza('L',{'mushrooms', 'pepperoni'})

>>> pie.price()

16.65

>>> pie2 = Pizza('L',{'mushrooms','pepperoni'})

>>> pie2

Pizza('L',{'mushrooms', 'pepperoni'})

>>> pie==pie2

True

class Pizza:
def __init__(self, s = 'M', t = []):
self.setSize(s)
self.toppings = t

def setSize(self, s):
self.size = s

def getSize(self):
return self.size
  
def addTopping(self, t):
self.toppings.add(t)

def removeTopping(self, t):
self.toppings.remove(t)

def price(self):
if self.size == 'S':
return 6.25 + (0.7 * len(self.toppings))
elif self.size == 'S':
return 9.95 + (1.45 * len(self.toppings))
else:
return 12.95 + (1.85 * len(self.toppings))

def __repr__(self):
ret = 'Pizza('
ret += ('\'' + self.size + '\'')
ret += ','
ret += str(self.toppings)
ret += ')'
return ret

def __eq__(self, other):
return self.size == other.size and (self.toppings - other.toppings == set())

How do I make sure the list for toppings gets cleared for new pizza. Doc test shows me failing by keeping toppings from prior pizza...?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented in python

Note: I actually solved this problem prior

code:


class Pizza():
def __init__(self, size = 'M', toppings = set()):
self.s = size
self.t = set(toppings)

def __repr__(self):
return("Pizza('{}',{})".format(self.s, self.t))

def setSize(self, size):
self.s = size

def getSize(self):
return self.s

def addTopping(self, toppings):
self.t.add(toppings)

def removeTopping(self, toppings):
self.t.remove(toppings)

def price(self):
if self.s == 'S':
return(6.25 + (len(self.t) * 0.70))
elif self.s == 'M':
return(9.95 + (len(self.t) * 1.45))
elif self.s == 'L':
return(12.95 + (len(self.t) * 1.85))

def __eq__(self, other):
return self.s == other.s and set(self.t) == set(other.t)


def orderPizza():
pie = Pizza()
print("Welcome to Python Pizza!")

size = str(input("What size pizza would you like (S,M,L): "))
pie.setSize(size)

while True:
topping = str(input("Type topping to add (or Enter to quit): "))
pie.addTopping(topping)
if topping == '':
pie.removeTopping('')
break

print("Thanks for ordering!")
print("Your pizza costs ${}".format(pie.price()))
return pie

if __name__=='__main__':
import doctest
print(doctest.testfile( 'test.py'))

#Note:Replace the doctest file with your file.

Code Screenshots:

Add a comment
Know the answer?
Add Answer to:
(Composition) Write a Pizza class so that this client code works. Please note that it is...
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
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