Skip to content

feat: adds davids savings account to bank atm #651

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,23 +1,24 @@
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<>();
private final Map<String, BankAccount> accountByNumber = new HashMap<>();

/**
* Adds a checking 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()
Expand All @@ -33,7 +34,7 @@ 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()
: Set.of();
Expand All @@ -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);
}

Expand All @@ -57,7 +58,7 @@ public void depositFunds(String accountNumber, double amount) {
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
}

Expand All @@ -68,7 +69,7 @@ public void depositFunds(String accountNumber, Check check) {
* @param amount
*/
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
}

Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

/** Represents a savings account. */
public class SavingAccount extends BankAccount {

/**
* Creates a new savings account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
* @return
*/
public SavingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}
}
Original file line number Diff line number Diff line change
@@ -1,110 +1,96 @@
package com.codedifferently.lesson17.bank;

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

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
class BankAtmTest {

private BankAtm classUnderTest;
private CheckingAccount account1;
private CheckingAccount account2;
private SavingAccount account2;
private Customer customer1;
private Customer customer2;

@BeforeEach
@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);
account2 = new SavingAccount("987654321", Set.of(customer1, customer2), 200.0);
customer1.addAccount(account1);
customer1.addAccount(account2);
customer2.addAccount(account2);
classUnderTest.addAccount(account1);
classUnderTest.addAccount(account2);
}

@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);

// Act
classUnderTest.addAccount(account3);

// Assert
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
assertThat(accounts).containsOnly(account3);
}

@Test
void testFindAccountsByCustomerId() {
// Act
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());

// Assert
assertThat(accounts).containsOnly(account1, account2);
}

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

// Assert
assertThat(account1.getBalance()).isEqualTo(150.0);
}

@Test
void testDepositFunds_Check() {
// 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);
}

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

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

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

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

// Act & Assert
assertThatExceptionOfType(AccountNotFoundException.class)
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
.withMessage("Account not found");
}
}
}
Loading