Skip to content

Commit 22fd2a9

Browse files
committed
feat: implemented savings account class and modified test and related files
1 parent 57e1ded commit 22fd2a9

File tree

8 files changed

+118
-43
lines changed

8 files changed

+118
-43
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public void depositFunds(String accountNumber, double amount) {
5858
*/
5959
public void depositFunds(String accountNumber, Check check) {
6060
CheckingAccount account = getAccountOrThrow(accountNumber);
61+
if ((account instanceof SavingsAccount)) {
62+
throw new IllegalArgumentException("Only checking accounts can make checks");
63+
}
6164
check.depositFunds(account);
6265
}
6366

@@ -69,6 +72,12 @@ public void depositFunds(String accountNumber, Check check) {
6972
*/
7073
public void withdrawFunds(String accountNumber, double amount) {
7174
CheckingAccount account = getAccountOrThrow(accountNumber);
75+
if ((account instanceof BusinessCheckingAccount)) {
76+
if (account.getBalance() - amount < 10000) {
77+
throw new IllegalArgumentException(
78+
"Business checking account balance must remain greater than $10,000");
79+
}
80+
}
7281
account.withdraw(amount);
7382
}
7483

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
import java.util.Set;
44

55
public class BusinessCheckingAccount extends CheckingAccount {
6-
6+
77
/**
8-
* Creates a new BusinessCheckingAccount with the specified account number, owners, and initial balance.
9-
*
10-
* This constructor ensures that the account meets the minimum requirements for a business checking account,
11-
* including a minimum initial balance of $10,000 and at least one business owner in the provided set of owners.
12-
* If these requirements are not met, an {@link IllegalArgumentException} is thrown.
13-
*
14-
* @param accountNumber The unique identifier for the account.
15-
* @param owners A set of {@link Customer} objects representing the owners of the account.
16-
* @param initialBalance The initial balance of the account, which must be at least $10,000.
17-
* @throws IllegalArgumentException If the initial balance is less than $10,000 or if none of the owners
18-
* meet the criteria to be considered a business owner.
19-
*/
8+
* Creates a new BusinessCheckingAccount with the specified account number, owners, and initial
9+
* balance.
10+
*
11+
* <p>This constructor ensures that the account meets the minimum requirements for a business
12+
* checking account, including a minimum initial balance of $10,000 and at least one business
13+
* owner in the provided set of owners. If these requirements are not met, an {@link
14+
* IllegalArgumentException} is thrown.
15+
*
16+
* @param accountNumber The unique identifier for the account.
17+
* @param owners A set of {@link Customer} objects representing the owners of the account.
18+
* @param initialBalance The initial balance of the account, which must be at least $10,000.
19+
* @throws IllegalArgumentException If the initial balance is less than $10,000 or if none of the
20+
* owners meet the criteria to be considered a business owner.
21+
*/
2022
public BusinessCheckingAccount(
2123
String accountNumber, Set<Customer> owners, double initialBalance) {
2224
super(accountNumber, owners, initialBalance);
@@ -32,15 +34,15 @@ public BusinessCheckingAccount(
3234
}
3335

3436
/**
35-
* Checks if at least one owner of the account is a business.
36-
*
37-
* <p>This method examines each owner in the "owners" set and returns true if any owner's name
38-
* contains "INC" or "LLC" (case-insensitive), which are used as indicators of a business owning
39-
* the account.
40-
*
41-
* @param owners The set of account owners to check.
42-
* @return true if the account has at least one business owner; false otherwise.
43-
*/
37+
* Checks if at least one owner of the account is a business.
38+
*
39+
* <p>This method examines each owner in the "owners" set and returns true if any owner's name
40+
* contains "INC" or "LLC" (case-insensitive), which are used as indicators of a business owning
41+
* the account.
42+
*
43+
* @param owners The set of account owners to check.
44+
* @return true if the account has at least one business owner; false otherwise.
45+
*/
4446
public boolean hasBusinessOwner(Set<Customer> owners) {
4547
return getOwners().stream()
4648
.anyMatch(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ public class Check {
1616
* @param checkNumber The check number.
1717
* @param amount The amount of the check.
1818
* @param account The account the check is drawn on.
19+
* @throws IllegalArgumentException if the amount is negative or the account is not a
20+
* `CheckingAccount` instance.
1921
*/
2022
public Check(String checkNumber, double amount, CheckingAccount account) {
23+
if ((account instanceof SavingsAccount)) {
24+
throw new IllegalArgumentException("Only checking accounts can make checks");
25+
}
2126
if (amount < 0) {
2227
throw new IllegalArgumentException("Check amount must be positive");
2328
}
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.Set;
4+
5+
public class SavingsAccount extends CheckingAccount {
6+
7+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
8+
super(accountNumber, owners, initialBalance);
9+
}
10+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ class BankAtmTest {
1515
private BankAtm classUnderTest;
1616
private CheckingAccount account1;
1717
private CheckingAccount account2;
18+
private BusinessCheckingAccount account3;
1819
private Customer customer1;
1920
private Customer customer2;
21+
private Customer customer3;
2022

2123
@BeforeEach
2224
void setUp() {
2325
classUnderTest = new BankAtm();
2426
customer1 = new Customer(UUID.randomUUID(), "John Doe");
2527
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
28+
customer3 = new Customer(UUID.randomUUID(), "Doodle-Bob INC.");
2629
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
2730
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
31+
account3 = new BusinessCheckingAccount("102938475", Set.of(customer3), 10000.0);
2832
customer1.addAccount(account1);
2933
customer1.addAccount(account2);
3034
customer2.addAccount(account2);
3135
classUnderTest.addAccount(account1);
3236
classUnderTest.addAccount(account2);
37+
classUnderTest.addAccount(account3);
3338
}
3439

3540
@Test
@@ -107,4 +112,11 @@ void testWithdrawFunds_AccountNotFound() {
107112
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
108113
.withMessage("Account not found");
109114
}
115+
116+
@Test
117+
void testWithdrawFunds_BusinessAccountBalanceBelowMinimum() {
118+
assertThatExceptionOfType(IllegalArgumentException.class)
119+
.isThrownBy(() -> classUnderTest.withdrawFunds(account3.getAccountNumber(), 200.0))
120+
.withMessage("Business checking account balance must remain greater than $10,000");
121+
}
110122
}

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

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

3-
import java.util.HashSet;
4-
import java.util.Set;
5-
import java.util.UUID;
6-
73
import static org.assertj.core.api.Assertions.assertThatThrownBy;
84
import static org.junit.jupiter.api.Assertions.assertEquals;
95
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
import java.util.UUID;
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.Test;
1212

@@ -45,30 +45,35 @@ public void testBusinessCheckingAccount_HasBusinessOwner_WithLlcInName() {
4545
}
4646

4747
@Test
48-
public void testBusinessCheckingAccount_WithoutBusinessOwner() {
48+
public void testBusinessCheckingAccount_WithoutBusinessOwner() {
4949
UUID individualId = UUID.randomUUID();
50-
Customer individualOwner = new Customer(individualId, "John Doe"); // Example customer without business designation
50+
Customer individualOwner =
51+
new Customer(individualId, "John Doe"); // Example customer without business designation
5152

5253
owners.add(individualOwner); // Add the individual owner to the set
5354

54-
assertThatThrownBy(() -> {
55-
new BusinessCheckingAccount("12345", owners, 15000);
56-
})
57-
.isInstanceOf(IllegalArgumentException.class)
58-
.hasMessageContaining("A business checking account must have at least one business owner.");
59-
}
55+
assertThatThrownBy(
56+
() -> {
57+
new BusinessCheckingAccount("12345", owners, 15000);
58+
})
59+
.isInstanceOf(IllegalArgumentException.class)
60+
.hasMessageContaining("A business checking account must have at least one business owner.");
61+
}
6062

61-
@Test
62-
public void testBusinessCheckingAccount_BelowMinimumBalance() {
63+
@Test
64+
public void testBusinessCheckingAccount_BelowMinimumBalance() {
6365
UUID businessId = UUID.randomUUID();
64-
Customer businessOwner = new Customer(businessId, "ABC LLC"); // Create a customer with a valid business name
66+
Customer businessOwner =
67+
new Customer(businessId, "ABC LLC"); // Create a customer with a valid business name
6568

6669
owners.add(businessOwner); // Add the business owner to the set
6770

68-
assertThatThrownBy(() -> {
69-
new BusinessCheckingAccount("12345", owners, 5000); // Attempt to create with a balance below 10,000
70-
})
71-
.isInstanceOf(IllegalArgumentException.class)
72-
.hasMessageContaining("Business accounts require a minimum balance of $10,000.");
73-
}
71+
assertThatThrownBy(
72+
() -> {
73+
new BusinessCheckingAccount(
74+
"12345", owners, 5000); // Attempt to create with a balance below 10,000
75+
})
76+
.isInstanceOf(IllegalArgumentException.class)
77+
.hasMessageContaining("Business accounts require a minimum balance of $10,000.");
78+
}
7479
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.assertThrows;
57

68
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
79
import org.junit.jupiter.api.BeforeEach;
@@ -49,6 +51,18 @@ void testConstructor_CantCreateCheckWithNegativeAmount() {
4951
.withMessage("Check amount must be positive");
5052
}
5153

54+
@Test
55+
void testConstructor_CantCreateCheckWithSavingsAccount() {
56+
SavingsAccount savingsAccount = new SavingsAccount("12345", null, 500.0);
57+
IllegalArgumentException exception =
58+
assertThrows(
59+
IllegalArgumentException.class,
60+
() -> {
61+
new Check("123", 100, savingsAccount);
62+
});
63+
assertEquals("Only checking accounts can make checks", exception.getMessage());
64+
}
65+
5266
@Test
5367
void testHashCode() {
5468
// Arrange
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.UUID;
6+
import org.junit.jupiter.api.BeforeEach;
7+
8+
public class SavingsAccountTest {
9+
private SavingsAccount savingsAccount;
10+
private Set<Customer> owners;
11+
12+
@BeforeEach
13+
void setUp() {
14+
owners = new HashSet<>();
15+
owners.add(new Customer(UUID.randomUUID(), "Bob Smith"));
16+
savingsAccount = new SavingsAccount("12345", owners, 500);
17+
}
18+
}

0 commit comments

Comments
 (0)