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 {
1818 * @param account The account to add.
1919 */
2020 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
2132 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 ));
2836 }
2937
3038 /**
Original file line number Diff line number Diff line change @@ -107,4 +107,31 @@ void testWithdrawFunds_AccountNotFound() {
107107 .isThrownBy (() -> classUnderTest .withdrawFunds (nonExistingAccountNumber , 50.0 ))
108108 .withMessage ("Account not found" );
109109 }
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+ }
110137}
You can’t perform that action at this time.
0 commit comments