Skip to content

Commit 67e4843

Browse files
committed
chore: added documentation
1 parent 3abf8cd commit 67e4843

File tree

12 files changed

+280
-77
lines changed

12 files changed

+280
-77
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,45 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
* Records audit logs for account-related actions. This class maintains a list of log entries for
8+
* tracking account additions.
9+
*/
610
public class AuditLog {
711

12+
// List to hold log entries
813
private final List<String> logEntries = new ArrayList<>();
914

15+
/**
16+
* Logs the addition of a new account by recording its account number.
17+
*
18+
* @param accountNumber The account number of the newly added account.
19+
*/
1020
public void logAccountAddition(String accountNumber) {
1121
logEntries.add("Account added: " + accountNumber);
1222
}
1323

24+
/**
25+
* Retrieves all log entries recorded in the audit log.
26+
*
27+
* @return A new list containing all log entries.
28+
*/
1429
public List<String> getLogEntries() {
30+
// Return a copy of the log entries to prevent external modification
1531
return new ArrayList<>(logEntries);
1632
}
1733

34+
/**
35+
* Retrieves the last log entry recorded in the audit log.
36+
*
37+
* @return The most recent log entry as a String, or null if there are no entries.
38+
*/
1839
public String getLastEntry() {
40+
// Check if the log is empty and return null if so
1941
if (logEntries.isEmpty()) {
2042
return null; // Or throw an exception if preferred
2143
}
44+
// Return the last log entry
2245
return logEntries.get(logEntries.size() - 1);
2346
}
2447
}

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

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,112 +8,151 @@
88
import java.util.Set;
99
import java.util.UUID;
1010

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+
*/
1215
public class BankAtm {
1316

17+
// Map to store customers by their unique ID
1418
private final Map<UUID, Customer> customerById = new HashMap<>();
19+
20+
// Map to store checking accounts by their account number
1521
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
22+
23+
// Audit log to record account-related actions
1624
private final AuditLog auditLog = new AuditLog();
1725

1826
/**
19-
* Adds a checking account to the bank.
27+
* Adds a checking account to the bank's records.
2028
*
21-
* @param account The account to add.
29+
* @param account The CheckingAccount to add.
2230
*/
2331
public void addAccount(CheckingAccount account) {
32+
// Add the account to the account map using its account number
2433
accountByNumber.put(account.getAccountNumber(), account);
34+
35+
// Register each owner of the account in the customer map
2536
account
2637
.getOwners()
2738
.forEach(
2839
owner -> {
2940
customerById.put(owner.getId(), owner);
3041
});
42+
43+
// Log the account addition in the audit log
3144
auditLog.logAccountAddition(account.getAccountNumber());
3245
}
3346

3447
/**
35-
* Finds all accounts owned by a customer.
48+
* Finds all accounts owned by a specific customer.
3649
*
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.
3953
*/
4054
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4155
return customerById.containsKey(customerId)
4256
? customerById.get(customerId).getAccounts()
43-
: Set.of();
57+
: Set.of(); // Return an empty set if the customer does not exist
4458
}
4559

4660
/**
47-
* Deposits funds into an account.
61+
* Deposits a specified amount of funds into an account.
4862
*
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.
5165
*/
5266
public void depositFunds(String accountNumber, double amount) {
67+
// Retrieve the account or throw an exception if not found
5368
CheckingAccount account = getAccountOrThrow(accountNumber);
54-
account.deposit(amount);
69+
account.deposit(amount); // Deposit the amount into the account
5570
}
5671

5772
/**
5873
* Deposits funds into an account using a check.
5974
*
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.
6277
*/
6378
public void depositFunds(String accountNumber, Check check) {
79+
// Retrieve the account or throw an exception if not found
6480
CheckingAccount account = getAccountOrThrow(accountNumber);
65-
check.depositFunds(account);
81+
check.depositFunds(account); // Process the check and deposit funds into the account
6682
}
6783

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+
*/
6891
public void depositFunds(String accountNumber, double amount, Currency currency) {
92+
// Retrieve the account or throw an exception if not found
6993
CheckingAccount account = getAccountOrThrow(accountNumber);
7094

71-
// Validate the currency
95+
// Validate the provided currency
7296
if (currency == null || !isValidCurrency(currency)) {
7397
throw new IllegalArgumentException("Invalid currency provided");
7498
}
7599

76-
// Convert the amount to the account's currency
100+
// Convert the amount to the account's currency (assuming USD for this example)
77101
Currency accountCurrency =
78-
Currency.getInstance("USD"); // Replace with account's actual currency
102+
Currency.getInstance("USD"); // Replace with actual account currency if necessary
79103
double convertedAmount = CurrencyConverter.convert(amount, currency, accountCurrency);
80104

81-
account.deposit(convertedAmount);
105+
account.deposit(convertedAmount); // Deposit the converted amount into the account
82106
}
83107

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+
*/
84114
private boolean isValidCurrency(Currency currency) {
85-
// You can add additional logic here to define valid currencies if needed
115+
// Define valid currencies
86116
return currency.getCurrencyCode().equals("USD")
87117
|| currency.getCurrencyCode().equals("GBP")
88-
|| currency.getCurrencyCode().equals("CAD");
118+
|| currency.getCurrencyCode().equals("CAD")
119+
|| currency.getCurrencyCode().equals("EUR");
89120
}
90121

122+
/**
123+
* Retrieves all entries from the audit log.
124+
*
125+
* @return A list of log entries recorded in the audit log.
126+
*/
91127
public List<String> getAuditLogEntries() {
92-
return auditLog.getLogEntries();
128+
return auditLog.getLogEntries(); // Return the audit log entries
93129
}
94130

95131
/**
96-
* Withdraws funds from an account.
132+
* Withdraws a specified amount of funds from an account.
97133
*
98-
* @param accountNumber
99-
* @param amount
134+
* @param accountNumber The account number to withdraw from.
135+
* @param amount The amount of money to withdraw.
100136
*/
101137
public void withdrawFunds(String accountNumber, double amount) {
138+
// Retrieve the account or throw an exception if not found
102139
CheckingAccount account = getAccountOrThrow(accountNumber);
103-
account.withdraw(amount);
140+
account.withdraw(amount); // Withdraw the specified amount from the account
104141
}
105142

106143
/**
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.
108145
*
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.
111149
*/
112150
private CheckingAccount getAccountOrThrow(String accountNumber) {
113151
CheckingAccount account = accountByNumber.get(accountNumber);
152+
// Check if the account is found and is not closed
114153
if (account == null || account.isClosed()) {
115154
throw new AccountNotFoundException("Account not found");
116155
}
117-
return account;
156+
return account; // Return the found account
118157
}
119158
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22

33
import java.util.Set;
44

