Skip to content

Commit bc81ec0

Browse files
committed
feat: implement bank account system with audit logging and support for multiple account types
1 parent a51414e commit bc81ec0

File tree

7 files changed

+136
-25
lines changed

7 files changed

+136
-25
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
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+
*/
13+
public class AuditLog {
14+
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public abstract class BankAccount {
7+
protected String accountNumber;
8+
protected Set<Customer> owners;
9+
protected double balance;
10+
protected boolean closed;
11+
12+
public BankAccount(String accountNumber, Customer owner) {
13+
this.accountNumber = accountNumber;
14+
this.owners = new HashSet<>(Set.of(owner));
15+
this.balance = 0.0;
16+
this.closed = false;
17+
owner.addAccount(this);
18+
}
19+
20+
public String getAccountNumber() {
21+
return accountNumber;
22+
}
23+
24+
public Set<Customer> getOwners() {
25+
return owners;
26+
}
27+
28+
public double getBalance() {
29+
return balance;
30+
}
31+
32+
public boolean isClosed() {
33+
return closed;
34+
}
35+
36+
public void deposit(double amount) {
37+
balance += amount;
38+
}
39+
40+
public void withdraw(double amount) {
41+
balance -= amount;
42+
}
43+
44+
public void closeAccount() {
45+
closed = true;
46+
}
47+
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,17 @@
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, BankAccount> accountByNumber = new HashMap<>();
14+
private final AuditLog auditLog = new AuditLog();
1415

1516
/**
1617
* Adds a checking account to the bank.
1718
*
1819
* @param account The account to add.
1920
*/
20-
public void addAccount(CheckingAccount account) {
21+
public void addAccount(BankAccount account) {
2122
accountByNumber.put(account.getAccountNumber(), account);
22-
account
23-
.getOwners()
24-
.forEach(
25-
owner -> {
26-
customerById.put(owner.getId(), owner);
27-
});
23+
account.getOwners().forEach(owner -> customerById.put(owner.getId(), owner));
2824
}
2925

3026
/**
@@ -33,7 +29,7 @@ public void addAccount(CheckingAccount account) {
3329
* @param customerId The ID of the customer.
3430
* @return The unique set of accounts owned by the customer.
3531
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
32+
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
3733
return customerById.containsKey(customerId)
3834
? customerById.get(customerId).getAccounts()
3935
: Set.of();
@@ -46,8 +42,9 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4642
* @param amount The amount to deposit.
4743
*/
4844
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
45+
BankAccount account = getAccountOrThrow(accountNumber);
5046
account.deposit(amount);
47+
auditLog.record("Deposited $" + amount + " to account " + accountNumber);
5148
}
5249

5350
/**
@@ -57,8 +54,9 @@ public void depositFunds(String accountNumber, double amount) {
5754
* @param check The check to deposit.
5855
*/
5956
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
57+
BankAccount account = getAccountOrThrow(accountNumber);
6158
check.depositFunds(account);
59+
auditLog.record("Deposited check of $" + check.getAmount() + " into account " + accountNumber);
6260
}
6361

6462
/**
@@ -68,8 +66,9 @@ public void depositFunds(String accountNumber, Check check) {
6866
* @param amount
6967
*/
7068
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
69+
BankAccount account = getAccountOrThrow(accountNumber);
7270
account.withdraw(amount);
71+
auditLog.record("Withdrew $" + amount + " from account " + accountNumber);
7372
}
7473

7574
/**
@@ -78,8 +77,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7877
* @param accountNumber The account number.
7978
* @return The account.
8079
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
80+
private BankAccount getAccountOrThrow(String accountNumber) {
81+
BankAccount account = accountByNumber.get(accountNumber);
8382
if (account == null || account.isClosed()) {
8483
throw new AccountNotFoundException("Account not found");
8584
}

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

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

3-
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
3+
// Correct import
44

55
/** Represents a check. */
66
public class Check {
@@ -45,13 +45,11 @@ public void voidCheck() {
4545
*
4646
* @param toAccount The account to deposit the check into.
4747
*/
48-
public void depositFunds(CheckingAccount toAccount) {
49-
if (isVoided) {
50-
throw new CheckVoidedException("Check is voided");
48+
public void depositFunds(BankAccount account) {
49+
if (account instanceof SavingsAccount) {
50+
throw new UnsupportedOperationException("Cannot deposit checks into a savings account");
5151
}
52-
account.withdraw(amount);
53-
toAccount.deposit(amount);
54-
voidCheck();
52+
account.deposit(amount);
5553
}
5654

5755
@Override

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
package com.codedifferently.lesson17.bank;
1+
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
43
import java.util.Set;
4+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
5+
6+
import main.java.com.codedifferently.lesson17.bank.BankAccount;
7+
8+
public class CheckingAccount extends BankAccount {
59

6-
/** Represents a checking account. */
7-
public class CheckingAccount {
10+
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
11+
super(accountNumber, owners, initialBalance);{
812

913
private final Set<Customer> owners;
1014
private final String accountNumber;
@@ -128,4 +132,19 @@ public String toString() {
128132
+ isActive
129133
+ '}';
130134
}
135+
@Override
136+
public void withdraw(double amount) throws InsufficientFundsException {
137+
if (isClosed()) {
138+
throw new IllegalStateException("Cannot withdraw from a closed account");
139+
}
140+
if (amount <= 0) {
141+
throw new IllegalStateException("Withdrawal amount must be positive");
142+
}
143+
if (balance < amount) {
144+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
145+
}
146+
balance -= amount;
147+
}
148+
131149
}
150+

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ public class Customer {
1111
private final String name;
1212
private final Set<CheckingAccount> accounts = new HashSet<>();
1313

14+
private boolean isBusiness;
15+
16+
public Customer(String name, boolean isBusiness) {
17+
this.name = name;
18+
this.id = UUID.randomUUID();
19+
this.isBusiness = isBusiness;
20+
}
21+
22+
public boolean isBusiness() {
23+
return isBusiness;
24+
}
25+
1426
/**
1527
* Creates a new customer.
1628
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/** A savings account does not support checks. */
6+
public class SavingsAccount extends BankAccount {
7+
8+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
9+
super(accountNumber, owners, initialBalance);
10+
}
11+
12+
// No check support = no extra method needed!
13+
}

0 commit comments

Comments
 (0)