Skip to content

Feat:lesson_17 implemented savings account and Business Checking Account for Tommy Tran #544

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 6 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,29 @@
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);
if (!hasBusinessOwner(owners)) {
throw new IllegalArgumentException("requires at least one business owner.");
}
}

private boolean hasBusinessOwner(Set<Customer> owners) {
return owners.stream().anyMatch(Customer::isBusiness);
}

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

import java.util.UUID;

public class BusinessOwner extends Customer {

private final String businessName;

public BusinessOwner(UUID id, String name, String businessName) {
super(id, name);
this.businessName = businessName;
}

public String businessName() {
return businessName;
}

@Override
public boolean isBusiness() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public Check(String checkNumber, double amount, CheckingAccount account) {
if (amount < 0) {
throw new IllegalArgumentException("Check amount must be positive");
}
if (account instanceof SavingsAccount) {
throw new UnsupportedOperationException("Cannot issue checks from a savings account.");
}
this.checkNumber = checkNumber;
this.amount = amount;
this.account = account;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
public class CheckingAccount {

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

/**
* Creates a new checking account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public Set<CheckingAccount> getAccounts() {
return accounts;
}

// defaults accounts to non-business Owners.
public boolean isBusiness() {
return false;
}

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

import java.util.Set;

// represents a savings account
public class SavingsAccount extends CheckingAccount {

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

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

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

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 Customer regularCustomer;
private BusinessOwner BusinessCustomer;
private Set<Customer> owners;

@BeforeEach
void setUp() {
regularCustomer = new Customer(UUID.randomUUID(), "james");
BusinessCustomer = new BusinessOwner(UUID.randomUUID(), "john", "long johns");
owners = new HashSet<>();
}

@Test
void hasBusinessOwnerTest() {
owners.add(regularCustomer);

IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> new BusinessCheckingAccount("12354", owners, 100.00),
"Expect Constructor to throw but did not");

assertTrue(thrown.getMessage().contains("requires at least one business owner"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.codedifferently.lesson17.bank;

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

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"));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
classUnderTest = new SavingsAccount("123456789", owners, 100.0);
}

@Test
void toStringTest() {
// arrange
String expected = "SavingsAccount{accountNumber='123456789', balance=100.0, isActive=true}";

// assert
assertEquals(expected, classUnderTest.toString());
}
}