File tree Expand file tree Collapse file tree 2 files changed +41
-6
lines changed
lesson_17/bank/bank_app/src
main/java/com/codedifferently/lesson17/bank
test/java/com/codedifferently/lesson17/bank Expand file tree Collapse file tree 2 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -18,13 +18,21 @@ public class BankAtm {
18
18
* @param account The account to add.
19
19
*/
20
20
public void addAccount (CheckingAccount account ) {
21
+ if (account instanceof BusinessCheckingAccount ) {
22
+ Set <Customer > owners = account .getOwners ();
23
+
24
+ // Ensure at least one owner is a business
25
+ if (owners .stream ().noneMatch (Customer ::isBusiness )) {
26
+ throw new IllegalArgumentException (
27
+ "A BusinessCheckingAccount must have at least one business owner." );
28
+ }
29
+ }
30
+
31
+ // Add the account to accountByNumber
21
32
accountByNumber .put (account .getAccountNumber (), account );
22
- account
23
- .getOwners ()
24
- .forEach (
25
- owner -> {
26
- customerById .put (owner .getId (), owner );
27
- });
33
+
34
+ // Add each owner to customerById
35
+ account .getOwners ().forEach (owner -> customerById .put (owner .getId (), owner ));
28
36
}
29
37
30
38
/**
Original file line number Diff line number Diff line change @@ -107,4 +107,31 @@ void testWithdrawFunds_AccountNotFound() {
107
107
.isThrownBy (() -> classUnderTest .withdrawFunds (nonExistingAccountNumber , 50.0 ))
108
108
.withMessage ("Account not found" );
109
109
}
110
+
111
+ @ Test
112
+ void testAddBusinessCheckingAccount_WithBusinessOwner () {
113
+ // Arrange
114
+ Customer businessCustomer = new Customer (UUID .randomUUID (), "TechCorp" , CustomerType .BUSINESS );
115
+ Set <Customer > owners = Set .of (businessCustomer );
116
+
117
+ // Act
118
+ BusinessCheckingAccount businessAccount =
119
+ new BusinessCheckingAccount ("BUS123456" , owners , 500.0 );
120
+
121
+ // Assert
122
+ assertThat (businessAccount .getOwners ()).contains (businessCustomer );
123
+ }
124
+
125
+ @ Test
126
+ void testAddBusinessCheckingAccount_WithoutBusinessOwner () {
127
+ // Arrange
128
+ Customer individualCustomer =
129
+ new Customer (UUID .randomUUID (), "John Doe" , CustomerType .INDIVIDUAL );
130
+ Set <Customer > owners = Set .of (individualCustomer );
131
+
132
+ // Act & Assert
133
+ assertThatExceptionOfType (IllegalArgumentException .class )
134
+ .isThrownBy (() -> new BusinessCheckingAccount ("BUS987654" , owners , 1000.0 ))
135
+ .withMessage ("A BusinessCheckingAccount must have at least one business owner." );
136
+ }
110
137
}
You can’t perform that action at this time.
0 commit comments