Skip to content

Commit cc82d3e

Browse files
committed
chore: adds BankAtm.java and BankAtmTest.java
1 parent 28ae2de commit cc82d3e

File tree

2 files changed

+19
-32
lines changed
  • lesson_17/bank/bank_app/src

2 files changed

+19
-32
lines changed

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
43
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.UUID;
87

8+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9+
910
/** Represents a bank ATM. */
1011
public class BankAtm {
1112

1213
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
14+
private final Map<String, BankAccount> accountByNumber = new HashMap<>();
1415

1516
/**
1617
* Adds a checking account to the bank.
1718
*
1819
* @param account The account to add.
1920
*/
20-
public void addAccount(CheckingAccount account) {
21+
public void addAccount(BankAccount account) {
2122
accountByNumber.put(account.getAccountNumber(), account);
2223
account
2324
.getOwners()
@@ -33,7 +34,7 @@ public void addAccount(CheckingAccount account) {
3334
* @param customerId The ID of the customer.
3435
* @return The unique set of accounts owned by the customer.
3536
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
37+
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
3738
return customerById.containsKey(customerId)
3839
? customerById.get(customerId).getAccounts()
3940
: Set.of();
@@ -46,7 +47,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4647
* @param amount The amount to deposit.
4748
*/
4849
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
50+
BankAccount account = getAccountOrThrow(accountNumber);
5051
account.deposit(amount);
5152
}
5253

@@ -57,7 +58,7 @@ public void depositFunds(String accountNumber, double amount) {
5758
* @param check The check to deposit.
5859
*/
5960
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
61+
BankAccount account = getAccountOrThrow(accountNumber);
6162
check.depositFunds(account);
6263
}
6364

@@ -68,7 +69,7 @@ public void depositFunds(String accountNumber, Check check) {
6869
* @param amount
6970
*/
7071
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
72+
BankAccount account = getAccountOrThrow(accountNumber);
7273
account.withdraw(amount);
7374
}
7475

@@ -78,8 +79,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7879
* @param accountNumber The account number.
7980
* @return The account.
8081
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
82+
private BankAccount getAccountOrThrow(String accountNumber) {
83+
BankAccount account = accountByNumber.get(accountNumber);
8384
if (account == null || account.isClosed()) {
8485
throw new AccountNotFoundException("Account not found");
8586
}
Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,96 @@
11
package com.codedifferently.lesson17.bank;
2-
32
import static org.assertj.core.api.Assertions.assertThat;
43
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
54

6-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
7-
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
85
import java.util.Set;
96
import java.util.UUID;
7+
108
import org.junit.jupiter.api.BeforeEach;
119
import org.junit.jupiter.api.Test;
1210

11+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
12+
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
1313
class BankAtmTest {
1414

1515
private BankAtm classUnderTest;
1616
private CheckingAccount account1;
17-
private CheckingAccount account2;
17+
private SavingAccount account2;
1818
private Customer customer1;
1919
private Customer customer2;
2020

21-
@BeforeEach
21+
@BeforeEach
2222
void setUp() {
2323
classUnderTest = new BankAtm();
2424
customer1 = new Customer(UUID.randomUUID(), "John Doe");
2525
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
2626
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
27-
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
27+
account2 = new SavingAccount("987654321", Set.of(customer1, customer2), 200.0);
2828
customer1.addAccount(account1);
2929
customer1.addAccount(account2);
3030
customer2.addAccount(account2);
3131
classUnderTest.addAccount(account1);
3232
classUnderTest.addAccount(account2);
3333
}
34-
3534
@Test
3635
void testAddAccount() {
3736
// Arrange
3837
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
3938
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
4039
customer3.addAccount(account3);
41-
4240
// Act
4341
classUnderTest.addAccount(account3);
4442

4543
// Assert
46-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
44+
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
4745
assertThat(accounts).containsOnly(account3);
4846
}
4947

5048
@Test
5149
void testFindAccountsByCustomerId() {
5250
// Act
53-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
51+
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5452

5553
// Assert
5654
assertThat(accounts).containsOnly(account1, account2);
5755
}
58-
5956
@Test
6057
void testDepositFunds() {
6158
// Act
6259
classUnderTest.depositFunds(account1.getAccountNumber(), 50.0);
63-
6460
// Assert
6561
assertThat(account1.getBalance()).isEqualTo(150.0);
6662
}
67-
6863
@Test
6964
void testDepositFunds_Check() {
7065
// Arrange
7166
Check check = new Check("987654321", 100.0, account1);
72-
7367
// Act
7468
classUnderTest.depositFunds("987654321", check);
75-
7669
// Assert
7770
assertThat(account1.getBalance()).isEqualTo(0);
7871
assertThat(account2.getBalance()).isEqualTo(300.0);
7972
}
80-
8173
@Test
8274
void testDepositFunds_DoesntDepositCheckTwice() {
8375
Check check = new Check("987654321", 100.0, account1);
84-
8576
classUnderTest.depositFunds("987654321", check);
86-
8777
assertThatExceptionOfType(CheckVoidedException.class)
8878
.isThrownBy(() -> classUnderTest.depositFunds("987654321", check))
8979
.withMessage("Check is voided");
9080
}
91-
9281
@Test
9382
void testWithdrawFunds() {
9483
// Act
9584
classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0);
96-
9785
// Assert
9886
assertThat(account2.getBalance()).isEqualTo(150.0);
9987
}
100-
10188
@Test
10289
void testWithdrawFunds_AccountNotFound() {
10390
String nonExistingAccountNumber = "999999999";
104-
10591
// Act & Assert
10692
assertThatExceptionOfType(AccountNotFoundException.class)
10793
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
10894
.withMessage("Account not found");
10995
}
110-
}
96+
}

0 commit comments

Comments
 (0)