Skip to content

feat: adds Kimberlee's Lesson 17-Applying SOLID Principles: Enhancing an ATM Simulator #546

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 15 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 @@ -12,6 +12,22 @@ public class BankAtm {
private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();

public void addAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
boolean hasBusinessOwner = owners.stream().anyMatch(Customer::isBusiness);

if (hasBusinessOwner) {
accountByNumber.put(
accountNumber, new BusinessCheckingAccount(accountNumber, owners, initialBalance));
} else {
accountByNumber.put(
accountNumber, new CheckingAccount(accountNumber, owners, initialBalance));
}
}

public Map<String, CheckingAccount> getAccountByNumber() {
return accountByNumber;
}

/**
* Adds a checking account to the bank.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public class BusinessCheckingAccount extends CheckingAccount {

public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double balance) {
super(accountNumber, owners, balance);
validateBusinessOwners(owners);
}

private void validateBusinessOwners(Set<Customer> owners) {
boolean hasBusinessOwner = owners.stream().anyMatch(Customer::isBusiness);
if (!hasBusinessOwner) {
throw new IllegalArgumentException(
"At least one owner must be a business for a BusinessCheckingAccount");
}
}

@Override
public String toString() {
return "BusinessCheckingAccount{"
+ "accountNumber='"
+ getAccountNumber()
+ '\''
+ ", balance="
+ getBalance()
+ ", isActive="
+ isClosed()
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ public class Customer {
private final UUID id;
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();
private boolean isBusiness;

/**
* Creates a new customer.
*
* @param id The ID of the customer.
* @param name The name of the customer.
*/
public Customer(UUID id, String name) {
public Customer(UUID id, String name, boolean isBusiness) {
this.id = id;
this.name = name;
this.isBusiness = isBusiness;
}

/**
Expand Down Expand Up @@ -75,4 +77,8 @@ public boolean equals(Object obj) {
public String toString() {
return "Customer{" + "id=" + id + ", name='" + name + '\'' + '}';
}

public boolean isBusiness() {
return isBusiness;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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;

/**
* Creates a new savings account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
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;
}

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

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

public class IllegalArgumentException extends RuntimeException {

public IllegalArgumentException() {}

public IllegalArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.*;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -17,25 +19,27 @@ class BankAtmTest {
private CheckingAccount account2;
private Customer customer1;
private Customer customer2;
private BankAtm bankAtm;

@BeforeEach
void setUp() {
classUnderTest = new BankAtm();
customer1 = new Customer(UUID.randomUUID(), "John Doe");
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
customer1 = new Customer(UUID.randomUUID(), "John Doe", false);
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false);
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
customer1.addAccount(account1);
customer1.addAccount(account2);
customer2.addAccount(account2);
classUnderTest.addAccount(account1);
classUnderTest.addAccount(account2);
bankAtm = new BankAtm();
}

@Test
void testAddAccount() {
// Arrange
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false);
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
customer3.addAccount(account3);

Expand Down Expand Up @@ -107,4 +111,38 @@ void testWithdrawFunds_AccountNotFound() {
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
.withMessage("Account not found");
}

@Test
void addAccount_withNoBusinessOwner_shouldReturnStandardCheckingAccount() {
String accountNumber = "123456789";
double initialBalance = 100.0;

Set<Customer> owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "Individual Owner", false));

bankAtm.addAccount(accountNumber, owners, initialBalance);
CheckingAccount account = bankAtm.getAccountByNumber().get(accountNumber);

assertNotNull(account, "Account should be created and added to map.");
assertFalse(
account instanceof BusinessCheckingAccount,
"Account should be a standard CheckingAccount.");
}

@Test
void addAccount_withBusinessOwner_shouldReturnBusinessCheckingAccount() {
String accountNumber = "987654321";
double initialBalance = 200.0;

Set<Customer> owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "Business Owner", true));

bankAtm.addAccount(accountNumber, owners, initialBalance);

CheckingAccount account = bankAtm.getAccountByNumber().get(accountNumber);

assertNotNull(account, "Account should be created and added to map.");
assertTrue(
account instanceof BusinessCheckingAccount, "Account should be a BusinessCheckingAccount.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.codedifferently.lesson17.bank;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.*;

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

public class BusinessCheckingAccountTest {

private Set<Customer> owners;

@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "John Doe", false));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
owners.add(new Customer(UUID.randomUUID(), "Don Juan DeMarco", true));
new BusinessCheckingAccount("123456789", owners, 100.0);
}

@Test
void businessCheckingAccount_withNoBusinessOwner_shouldThrowException() {
Set<Customer> owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "Individual Owner", false));

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BusinessCheckingAccount("123456789", owners, 100.0))
.withMessage("At least one owner must be a business for a BusinessCheckingAccount");
}

@Test
void businessCheckingAccount_withBusinessOwner_shouldCreateAccount() {
Set<Customer> owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "Business Owner", true));

BusinessCheckingAccount account = new BusinessCheckingAccount("123456789", owners, 100.0);
assertEquals("123456789", account.getAccountNumber());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class CheckingAccountTest {
@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
owners.add(new Customer(UUID.randomUUID(), "John Doe", false));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
classUnderTest = new CheckingAccount("123456789", owners, 100.0);
}

Expand Down
Loading