Skip to content

Commit 1f0276a

Browse files
committed
implemented Saving account and BusinessCheckingAccount functions and tests
1 parent 6b7e62a commit 1f0276a

File tree

9 files changed

+47
-11
lines changed

9 files changed

+47
-11
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
@@ -18,6 +18,15 @@ public class BankAtm {
1818
* @param account The account to add.
1919
*/
2020
public void addAccount(CheckingAccount account) {
21+
if (account instanceof BusinessCheckingAccount) {
22+
// Check if at least one of the owners is a business customer
23+
boolean hasBusinessOwner = account.getOwners().stream().anyMatch(Customer::isBusiness);
24+
25+
if (!hasBusinessOwner) {
26+
throw new IllegalArgumentException(
27+
"At least one owner must be a business customer for a BusinessCheckingAccount.");
28+
}
29+
}
2130
accountByNumber.put(account.getAccountNumber(), account);
2231
account
2332
.getOwners()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
public interface BusinessAccount {
4+
boolean isBusiness();
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public class BusinessCheckingAccount extends CheckingAccount implements BusinessAccount {
6+
7+
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double balance) {
8+
super(accountNumber, owners, balance);
9+
}
10+
11+
@Override
12+
public boolean isBusiness() {
13+
return true;
14+
}
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class CheckingAccount {
88

99
private final Set<Customer> owners;
10-
private final String accountNumber;
10+
final String accountNumber;
1111
private double balance;
1212
private boolean isActive;
1313

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Customer {
99

1010
private final UUID id;
1111
private final String name;
12+
private final boolean isBusiness;
1213
private final Set<CheckingAccount> accounts = new HashSet<>();
1314

1415
/**
@@ -17,9 +18,10 @@ public class Customer {
1718
* @param id The ID of the customer.
1819
* @param name The name of the customer.
1920
*/
20-
public Customer(UUID id, String name) {
21+
public Customer(UUID id, String name, boolean isBusiness) {
2122
this.id = id;
2223
this.name = name;
24+
this.isBusiness = isBusiness;
2325
}
2426

2527
/**
@@ -31,6 +33,10 @@ public UUID getId() {
3133
return id;
3234
}
3335

36+
public boolean isBusiness() {
37+
return isBusiness;
38+
}
39+
3440
/**
3541
* Gets the name of the customer.
3642
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class SavingAccount {
88

99
private final Set<Customer> owners;
10-
private final String accountNumber;
10+
final String accountNumber;
1111
private double balance;
1212
private boolean isActive;
1313

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class BankAtmTest {
2121
@BeforeEach
2222
void setUp() {
2323
classUnderTest = new BankAtm();
24-
customer1 = new Customer(UUID.randomUUID(), "John Doe");
25-
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
24+
customer1 = new Customer(UUID.randomUUID(), "John Doe", false);
25+
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false);
2626
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
2727
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
2828
customer1.addAccount(account1);
@@ -35,7 +35,7 @@ void setUp() {
3535
@Test
3636
void testAddAccount() {
3737
// Arrange
38-
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
38+
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false);
3939
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
4040
customer3.addAccount(account3);
4141

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class CheckingAccountTest {
2020
@BeforeEach
2121
void setUp() {
2222
owners = new HashSet<>();
23-
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
24-
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
23+
owners.add(new Customer(UUID.randomUUID(), "John Doe", false));
24+
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
2525
classUnderTest = new CheckingAccount("123456789", owners, 100.0);
2626
}
2727

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
import org.junit.jupiter.api.BeforeEach;
1313
import org.junit.jupiter.api.Test;
1414

15-
public class SavingAccountTest {
15+
class SavingAccountTest {
16+
1617
private SavingAccount classUnderTest;
1718
private Set<Customer> owners;
1819

1920
@BeforeEach
2021
void setUp() {
2122
owners = new HashSet<>();
22-
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
23-
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
23+
owners.add(new Customer(UUID.randomUUID(), "John Doe", false));
24+
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
2425
classUnderTest = new SavingAccount("123456789", owners, 100.0);
2526
}
2627

0 commit comments

Comments
 (0)