Files
ci-demo/app/discounts.py

36 lines
916 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Скидки демо-проекта ci-demo (СТРПО, неделя 9, Gitea edition)."""
from abc import ABC, abstractmethod
class Discount(ABC):
@abstractmethod
def apply(self, total: float) -> float:
"""Вернуть сумму с учётом скидки."""
class NoDiscount(Discount):
def apply(self, total: float) -> float:
return total
class VIPDiscount(Discount):
def apply(self, total: float) -> float:
return total * 0.9
class EmployeeDiscount(Discount):
def apply(self, total: float) -> float:
return total * 0.7
DISCOUNTS: dict[str, type[Discount]] = {
"regular": NoDiscount,
"vip": VIPDiscount,
"employee": EmployeeDiscount,
}
def discount_for(customer_type: str) -> Discount:
"""Выбрать стратегию скидки по типу клиента."""
return DISCOUNTS.get(customer_type, NoDiscount)()