Skip to content

feat: adds Savings Account and Business Checking Account and Tests #530

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 3 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
Expand Up @@ -10,15 +10,16 @@
public class BankAtm {

private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
final Map<String, CheckingAccount> checkingAccountsByNumber = new HashMap<>();
public Object accountByNumber;

/**
* Adds a checking account to the bank.
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
accountByNumber.put(account.getAccountNumber(), account);
checkingAccountsByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
.forEach(
Expand Down Expand Up @@ -79,10 +80,10 @@ public void withdrawFunds(String accountNumber, double amount) {
* @return The account.
*/
private CheckingAccount getAccountOrThrow(String accountNumber) {
CheckingAccount account = accountByNumber.get(accountNumber);
if (account == null || account.isClosed()) {
CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber);
if (checkingAccount == null || checkingAccount.isClosed()) {
throw new AccountNotFoundException("Account not found");
}
return account;
return checkingAccount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public final class BusinessCheckingAccount extends CheckingAccount {

/**
* Represents a business checking account.
*
* <p>It is a subclass of BankAccount and inherits its properties and methods.
*/
public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
if (!hasBusinessOwner(owners)) {
throw new IllegalArgumentException(
"A BusinessCheckingAccount must have at least one business owner.");
}
}

/**
* Checks if the owners of the account include at least one business customer.
*
* @param owners The owners of the account.
* @return true if there is a business owner, false otherwise.
*/
private boolean hasBusinessOwner(Set<Customer> owners) {
return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.codedifferently.lesson17.bank;

import java.util.UUID;

public class BusinessCustomer extends Customer {

public BusinessCustomer(UUID id, String name) {
super(id, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@

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

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

/**
* 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 CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
Expand All @@ -43,6 +35,23 @@ public Set<Customer> getOwners() {
return owners;
}

/**
* 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 positive balance");
}
isActive = false;
}

/**
* Deposits funds into the account.
*
Expand Down Expand Up @@ -71,29 +80,12 @@ public void withdraw(double amount) throws InsufficientFundsException {
if (amount <= 0) {
throw new IllegalStateException("Withdrawal amount must be positive");
}
if (balance < amount) {
if (amount > balance) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.codedifferently.lesson17.bank;

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

public class SavingsAccount {
private final Set<Customer> owners;
private final String accountNumber;
private double balance;
private boolean isActive;

/**
* Represents a savings account.
*
* <p>It is a subclass of BankAccount and inherits its properties and methods.
*/
public SavingsAccount(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;
}

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

/**
* 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");
}
}

/**
* 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 SavingsAccount 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
Expand Up @@ -27,6 +27,7 @@ void setUp() {
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
customer1.addAccount(account1);
customer1.addAccount(account2);
customer2.addAccount(account1);
customer2.addAccount(account2);
classUnderTest.addAccount(account1);
classUnderTest.addAccount(account2);
Expand Down Expand Up @@ -74,7 +75,7 @@ void testDepositFunds_Check() {
classUnderTest.depositFunds("987654321", check);

// Assert
assertThat(account1.getBalance()).isEqualTo(0);
assertThat(account1.getBalance()).isEqualTo(0.0);
assertThat(account2.getBalance()).isEqualTo(300.0);
}

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

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.Test;

public class BusinessCheckingAccountTest {
@Test
public void testAddBusinessCheckingAccountWithBusinessOwner() {

BankAtm atm = new BankAtm();

UUID customerId = UUID.randomUUID();

BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1");
Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1");

Set<Customer> owners = new HashSet<>();
owners.add(businessCustomer);
owners.add(personalCustomer);

BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("12345", owners, 2000.00);
atm.addAccount(businessAccount);
assertNotNull((atm.findAccountsByCustomerId(customerId)));
}

@Test
public void testAddBusinessCheckingAccountWithoutBusinessOwner() {

BankAtm atm = new BankAtm();

Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1");
Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2");

Set<Customer> owners = new HashSet<>();
owners.add(personalCustomer1);
owners.add(personalCustomer2);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> {
BusinessCheckingAccount businessAccount =
new BusinessCheckingAccount("12345", owners, 2000.00);
atm.addAccount(businessAccount);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codedifferently.lesson17.bank;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Set;
import org.junit.jupiter.api.Test;

public class SavingsAccountTest {
@Test
void testSavingsAccountCreation() {
Set<Customer> owners = Set.of(new Customer(null, "John Doe"));
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0);

assertEquals("123456789", savingsAccount.getAccountNumber());
assertEquals(owners, savingsAccount.getOwners());
assertEquals(1000.0, savingsAccount.getBalance());
}

@Test
void testDepositFunds() {
Set<Customer> owners = Set.of(new Customer(null, "John Doe"));
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0);

savingsAccount.deposit(500.0);
assertEquals(1500.0, savingsAccount.getBalance());
}
}