Skip to content

Commit 7b72cef

Browse files
author
calvin
committed
added java files and modified test files
Signed-off-by: calvin <[email protected]>
1 parent 1c93014 commit 7b72cef

File tree

8 files changed

+275
-127
lines changed

8 files changed

+275
-127
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Abstract base class for bank accounts. */
7+
public abstract class Account {
8+
9+
protected final Set<Customer> owners;
10+
protected final String accountNumber;
11+
protected double balance;
12+
protected boolean isActive;
13+
14+
/**
15+
* Creates a new account.
16+
*
17+
* @param accountNumber The account number.
18+
* @param owners The owners of the account.
19+
* @param initialBalance The initial balance of the account.
20+
*/
21+
public Account(String accountNumber, Set<Customer> owners, double initialBalance) {
22+
this.accountNumber = accountNumber;
23+
this.owners = owners;
24+
this.balance = initialBalance;
25+
this.isActive = true;
26+
}
27+
28+
/**
29+
* Gets the account number.
30+
*
31+
* @return The account number.
32+
*/
33+
public String getAccountNumber() {
34+
return accountNumber;
35+
}
36+
37+
/**
38+
* Gets the owners of the account.
39+
*
40+
* @return The owners of the account.
41+
*/
42+
public Set<Customer> getOwners() {
43+
return owners;
44+
}
45+
46+
/**
47+
* Deposits funds into the account.
48+
*
49+
* @param amount The amount to deposit.
50+
*/
51+
public void deposit(double amount) throws IllegalStateException {
52+
if (isClosed()) {
53+
throw new IllegalStateException("Cannot deposit to a closed account");
54+
}
55+
if (amount <= 0) {
56+
throw new IllegalArgumentException("Deposit amount must be positive");
57+
}
58+
balance += amount;
59+
}
60+
61+
/**
62+
* Withdraws funds from the account.
63+
*
64+
* @param amount The amount to withdraw.
65+
* @throws InsufficientFundsException If there are insufficient funds.
66+
*/
67+
public void withdraw(double amount) throws InsufficientFundsException {
68+
if (isClosed()) {
69+
throw new IllegalStateException("Cannot withdraw from a closed account");
70+
}
71+
if (amount <= 0) {
72+
throw new IllegalStateException("Withdrawal amount must be positive");
73+
}
74+
if (balance < amount) {
75+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
76+
}
77+
balance -= amount;
78+
}
79+
80+
/**
81+
* Gets the balance of the account.
82+
*
83+
* @return The balance of the account.
84+
*/
85+
public double getBalance() {
86+
return balance;
87+
}
88+
89+
/** Closes the account. */
90+
public void closeAccount() throws IllegalStateException {
91+
if (balance > 0) {
92+
throw new IllegalStateException("Cannot close account with a positive balance");
93+
}
94+
isActive = false;
95+
}
96+
97+
/**
98+
* Checks if the account is closed.
99+
*
100+
* @return True if the account is closed, otherwise false.
101+
*/
102+
public boolean isClosed() {
103+
return !isActive;
104+
}
105+
106+
@Override
107+
public int hashCode() {
108+
return accountNumber.hashCode();
109+
}
110+
111+
@Override
112+
public boolean equals(Object obj) {
113+
if (obj instanceof Account other) {
114+
return accountNumber.equals(other.accountNumber);
115+
}
116+
return false;
117+
}
118+
119+
@Override
120+
public String toString() {
121+
return this.getClass().getSimpleName()
122+
+ "{"
123+
+ "accountNumber='"
124+
+ accountNumber
125+
+ '\''
126+
+ ", balance="
127+
+ balance
128+
+ ", isActive="
129+
+ isActive
130+
+ '}';
131+
}
132+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/** Represents an audit log for recording transactions. */
8+
public class AuditLog {
9+
10+
private final List<String> logs = new ArrayList<>();
11+
12+
/**
13+
* Logs a deposit transaction.
14+
*
15+
* @param accountNumber The account number.
16+
* @param amount The amount deposited.
17+
*/
18+
public void logDeposit(String accountNumber, double amount) {
19+
String logEntry =
20+
String.format(
21+
"[%s] Deposited $%.2f to account %s", LocalDateTime.now(), amount, accountNumber);
22+
logs.add(logEntry);
23+
}
24+
25+
/**
26+
* Logs a withdrawal transaction.
27+
*
28+
* @param accountNumber The account number.
29+
* @param amount The amount withdrawn.
30+
*/
31+
public void logWithdrawal(String accountNumber, double amount) {
32+
String logEntry =
33+
String.format(
34+
"[%s] Withdrew $%.2f from account %s", LocalDateTime.now(), amount, accountNumber);
35+
logs.add(logEntry);
36+
}
37+
38+
/**
39+
* Gets all log entries.
40+
*
41+
* @return The list of log entries.
42+
*/
43+
public List<String> getLogs() {
44+
return new ArrayList<>(logs);
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "AuditLog{" + "logs=" + logs + '}';
50+
}
51+
}

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
public class BankAtm {
1111

1212
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
13+
private final Map<String, Account> accountByNumber = new HashMap<>();
14+
private final AuditLog auditLog = new AuditLog();
1415

1516
/**
16-
* Adds a checking account to the bank.
17+
* Adds an account to the bank.
1718
*
1819
* @param account The account to add.
1920
*/
20-
public void addAccount(CheckingAccount account) {
21+
public void addAccount(Account account) {
2122
accountByNumber.put(account.getAccountNumber(), account);
2223
account
2324
.getOwners()
@@ -33,7 +34,7 @@ public void addAccount(CheckingAccount account) {
3334
* @param customerId The ID of the customer.
3435
* @return The unique set of accounts owned by the customer.
3536
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
37+
public Set<Account> findAccountsByCustomerId(UUID customerId) {
3738
return customerById.containsKey(customerId)
3839
? customerById.get(customerId).getAccounts()
3940
: Set.of();
@@ -46,8 +47,9 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4647
* @param amount The amount to deposit.
4748
*/
4849
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
50+
Account account = getAccountOrThrow(accountNumber);
5051
account.deposit(amount);
52+
auditLog.logDeposit(accountNumber, amount);
5153
}
5254

5355
/**
@@ -57,8 +59,9 @@ public void depositFunds(String accountNumber, double amount) {
5759
* @param check The check to deposit.
5860
*/
5961
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
62+
Account account = getAccountOrThrow(accountNumber);
6163
check.depositFunds(account);
64+
auditLog.logDeposit(accountNumber, check.getAmount());
6265
}
6366

6467
/**
@@ -68,8 +71,9 @@ public void depositFunds(String accountNumber, Check check) {
6871
* @param amount
6972
*/
7073
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
74+
Account account = getAccountOrThrow(accountNumber);
7275
account.withdraw(amount);
76+
auditLog.logWithdrawal(accountNumber, amount);
7377
}
7478

7579
/**
@@ -78,11 +82,20 @@ public void withdrawFunds(String accountNumber, double amount) {
7882
* @param accountNumber The account number.
7983
* @return The account.
8084
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
85+
private Account getAccountOrThrow(String accountNumber) {
86+
Account account = accountByNumber.get(accountNumber);
8387
if (account == null || account.isClosed()) {
8488
throw new AccountNotFoundException("Account not found");
8589
}
8690
return account;
8791
}
92+
93+
/**
94+
* Gets the audit log.
95+
*
96+
* @return The audit log.
97+
*/
98+
public AuditLog getAuditLog() {
99+
return auditLog;
100+
}
88101
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public Check(String checkNumber, double amount, CheckingAccount account) {
2727
}
2828

2929
/**
30-
* Gets the voided status of the check.
30+
* Gets the amount of the check.
3131
*
32-
* @return True if the check is voided, and false otherwise.
32+
* @return The amount.
3333
*/
34-
public boolean getIsVoided() {
35-
return isVoided;
34+
public double getAmount() {
35+
return amount;
3636
}
3737

3838
/** Voids the check. */
@@ -45,10 +45,13 @@ public void voidCheck() {
4545
*
4646
* @param toAccount The account to deposit the check into.
4747
*/
48-
public void depositFunds(CheckingAccount toAccount) {
48+
public void depositFunds(Account toAccount) {
4949
if (isVoided) {
5050
throw new CheckVoidedException("Check is voided");
5151
}
52+
if (!(toAccount instanceof CheckingAccount)) {
53+
throw new IllegalArgumentException("Checks can only be deposited into checking accounts");
54+
}
5255
account.withdraw(amount);
5356
toAccount.deposit(amount);
5457
voidCheck();

0 commit comments

Comments
 (0)