Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Is this a proper implementation of the abstract factory design pattern? solution in Clear category for 3 Chefs by lucas.stonedrake
from abc import abstractstaticmethod
class AbstractCook():
food = 0
drink = 0
total = 0
@abstractstaticmethod
def add_food():
'''Add Food'''
@abstractstaticmethod
def add_drink():
'''Add Drink'''
@abstractstaticmethod
def total():
'''Total'''
class JapaneseCook(AbstractCook):
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"Sushi: {self.food}, Tea: {self.drink}, Total: {self.drink + self.food}"
class RussianCook(AbstractCook):
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"Dumplings: {self.food}, Compote: {self.drink}, Total: {self.drink + self.food}"
class ItalianCook(AbstractCook):
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"Pizza: {self.food}, Juice: {self.drink}, Total: {self.drink + self.food}"
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!")
March 25, 2021
Comments: