Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
class factory solution in Clear category for 3 Chefs by Sim0000
def class_factory(food_name, drink_name):
class T:
def __init__(self):
self.food = self.drink = 0
def add_food(self, food_amount, food_price):
self.food += food_amount * food_price
def add_drink(self, drink_amount, drink_price):
self.drink += drink_amount * drink_price
def total(self):
return f'{food_name}: {self.food}, {drink_name}: {self.drink}, Total: {self.food + self.drink}'
return T
JapaneseCook = class_factory('Sushi', 'Tea')
RussianCook = class_factory('Dumplings', 'Compote')
ItalianCook = class_factory('Pizza', 'Juice')
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def json_dict(d):
return dict((k, list(v)) for k, v in d.items())
client_1 = JapaneseCook()
client_1.add_food(2, 30)
client_1.add_drink(2, 10)
client_2 = RussianCook()
client_2.add_food(1, 40)
client_2.add_drink(5, 20)
assert client_1.total() == "Sushi: 60, Tea: 20, Total: 80"
assert client_2.total() == "Dumplings: 40, Compote: 100, Total: 140"
print("Coding complete? Let's try tests!")
June 9, 2018
Comments: