Skip to content

Fix: lesson17 upload files #538

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 18 commits into from
Closed
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,132 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a checking account. */
public class BankAccount {

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

/**
* Creates a new checking account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
// Constructor
public BankAccount(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 BankAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

@Override
public String toString() {
return "BankAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -5,19 +5,20 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/** Represents a bank ATM. */
public class BankAtm {

private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
private final Map<String, BankAccount> accountByNumber = new HashMap<>();

/**
* Adds a checking account to the bank.
* Adds an account to the bank.
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
public void addAccount(BankAccount account) {
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
@@ -33,9 +34,9 @@ public void addAccount(CheckingAccount account) {
* @param customerId The ID of the customer.
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
return customerById.containsKey(customerId)
? customerById.get(customerId).getAccounts()
? customerById.get(customerId).getAccounts().stream().collect(Collectors.toSet())
: Set.of();
}

@@ -46,7 +47,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
* @param amount The amount to deposit.
*/
public void depositFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.deposit(amount);
}

@@ -56,8 +57,8 @@ public void depositFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
public void depositFunds(String accountNumber, Check check) throws Exception {
BankAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
}

@@ -67,8 +68,8 @@ public void depositFunds(String accountNumber, Check check) {
* @param accountNumber
* @param amount
*/
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
public void withdrawFunds(String accountNumber, double amount) throws Exception {
BankAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
}

@@ -78,8 +79,8 @@ public void withdrawFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @return The account.
*/
private CheckingAccount getAccountOrThrow(String accountNumber) {
CheckingAccount account = accountByNumber.get(accountNumber);
private BankAccount getAccountOrThrow(String accountNumber) {
BankAccount account = accountByNumber.get(accountNumber);
if (account == null || account.isClosed()) {
throw new AccountNotFoundException("Account not found");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

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

public class BusinessAccount extends CheckingAccount {

/**
* Creates a new business account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public BusinessAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}

/**
* Withdraws funds from the account.
*
* @param amount The amount to withdraw.
* @throws InsufficientFundsException If there are insufficient funds in the account.
*/
@Override
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > getBalance()) {
throw new InsufficientFundsException("Insufficient funds in the account.");
}
super.withdraw(amount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.codedifferently.lesson17.bank;

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

/**
* Creates a business account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public class BusinessCheckingAccount extends CheckingAccount {
public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);

boolean hasBusinessOwner = owners.stream().anyMatch(customer -> customer.isBusiness());
if (!hasBusinessOwner) {
throw new InvalidBusinessAccountException("At least one owner must be a business.");
}
}
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

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

/** Represents a check. */
public class Check {

private final String checkNumber;
@@ -44,8 +43,9 @@ public void voidCheck() {
* Deposits the check into an account.
*
* @param toAccount The account to deposit the check into.
* @throws Exception
*/
public void depositFunds(CheckingAccount toAccount) {
public void depositFunds(BankAccount toAccount) throws Exception {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a checking account. */
public class CheckingAccount {
public class CheckingAccount extends BankAccount {

private final Set<Customer> owners;
private final String accountNumber;
@@ -19,103 +18,13 @@ public class CheckingAccount {
* @param initialBalance The initial balance of the account.
*/
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, 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 CheckingAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

@Override
public String toString() {
return "CheckingAccount{"
Original file line number Diff line number Diff line change
@@ -7,19 +7,25 @@
/** Represents a customer of the bank. */
public class Customer {

static boolean isBusinessStatic() {
throw new UnsupportedOperationException("Not supported yet.");
}

private final UUID id;
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();
private final Set<BankAccount> accounts = new HashSet<>();
private final 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;
}

/**
@@ -41,11 +47,20 @@ public String getName() {
}

/**
* Adds a checking account to the customer.
* Adds a checking account to the customer. Checks if this is a business
*
* @return True if this is a business, otherwise false.
*/
public boolean isBusiness() {
return isBusiness;
}

/**
* Adds an account to the customer.
*
* @param account The account to add.
* @param checkingAccount2 The account to add.
*/
public void addAccount(CheckingAccount account) {
public void addAccount(BankAccount account) {
accounts.add(account);
}

@@ -54,7 +69,7 @@ public void addAccount(CheckingAccount account) {
*
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> getAccounts() {
public Set<BankAccount> getAccounts() {
return accounts;
}

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

import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Represents a saving account. */
public class SavingsAccount extends BankAccount {
// private boolean isActive;
private final boolean isCheckCreationAllowed;
private static final Logger logger = LoggerFactory.getLogger(SavingsAccount.class);

/**
* Creates a new saving 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 balance) {
super(accountNumber, owners, balance);
logger.info("Saving Account constructor accessed...");
isCheckCreationAllowed = false;
}

public boolean isActive() {
// Logic to determine if the account is active
return true; // Replace with actual logic
}

@Override
public String toString() {
return "SavingsAccount{"
+ "accountNumber='"
+ getAccountNumber()
+ '\''
+ ", balance="
+ getBalance()
+ ", isActive="
+ isActive()
+ '}';
}

public boolean isCheckCreationAllowed() {
return isCheckCreationAllowed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson17.bank.exceptions;

public class InvalidBusinessAccountException extends RuntimeException {
public InvalidBusinessAccountException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -2,9 +2,10 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
@@ -13,95 +14,123 @@
class BankAtmTest {

private BankAtm classUnderTest;
private CheckingAccount account1;
private CheckingAccount account2;
private CheckingAccount checkingAccount1;
private CheckingAccount checkingAccount2;
private SavingsAccount savingsAccount1;
private SavingsAccount savingsAccount2;
private Customer customer1;
private Customer customer2;

@BeforeEach
void setUp() {
classUnderTest = new BankAtm();
customer1 = new Customer(UUID.randomUUID(), "John Doe");
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
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);
customer1 = new Customer(UUID.randomUUID(), "John Doe", false);
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false);

checkingAccount1 = new CheckingAccount("12345", Set.of(customer1), 100.0);
checkingAccount2 = new CheckingAccount("54321", Set.of(customer1, customer2), 200.0);
savingsAccount1 = new SavingsAccount("56789", Set.of(customer1), 100.0);
savingsAccount2 = new SavingsAccount("98765", Set.of(customer1, customer2), 200.0);
customer1.addAccount(checkingAccount1);
customer1.addAccount(checkingAccount2);
customer1.addAccount(savingsAccount1);
customer1.addAccount(savingsAccount2);
classUnderTest.addAccount(checkingAccount1);
classUnderTest.addAccount(checkingAccount2);
classUnderTest.addAccount(savingsAccount1);
classUnderTest.addAccount(savingsAccount2);
}

@Test
void testAddAccount() {
// Arrange
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
customer3.addAccount(account3);
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false);
CheckingAccount checkingAccount3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
SavingsAccount savingsAccount3 = new SavingsAccount("666666666", Set.of(customer3), 400.0);
customer3.addAccount(checkingAccount3);
customer3.addAccount(savingsAccount3);
classUnderTest.addAccount(checkingAccount3);
classUnderTest.addAccount(savingsAccount3);
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
assertThat(accounts).contains(checkingAccount3);
assertThat(accounts).contains(savingsAccount3);
}

@Test
void testBusinessCheckingAccount_WithBusinessOwner_Succeeds() {
// Arrange
Customer businessCustomer = new Customer(UUID.randomUUID(), "Business LLC", true);
Customer individualCustomer = new Customer(UUID.randomUUID(), "Regular Joe", false);
Set<Customer> owners = Set.of(businessCustomer, individualCustomer);

// Act
classUnderTest.addAccount(account3);
BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("BUS123", owners, 1000.0);

// Assert
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
assertThat(accounts).containsOnly(account3);
assertThat(businessAccount.getBalance()).isEqualTo(1000.0);
assertThat(businessAccount.getOwners()).contains(businessCustomer);
}

@Test
void testFindAccountsByCustomerId() {
// Act
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
void testBusinessCheckingAccount_WithoutBusinessOwner_ThrowsException() {
// Arrange
Customer c1 = new Customer(UUID.randomUUID(), "Person One", false);
Customer c2 = new Customer(UUID.randomUUID(), "Person Two", false);
Set<Customer> owners = Set.of(c1, c2);

// Assert
assertThat(accounts).containsOnly(account1, account2);
// Act & Assert
assertThatThrownBy(() -> new BusinessCheckingAccount("BUS999", owners, 500.0))
.isInstanceOf(InvalidBusinessAccountException.class)
.hasMessage("At least one owner must be a business.");
}

@Test
void testFindAccountsByCustomerId() {
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
assertThat(accounts).contains(checkingAccount1, checkingAccount2);
assertThat(accounts).contains(savingsAccount1, savingsAccount2);
}

@Test
void testDepositFunds() {
// Act
classUnderTest.depositFunds(account1.getAccountNumber(), 50.0);

// Assert
assertThat(account1.getBalance()).isEqualTo(150.0);
classUnderTest.depositFunds(checkingAccount1.getAccountNumber(), 50.0);
classUnderTest.depositFunds(savingsAccount1.getAccountNumber(), 50.0);
assertThat(checkingAccount1.getBalance()).isEqualTo(150.0);
assertThat(savingsAccount1.getBalance()).isEqualTo(150.0);
}

@Test
void testDepositFunds_Check() {
void testDepositFunds_Check() throws Exception {
// Arrange
Check check = new Check("987654321", 100.0, account1);

// Act
classUnderTest.depositFunds("987654321", check);

// Assert
assertThat(account1.getBalance()).isEqualTo(0);
assertThat(account2.getBalance()).isEqualTo(300.0);
Check check = new Check("12345", 100.0, checkingAccount1);
classUnderTest.depositFunds("54321", check);
assertThat(checkingAccount1.getBalance()).isEqualTo(0);
assertThat(checkingAccount2.getBalance()).isEqualTo(300.0);
}

@Test
void testDepositFunds_DoesntDepositCheckTwice() {
Check check = new Check("987654321", 100.0, account1);

classUnderTest.depositFunds("987654321", check);

assertThatExceptionOfType(CheckVoidedException.class)
.isThrownBy(() -> classUnderTest.depositFunds("987654321", check))
.withMessage("Check is voided");
void testDepositFunds_DoesntDepositCheckTwice() throws Exception {
Check check = new Check("987654321", 100.0, checkingAccount1);
classUnderTest.depositFunds("54321", check);
assertThatThrownBy(() -> classUnderTest.depositFunds("54321", check))
.hasMessage("Check is voided");
}

@Test
void testWithdrawFunds() {
void testWithdrawFunds() throws Exception {
// Act
classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0);
classUnderTest.withdrawFunds(checkingAccount2.getAccountNumber(), 50.0);
classUnderTest.withdrawFunds(savingsAccount2.getAccountNumber(), 50.0);

// Assert
assertThat(account2.getBalance()).isEqualTo(150.0);
assertThat(checkingAccount2.getBalance()).isEqualTo(150.0);
assertThat(savingsAccount2.getBalance()).isEqualTo(150.0);
}

@Test
void testWithdrawFunds_AccountNotFound() {
String nonExistingAccountNumber = "999999999";

// Act & Assert
assertThatExceptionOfType(AccountNotFoundException.class)
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
Original file line number Diff line number Diff line change
@@ -1,78 +1,105 @@
package com.codedifferently.lesson17.bank;

import static org.assertj.core.api.Assertions.assertThat;
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.CheckVoidedException;
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;

class CheckTest {

private CheckingAccount account1;
private CheckingAccount account2;
private Check classUnderTest;
private CheckingAccount classUnderTest;
private Set<Customer> owners;

@BeforeEach
void setUp() {
account1 = new CheckingAccount("123456789", null, 100.0);
account2 = new CheckingAccount("987654321", null, 200.0);
classUnderTest = new Check("123456789", 50.0, account1);
owners = new HashSet<>();
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);
}

@Test
void testDepositFunds() {
// Act
classUnderTest.depositFunds(account2);
void getAccountNumber() {
assertEquals("123456789", classUnderTest.getAccountNumber());
}

// Assert
assertThat(account1.getBalance()).isEqualTo(50.0);
assertThat(account2.getBalance()).isEqualTo(250.0);
@Test
void getOwners() {
assertEquals(owners, classUnderTest.getOwners());
}

@Test
void testDepositFunds_CheckVoided() {
// Arrange
classUnderTest.voidCheck();

// Act & Assert
assertThatExceptionOfType(CheckVoidedException.class)
.isThrownBy(() -> classUnderTest.depositFunds(account2))
.withMessage("Check is voided");
void deposit() {
classUnderTest.deposit(50.0);
assertEquals(150.0, classUnderTest.getBalance());
}

@Test
void testConstructor_CantCreateCheckWithNegativeAmount() {
// Act & Assert
void deposit_withNegativeAmount() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new Check("123456789", -50.0, account1))
.withMessage("Check amount must be positive");
.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 testHashCode() {
// Arrange
Check otherCheck = new Check("123456789", 100.0, account1);
void closeAccount_withPositiveBalance() {
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> classUnderTest.closeAccount());
}

// Assert
assertThat(classUnderTest.hashCode()).isEqualTo(otherCheck.hashCode());
@Test
void isClosed() {
assertFalse(classUnderTest.isClosed());
classUnderTest.withdraw(100);
classUnderTest.closeAccount();
assertTrue(classUnderTest.isClosed());
}

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

@Test
void testEquals() {
// Arrange
Check otherCheck = new Check("123456789", 100.0, account1);
Check differentCheck = new Check("987654321", 100.0, account1);

// Assert
assertThat(classUnderTest.equals(otherCheck)).isTrue();
assertThat(classUnderTest.equals(differentCheck)).isFalse();
void hashCodeTest() {
CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0);
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode());
}

@Test
void testToString() {
// Assert
assertThat(classUnderTest.toString())
.isEqualTo("Check{checkNumber='123456789', amount=50.0, account=123456789}");
void toStringTest() {
String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}";
assertEquals(expected, classUnderTest.toString());
}
}
Original file line number Diff line number Diff line change
@@ -13,15 +13,14 @@
import org.junit.jupiter.api.Test;

class CheckingAccountTest {

private CheckingAccount 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"));
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);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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;

class SavingAccountTest {

private SavingsAccount classUnderTest;
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));
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() {
CheckingAccount otherAccount = new CheckingAccount("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());
}

@Test
public void testIsCheckCreationAllowed_WhenFalse() {
// Arrange:

// Act:
boolean result = classUnderTest.isCheckCreationAllowed();

// Assert:
assertFalse(
result, "Check creation should not be allowed when isCheckCreationAllowed is false");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.codedifferently.lesson17.bank;

public class SavingAccount {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.codedifferently.lesson17.bank;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

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 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 = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}";
assertEquals(expected, classUnderTest.toString());
}

@Test
void writeCheck() {
// Arrange
Check check = new Check("987654321", 50.0, classUnderTest, classUnderTest);

// Act
classUnderTest.writeCheck(check);

// Assert
assertEquals(50.0, classUnderTest.getBalance());
assertTrue(check.getIsVoided());

}
}