forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
35 lines (28 loc) · 826 Bytes
/
example.py
File metadata and controls
35 lines (28 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import threading
class BankAccount(object):
def __init__(self):
self.is_open = False
self.balance = 0
self.lock = threading.Lock()
def get_balance(self):
with self.lock:
if self.is_open:
return self.balance
else:
raise ValueError
def open(self):
self.is_open = True
def deposit(self, amount):
with self.lock:
if self.is_open and amount > 0:
self.balance += amount
else:
raise ValueError
def withdraw(self, amount):
with self.lock:
if self.is_open and 0 < amount <= self.balance:
self.balance -= amount
else:
raise ValueError
def close(self):
self.is_open = False