Skip to content

feat: implemented Saving account and BusinessCheckingAccount functions and tests lesson17 #554

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
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,6 +18,15 @@ public class BankAtm {
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
if (account instanceof BusinessCheckingAccount) {
// Check if at least one of the owners is a business customer
boolean hasBusinessOwner = account.getOwners().stream().anyMatch(Customer::isBusiness);

if (!hasBusinessOwner) {
throw new IllegalArgumentException(
"At least one owner must be a business customer for a BusinessCheckingAccount.");
}
}
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.codedifferently.lesson17.bank;

public interface BusinessAccount {
boolean isBusiness();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public class BusinessCheckingAccount extends CheckingAccount implements BusinessAccount {

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

@Override
public boolean isBusiness() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class CheckingAccount {

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

Expand Down
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 boolean isBusiness;
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, boolean isBusiness) {
this.id = id;
this.name = name;
this.isBusiness = isBusiness;
}

/**
Expand All @@ -31,6 +33,10 @@ public UUID getId() {
return id;
}

public boolean isBusiness() {
return isBusiness;
}

/**
* Gets the name of the customer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a saving account. */
public class SavingAccount {

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

/**
* 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 SavingAccount(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 SavingAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

@Override
public String toString() {
return "SavingAccount{"
+ "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", false);
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false);
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", false);
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
customer3.addAccount(account3);

Expand Down
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", false));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
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;

class SavingAccountTest {

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