Skip to content

Commit 34d7f5a

Browse files
committed
bank-account: Formatting
1 parent a80f196 commit 34d7f5a

File tree

3 files changed

+48
-98
lines changed

3 files changed

+48
-98
lines changed
Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21

32

43
class BankAccount(object):
@@ -20,29 +19,3 @@ def withdraw(self, amount):
2019

2120
def close(self):
2221
pass
23-
=======
24-
import threading
25-
26-
class BankAccount(object):
27-
def __init__(self):
28-
pass
29-
30-
def getBalance(self):
31-
pass
32-
33-
def open(self):
34-
pass
35-
36-
def deposit(self, amount):
37-
pass
38-
39-
40-
def withdraw(self, amount):
41-
pass
42-
43-
def close(self):
44-
pass
45-
46-
47-
48-
>>>>>>> b8e7ac1... bank-account: exercise configuration

exercises/bank-account/bank_account_test.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from bank_account import BankAccount
55

6+
67
class BankAccountTests(unittest.TestCase):
78

89
def test_newly_opened_account_has_zero_balance(self):
@@ -24,19 +25,6 @@ def test_get_balance_with_amount(self):
2425

2526
self.assertEqual(self.account.getBalance(), 100)
2627

27-
def test_get_balance_0(self):
28-
self.account = BankAccount()
29-
self.account.open()
30-
31-
self.assertEqual(self.account.getBalance(), 0)
32-
33-
def test_get_balance_with_amount(self):
34-
self.account = BankAccount()
35-
self.account.open()
36-
self.account.deposit(100)
37-
38-
self.assertEqual(self.account.getBalance(), 100)
39-
4028
def test_deposit_into_account(self):
4129
self.account = BankAccount()
4230
self.account.open()
@@ -76,8 +64,6 @@ def test_deposit_into_closed_account(self):
7664
with self.assertRaises(Exception):
7765
self.account.deposit(50)
7866

79-
80-
8167
def test_withdraw_from_closed_account(self):
8268
self.account = BankAccount()
8369
self.account.open()
@@ -109,8 +95,6 @@ def test_cannot_deposit_negative(self):
10995
with self.assertRaises(Exception):
11096
self.account.deposit(-50)
11197

112-
113-
11498
def test_can_handle_concurrent_transactions(self):
11599
def increment_and_decrement(self):
116100
self.account.deposit(10)
@@ -124,9 +108,10 @@ def increment_and_decrement(self):
124108

125109
threadlist = []
126110
threads = 100
127-
i=0
128-
while (i<threads):
129-
thread = threading.Thread(target=increment_and_decrement, args=(self, ))
111+
i = 0
112+
while (i < threads):
113+
thread = threading.Thread(target=increment_and_decrement,
114+
args=(self, ))
130115
threadlist.append(thread)
131116
i += 1
132117

@@ -138,5 +123,6 @@ def increment_and_decrement(self):
138123

139124
self.assertEqual(self.account.getBalance(), 900)
140125

126+
141127
if __name__ == '__main__':
142-
unittest.main();
128+
unittest.main()

exercises/bank-account/example.py

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
11
import threading
22

3-
class BankAccount(object):
4-
def __init__(self):
5-
self.lock = threading.Lock()
6-
7-
def getBalance(self):
8-
self.lock.acquire()
9-
if (self.open == True):
10-
self.lock.release()
11-
return self.balance
12-
else:
13-
self.lock.release()
14-
raise Exception
15-
16-
def getBalance(self):
17-
self.lock.acquire()
18-
if (self.open == True):
19-
self.lock.release()
20-
return self.balance
21-
else:
22-
self.lock.release()
23-
raise Exception
24-
25-
def open(self):
26-
self.lock.acquire()
27-
self.balance = 0
28-
self.open = True
29-
self.lock.release()
303

31-
def deposit(self, amount):
32-
self.lock.acquire()
33-
if (self.open == True and amount>0):
34-
self.balance += amount
35-
self.lock.release()
36-
else:
37-
self.lock.release()
38-
raise Exception
39-
40-
41-
def withdraw(self, amount):
42-
self.lock.acquire()
43-
if (self.open == True and self.balance >= amount and amount>0):
44-
self.balance -= amount
45-
self.lock.release()
46-
else:
47-
self.lock.release()
48-
raise Exception
49-
50-
def close(self):
51-
self.lock.acquire()
52-
self.open = False
53-
self.lock.release()
4+
class BankAccount(object):
5+
def __init__(self):
6+
self.lock = threading.Lock()
7+
8+
def getBalance(self):
9+
self.lock.acquire()
10+
if (self.open is True):
11+
self.lock.release()
12+
return self.balance
13+
else:
14+
self.lock.release()
15+
raise Exception
16+
17+
def open(self):
18+
self.lock.acquire()
19+
self.balance = 0
20+
self.open = True
21+
self.lock.release()
22+
23+
def deposit(self, amount):
24+
self.lock.acquire()
25+
if (self.open is True and amount > 0):
26+
self.balance += amount
27+
self.lock.release()
28+
else:
29+
self.lock.release()
30+
raise Exception
31+
32+
def withdraw(self, amount):
33+
self.lock.acquire()
34+
if (self.open is True and self.balance >= amount and amount > 0):
35+
self.balance -= amount
36+
self.lock.release()
37+
else:
38+
self.lock.release()
39+
raise Exception
40+
41+
def close(self):
42+
self.lock.acquire()
43+
self.open = False
44+
self.lock.release()

0 commit comments

Comments
 (0)