Skip to content

Commit 5442e18

Browse files
committed
Fixing failures
1 parent 4db6846 commit 5442e18

File tree

7 files changed

+117
-11
lines changed

7 files changed

+117
-11
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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
/**
1011
* Represents a bank ATM with support for multiple account types and comprehensive audit logging.
1112
* Enhanced to follow SOLID principles.
@@ -86,7 +87,7 @@ public void depositFunds(String accountNumber, Check check) {
8687

8788
// Only checking accounts can accept check deposits
8889
if (!(account instanceof CheckingAccount)) {
89-
throw new IllegalArgumentException("Cannot deposit checks to savings accounts");
90+
throw new IllegalArgumentException("Cannot deposit checks to savings accounts. Account type: " + account.getClass().getSimpleName());
9091
}
9192

9293
CheckingAccount checkingAccount = (CheckingAccount) account;

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmIntegrationTest.java

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

3+
import static org.assertj.core.api.Assertions.assertThat;
34
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
56
import static org.junit.jupiter.api.Assertions.assertThrows;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
78

8-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
109
import java.util.List;
1110
import java.util.Set;
1211
import java.util.UUID;
12+
1313
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.Test;
1515

16+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
17+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
18+
1619
/** Integration tests for BankAtm with SavingsAccount and AuditLog. */
1720
class BankAtmIntegrationTest {
1821

@@ -89,10 +92,13 @@ void testSavingsAccountCannotAcceptCheckDeposits() {
8992

9093
// This should throw an exception - depositing check to savings account
9194
Check savingsCheck = new Check("C002", 50.0, checkingAccount);
92-
assertThrows(
95+
IllegalArgumentException exception = assertThrows(
9396
IllegalArgumentException.class,
9497
() -> bankAtm.depositFunds("SAV001", savingsCheck),
9598
"Should not allow check deposits to savings accounts");
99+
100+
assertThat(exception.getMessage())
101+
.isEqualTo("Cannot deposit checks to savings accounts. Account type: SavingsAccount");
96102
}
97103

98104
@Test

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
55

6-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
7-
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
86
import java.util.Set;
97
import java.util.UUID;
8+
109
import org.junit.jupiter.api.BeforeEach;
1110
import org.junit.jupiter.api.Test;
1211

12+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
13+
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
14+
1315
class BankAtmTest {
1416

1517
private BankAtm classUnderTest;
@@ -166,7 +168,7 @@ void testDepositCheckToSavingsAccountThrowsException() {
166168
// Act & Assert
167169
assertThatExceptionOfType(IllegalArgumentException.class)
168170
.isThrownBy(() -> classUnderTest.depositFunds("SAV001", check))
169-
.withMessage("Cannot deposit checks to savings accounts");
171+
.withMessage("Cannot deposit checks to savings accounts. Account type: SavingsAccount");
170172
}
171173

172174
@Test

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BaseAccountTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
55

6-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
76
import java.util.Set;
87
import java.util.UUID;
8+
99
import org.junit.jupiter.api.BeforeEach;
1010
import org.junit.jupiter.api.Test;
1111

12+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
13+
1214
class BaseAccountTest {
1315

1416
private CheckingAccount account; // Using CheckingAccount as concrete implementation

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import static org.junit.jupiter.api.Assertions.assertFalse;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77

8-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
98
import java.util.HashSet;
109
import java.util.Set;
1110
import java.util.UUID;
11+
1212
import org.junit.jupiter.api.BeforeEach;
1313
import org.junit.jupiter.api.Test;
1414

15+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
16+
1517
class CheckingAccountTest {
1618

1719
private CheckingAccount classUnderTest;

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CustomerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.codedifferently.lesson17.bank;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
54

65
import java.util.Set;
76
import java.util.UUID;
7+
88
import org.junit.jupiter.api.BeforeEach;
99
import org.junit.jupiter.api.Test;
1010

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Set;
6+
import java.util.UUID;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
class PolymorphicAccountTest {
12+
13+
private BankAtm bankAtm;
14+
private Customer customer;
15+
private CheckingAccount checkingAccount;
16+
private SavingsAccount savingsAccount;
17+
18+
@BeforeEach
19+
void setUp() {
20+
bankAtm = new BankAtm();
21+
customer = new Customer(UUID.randomUUID(), "John Doe");
22+
23+
checkingAccount = new CheckingAccount("CHK001", Set.of(customer), 1000.0);
24+
savingsAccount = new SavingsAccount("SAV001", Set.of(customer), 500.0);
25+
26+
customer.addAccount(checkingAccount);
27+
customer.addAccount(savingsAccount);
28+
29+
bankAtm.addAccount(checkingAccount);
30+
bankAtm.addAccount(savingsAccount);
31+
}
32+
33+
@Test
34+
void testFindAccountsByCustomerIdReturnsPolymorphicAccounts() {
35+
// Act
36+
Set<Account> accounts = bankAtm.findAccountsByCustomerId(customer.getId());
37+
38+
// Assert - Should return both checking and savings accounts as Account interface
39+
assertThat(accounts).hasSize(2);
40+
assertThat(accounts).containsExactlyInAnyOrder(checkingAccount, savingsAccount);
41+
42+
// Verify they are polymorphic - can be treated as Account interface
43+
for (Account account : accounts) {
44+
assertThat(account.getAccountNumber()).isNotEmpty();
45+
assertThat(account.getBalance()).isGreaterThanOrEqualTo(0);
46+
assertThat(account.getOwners()).contains(customer);
47+
}
48+
}
49+
50+
@Test
51+
void testPolymorphicAccountOperations() {
52+
// Get accounts polymorphically
53+
Set<Account> accounts = bankAtm.findAccountsByCustomerId(customer.getId());
54+
55+
// Find savings account polymorphically
56+
Account savingsAsAccount = accounts.stream()
57+
.filter(acc -> acc.getAccountNumber().equals("SAV001"))
58+
.findFirst()
59+
.orElseThrow();
60+
61+
// Find checking account polymorphically
62+
Account checkingAsAccount = accounts.stream()
63+
.filter(acc -> acc.getAccountNumber().equals("CHK001"))
64+
.findFirst()
65+
.orElseThrow();
66+
67+
// Both can be operated on through Account interface
68+
double initialSavingsBalance = savingsAsAccount.getBalance();
69+
double initialCheckingBalance = checkingAsAccount.getBalance();
70+
71+
savingsAsAccount.deposit(100.0);
72+
checkingAsAccount.deposit(200.0);
73+
74+
assertThat(savingsAsAccount.getBalance()).isEqualTo(initialSavingsBalance + 100.0);
75+
assertThat(checkingAsAccount.getBalance()).isEqualTo(initialCheckingBalance + 200.0);
76+
}
77+
78+
@Test
79+
void testCustomerCanHoldMultipleAccountTypes() {
80+
// Verify customer can hold both account types
81+
Set<Account> customerAccounts = customer.getAccounts();
82+
83+
assertThat(customerAccounts).hasSize(2);
84+
85+
boolean hasChecking = customerAccounts.stream()
86+
.anyMatch(acc -> acc instanceof CheckingAccount);
87+
boolean hasSavings = customerAccounts.stream()
88+
.anyMatch(acc -> acc instanceof SavingsAccount);
89+
90+
assertThat(hasChecking).isTrue();
91+
assertThat(hasSavings).isTrue();
92+
}
93+
}

0 commit comments

Comments
 (0)