Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for 3 Chefs by keromage
class AbstractCook(object):
"""
AbstractCook: SuperClass of the other Cook class
attributes:
receipt : dictionary
key : items(food,drink)
value : price(= unit price * amount)
food : str
drink : str
"""
def __init__(self):
self.receipt = {}
self.food = ''
self.drink = ''
def add_food(self, amount, price):
"""
Register the item name and price on "receipt".
If item name is already registered, add price.
:param amount: int
:param price: int
:return: -
"""
if self.food in self.receipt:
self.receipt[self.food] += amount * price
else:
self.receipt[self.food] = amount * price
def add_drink(self, amount, price):
if self.drink in self.receipt:
self.receipt[self.drink] += amount * price
else:
self.receipt[self.drink] = amount * price
def total(self):
"""
Pick up the item and amount from the "receipt" data
and write it down in "text" together with the total value.
:return: formatted text(str)
"""
total_price = 0
text = ""
for item, price in self.receipt.items():
text += "{}: {}, ".format(item, price)
total_price += price
text += "Total: {}".format(total_price)
return text
class JapaneseCook(AbstractCook):
def __init__(self):
AbstractCook.__init__(self)
self.food = 'Sushi'
self.drink = 'Tea'
class RussianCook(AbstractCook):
def __init__(self):
AbstractCook.__init__(self)
self.food = 'Dumplings'
self.drink = 'Compote'
class ItalianCook(AbstractCook):
def __init__(self):
AbstractCook.__init__(self)
self.food = 'Pizza'
self.drink = 'Juice'
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
client_1 = JapaneseCook()
client_1.add_food(2, 30)
client_1.add_food(3, 15)
client_1.add_drink(2, 10)
client_2 = RussianCook()
client_2.add_food(1, 40)
client_2.add_food(2, 25)
client_2.add_drink(5, 20)
client_3 = ItalianCook()
client_3.add_food(2, 20)
client_3.add_food(2, 30)
client_3.add_drink(2, 10)
assert client_1.total() == "Sushi: 105, Tea: 20, Total: 125"
assert client_2.total() == "Dumplings: 90, Compote: 100, Total: 190"
assert client_3.total() == "Pizza: 100, Juice: 20, Total: 120"
print("Coding complete? Let's try tests!")
July 26, 2020
Comments: