Skip to content

Commit d0c65b6

Browse files
committed
Deleted unecessary files
1 parent ccf8696 commit d0c65b6

File tree

7 files changed

+436
-127
lines changed

7 files changed

+436
-127
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/**
7+
* Common interface for all account types. Provides a contract for basic account operations
8+
* following the Interface Segregation Principle.
9+
*/
10+
public interface Account {
11+
12+
/**
13+
* Gets the account number.
14+
*
15+
* @return The account number.
16+
*/
17+
String getAccountNumber();
18+
19+
/**
20+
* Gets the owners of the account.
21+
*
22+
* @return The set of customers who own the account.
23+
*/
24+
Set<Customer> getOwners();
25+
26+
/**
27+
* Gets the current balance of the account.
28+
*
29+
* @return The current balance.
30+
*/
31+
double getBalance();
32+
33+
/**
34+
* Deposits funds into the account.
35+
*
36+
* @param amount The amount to deposit.
37+
* @throws IllegalArgumentException if amount is negative or zero.
38+
* @throws IllegalStateException if account is closed.
39+
*/
40+
void deposit(double amount);
41+
42+
/**
43+
* Withdraws funds from the account.
44+
*
45+
* @param amount The amount to withdraw.
46+
* @throws InsufficientFundsException if insufficient funds.
47+
* @throws IllegalArgumentException if amount is negative or zero.
48+
* @throws IllegalStateException if account is closed.
49+
*/
50+
void withdraw(double amount) throws InsufficientFundsException;
51+
52+
/**
53+
* Checks if the account is closed.
54+
*
55+
* @return True if the account is closed, false otherwise.
56+
*/
57+
boolean isClosed();
58+
59+
/**
60+
* Closes the account.
61+
*
62+
* @throws IllegalStateException if account cannot be closed.
63+
*/
64+
void closeAccount();
65+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Audit log for tracking all banking transactions. Records debits and credits to accounts for
9+
* compliance and traceability.
10+
*/
11+
public class AuditLog {
12+
13+
private final List<AuditEntry> entries = new ArrayList<>();
14+
15+
/**
16+
* Records a debit transaction.
17+
*
18+
* @param accountNumber The account number.
19+
* @param amount The amount debited.
20+
* @param description Description of the transaction.
21+
*/
22+
public void recordDebit(String accountNumber, double amount, String description) {
23+
entries.add(new AuditEntry(accountNumber, TransactionType.DEBIT, amount, description));
24+
}
25+
26+
/**
27+
* Records a credit transaction.
28+
*
29+
* @param accountNumber The account number.
30+
* @param amount The amount credited.
31+
* @param description Description of the transaction.
32+
*/
33+
public void recordCredit(String accountNumber, double amount, String description) {
34+
entries.add(new AuditEntry(accountNumber, TransactionType.CREDIT, amount, description));
35+
}
36+
37+
/**
38+
* Gets all audit entries.
39+
*
40+
* @return List of all audit entries.
41+
*/
42+
public List<AuditEntry> getAllEntries() {
43+
return new ArrayList<>(entries);
44+
}
45+
46+
/**
47+
* Gets audit entries for a specific account.
48+
*
49+
* @param accountNumber The account number to filter by.
50+
* @return List of audit entries for the specified account.
51+
*/
52+
public List<AuditEntry> getEntriesForAccount(String accountNumber) {
53+
return entries.stream()
54+
.filter(entry -> entry.getAccountNumber().equals(accountNumber))
55+
.toList();
56+
}
57+
58+
/**
59+
* Gets the total number of recorded transactions.
60+
*
61+
* @return The total number of transactions.
62+
*/
63+
public int getTransactionCount() {
64+
return entries.size();
65+
}
66+
67+
/** Represents a single audit entry. */
68+
public static class AuditEntry {
69+
private final String accountNumber;
70+
private final TransactionType type;
71+
private final double amount;
72+
private final String description;
73+
private final LocalDateTime timestamp;
74+
75+
/**
76+
* Creates a new audit entry.
77+
*
78+
* @param accountNumber The account number.
79+
* @param type The transaction type.
80+
* @param amount The transaction amount.
81+
* @param description The transaction description.
82+
*/
83+
public AuditEntry(
84+
String accountNumber, TransactionType type, double amount, String description) {
85+
this.accountNumber = accountNumber;
86+
this.type = type;
87+
this.amount = amount;
88+
this.description = description;
89+
this.timestamp = LocalDateTime.now();
90+
}
91+
92+
/**
93+
* Gets the account number.
94+
*
95+
* @return The account number.
96+
*/
97+
public String getAccountNumber() {
98+
return accountNumber;
99+
}
100+
101+
/**
102+
* Gets the transaction type.
103+
*
104+
* @return The transaction type.
105+
*/
106+
public TransactionType getType() {
107+
return type;
108+
}
109+
110+
/**
111+
* Gets the transaction amount.
112+
*
113+
* @return The transaction amount.
114+
*/
115+
public double getAmount() {
116+
return amount;
117+
}
118+
119+
/**
120+
* Gets the transaction description.
121+
*
122+
* @return The transaction description.
123+
*/
124+
public String getDescription() {
125+
return description;
126+
}
127+
128+
/**
129+
* Gets the transaction timestamp.
130+
*
131+
* @return The transaction timestamp.
132+
*/
133+
public LocalDateTime getTimestamp() {
134+
return timestamp;
135+
}
136+
137+
@Override
138+
public String toString() {
139+
return String.format(
140+
"[%s] %s %s: %.2f - %s", timestamp, accountNumber, type, amount, description);
141+
}
142+
}
143+
144+
/** Enumeration for transaction types. */
145+
public enum TransactionType {
146+
CREDIT,
147+
DEBIT
148+
}
149+
}

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,40 @@
66
import java.util.Set;
77
import java.util.UUID;
88

9-
/** Represents a bank ATM. */
9+
/**
10+
* Represents a bank ATM with support for multiple account types and comprehensive audit logging.
11+
* Enhanced to follow SOLID principles.
12+
*/
1013
public class BankAtm {
1114

1215
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
16+
private final Map<String, Account> accountByNumber = new HashMap<>();
17+
private final AuditLog auditLog = new AuditLog();
1418

1519
/**
1620
* Adds a checking account to the bank.
1721
*
1822
* @param account The account to add.
1923
*/
2024
public void addAccount(CheckingAccount account) {
25+
addAccountInternal(account);
26+
}
27+
28+
/**
29+
* Adds a savings account to the bank.
30+
*
31+
* @param account The savings account to add.
32+
*/
33+
public void addAccount(SavingsAccount account) {
34+
addAccountInternal(account);
35+
}
36+
37+
/**
38+
* Internal method to add any type of account.
39+
*
40+
* @param account The account to add.
41+
*/
42+
private void addAccountInternal(Account account) {
2143
accountByNumber.put(account.getAccountNumber(), account);
2244
account
2345
.getOwners()
@@ -46,40 +68,65 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4668
* @param amount The amount to deposit.
4769
*/
4870
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
71+
Account account = getAccountOrThrow(accountNumber);
5072
account.deposit(amount);
73+
auditLog.recordCredit(accountNumber, amount, "Cash deposit");
5174
}
5275

5376
/**
54-
* Deposits funds into an account using a check.
77+
* Deposits funds into an account using a check. Note: Only checking accounts support check
78+
* deposits.
5579
*
5680
* @param accountNumber The account number.
5781
* @param check The check to deposit.
82+
* @throws IllegalArgumentException if trying to deposit a check to a savings account.
5883
*/
5984
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
61-
check.depositFunds(account);
85+
Account account = getAccountOrThrow(accountNumber);
86+
87+
// Only checking accounts can accept check deposits
88+
if (!(account instanceof CheckingAccount)) {
89+
throw new IllegalArgumentException("Cannot deposit checks to savings accounts");
90+
}
91+
92+
CheckingAccount checkingAccount = (CheckingAccount) account;
93+
check.depositFunds(checkingAccount);
94+
auditLog.recordCredit(
95+
accountNumber,
96+
check.getAmount(),
97+
String.format("Check deposit from account %s", check.getSourceAccount().getAccountNumber()));
6298
}
6399

64100
/**
65101
* Withdraws funds from an account.
66102
*
67-
* @param accountNumber
68-
* @param amount
103+
* @param accountNumber The account number.
104+
* @param amount The amount to withdraw.
69105
*/
70106
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
107+
Account account = getAccountOrThrow(accountNumber);
72108
account.withdraw(amount);
109+
auditLog.recordDebit(accountNumber, amount, "Cash withdrawal");
110+
}
111+
112+
/**
113+
* Gets the audit log for transaction tracking.
114+
*
115+
* @return The audit log instance.
116+
*/
117+
public AuditLog getAuditLog() {
118+
return auditLog;
73119
}
74120

75121
/**
76122
* Gets an account by its number or throws an exception if not found.
77123
*
78124
* @param accountNumber The account number.
79125
* @return The account.
126+
* @throws AccountNotFoundException if the account is not found or is closed.
80127
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
128+
private Account getAccountOrThrow(String accountNumber) {
129+
Account account = accountByNumber.get(accountNumber);
83130
if (account == null || account.isClosed()) {
84131
throw new AccountNotFoundException("Account not found");
85132
}

0 commit comments

Comments
 (0)