77
88import com .codedifferently .lesson17 .bank .exceptions .AccountNotFoundException ;
99
10- /** Represents a bank ATM. */
10+
1111public class BankAtm {
1212
1313 private final Map <UUID , AccountOwner > accountOwnerById = new HashMap <>();
1414 private final Map <String , CheckingAccount > accountByNumber = new HashMap <>();
1515 private final Map <String , SavingAccount > savingsAccountByNumber = new HashMap <>();
16- /**
17- * Adds a checking account to the bank. Also supports BusinessCheckingAccount.
18- *
19- * @param account The account to add.
20- */
16+
2117 public void addAccount (CheckingAccount account ) {
2218 accountByNumber .put (account .getAccountNumber (), account );
2319 account
@@ -29,11 +25,7 @@ public void addAccount(CheckingAccount account) {
2925 });
3026 }
3127
32- /**
33- * Adds a savings account to the bank.
34- *
35- * @param account The account to add.
36- */
28+
3729 public void addAccount (SavingAccount account ) {
3830 savingsAccountByNumber .put (account .getAccountNumber (), account );
3931 account
@@ -45,35 +37,23 @@ public void addAccount(SavingAccount account) {
4537 });
4638 }
4739
48- /**
49- * Finds all checking accounts owned by a customer or business.
50- * Updated to support both Customer and Business owners.
51- *
52- * @param customerId The ID of the customer or business.
53- * @return The unique set of checking accounts owned by the customer or business.
54- */
40+
5541 public Set <CheckingAccount > findAccountsByCustomerId (UUID customerId ) {
5642 return accountOwnerById .containsKey (customerId )
5743 ? accountOwnerById .get (customerId ).getAccounts ()
5844 : Set .of ();
5945 }
6046
61- /**
62- * Deposits funds into an account. Now supports both checking and savings accounts.
63- * The method first tries to find a checking account, then a savings account.
64- *
65- * @param accountNumber The account number.
66- * @param amount The amount to deposit.
67- */
47+
6848 public void depositFunds (String accountNumber , double amount ) {
69- // Try checking account first
49+
7050 CheckingAccount checkingAccount = accountByNumber .get (accountNumber );
7151 if (checkingAccount != null ) {
7252 checkingAccount .deposit (amount );
7353 return ;
7454 }
7555
76- // Try savings account
56+
7757 SavingAccount savingsAccount = savingsAccountByNumber .get (accountNumber );
7858 if (savingsAccount != null ) {
7959 savingsAccount .deposit (amount );
@@ -83,34 +63,22 @@ public void depositFunds(String accountNumber, double amount) {
8363 throw new AccountNotFoundException ("Account not found" );
8464 }
8565
86- /**
87- * Deposits funds into a checking account using a check.
88- * Note: Checks can only be deposited into checking accounts, not savings accounts.
89- *
90- * @param accountNumber The checking account number.
91- * @param check The check to deposit.
92- */
66+
9367 public void depositFunds (String accountNumber , Check check ) {
9468 CheckingAccount account = getAccountOrThrow (accountNumber );
9569 check .depositFunds (account );
9670 }
9771
98- /**
99- * Withdraws funds from an account. Now supports both checking and savings accounts.
100- * The method first tries to find a checking account, then a savings account.
101- *
102- * @param accountNumber The account number.
103- * @param amount The amount to withdraw.
104- */
72+
10573 public void withdrawFunds (String accountNumber , double amount ) {
106- // Try checking account first
74+
10775 CheckingAccount checkingAccount = accountByNumber .get (accountNumber );
10876 if (checkingAccount != null ) {
10977 checkingAccount .withdraw (amount );
11078 return ;
11179 }
11280
113- // Try savings account
81+
11482 SavingAccount savingsAccount = savingsAccountByNumber .get (accountNumber );
11583 if (savingsAccount != null ) {
11684 savingsAccount .withdraw (amount );
@@ -120,13 +88,7 @@ public void withdrawFunds(String accountNumber, double amount) {
12088 throw new AccountNotFoundException ("Account not found" );
12189 }
12290
123- /**
124- * Gets a checking account by its number or throws an exception if not found.
125- *
126- * @param accountNumber The account number.
127- * @return The checking account.
128- * @throws AccountNotFoundException If the checking account is not found.
129- */
91+
13092 private CheckingAccount getAccountOrThrow (String accountNumber ) {
13193 CheckingAccount account = accountByNumber .get (accountNumber );
13294 if (account == null ) {
0 commit comments