Skip to content

feat: added Audit log and Savings Account class with test for lesson_17 homework by Yemi #553

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 2 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,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(
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,140 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Represents a checking account. */
public class BankAccount {

private final Set<Customer> owners;
private final String accountNumber;
private double balance;
private boolean isActive;
private static final Logger logger = LoggerFactory.getLogger(SavingAccount.class);

/**
* Creates a new checking account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
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()) {
logger.info("Cannot deposit to a closed account");
throw new IllegalStateException("Cannot deposit to a closed account");
}
if (amount <= 0) {
logger.info("Deposit amount must be positive");
throw new IllegalArgumentException("Deposit amount must be positive");
}
balance += amount;
}

/**
* Withdraws funds from the account.
*
* @param amount
* @throws InsufficientFundsException
*/
public void withdraw(double amount) throws InsufficientFundsException {
if (isClosed()) {
logger.info("Cannot withdraw from a closed account");
throw new IllegalStateException("Cannot withdraw from a closed account");
}
if (amount <= 0) {
logger.info("Withdrawal amount must be positive");
throw new IllegalStateException("Withdrawal amount must be positive");
}
if (balance < amount) {
logger.info("Account does not have enough funds for withdrawal");
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
}
balance -= amount;
}

/**
* 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) {
logger.info("Cannot close account with a positive balance");
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 "CheckingAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -11,6 +12,8 @@ public class BankAtm {

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

/**
* Adds a checking account to the bank.
Expand Down Expand Up @@ -48,6 +51,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
public void depositFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
account.deposit(amount);
auditLog.addToMap(accountNumberByValue, accountNumber, amount);
}

/**
Expand All @@ -70,6 +74,7 @@ public void depositFunds(String accountNumber, Check check) {
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
auditLog.addToMap(accountNumberByValue, accountNumber, amount);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a checking account. */
public class CheckingAccount {
public class CheckingAccount extends BankAccount {

private final Set<Customer> owners;
private final String accountNumber;
private double balance;
private boolean isActive;
// private boolean isActive;

/**
* Creates a new checking account.
Expand All @@ -18,114 +14,8 @@ public class CheckingAccount {
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public CheckingAccount(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");
}
balance += amount;
}

/**
* Withdraws funds from the account.
*
* @param amount
* @throws InsufficientFundsException
*/
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 (balance < amount) {
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
}
balance -= amount;
}

/**
* 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 CheckingAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

@Override
public String toString() {
return "CheckingAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
public CheckingAccount(String accountNumber, Set<Customer> owners, double balance) {
super(accountNumber, owners, balance);
// isActive = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Represents a saving account. */
public class SavingAccount extends BankAccount {
// private boolean isActive;
private boolean isCheckCreationAllowed;
private static final Logger logger = LoggerFactory.getLogger(SavingAccount.class);

/**
* Creates a new saving account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public SavingAccount(String accountNumber, Set<Customer> owners, double balance) {
super(accountNumber, owners, balance);
logger.info("Saving Account constructor accessed...");
isCheckCreationAllowed = false;
}

public boolean isCheckCreationAllowed() {
return isCheckCreationAllowed;
}
}
Loading