Skip to content

feat: implement bank account system with audit logging and support fo… #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.codedifferently.lesson17.bank;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Creates a new saving account.
*
* @param accountNumber The account number as log transaction key.
* @param value The actual debit/credit transaction.
* @param accountNumberByValueLog The audit log object.
*/
public class AuditLog {

// Method to add a value to an existing ArrayList or create a new one if accountNumber doesn't
// exist
public void addToMap(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird. Maybe do some research on how to build a logger object. No too difficult to make a simple one.

HashMap<String, ArrayList<Double>> accountNumberByValueLog,
String accountNumber,
Double value) {
accountNumberByValueLog.computeIfAbsent(accountNumber, k -> new ArrayList<>()).add(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.codedifferently.lesson17.bank;

import java.util.HashSet;
import java.util.Set;

public abstract class BankAccount {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of building your own BankAccount class from scratch, you could take the logic from CheckingAccount.

protected String accountNumber;
protected Set<Customer> owners;
protected double balance;
protected boolean closed;

public BankAccount(String accountNumber, Customer owner) {
this.accountNumber = accountNumber;
this.owners = new HashSet<>(Set.of(owner));
this.balance = 0.0;
this.closed = false;
owner.addAccount(this);
}

public String getAccountNumber() {
return accountNumber;
}

public Set<Customer> getOwners() {
return owners;
}

public double getBalance() {
return balance;
}

public boolean isClosed() {
return closed;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
balance -= amount;
}

public void closeAccount() {
closed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@
public class BankAtm {

private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
private final Map<String, BankAccount> accountByNumber = new HashMap<>();
private final AuditLog auditLog = new AuditLog();

/**
* Adds a checking account to the bank.
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
public void addAccount(BankAccount account) {
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
.forEach(
owner -> {
customerById.put(owner.getId(), owner);
});
account.getOwners().forEach(owner -> customerById.put(owner.getId(), owner));
}

/**
Expand All @@ -33,7 +29,7 @@ public void addAccount(CheckingAccount account) {
* @param customerId The ID of the customer.
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
return customerById.containsKey(customerId)
? customerById.get(customerId).getAccounts()
: Set.of();
Expand All @@ -46,8 +42,9 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
* @param amount The amount to deposit.
*/
public void depositFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.deposit(amount);
auditLog.record("Deposited $" + amount + " to account " + accountNumber);
}

/**
Expand All @@ -57,8 +54,9 @@ public void depositFunds(String accountNumber, double amount) {
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
auditLog.record("Deposited check of $" + check.getAmount() + " into account " + accountNumber);
}

/**
Expand All @@ -68,8 +66,9 @@ public void depositFunds(String accountNumber, Check check) {
* @param amount
*/
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
auditLog.record("Withdrew $" + amount + " from account " + accountNumber);
}

/**
Expand All @@ -78,8 +77,8 @@ public void withdrawFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @return The account.
*/
private CheckingAccount getAccountOrThrow(String accountNumber) {
CheckingAccount account = accountByNumber.get(accountNumber);
private BankAccount getAccountOrThrow(String accountNumber) {
BankAccount account = accountByNumber.get(accountNumber);
if (account == null || account.isClosed()) {
throw new AccountNotFoundException("Account not found");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
// Correct import

/** Represents a check. */
public class Check {
Expand Down Expand Up @@ -45,13 +45,11 @@ public void voidCheck() {
*
* @param toAccount The account to deposit the check into.
*/
public void depositFunds(CheckingAccount toAccount) {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
public void depositFunds(BankAccount account) {
if (account instanceof SavingsAccount) {
throw new UnsupportedOperationException("Cannot deposit checks into a savings account");
}
account.withdraw(amount);
toAccount.deposit(amount);
voidCheck();
account.deposit(amount);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.codedifferently.lesson17.bank;
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
import java.util.Set;
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;

import main.java.com.codedifferently.lesson17.bank.BankAccount;

public class CheckingAccount extends BankAccount {

/** Represents a checking account. */
public class CheckingAccount {
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);{

private final Set<Customer> owners;
private final String accountNumber;
Expand Down Expand Up @@ -128,4 +132,19 @@ public String toString() {
+ isActive
+ '}';
}
@Override
public void withdraw(double amount) throws InsufficientFundsException {
if (isClosed()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should all be in the superclass since these are good checks for all accounts, no matter the type.

throw new IllegalStateException("Cannot withdraw from a closed account");
}
if (amount <= 0) {
throw new IllegalStateException("Withdrawal amount must be positive");
}
if (balance < amount) {
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
}
balance -= amount;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ public class Customer {
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();

private boolean isBusiness;

public Customer(String name, boolean isBusiness) {
this.name = name;
this.id = UUID.randomUUID();
this.isBusiness = isBusiness;
}

public boolean isBusiness() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

return isBusiness;
}

/**
* Creates a new customer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

/** A savings account does not support checks. */
public class SavingsAccount extends BankAccount {

public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}

// No check support = no extra method needed!
}
Loading