Skip to content

Commit 387c5e1

Browse files
committed
Feat: Enhance withdrawal to not allow checks to be written from savings accoount
1 parent 4d25713 commit 387c5e1

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ public void depositFunds(String accountNumber, Check check) {
7373
* @param accountNumber
7474
* @param amount
7575
*/
76-
public void withdrawFunds(String accountNumber, double amount) {
76+
public void withdrawFunds(String accountNumber, double amount, Check check) {
7777
CheckingAccount account = getAccountOrThrow(accountNumber);
7878
account.withdraw(amount);
79+
7980
auditLog.log("Withdrew " + amount + " from account " + accountNumber);
8081
}
8182

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ public void withdraw(double amount) throws InsufficientFundsException {
6868
if (isClosed()) {
6969
throw new IllegalStateException("Cannot withdraw from a closed account");
7070
}
71+
7172
if (amount <= 0) {
7273
throw new IllegalStateException("Withdrawal amount must be positive");
7374
}
75+
7476
if (balance < amount) {
7577
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
7678
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/SavingsAccount.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Set;
44

5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
57
/**
68
* The {@code SavingsAccount} class represents a savings account in a banking system. It extends the
79
* {@code CheckingAccount} class and provides additional functionality specific to savings accounts.
@@ -49,12 +51,22 @@ && getBalance() == other.getBalance()
4951
/**
5052
* Will throw and exception if the user attempt to make a withdrawl while under the instance of
5153
* SavingsAccount.
54+
*
55+
* And the makes a second withdrawl that will check if there
56+
* is a check and throw an error if one comes in.
5257
*
5358
* @param amount The amount to deposit.
5459
*/
5560
@Override
56-
public void withdraw(double amount) {
57-
throw new RuntimeException("Cannot withdraw from a savings account using a check");
61+
public void withdraw(double amount) throws InsufficientFundsException {
62+
throw new IllegalStateException("you cannot write a check from the savings account");
63+
}
64+
65+
public void withdraw(double amount, Check check) {
66+
if (check != null) {
67+
throw new IllegalStateException("Cannot withdraw from a savings account using a check");
68+
}
69+
withdraw(amount); // Call the original withdraw method
5870
}
5971

6072
/**

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.codedifferently.lesson17.bank;
22

3+
import java.util.Set;
4+
import java.util.UUID;
5+
36
import static org.assertj.core.api.Assertions.assertThat;
47
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
510

611
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
712
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
8-
import java.util.Set;
9-
import java.util.UUID;
10-
import org.junit.jupiter.api.BeforeEach;
11-
import org.junit.jupiter.api.Test;
1213

1314
class BankAtmTest {
1415

@@ -92,7 +93,7 @@ void testDepositFunds_DoesntDepositCheckTwice() {
9293
@Test
9394
void testWithdrawFunds() {
9495
// Act
95-
classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0);
96+
classUnderTest.withdrawFunds(account2.getAccountNumber(), 50.0, null);
9697

9798
// Assert
9899
assertThat(account2.getBalance()).isEqualTo(150.0);
@@ -104,7 +105,7 @@ void testWithdrawFunds_AccountNotFound() {
104105

105106
// Act & Assert
106107
assertThatExceptionOfType(AccountNotFoundException.class)
107-
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
108+
.isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0, null))
108109
.withMessage("Account not found");
109110
}
110111
}

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void deposit_withNegativeAmount() {
4848
}
4949

5050
@Test
51-
void withdraw() {
51+
void withdraw() { // Assuming Check has a constructor with id and amount
52+
// Assuming Check has a constructor with id and amount
5253
classUnderTest.withdraw(50.0);
5354
assertEquals(50.0, classUnderTest.getBalance());
5455
}

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codedifferently.lesson17.bank;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.HashSet;
67
import java.util.UUID;
@@ -34,4 +35,18 @@ void testEquals() {
3435
boolean actual = classUnderTest.equals(other);
3536
assertEquals(expected, actual);
3637
}
38+
39+
@Test
40+
void testCheckWithdrawl() {
41+
Check check = new Check("123456789", 1000.0, classUnderTest);
42+
43+
IllegalStateException exception =
44+
assertThrows(
45+
IllegalStateException.class,
46+
() -> {
47+
classUnderTest.withdraw(1000, check);
48+
});
49+
50+
assertEquals("Cannot withdraw from a savings account using a check", exception.getMessage());
51+
}
3752
}

0 commit comments

Comments
 (0)