diff --git a/src/framework/domain/components.py b/src/framework/domain/components.py index 345d839..a819673 100644 --- a/src/framework/domain/components.py +++ b/src/framework/domain/components.py @@ -3,6 +3,7 @@ from typing import List from .entity import Entity +from .exception import KnapsackBurst __all__ = [ "Component", @@ -220,6 +221,20 @@ class PSUComponent(Component): modularity: EPSUModularity +@dataclass +class Knapsack(Entity): + components: list[Component] + max_price: Money + current_price: Money + + def push(self, component: Component, price: Money): + if self.current_price + price > self.max_price: + raise KnapsackBurst() + + # TODO checar restrições + self.components.append(component) + + component_cls_idx = [ Component, MotherboardComponent, diff --git a/src/framework/domain/exception.py b/src/framework/domain/exception.py index 0f45d56..6d501e0 100644 --- a/src/framework/domain/exception.py +++ b/src/framework/domain/exception.py @@ -9,3 +9,13 @@ class DomainException(Exception): def __repr__(self): return f"{self.__class__.__name__}: {self._message}" + + +@dataclass +class KnapsackBurst(DomainException): + _message: str = "A bolsa atingiu o limite de preço." + + +@dataclass +class CurrencyNotEqual(DomainException): + _message: str = "As moedas são diferentes" diff --git a/src/framework/domain/value_object.py b/src/framework/domain/value_object.py index 5813968..86466da 100644 --- a/src/framework/domain/value_object.py +++ b/src/framework/domain/value_object.py @@ -6,6 +6,7 @@ from typing import Tuple from .rule import Rule, BussinessAssertionExtension +from .exception import CurrencyNotEqual __all__ = ["UUID", "UUIDv4", "UUIDv5", "ValueObject", "Money", "URL"] @@ -32,6 +33,11 @@ def __eq__(self, oMoney: "Money") -> bool: def __lt__(self, oMoney: "Money") -> bool: return self.currency == oMoney.currency and self.amount < oMoney.amount + def __add__(self, oMoney: "Money") -> "Money": + if self.currency != oMoney.currency: + raise CurrencyNotEqual() + return Money(self.amount + oMoney.amount) + def __repr__(self): return f"{self.currency} {self.amount:.2f}"