5+
/**
6+
* Represents a business checking account, which is a specialized type of checking account. This
7+
* account must have at least one owner designated as a business.
8+
*/
59
public class BusinessCheckingAccount extends CheckingAccount {
610

11+
/**
12+
* Creates a new BusinessCheckingAccount with the specified account number, owners, and initial
13+
* balance.
14+
*
15+
* @param accountNumber The account number for the business checking account.
16+
* @param owners A set of Customer objects representing the owners of the account.
17+
* @param initialBalance The initial balance to set for the account.
18+
* @throws IllegalArgumentException if no owner is designated as a business.
19+
*/
720
public BusinessCheckingAccount(
821
String accountNumber, Set<Customer> owners, double initialBalance) {
9-
super(accountNumber, owners, initialBalance);
22+
super(
23+
accountNumber,
24+
owners,
25+
initialBalance); // Call the superclass constructor to initialize common fields
26+
27+
// Ensure that at least one owner is a business customer
1028
if (!owners.stream().anyMatch(Customer::isBusiness)) {
1129
throw new IllegalArgumentException("At least one owner must be a business");
1230
}

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,80 @@
22

33
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
44

5-
/** Represents a check. */
5+
/**
6+
* Represents a check that can be deposited into a checking account. A check has a number, an
7+
* amount, and is associated with a checking account.
8+
*/
69
public class Check {
710

11+
// The unique identifier for the check
812
private final String checkNumber;
13+
14+
// The amount specified on the check
915
private final double amount;
16+
17+
// The checking account that the check is drawn from
1018
private final CheckingAccount account;
19+
20+
// Indicates whether the check has been voided
1121
private boolean isVoided = false;
1222

1323
/**
14-
* Creates a new check.
24+
* Creates a new check with the specified check number, amount, and associated account.
1525
*
1626
* @param checkNumber The check number.
17-
* @param amount The amount of the check.
18-
* @param account The account the check is drawn on.
27+
* @param amount The amount of the check (must be positive).
28+
* @param account The checking account the check is drawn on.
29+
* @throws IllegalArgumentException if the amount is negative.
1930
*/
2031
public Check(String checkNumber, double amount, CheckingAccount account) {
2132
if (amount < 0) {
22-
throw new IllegalArgumentException("Check amount must be positive");
33+
throw new IllegalArgumentException(
34+
"Check amount must be positive"); // Validate the check amount
2335
}
2436
this.checkNumber = checkNumber;
2537
this.amount = amount;
26-
this.account = account;
38+
this.account = account; // Assign the associated checking account
2739
}
2840

2941
/**
3042
* Gets the voided status of the check.
3143
*
32-
* @return True if the check is voided, and false otherwise.
44+
* @return True if the check is voided; false otherwise.
3345
*/
3446
public boolean getIsVoided() {
35-
return isVoided;
47+
return isVoided; // Return the voided status
3648
}
3749

38-
/** Voids the check. */
50+
/** Voids the check, preventing it from being deposited. */
3951
public void voidCheck() {
40-
isVoided = true;
52+
isVoided = true; // Set the voided status to true
4153
}
4254

4355
/**
44-
* Deposits the check into an account.
56+
* Deposits the check into the specified account.
4557
*
4658
* @param toAccount The account to deposit the check into.
59+
* @throws CheckVoidedException if the check is voided.
4760
*/
4861
public void depositFunds(CheckingAccount toAccount) {
4962
if (isVoided) {
50-
throw new CheckVoidedException("Check is voided");
63+
throw new CheckVoidedException("Check is voided"); // Check if the check is voided
5164
}
52-
account.withdraw(amount);
53-
toAccount.deposit(amount);
54-
voidCheck();
65+
account.withdraw(amount); // Withdraw the amount from the original account
66+
toAccount.deposit(amount); // Deposit the amount into the specified account
67+
voidCheck(); // Void the check after successful deposit
5568
}
5669

5770
@Override
5871
public int hashCode() {
59-
return checkNumber.hashCode();
72+
return checkNumber.hashCode(); // Generate hash code based on the check number
6073
}
6174

6275
@Override
6376
public boolean equals(Object obj) {
6477
if (obj instanceof Check other) {
65-
return checkNumber.equals(other.checkNumber);
78+
return checkNumber.equals(other.checkNumber); // Check equality based on the check number
6679
}
6780
return false;
6881
}
@@ -77,6 +90,6 @@ public String toString() {
7790
+ amount
7891
+ ", account="
7992
+ account.getAccountNumber()
80-
+ '}';
93+
+ '}'; // Return a string representation of the check
8194
}
8295
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ public String getName() {
4242
return name;
4343
}
4444

45-
public boolean isBusiness() { // Add this method
45+
/**
46+
* Checks if the customer is a business.
47+
*
48+
* @return true if the customer is a business; false otherwise.
49+
*/
50+
public boolean isBusiness() {
4651
return isBusiness;
4752
}
4853

@@ -66,11 +71,13 @@ public Set<CheckingAccount> getAccounts() {
6671

6772
@Override
6873
public int hashCode() {
74+
// Generate a hash code based on the customer's ID for proper set operations
6975
return id.hashCode();
7076
}
7177

7278
@Override
7379
public boolean equals(Object obj) {
80+
// Check if this customer is equal to another customer based on their ID
7481
if (obj instanceof Customer other) {
7582
return id.equals(other.id);
7683
}
@@ -79,6 +86,7 @@ public boolean equals(Object obj) {
7986

8087
@Override
8188
public String toString() {
89+
// Provide a string representation of the customer including ID and name
8290
return "Customer{" + "id=" + id + ", name='" + name + '\'' + '}';
8391
}
8492
}

0 commit comments

Comments
 (0)