Skip to content

Commit 299543b

Browse files
fix: adjusted so SavingsAccount can withdraw and reverting Check constructor back to only take CheckingAccount
1 parent 910977e commit 299543b

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codedifferently.lesson17.bank;
22

3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
34
import java.util.Set;
45

56
public abstract class BankAccount {
@@ -55,7 +56,24 @@ public void deposit(double amount) throws IllegalStateException {
5556
balance += amount;
5657
}
5758

58-
public abstract void withdraw(double amount) throws Exception;
59+
/**
60+
* Withdraws funds from the account.
61+
*
62+
* @param amount
63+
* @throws InsufficientFundsException
64+
*/
65+
public void withdraw(double amount) throws InsufficientFundsException {
66+
if (isClosed()) {
67+
throw new IllegalStateException("Cannot withdraw from a closed account");
68+
}
69+
if (amount <= 0) {
70+
throw new IllegalStateException("Withdrawal amount must be positive");
71+
}
72+
if (balance < amount) {
73+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
74+
}
75+
balance -= amount;
76+
}
5977

6078
/**
6179
* Gets the balance of the account.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public class Check {
1717
* @param amount The amount of the check.
1818
* @param account The account the check is drawn on.
1919
*/
20-
public Check(String checkNumber, double amount, BankAccount account) {
21-
if (account instanceof SavingsAccount) {
22-
throw new IllegalArgumentException("Cannot write a check from a savings account");
23-
}
20+
public Check(String checkNumber, double amount, CheckingAccount account) {
2421
if (amount < 0) {
2522
throw new IllegalArgumentException("Check amount must be positive");
2623
}
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
43
import java.util.Set;
54

65
/** Represents a checking account. */
@@ -16,23 +15,4 @@ public class CheckingAccount extends BankAccount {
1615
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
1716
super(accountNumber, owners, initialBalance);
1817
}
19-
20-
/**
21-
* Withdraws funds from the account.
22-
*
23-
* @param amount
24-
* @throws InsufficientFundsException
25-
*/
26-
public void withdraw(double amount) throws InsufficientFundsException {
27-
if (isClosed()) {
28-
throw new IllegalStateException("Cannot withdraw from a closed account");
29-
}
30-
if (amount <= 0) {
31-
throw new IllegalStateException("Withdrawal amount must be positive");
32-
}
33-
if (balance < amount) {
34-
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
35-
}
36-
balance -= amount;
37-
}
3818
}
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
43
import java.util.Set;
54

65
/** Represents a savings account. */
@@ -16,14 +15,4 @@ public class SavingsAccount extends BankAccount {
1615
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
1716
super(accountNumber, owners, initialBalance);
1817
}
19-
20-
/**
21-
* Withdraws funds from the account.
22-
*
23-
* @param amount
24-
* @throws InsufficientFundsException
25-
*/
26-
public void withdraw(double amount) {
27-
throw new UnsupportedOperationException("Withdrawals are not allowed from savings accounts");
28-
}
2918
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
class BankAtmTest {
1616

1717
private BankAtm classUnderTest;
18-
private BankAccount checkingAccount1;
19-
private BankAccount checkingAccount2;
20-
private BankAccount savingsAccount1;
21-
private BankAccount savingsAccount2;
18+
private CheckingAccount checkingAccount1;
19+
private CheckingAccount checkingAccount2;
20+
private SavingsAccount savingsAccount1;
21+
private SavingsAccount savingsAccount2;
2222
private Customer customer1;
2323
private Customer customer2;
2424

@@ -49,8 +49,8 @@ void setUp() {
4949
void testAddAccount() {
5050
// Arrange
5151
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false);
52-
BankAccount checkingAccount3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
53-
BankAccount savingsAccount3 = new SavingsAccount("666666666", Set.of(customer3), 300.0);
52+
CheckingAccount checkingAccount3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
53+
SavingsAccount savingsAccount3 = new SavingsAccount("666666666", Set.of(customer3), 300.0);
5454
customer3.addAccount(checkingAccount3);
5555
customer3.addAccount(savingsAccount3);
5656

@@ -124,9 +124,6 @@ void testDepositFunds_Check() throws Exception {
124124
// Assert
125125
assertThat(checkingAccount1.getBalance()).isEqualTo(0);
126126
assertThat(checkingAccount2.getBalance()).isEqualTo(300.0);
127-
assertThatThrownBy(() -> new Check("88888", 100.0, savingsAccount1))
128-
.isInstanceOf(IllegalArgumentException.class)
129-
.hasMessage("Cannot write a check from a savings account");
130127
}
131128

132129
@Test
@@ -144,9 +141,11 @@ void testDepositFunds_DoesntDepositCheckTwice() throws Exception {
144141
void testWithdrawFunds() throws Exception {
145142
// Act
146143
classUnderTest.withdrawFunds(checkingAccount2.getAccountNumber(), 50.0);
144+
classUnderTest.withdrawFunds(savingsAccount2.getAccountNumber(), 50.0);
147145

148146
// Assert
149147
assertThat(checkingAccount2.getBalance()).isEqualTo(150.0);
148+
assertThat(savingsAccount2.getBalance()).isEqualTo(150.0);
150149
}
151150

152151
@Test

0 commit comments

Comments
 (0)