|
8 | 8 | import java.util.Set;
|
9 | 9 | import java.util.UUID;
|
10 | 10 |
|
11 |
| -/** Represents a bank ATM. */ |
| 11 | +/** |
| 12 | + * Represents a bank ATM that manages customer accounts and transactions. This class handles account |
| 13 | + * management, deposits, withdrawals, and logging actions. |
| 14 | + */ |
12 | 15 | public class BankAtm {
|
13 | 16 |
|
| 17 | + // Map to store customers by their unique ID |
14 | 18 | private final Map<UUID, Customer> customerById = new HashMap<>();
|
| 19 | + |
| 20 | + // Map to store checking accounts by their account number |
15 | 21 | private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
|
| 22 | + |
| 23 | + // Audit log to record account-related actions |
16 | 24 | private final AuditLog auditLog = new AuditLog();
|
17 | 25 |
|
18 | 26 | /**
|
19 |
| - * Adds a checking account to the bank. |
| 27 | + * Adds a checking account to the bank's records. |
20 | 28 | *
|
21 |
| - * @param account The account to add. |
| 29 | + * @param account The CheckingAccount to add. |
22 | 30 | */
|
23 | 31 | public void addAccount(CheckingAccount account) {
|
| 32 | + // Add the account to the account map using its account number |
24 | 33 | accountByNumber.put(account.getAccountNumber(), account);
|
| 34 | + |
| 35 | + // Register each owner of the account in the customer map |
25 | 36 | account
|
26 | 37 | .getOwners()
|
27 | 38 | .forEach(
|
28 | 39 | owner -> {
|
29 | 40 | customerById.put(owner.getId(), owner);
|
30 | 41 | });
|
| 42 | + |
| 43 | + // Log the account addition in the audit log |
31 | 44 | auditLog.logAccountAddition(account.getAccountNumber());
|
32 | 45 | }
|
33 | 46 |
|
34 | 47 | /**
|
35 |
| - * Finds all accounts owned by a customer. |
| 48 | + * Finds all accounts owned by a specific customer. |
36 | 49 | *
|
37 |
| - * @param customerId The ID of the customer. |
38 |
| - * @return The unique set of accounts owned by the customer. |
| 50 | + * @param customerId The ID of the customer whose accounts are to be found. |
| 51 | + * @return A unique set of CheckingAccounts owned by the customer, or an empty set if none are |
| 52 | + * found. |
39 | 53 | */
|
40 | 54 | public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
|
41 | 55 | return customerById.containsKey(customerId)
|
42 | 56 | ? customerById.get(customerId).getAccounts()
|
43 |
| - : Set.of(); |
| 57 | + : Set.of(); // Return an empty set if the customer does not exist |
44 | 58 | }
|
45 | 59 |
|
46 | 60 | /**
|
47 |
| - * Deposits funds into an account. |
| 61 | + * Deposits a specified amount of funds into an account. |
48 | 62 | *
|
49 |
| - * @param accountNumber The account number. |
50 |
| - * @param amount The amount to deposit. |
| 63 | + * @param accountNumber The account number to deposit into. |
| 64 | + * @param amount The amount of money to deposit. |
51 | 65 | */
|
52 | 66 | public void depositFunds(String accountNumber, double amount) {
|
| 67 | + // Retrieve the account or throw an exception if not found |
53 | 68 | CheckingAccount account = getAccountOrThrow(accountNumber);
|
54 |
| - account.deposit(amount); |
| 69 | + account.deposit(amount); // Deposit the amount into the account |
55 | 70 | }
|
56 | 71 |
|
57 | 72 | /**
|
58 | 73 | * Deposits funds into an account using a check.
|
59 | 74 | *
|
60 |
| - * @param accountNumber The account number. |
61 |
| - * @param check The check to deposit. |
| 75 | + * @param accountNumber The account number to deposit into. |
| 76 | + * @param check The Check object representing the check to be deposited. |
62 | 77 | */
|
63 | 78 | public void depositFunds(String accountNumber, Check check) {
|
| 79 | + // Retrieve the account or throw an exception if not found |
64 | 80 | CheckingAccount account = getAccountOrThrow(accountNumber);
|
65 |
| - check.depositFunds(account); |
| 81 | + check.depositFunds(account); // Process the check and deposit funds into the account |
66 | 82 | }
|
67 | 83 |
|
| 84 | + /** |
| 85 | + * Deposits funds into an account while converting from a specified currency. |
| 86 | + * |
| 87 | + * @param accountNumber The account number to deposit into. |
| 88 | + * @param amount The amount to deposit. |
| 89 | + * @param currency The currency of the deposit amount. |
| 90 | + */ |
68 | 91 | public void depositFunds(String accountNumber, double amount, Currency currency) {
|
| 92 | + // Retrieve the account or throw an exception if not found |
69 | 93 | CheckingAccount account = getAccountOrThrow(accountNumber);
|
70 | 94 |
|
71 |
| - // Validate the currency |
| 95 | + // Validate the provided currency |
72 | 96 | if (currency == null || !isValidCurrency(currency)) {
|
73 | 97 | throw new IllegalArgumentException("Invalid currency provided");
|
74 | 98 | }
|
75 | 99 |
|
76 |
| - // Convert the amount to the account's currency |
| 100 | + // Convert the amount to the account's currency (assuming USD for this example) |
77 | 101 | Currency accountCurrency =
|
78 |
| - Currency.getInstance("USD"); // Replace with account's actual currency |
| 102 | + Currency.getInstance("USD"); // Replace with actual account currency if necessary |
79 | 103 | double convertedAmount = CurrencyConverter.convert(amount, currency, accountCurrency);
|
80 | 104 |
|
81 |
| - account.deposit(convertedAmount); |
| 105 | + account.deposit(convertedAmount); // Deposit the converted amount into the account |
82 | 106 | }
|
83 | 107 |
|
| 108 | + /** |
| 109 | + * Checks if the provided currency is valid. |
| 110 | + * |
| 111 | + * @param currency The currency to validate. |
| 112 | + * @return true if the currency is valid; false otherwise. |
| 113 | + */ |
84 | 114 | private boolean isValidCurrency(Currency currency) {
|
85 |
| - // You can add additional logic here to define valid currencies if needed |
| 115 | + // Define valid currencies |
86 | 116 | return currency.getCurrencyCode().equals("USD")
|
87 | 117 | || currency.getCurrencyCode().equals("GBP")
|
88 |
| - || currency.getCurrencyCode().equals("CAD"); |
| 118 | + || currency.getCurrencyCode().equals("CAD") |
| 119 | + || currency.getCurrencyCode().equals("EUR"); |
89 | 120 | }
|
90 | 121 |
|
| 122 | + /** |
| 123 | + * Retrieves all entries from the audit log. |
| 124 | + * |
| 125 | + * @return A list of log entries recorded in the audit log. |
| 126 | + */ |
91 | 127 | public List<String> getAuditLogEntries() {
|
92 |
| - return auditLog.getLogEntries(); |
| 128 | + return auditLog.getLogEntries(); // Return the audit log entries |
93 | 129 | }
|
94 | 130 |
|
95 | 131 | /**
|
96 |
| - * Withdraws funds from an account. |
| 132 | + * Withdraws a specified amount of funds from an account. |
97 | 133 | *
|
98 |
| - * @param accountNumber |
99 |
| - * @param amount |
| 134 | + * @param accountNumber The account number to withdraw from. |
| 135 | + * @param amount The amount of money to withdraw. |
100 | 136 | */
|
101 | 137 | public void withdrawFunds(String accountNumber, double amount) {
|
| 138 | + // Retrieve the account or throw an exception if not found |
102 | 139 | CheckingAccount account = getAccountOrThrow(accountNumber);
|
103 |
| - account.withdraw(amount); |
| 140 | + account.withdraw(amount); // Withdraw the specified amount from the account |
104 | 141 | }
|
105 | 142 |
|
106 | 143 | /**
|
107 |
| - * Gets an account by its number or throws an exception if not found. |
| 144 | + * Retrieves an account by its account number, throwing an exception if not found. |
108 | 145 | *
|
109 |
| - * @param accountNumber The account number. |
110 |
| - * @return The account. |
| 146 | + * @param accountNumber The account number to search for. |
| 147 | + * @return The CheckingAccount associated with the given account number. |
| 148 | + * @throws AccountNotFoundException if the account is not found or is closed. |
111 | 149 | */
|
112 | 150 | private CheckingAccount getAccountOrThrow(String accountNumber) {
|
113 | 151 | CheckingAccount account = accountByNumber.get(accountNumber);
|
| 152 | + // Check if the account is found and is not closed |
114 | 153 | if (account == null || account.isClosed()) {
|
115 | 154 | throw new AccountNotFoundException("Account not found");
|
116 | 155 | }
|
117 |
| - return account; |
| 156 | + return account; // Return the found account |
118 | 157 | }
|
119 | 158 | }
|
0 commit comments