Skip to content

Commit dc4c1f5

Browse files
committed
bank-account: Formatting
1 parent 8cfd2fd commit dc4c1f5

File tree

3 files changed

+61
-87
lines changed

3 files changed

+61
-87
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import threading
21

3-
class BankAccount(object):
4-
def __init__(self):
5-
pass
6-
7-
def getBalance(self):
8-
pass
92

10-
def open(self):
11-
pass
12-
13-
def deposit(self, amount):
14-
pass
3+
class BankAccount(object):
154

5+
def __init__(self):
6+
pass
167

17-
def withdraw(self, amount):
18-
pass
8+
def getBalance(self):
9+
pass
1910

20-
def close(self):
21-
pass
11+
def open(self):
12+
pass
2213

14+
def deposit(self, amount):
15+
pass
2316

17+
def withdraw(self, amount):
18+
pass
2419

20+
def close(self):
21+
pass

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)
@@ -121,9 +105,10 @@ def increment_and_decrement(self):
121105

122106
threadlist = []
123107
threads = 100
124-
i=0
125-
while (i<threads):
126-
thread = threading.Thread(target=increment_and_decrement, args=(self, ))
108+
i = 0
109+
while (i < threads):
110+
thread = threading.Thread(target=increment_and_decrement,
111+
args=(self, ))
127112
threadlist.append(thread)
128113
i += 1
129114

@@ -135,5 +120,6 @@ def increment_and_decrement(self):
135120

136121
self.assertEqual(self.account.getBalance(), 900)
137122

123+
138124
if __name__ == '__main__':
139-
unittest.main();
125+
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)