Skip to content

Commit 13eee25

Browse files
committed
feat: refactor bank account structure and add MoneyOrder and BusinessCheckingAccount classes
1 parent bc81ec0 commit 13eee25

File tree

8 files changed

+214
-184
lines changed

8 files changed

+214
-184
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package com.codedifferently.lesson17.bank;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
4+
import java.util.List;
55

6-
/**
7-
* Creates a new saving account.
8-
*
9-
* @param accountNumber The account number as log transaction key.
10-
* @param value The actual debit/credit transaction.
11-
* @param accountNumberByValueLog The audit log object.
12-
*/
136
public class AuditLog {
7+
private List<String> logEntries;
148

15-
// Method to add a value to an existing ArrayList or create a new one if accountNumber doesn't
16-
// exist
17-
public void addToMap(
18-
HashMap<String, ArrayList<Double>> accountNumberByValueLog,
19-
String accountNumber,
20-
Double value) {
21-
accountNumberByValueLog.computeIfAbsent(accountNumber, k -> new ArrayList<>()).add(value);
22-
}
23-
}
9+
public AuditLog() {
10+
logEntries = new ArrayList<>();
11+
}
12+
13+
public void logTransaction(BankAccount account, double amount, String transactionType) {
14+
String entry = String.format("%s transaction: %s | Amount: %.2f | Balance: %.2f",
15+
transactionType, account.getAccountNumber(), amount, account.getBalance());
16+
logEntries.add(entry);
17+
System.out.println(entry); // You can remove this line if you prefer logging to a file or database
18+
}
19+
20+
public List<String> getLogEntries() {
21+
return logEntries;
22+
}
23+
}
Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,133 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import java.util.HashSet;
43
import java.util.Set;
54

