Skip to content

Commit c5ee4d9

Browse files
refactor: Simplify ATM implementation and remove unnecessary components
1 parent b3bcdcf commit c5ee4d9

23 files changed

+455
-1004
lines changed
Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,144 @@
11
package com.codedifferently.lesson17.bank;
22

3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
34
import java.util.Set;
45

5-
/** Base interface for all account types. */
6-
public interface Account {
7-
String getAccountNumber();
6+
/** Represents an abstract bank account. */
7+
public abstract class Account {
88

9-
Set<Customer> getOwners();
9+
protected final Set<Customer> owners;
10+
protected final String accountNumber;
11+
protected double balance;
12+
protected boolean isActive;
13+
private final AuditLog auditLog;
1014

11-
void deposit(double amount);
15+
/**
16+
* Creates a new account.
17+
*
18+
* @param accountNumber The account number.
19+
* @param owners The owners of the account.
20+
* @param initialBalance The initial balance of the account.
21+
*/
22+
public Account(String accountNumber, Set<Customer> owners, double initialBalance) {
23+
this.accountNumber = accountNumber;
24+
this.owners = owners;
25+
this.balance = initialBalance;
26+
isActive = true;
27+
this.auditLog = new AuditLog();
28+
}
1229

13-
void withdraw(double amount);
30+
/**
31+
* Gets the account number.
32+
*
33+
* @return The account number.
34+
*/
35+
public String getAccountNumber() {
36+
return accountNumber;
37+
}
1438

15-
double getBalance();
39+
/**
40+
* Gets the owners of the account.
41+
*
42+
* @return The owners of the account.
43+
*/
44+
public Set<Customer> getOwners() {
45+
return owners;
46+
}
1647

17-
boolean isClosed();
48+
/**
49+
* Deposits funds into the account. The account must be active to accept deposits.
50+
*
51+
* @param amount The amount to deposit (must be greater than 0)
52+
* @throws IllegalStateException if the account is closed
53+
* @throws IllegalArgumentException if the amount is not positive
54+
*/
55+
public void deposit(double amount) throws IllegalStateException {
56+
if (isClosed()) {
57+
throw new IllegalStateException("Cannot deposit to a closed account");
58+
}
59+
if (amount <= 0) {
60+
throw new IllegalArgumentException("Deposit amount must be positive");
61+
}
62+
balance += amount;
63+
}
1864

19-
void closeAccount();
65+
/**
66+
* Gets the balance of the account.
67+
*
68+
* @return The balance of the account.
69+
*/
70+
public double getBalance() {
71+
return balance;
72+
}
73+
74+
/**
75+
* Closes the account.
76+
*
77+
* @throws IllegalStateException if the account has a positive balance
78+
*/
79+
public void closeAccount() throws IllegalStateException {
80+
if (balance > 0) {
81+
throw new IllegalStateException("Cannot close account with a positive balance");
82+
}
83+
isActive = false;
84+
}
85+
86+
/**
87+
* Checks if the account is closed.
88+
*
89+
* @return True if the account is closed, otherwise false.
90+
*/
91+
public boolean isClosed() {
92+
return !isActive;
93+
}
94+
95+
/**
96+
* Withdraws funds from the account.
97+
*
98+
* @param amount The amount to withdraw (must be greater than 0)
99+
* @throws InsufficientFundsException if the account balance is less than the withdrawal amount
100+
* @throws IllegalStateException if the account is closed or the amount is not positive
101+
*/
102+
public void withdraw(double amount) throws InsufficientFundsException {
103+
if (isClosed()) {
104+
throw new IllegalStateException("Cannot withdraw from a closed account");
105+
}
106+
if (amount <= 0) {
107+
throw new IllegalStateException("Withdrawal amount must be positive");
108+
}
109+
if (balance < amount) {
110+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
111+
}
112+
balance -= amount;
113+
}
114+
115+
public AuditLog getAuditLog() {
116+
return auditLog;
117+
}
118+
119+
@Override
120+
public int hashCode() {
121+
return accountNumber.hashCode();
122+
}
123+
124+
@Override
125+
public boolean equals(Object obj) {
126+
if (obj instanceof Account other) {
127+
return accountNumber.equals(other.accountNumber);
128+
}
129+
return false;
130+
}
131+
132+
@Override
133+
public String toString() {
134+
return "Account{"
135+
+ "accountNumber='"
136+
+ accountNumber
137+
+ '\''
138+
+ ", balance="
139+
+ balance
140+
+ ", isActive="
141+
+ isActive
142+
+ '}';
143+
}
20144
}
Lines changed: 37 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,54 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import java.time.LocalDateTime;
4-
import java.util.ArrayList;
5-
import java.util.List;
3+
import java.util.HashSet;
4+
import java.util.Set;
65

7-
/** Keeps track of all financial transactions for auditing purposes. */
6+
/** Maintains a set of records for each transaction performed on bank accounts. */
87
public class AuditLog {
8+
private Set<String> audits;
99

10-
private final List<TransactionRecord> transactions = new ArrayList<>();
11-
12-
/**
13-
* Records a debit transaction.
14-
*
15-
* @param accountNumber The account number.
16-
* @param amount The amount debited.
17-
* @param description The description of the transaction.
18-
*/
19-
public void recordDebit(String accountNumber, double amount, String description) {
20-
transactions.add(
21-
new TransactionRecord(
22-
accountNumber, amount, TransactionType.DEBIT, description, LocalDateTime.now()));
10+
/** Constructs a new AuditLog with an empty set of audit records. */
11+
public AuditLog() {
12+
audits = new HashSet<>();
2313
}
2414

2515
/**
26-
* Records a credit transaction.
16+
* Logs a financial transaction for the specified account. Contains the account number, action
17+
* performed, transaction amount, and the resulting account balance.
2718
*
28-
* @param accountNumber The account number.
29-
* @param amount The amount credited.
30-
* @param description The description of the transaction.
19+
* @param account The account of the transaction
20+
* @param action The type of action performed (e.g., "DEPOSIT", "WITHDRAW")
21+
* @param amount The amount used in the transaction
3122
*/
32-
public void recordCredit(String accountNumber, double amount, String description) {
33-
transactions.add(
34-
new TransactionRecord(
35-
accountNumber, amount, TransactionType.CREDIT, description, LocalDateTime.now()));
36-
}
37-
38-
public List<TransactionRecord> getAllTransactions() {
39-
return new ArrayList<>(transactions);
23+
public void log(Account account, String action, double amount) {
24+
String audit =
25+
"Account: "
26+
+ account.getAccountNumber()
27+
+ " | Action: "
28+
+ action
29+
+ " | Amount: "
30+
+ amount
31+
+ " | New Balance: "
32+
+ account.getBalance();
33+
audits.add(audit);
4034
}
4135

42-
public List<TransactionRecord> getTransactionsForAccount(String accountNumber) {
43-
return transactions.stream()
44-
.filter(record -> record.getAccountNumber().equals(accountNumber))
45-
.toList();
36+
public Set<String> getAudits() {
37+
return audits;
4638
}
4739

48-
/** Represents a transaction record. */
49-
public static class TransactionRecord {
50-
private final String accountNumber;
51-
private final double amount;
52-
private final TransactionType type;
53-
private final String description;
54-
private final LocalDateTime timestamp;
55-
56-
public TransactionRecord(
57-
String accountNumber,
58-
double amount,
59-
TransactionType type,
60-
String description,
61-
LocalDateTime timestamp) {
62-
this.accountNumber = accountNumber;
63-
this.amount = amount;
64-
this.type = type;
65-
this.description = description;
66-
this.timestamp = timestamp;
67-
}
68-
69-
public String getAccountNumber() {
70-
return accountNumber;
71-
}
72-
73-
public double getAmount() {
74-
return amount;
75-
}
76-
77-
public TransactionType getType() {
78-
return type;
79-
}
80-
81-
public String getDescription() {
82-
return description;
83-
}
84-
85-
public LocalDateTime getTimestamp() {
86-
return timestamp;
87-
}
88-
89-
@Override
90-
public String toString() {
91-
return "TransactionRecord{"
92-
+ "accountNumber='"
93-
+ accountNumber
94-
+ '\''
95-
+ ", amount="
96-
+ amount
97-
+ ", type="
98-
+ type
99-
+ ", description='"
100-
+ description
101-
+ '\''
102-
+ ", timestamp="
103-
+ timestamp
104-
+ '}';
40+
/**
41+
* Returns a string representation of all audit records. Each audit entry is separated by a
42+
* newline character.
43+
*
44+
* @return A formatted string containing all audit entries, each on a separate line
45+
*/
46+
@Override
47+
public String toString() {
48+
String auditString = "";
49+
for (String audit : audits) {
50+
auditString += audit + "\n";
10551
}
106-
}
107-
108-
/** Enumeration of transaction types. */
109-
public enum TransactionType {
110-
DEBIT,
111-
CREDIT
52+
return auditString;
11253
}
11354
}

0 commit comments

Comments
 (0)