mvc.py

Home   »   mvc.py

from dataclasses import dataclass


# Model
@dataclass
class Vessel:
    volume: str
    type_drink: str = None


class Bottle(Vessel):
    def __init__(self, volume, type_drink):
        super().__init__(volume)
        self.type_drink = type_drink


@dataclass
class Entity:
    volume: int
    last_drink: str = None
    name: str = None


class Human(Entity):
    def __init__(self, name):
        super().__init__(100)
        self.name = name


# Control
def _drink(entity: Entity, vessel: Vessel, volume):
    vessel.volume -= volume
    entity.volume += volume
    entity.last_drink = vessel.type_drink
    return {
        "volume": volume,
        "type_drink": entity.last_drink,
        "vessel_volume": vessel.volume
    }


# View
def drink(entity: Entity, vessel: Vessel, volume):
    request = _drink(entity, vessel, volume)
    return f"{entity.name} drank {request.get('volume')} " \
           f"litters of {request.get('type_drink')}. " \
           f"There’s still {request.get('vessel_volume')} " \
           f"liters left in the {vessel.__class__.__name__}."


human = Human('Jorge')
bottle = Bottle(10, 'Water')

print(drink(human, bottle, 2))

Leave a Reply

Your email address will not be published. Required fields are marked *