Skip to content

Commit 898267b

Browse files
author
jjcapparell
committed
feat: added CheckingAccount and BusinessCheckingAccount functions to BankAtm and tests
1 parent 2c5fe9e commit 898267b

File tree

6 files changed

+161
-3
lines changed

6 files changed

+161
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class BankAtm {
1111

1212
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
13+
final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
1414

1515
/**
1616
* Adds a checking account to the bank.
@@ -25,6 +25,14 @@ public void addAccount(CheckingAccount account) {
2525
owner -> {
2626
customerById.put(owner.getId(), owner);
2727
});
28+
// Check if the account is a BusinessCheckingAccount and ensure it has a business owner
29+
if (account instanceof BusinessCheckingAccount) {
30+
BusinessCheckingAccount businessAccount = (BusinessCheckingAccount) account;
31+
if (!businessAccount.hasBusinessOwner(account.getOwners())) {
32+
throw new IllegalArgumentException(
33+
"BusinessCheckingAccount must have at least one business owner.");
34+
}
35+
}
2836
}
2937

3038
/**
@@ -58,6 +66,9 @@ public void depositFunds(String accountNumber, double amount) {
5866
*/
5967
public void depositFunds(String accountNumber, Check check) {
6068
CheckingAccount account = getAccountOrThrow(accountNumber);
69+
if (account instanceof SavingsAccount) {
70+
throw new UnsupportedOperationException("Cannot deposit checks into a Savings Account.");
71+
}
6172
check.depositFunds(account);
6273
}
6374

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public final class BusinessCheckingAccount extends CheckingAccount {
6+
public BusinessCheckingAccount(
7+
String accountNumber, double initialBalance, Set<Customer> owners) {
8+
// Pass the accountNumber, initialBalance, and owners set to the CheckingAccount constructor
9+
super(accountNumber, owners, initialBalance);
10+
if (!hasBusinessOwner(owners)) {
11+
throw new IllegalArgumentException(
12+
"A BusinessCheckingAccount must have at least one business owner.");
13+
}
14+
}
15+
16+
/**
17+
* @param check The check to deposit.
18+
* @throws UnsupportedOperationException If a check is attempted to be deposited.
19+
*/
20+
boolean hasBusinessOwner(Set<Customer> owners) {
21+
return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer);
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.UUID;
4+
5+
public class BusinessCustomer extends Customer {
6+
7+
public BusinessCustomer(UUID id, String name) {
8+
super(id, name);
9+
}
10+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/** Represents a checking account. */
77
public class CheckingAccount {
88

9-
private final Set<Customer> owners;
9+
private static Set<Customer> owners;
1010
private final String accountNumber;
1111
private double balance;
1212
private boolean isActive;
@@ -20,7 +20,7 @@ public class CheckingAccount {
2020
*/
2121
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
2222
this.accountNumber = accountNumber;
23-
this.owners = owners;
23+
CheckingAccount.owners = owners;
2424
this.balance = initialBalance;
2525
isActive = true;
2626
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public class SavingsAccount extends CheckingAccount {
6+
7+
public SavingsAccount(String accountNumber, double initialBalance, Set<Customer> owners) {
8+
// Pass the accountNumber, initialBalance, and owners set to the CheckingAccount constructor
9+
super(accountNumber, owners, initialBalance);
10+
}
11+
12+
/**
13+
* @param check The check to deposit.
14+
* @throws UnsupportedOperationException If a check is attempted to be deposited.
15+
*/
16+
public void depositFunds(Check check) {
17+
throw new UnsupportedOperationException("Cannot deposit checks into a Savings Account.");
18+
}
19+
}

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
58

69
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
710
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
11+
import java.util.HashSet;
812
import java.util.Set;
913
import java.util.UUID;
1014
import org.junit.jupiter.api.BeforeEach;
@@ -107,4 +111,95 @@ void testWithdrawFunds_AccountNotFound() {
107111
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
108112
.withMessage("Account not found");
109113
}
114+
115+
@Test
116+
public void testDepositFundsIntoSavingsAccount() {
117+
BankAtm atm = new BankAtm();
118+
SavingsAccount savingsAccount = new SavingsAccount("12345", 1000.00, Set.of(customer1));
119+
atm.addAccount(savingsAccount);
120+
121+
// Valid deposit
122+
atm.depositFunds("12345", 500.00);
123+
assertEquals(1500.00, savingsAccount.getBalance(), 0.01);
124+
}
125+
126+
@Test
127+
public void testDepositCheckIntoSavingsAccount() {
128+
// Arrange
129+
BankAtm atm = new BankAtm();
130+
Set<Customer> owners = new HashSet<>();
131+
owners.add(new Customer(UUID.randomUUID(), "Customer1"));
132+
SavingsAccount savingsAccount = new SavingsAccount("12345", 1000.00, Set.of(customer1));
133+
134+
atm.addAccount(savingsAccount); // Add savings account to ATM
135+
136+
// Act & Assert: Check that the exception is thrown when attempting to deposit a check into a
137+
// savings account
138+
Check check = new Check("234", 100.00, savingsAccount); // Creating a check to deposit
139+
140+
// Assert that an UnsupportedOperationException is thrown
141+
UnsupportedOperationException exception =
142+
assertThrows(
143+
UnsupportedOperationException.class,
144+
() -> {
145+
atm.depositFunds("12345", check); // Attempt to deposit check
146+
});
147+
148+
// Optionally, you can also assert the message of the exception if you want more specific
149+
// validation
150+
assertEquals("Cannot deposit checks into a Savings Account.", exception.getMessage());
151+
}
152+
153+
@Test
154+
public void testAddBusinessCheckingAccountWithBusinessOwner() {
155+
// Arrange
156+
BankAtm atm = new BankAtm();
157+
158+
// Create business and personal customers
159+
BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1");
160+
Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1");
161+
162+
// Create a set of owners, including a business customer
163+
Set<Customer> owners = new HashSet<>();
164+
owners.add(businessCustomer);
165+
owners.add(personalCustomer);
166+
167+
// Create a BusinessCheckingAccount with the above owners
168+
BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("67890", 1000.00, owners);
169+
170+
// Act: Add the BusinessCheckingAccount to the ATM
171+
atm.addAccount(businessAccount);
172+
173+
// Assert: Ensure no exceptions were thrown and the account was added
174+
assertNotNull(atm.accountByNumber.get("67890"));
175+
}
176+
177+
@Test
178+
public void testAddBusinessCheckingAccountWithoutBusinessOwner() {
179+
// Arrange
180+
BankAtm atm = new BankAtm();
181+
182+
// Create only personal customers (no business owner)
183+
Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1");
184+
Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2");
185+
186+
// Create a set of owners with only personal customers
187+
Set<Customer> owners = new HashSet<>();
188+
owners.add(personalCustomer1);
189+
owners.add(personalCustomer2);
190+
191+
// Act & Assert: Try to add a BusinessCheckingAccount and expect an exception
192+
IllegalArgumentException exception =
193+
assertThrows(
194+
IllegalArgumentException.class,
195+
() -> {
196+
BusinessCheckingAccount businessAccount =
197+
new BusinessCheckingAccount("67890", 1000.00, owners);
198+
atm.addAccount(businessAccount);
199+
});
200+
201+
// Assert: Ensure the correct exception is thrown
202+
assertEquals(
203+
"A BusinessCheckingAccount must have at least one business owner.", exception.getMessage());
204+
}
110205
}

0 commit comments

Comments
 (0)