Skip to content

Commit b0ac1a4

Browse files
committed
feat: implement bank account system with audit logging and adds a savings account
1 parent a51414e commit b0ac1a4

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
4+
import java.util.ArrayList;
5+
6+
public class AuditLog {
7+
private List<String> logEntries = new ArrayList<>();
8+
9+
public void log (String message) {
10+
logEntries.add(message);
11+
System.out.println(message);
12+
13+
}
14+
15+
public List<String> getEntries() {
16+
return logEntries;
17+
}
18+
19+
public void clear() {
20+
logEntries.clear();
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codedifferently.lesson17.bank;
2+
/* We want to support a Savings Account that works just like CheckinAccount,
3+
* But will not allow you to write checks aainst the account.
4+
*
5+
* We want the BankAtm class to support the concept of a Business Checking Account
6+
* A business acount will require that at least ne of the owning accoutns is a business account.
7+
*
8+
*/
9+
10+
public abstract class BankAccount {
11+
protected String accountNumber;
12+
protected double balance;
13+
14+
public abstract void deposit(double amount, String method);
15+
public abstract void withdraw(double amount);
16+
17+
public double getBalance() {
18+
return balance;
19+
}
20+
public String getAccountNumber() {
21+
return accountNumber;
22+
}
23+
24+
//Follows LSP
25+
public interface depositable {
26+
void deposit(double amount, String method);
27+
}
28+
public interface withdrawable {
29+
void withdraw(double amount);
30+
}
31+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
43
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.UUID;
87

8+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9+
910
/** Represents a bank ATM. */
1011
public class BankAtm {
1112

1213
private final Map<UUID, Customer> customerById = new HashMap<>();
1314
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
14-
15+
private final AuditLog auditLog = new AuditLog();
1516
/**
1617
* Adds a checking account to the bank.
1718
*
@@ -48,6 +49,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4849
public void depositFunds(String accountNumber, double amount) {
4950
CheckingAccount account = getAccountOrThrow(accountNumber);
5051
account.deposit(amount);
52+
auditLog.log("Deposited " + amount + " into account " + accountNumber);
5153
}
5254

5355
/**
@@ -58,8 +60,11 @@ public void depositFunds(String accountNumber, double amount) {
5860
*/
5961
public void depositFunds(String accountNumber, Check check) {
6062
CheckingAccount account = getAccountOrThrow(accountNumber);
63+
6164
check.depositFunds(account);
62-
}
65+
auditLog.log("Deposited check into account " + accountNumber);
66+
67+
}
6368

6469
/**
6570
* Withdraws funds from an account.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Only Allows Deposits when accessing Atm on your Savings Account
2+
3+
public class SavingsAccount extends BankAccount {
4+
5+
@Override
6+
public void deposit(double amount, String method) {
7+
if("check".equalsIgnoreCase(method)) {
8+
throw new UnsupportedOperationException("Deposit not allowed");
9+
}
10+
11+
this.balance += amount;
12+
}
13+
14+
@Override
15+
public void withdraw(double amount) {
16+
throw new IllegalArgumentException("Insufficient funds");
17+
}
18+
}

0 commit comments

Comments
 (0)