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