Skip to content

Commit e8f7503

Browse files
author
AmiyahJo
committed
feat: adds business checking account boolean to customer, and exception in bankATM
1 parent c5edbfe commit e8f7503

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

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

3-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
43
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.UUID;
87

8+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9+
910
/** Represents a bank ATM. */
1011
public class BankAtm {
1112

@@ -18,6 +19,20 @@ public class BankAtm {
1819
* @param account The account to add.
1920
*/
2021
public void addAccount(CheckingAccount account) {
22+
if (account instanceof BusinessCheckingAccount) {
23+
boolean hasBusinessAccount = false;
24+
for (Customer owner : account.getOwners()) {
25+
Customer customer = customerById.get(owner.getId());
26+
if (customer != null && customer.hasBusinessAccount()) {
27+
hasBusinessAccount = true;
28+
break;
29+
}
30+
}
31+
if (!hasBusinessAccount) {
32+
throw new IllegalArgumentException("At least one owning account must be a business account.");
33+
}
34+
}
35+
2136
accountByNumber.put(account.getAccountNumber(), account);
2237
account
2338
.getOwners()
@@ -27,6 +42,7 @@ public void addAccount(CheckingAccount account) {
2742
});
2843
}
2944

45+
3046
/**
3147
* Finds all accounts owned by a customer.
3248
*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public Set<CheckingAccount> getAccounts() {
5858
return accounts;
5959
}
6060

61+
/**
62+
* Checks if the customer has at least one business account.
63+
*
64+
* @return true if the customer has a business account, false otherwise.
65+
*/
66+
public boolean hasBusinessAccount() {
67+
for (CheckingAccount account : accounts) {
68+
if (account instanceof BusinessCheckingAccount) {
69+
return true; // Found a business account
70+
}
71+
}
72+
return false;
73+
}
74+
6175
@Override
6276
public int hashCode() {
6377
return id.hashCode();

0 commit comments

Comments
 (0)