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 1 commit
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
@@ -1,24 +1,27 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;

/** Represents a bank ATM. */
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 All @@ -27,6 +30,8 @@ public void addAccount(CheckingAccount account) {
});
}



/**
* Finds all accounts owned by a customer.
*
Expand Down Expand Up @@ -79,10 +84,12 @@ 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()) {
throw new AccountNotFoundException("Account not found");
CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber);
if (checkingAccount != null && !checkingAccount.isClosed()) {
throw new AccountNotFoundException("Account not foudn");
}
return checkingAccount;
}
return account;

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 CheckingAccount 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
@@ -1,12 +1,13 @@
package com.codedifferently.lesson17.bank;

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

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;

/** 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;
Expand All @@ -20,7 +21,7 @@ public class CheckingAccount {
*/
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
CheckingAccount.owners = owners;
this.balance = initialBalance;
isActive = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;




public class SavingsAccount extends CheckingAccount {

/**
* Represents a savings account.
*
* <p>It is a subclass of CheckingAccount and inherits its properties and methods.
*/



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

public void depositFunds(Check check) {
throw new UnsupportedOperationException("Cannot deposit check into a savings account");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.codedifferently.lesson17;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

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

import com.codedifferently.lesson17.bank.BankAtm;
import com.codedifferently.lesson17.bank.BusinessCheckingAccount;
import com.codedifferently.lesson17.bank.BusinessCustomer;
import com.codedifferently.lesson17.bank.Customer;
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,40 @@
package com.codedifferently.lesson17.bank;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
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());
}

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

assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> savingsAccount.depositFunds(check))
.withMessage("Cannot deposit check into a savings account");
}
}
Loading