6-
public abstract class BankAccount {
5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
7+
public class BankAccount {
78
protected String accountNumber;
89
protected Set<Customer> owners;
910
protected double balance;
10-
protected boolean closed;
11+
protected boolean isActive;
12+
private static final double LARGE_TRANSACTION_THRESHOLD = 5000.0;
1113

12-
public BankAccount(String accountNumber, Customer owner) {
14+
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
1315
this.accountNumber = accountNumber;
14-
this.owners = new HashSet<>(Set.of(owner));
15-
this.balance = 0.0;
16-
this.closed = false;
17-
owner.addAccount(this);
16+
this.owners = owners;
17+
this.balance = initialBalance;
18+
isActive = true;
1819
}
1920

21+
/**
22+
* Gets the account number.
23+
*
24+
* @return The account number.
25+
*/
2026
public String getAccountNumber() {
2127
return accountNumber;
2228
}
2329

30+
/**
31+
* Gets the owners of the account.
32+
*
33+
* @return The owners of the account.
34+
*/
2435
public Set<Customer> getOwners() {
2536
return owners;
2637
}
2738

39+
/**
40+
* Deposits funds into the account.
41+
*
42+
* @param amount The amount to deposit.
43+
*/
44+
public void deposit(double amount) throws IllegalStateException {
45+
if (isClosed()) {
46+
throw new IllegalStateException("Cannot deposit to a closed account");
47+
}
48+
if (amount <= 0) {
49+
throw new IllegalArgumentException("Deposit amount must be positive");
50+
}
51+
if (amount > LARGE_TRANSACTION_THRESHOLD) {
52+
System.out.println("Warning: Large transaction detected for deposit.");
53+
}
54+
balance += amount;
55+
}
56+
57+
58+
// Withdraws funds from the account.
59+
60+
public void withdraw(double amount) throws InsufficientFundsException {
61+
if (isClosed()) {
62+
throw new IllegalStateException("Cannot withdraw from a closed account");
63+
}
64+
if (amount <= 0) {
65+
throw new IllegalStateException("Withdrawal amount must be positive");
66+
}
67+
if (amount > LARGE_TRANSACTION_THRESHOLD) {
68+
System.out.println("Warning: Large transaction detected for withdrawal.");
69+
}
70+
if (balance < amount) {
71+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
72+
}
73+
balance -= amount;
74+
}
75+
/**
76+
* Checks if the account is active.
77+
*
78+
* @return True if the account is active, otherwise false.
79+
*/
80+
81+
/**
82+
* Gets the balance of the account.
83+
*
84+
* @return The balance of the account.
85+
*/
2886
public double getBalance() {
2987
return balance;
3088
}
3189

90+
/** Closes the account. */
91+
public void closeAccount() throws IllegalStateException {
92+
if (balance > 0) {
93+
throw new IllegalStateException("Cannot close account with a positive balance");
94+
}
95+
isActive = false;
96+
}
97+
98+
/**
99+
* Checks if the account is closed.
100+
*
101+
* @return True if the account is closed, otherwise false.
102+
*/
32103
public boolean isClosed() {
33-
return closed;
104+
return !isActive;
34105
}
35106

36-
public void deposit(double amount) {
37-
balance += amount;
107+
@Override
108+
public int hashCode() {
109+
return accountNumber.hashCode();
38110
}
39111

40-
public void withdraw(double amount) {
41-
balance -= amount;
112+
@Override
113+
public boolean equals(Object obj) {
114+
if (obj instanceof BankAccount other) {
115+
return accountNumber.equals(other.accountNumber);
116+
}
117+
return false;
42118
}
43119

44-
public void closeAccount() {
45-
closed = true;
120+
@Override
121+
public String toString() {
122+
return "BnnkAccount{"
123+
+ "accountNumber='"
124+
+ accountNumber
125+
+ '\''
126+
+ ", balance="
127+
+ balance
128+
+ ", isActive="
129+
+ isActive
130+
+ '}';
46131
}
47132
}
133+

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.codedifferently.lesson17.bank;
22

33
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
4+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
5+
6+
import main.java.com.codedifferently.lesson17.bank.AuditLog;
7+
import main.java.com.codedifferently.lesson17.bank.BankAccount;
8+
import main.java.com.codedifferently.lesson17.bank.MoneyOrder;
9+
410
import java.util.HashMap;
511
import java.util.Map;
612
import java.util.Set;
@@ -70,7 +76,15 @@ public void withdrawFunds(String accountNumber, double amount) {
7076
account.withdraw(amount);
7177
auditLog.record("Withdrew $" + amount + " from account " + accountNumber);
7278
}
73-
79+
/**
80+
* Handles a money order transaction.
81+
* @param moneyOrder The money order to process.
82+
*/
83+
public void handleMoneyOrder(MoneyOrder moneyOrder) throws InsufficientFundsException {
84+
BankAccount account = getAccountOrThrow(moneyOrder.getSourceAccount().getAccountNumber());
85+
moneyOrder.process();
86+
auditLog.record("Processed money order of $" + moneyOrder.getAmount() + " from account " + account.getAccountNumber());
87+
}
7488
/**
7589
* Gets an account by its number or throws an exception if not found.
7690
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
7+
public class BusinessCheckingAccount extends BankAccount {
8+
private boolean isBusiness;
9+
10+
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance, boolean isBusiness) {
11+
super(accountNumber, owners, initialBalance);
12+
this.isBusiness = isBusiness;
13+
}
14+
15+
/**
16+
* Business accounts may have special conditions for checks, etc.
17+
*/
18+
@Override
19+
public void withdraw(double amount) throws InsufficientFundsException {
20+
// Business checking accounts can withdraw with more flexibility
21+
if (amount > balance) {
22+
throw new InsufficientFundsException("Insufficient funds in business checking account.");
23+
}
24+
balance -= amount;
25+
}
26+
27+
public boolean isBusiness() {
28+
return isBusiness;
29+
}
30+
}

0 commit comments

Comments
 (0)