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 all commits
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,43 @@
package com.codedifferently.lesson17.bank;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class AuditLog {
private final List<String> logEntries = new ArrayList<>();

public void logTransaction(BankAccount account, double amount, String transactionType) {
String entry =
String.format(
"%s transaction: %s | Amount: %.2f | Balance: %.2f",
transactionType, account.getAccountNumber(), amount, account.getBalance());
logEntries.add(entry);
System.out.println(
entry); // You can remove this line if you prefer logging to a file or database
}

public List<String> getLogEntries() {
return logEntries;
}

public void record(String message) {
String timestampedEntry = LocalDateTime.now() + " | " + message;
logEntries.add(timestampedEntry);
System.out.println(timestampedEntry); // Optional: For real-time feedback
}

public void printLog() {
System.out.println("Audit Log:");
for (String entry : logEntries) {
System.out.println(entry);
}
}

/**
* Records an audit log message.
*
* @param message The message to record.
*/
// Removed duplicate method definition for record(String message)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.codedifferently.lesson17.bank;

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

public class BankAccount {
protected String accountNumber;
protected Set<Customer> owners;
protected double balance;
protected boolean isActive;
private static final double LARGE_TRANSACTION_THRESHOLD = 5000.0;

public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
this.balance = initialBalance;
isActive = true;
}

/**
* Gets the account number.
*
* @return The account number.
*/
public String getAccountNumber() {
return accountNumber;
}

/**
* Gets the owners of the account.
*
* @return The owners of the account.
*/
public Set<Customer> getOwners() {
return owners;
}

/**
* Deposits funds into the account.
*
* @param amount The amount to deposit.
*/
public void deposit(double amount) throws IllegalStateException {
if (isClosed()) {
throw new IllegalStateException("Cannot deposit to a closed account");
}
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive");
}
if (amount > LARGE_TRANSACTION_THRESHOLD) {
System.out.println("Warning: Large transaction detected for deposit.");
}
balance += amount;
}

// Withdraws funds from the account.

public void withdraw(double amount) throws InsufficientFundsException {
if (isClosed()) {
throw new IllegalStateException("Cannot withdraw from a closed account");
}
if (amount <= 0) {
throw new IllegalStateException("Withdrawal amount must be positive");
}
if (amount > LARGE_TRANSACTION_THRESHOLD) {
System.out.println("Warning: Large transaction detected for withdrawal.");
}
if (balance < amount) {
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
}
balance -= amount;
}

/**
* Checks if the account is active.
*
* @return True if the account is active, otherwise false.
*/

/**
* Gets the balance of the account.
*
* @return The balance of the account.
*/
public double getBalance() {
return balance;
}

/** Closes the account. */
public void closeAccount() throws IllegalStateException {
if (balance > 0) {
throw new IllegalStateException("Cannot close account with a positive balance");
}
isActive = false;
}

/**
* Checks if the account is closed.
*
* @return True if the account is closed, otherwise false.
*/
public boolean isClosed() {
return !isActive;
}

@Override
public int hashCode() {
return accountNumber.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof BankAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

@Override
public String toString() {
return "BnnkAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -10,21 +12,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 +31,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 +44,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 @@ -56,9 +55,15 @@ public void depositFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
public void depositFunds(String accountNumber, Check check) throws CheckVoidedException {
BankAccount account = getAccountOrThrow(accountNumber);

if (check.getIsVoided()) {
throw new CheckVoidedException("Check is voided");
}

check.depositFunds(account);
auditLog.record("Deposited check of $" + check.getAmount() + " to account " + accountNumber);
}

/**
Expand All @@ -68,8 +73,24 @@ 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);
}

/**
* Handles a money order transaction.
*
* @param moneyOrder The money order to process.
*/
public void handleMoneyOrder(MoneyOrder moneyOrder) throws InsufficientFundsException {
BankAccount account = getAccountOrThrow(moneyOrder.getSourceAccount().getAccountNumber());
moneyOrder.process();
auditLog.record(
"Processed money order of $"
+ moneyOrder.getAmount()
+ " from account "
+ account.getAccountNumber());
}

/**
Expand All @@ -78,8 +99,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
@@ -0,0 +1,17 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public class BusinessCheckingAccount extends BankAccount {
private final boolean isBusiness;

public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance, boolean isBusiness) {
super(accountNumber, owners, initialBalance);
this.isBusiness = isBusiness;
}

public boolean isBusiness() {
return isBusiness;
}
}
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,14 @@ 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();
if (account == null) {
throw new IllegalArgumentException("Account cannot be null");
}
account.deposit(amount);
}

@Override
Expand Down Expand Up @@ -79,4 +80,13 @@ public String toString() {
+ account.getAccountNumber()
+ '}';
}

/**
* Gets the check number.
*
* @return The check number.
*/
public double getAmount() {
return amount;
}
}
Loading