Path: blob/master/exercices/oop/bank/solution_bank.py
306 views
# bank.py1from typing import Self234class BankAccount:5def __init__(self, holder: str, balance: int = 0):6if balance < 0:7raise ValueError8self._balance = balance9self.holder = holder1011def deposit(self, amount: int) -> int:12if amount <= 0:13raise ValueError14self._balance += amount15return self._balance1617def withdraw(self, amount: int) -> int:18if amount > self._balance:19raise ValueError20self._balance -= amount21return self._balance2223def transfer(self, amount: int, receiver: Self) -> int:24if amount > self._balance or amount < 0:25raise ValueError26self.withdraw(amount)27receiver.deposit(amount)28return self._balance293031account = BankAccount("esna")32account.deposit(100)33account.withdraw(30)34print(account._balance)3536receiver = BankAccount("gregouz")37account.transfer(70, receiver)38print(account._balance)39print(receiver._balance)404142