Files
python-learning/exercises/01-core/06-oop-dataclasses/oop_ex.py
T

46 lines
1.2 KiB
Python

from dataclasses import dataclass
from typing import Protocol
class BankAccount:
def __init__(self, initial_balance: int = 0) -> None:
self._balance = initial_balance
@property
def balance(self) -> int:
return self._balance
def deposit(self, amount: int) -> None:
raise NotImplementedError("Dodaj amount do _balance (odrzuć <= 0 przez ValueError)")
def withdraw(self, amount: int) -> bool:
raise NotImplementedError("Wypłać jeśli _balance >= amount i zwróć True, inaczej False")
class Notifier(Protocol):
def format_message(self, msg: str) -> str: ...
@dataclass(frozen=True)
class EmailNotifier:
address: str
def format_message(self, msg: str) -> str:
raise NotImplementedError("Zwróć '[email] <msg>'")
@dataclass(frozen=True)
class SmsNotifier:
phone: str
def format_message(self, msg: str) -> str:
raise NotImplementedError("Zwróć '[sms] <msg>'")
class AlertService:
def __init__(self, notifiers: list[Notifier]) -> None:
self._notifiers = notifiers
def broadcast(self, msg: str) -> list[str]:
raise NotImplementedError("Wywołaj format_message(msg) dla każdego notifiera")