Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/exercices/oop/bank/solution_bank.py
306 views
1
# bank.py
2
from typing import Self
3
4
5
class BankAccount:
6
def __init__(self, holder: str, balance: int = 0):
7
if balance < 0:
8
raise ValueError
9
self._balance = balance
10
self.holder = holder
11
12
def deposit(self, amount: int) -> int:
13
if amount <= 0:
14
raise ValueError
15
self._balance += amount
16
return self._balance
17
18
def withdraw(self, amount: int) -> int:
19
if amount > self._balance:
20
raise ValueError
21
self._balance -= amount
22
return self._balance
23
24
def transfer(self, amount: int, receiver: Self) -> int:
25
if amount > self._balance or amount < 0:
26
raise ValueError
27
self.withdraw(amount)
28
receiver.deposit(amount)
29
return self._balance
30
31
32
account = BankAccount("esna")
33
account.deposit(100)
34
account.withdraw(30)
35
print(account._balance)
36
37
receiver = BankAccount("gregouz")
38
account.transfer(70, receiver)
39
print(account._balance)
40
print(receiver._balance)
41
42