Skip to content

Feat : Yafiah Lesson-17 SavingsAccount & BusinessCheckingAccount #549

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
Expand Up @@ -18,13 +18,21 @@ public class BankAtm {
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
if (account instanceof BusinessCheckingAccount) {
Set<Customer> owners = account.getOwners();

// Ensure at least one owner is a business
if (owners.stream().noneMatch(Customer::isBusiness)) {
throw new IllegalArgumentException(
"A BusinessCheckingAccount must have at least one business owner.");
}
}

// Add the account to accountByNumber
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
.forEach(
owner -> {
customerById.put(owner.getId(), owner);
});

// Add each owner to customerById
account.getOwners().forEach(owner -> customerById.put(owner.getId(), owner));
}

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

import java.util.Set;

public class BusinessCheckingAccount extends CheckingAccount {

public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance); // Call to Account's constructor

if (owners.stream().noneMatch(Customer::isBusiness)) {
throw new IllegalArgumentException(
"A BusinessCheckingAccount must have at least one business owner.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Customer {

private final UUID id;
private final String name;
private final CustomerType type;
private final Set<CheckingAccount> accounts = new HashSet<>();

/**
Expand All @@ -17,9 +18,10 @@ public class 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, CustomerType type) {
this.id = id;
this.name = name;
this.type = type;
}

/**
Expand Down Expand Up @@ -58,6 +60,11 @@ public Set<CheckingAccount> getAccounts() {
return accounts;
}

// make sure if the custumer is using a business account
public boolean isBusiness() {
return type == CustomerType.BUSINESS;
}

@Override
public int hashCode() {
return id.hashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.codedifferently.lesson17.bank;

public enum CustomerType {
INDIVIDUAL,
BUSINESS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.codedifferently.lesson17.bank;

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

// represent a savings account
public class SavingsAccount {

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

// consturcter of saving account
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
this.balance = initialBalance;
isActive = true;
}

// getters & setters
public String getAccountNumber() {
return accountNumber;
}

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

public double getBalance() {
return balance;
}

public boolean isClosed() {
return !isActive;
}

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

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

@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
Expand Up @@ -21,8 +21,8 @@ class BankAtmTest {
@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", CustomerType.INDIVIDUAL);
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", CustomerType.INDIVIDUAL);
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
customer1.addAccount(account1);
Expand All @@ -35,7 +35,7 @@ void setUp() {
@Test
void testAddAccount() {
// Arrange
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", CustomerType.INDIVIDUAL);
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
customer3.addAccount(account3);

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

@Test
void testAddBusinessCheckingAccount_WithBusinessOwner() {
// Arrange
Customer businessCustomer = new Customer(UUID.randomUUID(), "TechCorp", CustomerType.BUSINESS);
Set<Customer> owners = Set.of(businessCustomer);

// Act
BusinessCheckingAccount businessAccount =
new BusinessCheckingAccount("BUS123456", owners, 500.0);

// Assert
assertThat(businessAccount.getOwners()).contains(businessCustomer);
}

@Test
void testAddBusinessCheckingAccount_WithoutBusinessOwner() {
// Arrange
Customer individualCustomer =
new Customer(UUID.randomUUID(), "John Doe", CustomerType.INDIVIDUAL);
Set<Customer> owners = Set.of(individualCustomer);

// Act & Assert
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BusinessCheckingAccount("BUS987654", owners, 1000.0))
.withMessage("A BusinessCheckingAccount must have at least one business owner.");
}
}
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", CustomerType.INDIVIDUAL));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", CustomerType.INDIVIDUAL));
classUnderTest = new CheckingAccount("123456789", owners, 100.0);
}

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

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
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 SavingsAccountTest {

private SavingsAccount classUnderTest;
private Set<Customer> owners;

@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "John Doe", CustomerType.INDIVIDUAL));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", CustomerType.INDIVIDUAL));
classUnderTest = new SavingsAccount("123456789", owners, 100.0);
}

@Test
void getAccountNumber() {
assertEquals("123456789", classUnderTest.getAccountNumber());
}

@Test
void getOwners() {
assertEquals(owners, classUnderTest.getOwners());
}

@Test
void deposit() {
classUnderTest.deposit(50.0);
assertEquals(150.0, classUnderTest.getBalance());
}

@Test
void deposit_withNegativeAmount() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> classUnderTest.deposit(-50.0));
}

@Test
void withdraw() {
classUnderTest.withdraw(50.0);
assertEquals(50.0, classUnderTest.getBalance());
}

@Test
void withdraw_withNegativeAmount() {
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> classUnderTest.withdraw(-50.0))
.withMessage("Withdrawal amount must be positive");
}

@Test
void withdraw_withInsufficientBalance() {
assertThatExceptionOfType(InsufficientFundsException.class)
.isThrownBy(() -> classUnderTest.withdraw(150.0))
.withMessage("Account does not have enough funds for withdrawal");
}

@Test
void getBalance() {
assertEquals(100.0, classUnderTest.getBalance());
}

@Test
void closeAccount_withPositiveBalance() {
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> classUnderTest.closeAccount());
}

@Test
void isClosed() {
assertFalse(classUnderTest.isClosed());
classUnderTest.withdraw(100);
classUnderTest.closeAccount();
assertTrue(classUnderTest.isClosed());
}

@Test
void equals() {
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
assertEquals(classUnderTest, otherAccount);
}

@Test
void hashCodeTest() {
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode());
}

@Test
void toStringTest() {
String expected = "SavingsAccount{accountNumber='123456789', balance=100.0, isActive=true}";
assertEquals(expected, classUnderTest.toString());
}
}