Skip to content

feat:Adds Dasias SavingAccount Method Extension & Test #541

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
Expand Up @@ -10,6 +10,7 @@ public class Customer {
private final UUID id;
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();
private boolean hasBusinessOwner;

/**
* Creates a new customer.
Expand All @@ -20,6 +21,7 @@ public class Customer {
public Customer(UUID id, String name) {
this.id = id;
this.name = name;
this.hasBusinessOwner = false;
}

/**
Expand Down Expand Up @@ -71,6 +73,18 @@ public boolean equals(Object obj) {
return false;
}

public void changeToBusinessAccount() {
hasBusinessOwner = true;
}

public boolean hasBusinessOwner() {
return false;
}

public boolean getHasBusinessOwner() {
return true;
}

@Override
public String toString() {
return "Customer{" + "id=" + id + ", name='" + name + '\'' + '}';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public class SavingAccount extends CheckingAccount {
public SavingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.codedifferently.lesson17.bank.exceptions;

import com.codedifferently.lesson17.bank.CheckingAccount;
import com.codedifferently.lesson17.bank.Customer;
import java.util.Set;

public class BusinessCheckingAccount extends CheckingAccount {
public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
if (!hasBusinessOwner(owners)) {
throw new IllegalArgumentException(
"A business account requires at least one owner to be a business.");
}
}

private boolean hasBusinessOwner(Set<Customer> owners) {
return owners.stream()
.anyMatch(
owner ->
owner.getName().toUpperCase().contains("INC")
|| owner.getName().toUpperCase().contains("LLC")
|| owner.getName().toUpperCase().contains("CO."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.codedifferently.lesson17.bank;

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

import com.codedifferently.lesson17.bank.exceptions.BusinessCheckingAccount;
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 BusinessCheckingAccount classUnderTest;
private Set<Customer> owners;

@BeforeEach
void setUp() {
owners = new HashSet<>();
}

@Test
void testHasBusinessOwner_withINCInTheName() {
UUID id = UUID.randomUUID();
Customer businessOwner = new Customer(id, "Company INC");
owners.add(businessOwner);

BusinessCheckingAccount account = new BusinessCheckingAccount("123456", owners, 200);
assertNotNull(account);
assertEquals("123456", account.getAccountNumber());
assertEquals(200, account.getBalance());
}

@Test
void testHasBusinessOwner_withLLCInTheName() {
UUID id = UUID.randomUUID();
Customer businessOwner = new Customer(id, "Company ILLC");
owners.add(businessOwner);

BusinessCheckingAccount account = new BusinessCheckingAccount("123456", owners, 200);
assertNotNull(account);
assertEquals("123456", account.getAccountNumber());
assertEquals(200, account.getBalance());
}

@Test
void testHasBusinessOwner_withCOInTheName() {
UUID id = UUID.randomUUID();
Customer businessOwner = new Customer(id, "Company CO.");
owners.add(businessOwner);

BusinessCheckingAccount account = new BusinessCheckingAccount("123456", owners, 200);
assertNotNull(account);
assertEquals("123456", account.getAccountNumber());
assertEquals(200, account.getBalance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 SavingAccountTest {
private SavingAccount classUnderTest;
private Set<Customer> owners;

@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
classUnderTest = new SavingAccount("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() {
SavingAccount otherAccount = new SavingAccount("123456789", owners, 200.0);
assertEquals(classUnderTest, otherAccount);
}

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

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