Skip to content

Commit 6ed82fb

Browse files
Sync tests for practice exercise bank-account (#2469)
1 parent 7dce287 commit 6ed82fb

File tree

5 files changed

+161
-63
lines changed

5 files changed

+161
-63
lines changed

exercises/practice/bank-account/.meta/src/reference/java/BankAccount.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ class BankAccount {
22
private int balance = 0;
33
private boolean isClosed = true;
44

5-
void open() {
5+
void open() throws BankAccountActionInvalidException {
6+
if (!isClosed) {
7+
throw new BankAccountActionInvalidException("Account already open");
8+
}
69
isClosed = false;
10+
balance = 0;
711
}
812

9-
void close() {
13+
void close() throws BankAccountActionInvalidException {
14+
if (isClosed) {
15+
throw new BankAccountActionInvalidException("Account not open");
16+
}
1017
isClosed = true;
1118
}
1219

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[983a1528-4ceb-45e5-8257-8ce01aceb5ed]
13+
description = "Newly opened account has zero balance"
14+
15+
[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]
16+
description = "Single deposit"
17+
18+
[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]
19+
description = "Multiple deposits"
20+
21+
[08f1af07-27ae-4b38-aa19-770bde558064]
22+
description = "Withdraw once"
23+
24+
[6f6d242f-8c31-4ac6-8995-a90d42cad59f]
25+
description = "Withdraw twice"
26+
27+
[45161c94-a094-4c77-9cec-998b70429bda]
28+
description = "Can do multiple operations sequentially"
29+
30+
[f9facfaa-d824-486e-8381-48832c4bbffd]
31+
description = "Cannot check balance of closed account"
32+
33+
[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]
34+
description = "Cannot deposit into closed account"
35+
36+
[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]
37+
description = "Cannot deposit into unopened account"
38+
39+
[570dfaa5-0532-4c1f-a7d3-0f65c3265608]
40+
description = "Cannot withdraw from closed account"
41+
42+
[c396d233-1c49-4272-98dc-7f502dbb9470]
43+
description = "Cannot close an account that was not opened"
44+
45+
[c06f534f-bdc2-4a02-a388-1063400684de]
46+
description = "Cannot open an already opened account"
47+
48+
[0722d404-6116-4f92-ba3b-da7f88f1669c]
49+
description = "Reopened account does not retain balance"
50+
51+
[ec42245f-9361-4341-8231-a22e8d19c52f]
52+
description = "Cannot withdraw more than deposited"
53+
54+
[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]
55+
description = "Cannot withdraw negative"
56+
57+
[d45df9ea-1db0-47f3-b18c-d365db49d938]
58+
description = "Cannot deposit negative"
59+
60+
[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]
61+
description = "Can handle concurrent transactions"

exercises/practice/bank-account/src/main/java/BankAccount.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class BankAccount {
22

3-
void open() {
3+
void open() throws BankAccountActionInvalidException {
44
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
55
}
66

7-
void close() {
7+
void close() throws BankAccountActionInvalidException {
88
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
99
}
1010

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
class BankAccountActionInvalidException extends Exception {
1+
public class BankAccountActionInvalidException extends Exception {
22

3-
BankAccountActionInvalidException(String message) {
3+
public BankAccountActionInvalidException(String message) {
44
super(message);
55
}
66
}

exercises/practice/bank-account/src/test/java/BankAccountTest.java

+87-57
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
2-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3-
import static org.assertj.core.api.Assertions.fail;
4-
1+
import org.junit.Before;
52
import org.junit.Ignore;
63
import org.junit.Test;
74

85
import java.util.Random;
96

7+
import static org.assertj.core.api.Assertions.*;
8+
109
public class BankAccountTest {
11-
private BankAccount bankAccount = new BankAccount();
10+
private BankAccount bankAccount;
11+
12+
@Before
13+
public void setUp() {
14+
bankAccount = new BankAccount();
15+
}
1216

1317
@Test
1418
public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException {
@@ -19,136 +23,162 @@ public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidE
1923

2024
@Ignore("Remove to run test")
2125
@Test
22-
public void canDepositMoney() throws BankAccountActionInvalidException {
26+
public void singleDeposit() throws BankAccountActionInvalidException {
2327
bankAccount.open();
28+
bankAccount.deposit(100);
2429

25-
bankAccount.deposit(10);
26-
27-
assertThat(bankAccount.getBalance()).isEqualTo(10);
30+
assertThat(bankAccount.getBalance()).isEqualTo(100);
2831
}
2932

3033
@Ignore("Remove to run test")
3134
@Test
32-
public void canDepositMoneySequentially() throws BankAccountActionInvalidException {
35+
public void multipleDeposits() throws BankAccountActionInvalidException {
3336
bankAccount.open();
37+
bankAccount.deposit(100);
38+
bankAccount.deposit(50);
3439

35-
bankAccount.deposit(5);
36-
bankAccount.deposit(23);
37-
38-
assertThat(bankAccount.getBalance()).isEqualTo(28);
40+
assertThat(bankAccount.getBalance()).isEqualTo(150);
3941
}
4042

4143
@Ignore("Remove to run test")
4244
@Test
43-
public void canWithdrawMoney() throws BankAccountActionInvalidException {
45+
public void withdrawOnce() throws BankAccountActionInvalidException {
4446
bankAccount.open();
45-
bankAccount.deposit(10);
46-
47-
bankAccount.withdraw(5);
47+
bankAccount.deposit(100);
48+
bankAccount.withdraw(75);
4849

49-
assertThat(bankAccount.getBalance()).isEqualTo(5);
50+
assertThat(bankAccount.getBalance()).isEqualTo(25);
5051
}
5152

5253
@Ignore("Remove to run test")
5354
@Test
54-
public void canWithdrawMoneySequentially() throws BankAccountActionInvalidException {
55+
public void withdrawTwice() throws BankAccountActionInvalidException {
5556
bankAccount.open();
56-
bankAccount.deposit(23);
57-
58-
bankAccount.withdraw(10);
59-
bankAccount.withdraw(13);
57+
bankAccount.deposit(100);
58+
bankAccount.withdraw(80);
59+
bankAccount.withdraw(20);
6060

6161
assertThat(bankAccount.getBalance()).isEqualTo(0);
6262
}
6363

6464
@Ignore("Remove to run test")
6565
@Test
66-
public void cannotWithdrawMoneyFromEmptyAccount() {
66+
public void canDoMultipleOperationsSequentially() throws BankAccountActionInvalidException {
6767
bankAccount.open();
68+
bankAccount.deposit(100);
69+
bankAccount.deposit(110);
70+
bankAccount.withdraw(200);
71+
bankAccount.deposit(60);
72+
bankAccount.withdraw(50);
6873

69-
assertThatExceptionOfType(BankAccountActionInvalidException.class)
70-
.isThrownBy(() -> bankAccount.withdraw(5))
71-
.withMessage("Cannot withdraw money from an empty account");
74+
assertThat(bankAccount.getBalance()).isEqualTo(20);
7275
}
7376

7477
@Ignore("Remove to run test")
7578
@Test
76-
public void cannotWithdrawMoreMoneyThanYouHave() throws BankAccountActionInvalidException {
79+
public void cannotCheckBalanceOfClosedAccount() throws BankAccountActionInvalidException {
7780
bankAccount.open();
78-
bankAccount.deposit(6);
81+
bankAccount.close();
7982

8083
assertThatExceptionOfType(BankAccountActionInvalidException.class)
81-
.isThrownBy(() -> bankAccount.withdraw(7))
82-
.withMessage("Cannot withdraw more money than is currently in the account");
84+
.isThrownBy(bankAccount::getBalance)
85+
.withMessage("Account closed");
8386
}
8487

8588
@Ignore("Remove to run test")
8689
@Test
87-
public void cannotDepositNegativeAmount() {
90+
public void cannotDepositIntoClosedAccount() throws BankAccountActionInvalidException {
8891
bankAccount.open();
92+
bankAccount.close();
8993

9094
assertThatExceptionOfType(BankAccountActionInvalidException.class)
91-
.isThrownBy(() -> bankAccount.deposit(-1))
92-
.withMessage("Cannot deposit or withdraw negative amount");
95+
.isThrownBy(() -> bankAccount.deposit(50))
96+
.withMessage("Account closed");
9397
}
9498

9599
@Ignore("Remove to run test")
96100
@Test
97-
public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidException {
101+
public void cannotDepositIntoUnopenedAccount() {
102+
assertThatExceptionOfType(BankAccountActionInvalidException.class)
103+
.isThrownBy(() -> bankAccount.deposit(50))
104+
.withMessage("Account closed");
105+
}
106+
107+
@Ignore("Remove to run test")
108+
@Test
109+
public void cannotWithdrawFromClosedAccount() throws BankAccountActionInvalidException {
98110
bankAccount.open();
99-
bankAccount.deposit(105);
111+
bankAccount.close();
100112

101113
assertThatExceptionOfType(BankAccountActionInvalidException.class)
102-
.isThrownBy(() -> bankAccount.withdraw(-5))
103-
.withMessage("Cannot deposit or withdraw negative amount");
114+
.isThrownBy(() -> bankAccount.withdraw(50))
115+
.withMessage("Account closed");
104116
}
105117

106118
@Ignore("Remove to run test")
107119
@Test
108-
public void cannotGetBalanceOfClosedAccount() throws BankAccountActionInvalidException {
120+
public void cannotCloseAnAccountThatWasNotOpened() {
121+
assertThatExceptionOfType(BankAccountActionInvalidException.class)
122+
.isThrownBy(bankAccount::close)
123+
.withMessage("Account not open");
124+
}
125+
126+
@Ignore("Remove to run test")
127+
@Test
128+
public void cannotOpenAnAlreadyOpenedAccount() throws BankAccountActionInvalidException {
109129
bankAccount.open();
110-
bankAccount.deposit(10);
111-
bankAccount.close();
112130

113131
assertThatExceptionOfType(BankAccountActionInvalidException.class)
114-
.isThrownBy(bankAccount::getBalance)
115-
.withMessage("Account closed");
132+
.isThrownBy(bankAccount::open)
133+
.withMessage("Account already open");
116134
}
117135

118136
@Ignore("Remove to run test")
119137
@Test
120-
public void cannotDepositMoneyIntoClosedAccount() {
138+
public void reopenedAccountDoesNotRetainBalance() throws BankAccountActionInvalidException {
121139
bankAccount.open();
140+
bankAccount.deposit(50);
122141
bankAccount.close();
142+
bankAccount.open();
143+
144+
assertThat(bankAccount.getBalance()).isEqualTo(0);
145+
}
146+
147+
@Ignore("Remove to run test")
148+
@Test
149+
public void cannotWithdrawMoreThanWasDeposited() throws BankAccountActionInvalidException {
150+
bankAccount.open();
151+
bankAccount.deposit(25);
123152

124153
assertThatExceptionOfType(BankAccountActionInvalidException.class)
125-
.isThrownBy(() -> bankAccount.deposit(5))
126-
.withMessage("Account closed");
154+
.isThrownBy(() -> bankAccount.withdraw(50))
155+
.withMessage("Cannot withdraw more money than is currently in the account");
127156
}
128157

129158
@Ignore("Remove to run test")
130159
@Test
131-
public void cannotWithdrawMoneyFromClosedAccount() throws BankAccountActionInvalidException {
160+
public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidException {
132161
bankAccount.open();
133-
bankAccount.deposit(20);
134-
bankAccount.close();
162+
bankAccount.deposit(100);
135163

136164
assertThatExceptionOfType(BankAccountActionInvalidException.class)
137-
.isThrownBy(() -> bankAccount.withdraw(5))
138-
.withMessage("Account closed");
165+
.isThrownBy(() -> bankAccount.withdraw(-50))
166+
.withMessage("Cannot deposit or withdraw negative amount");
139167
}
140168

141169
@Ignore("Remove to run test")
142170
@Test
143-
public void bankAccountIsClosedBeforeItIsOpened() {
171+
public void cannotDepositNegativeAmount() throws BankAccountActionInvalidException {
172+
bankAccount.open();
173+
144174
assertThatExceptionOfType(BankAccountActionInvalidException.class)
145-
.isThrownBy(bankAccount::getBalance)
146-
.withMessage("Account closed");
175+
.isThrownBy(() -> bankAccount.deposit(-50))
176+
.withMessage("Cannot deposit or withdraw negative amount");
147177
}
148178

149179
@Ignore("Remove to run test")
150180
@Test
151-
public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
181+
public void canHandleConcurrentTransactions() throws BankAccountActionInvalidException, InterruptedException {
152182
bankAccount.open();
153183
bankAccount.deposit(1000);
154184

@@ -158,7 +188,7 @@ public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidExcept
158188
}
159189
}
160190

161-
private void adjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
191+
private void adjustBalanceConcurrently() throws InterruptedException {
162192
Random random = new Random();
163193

164194
Thread[] threads = new Thread[1000];

0 commit comments

Comments
 (0)