Skip to content

Commit d018398

Browse files
committed
feat: add money order feature for Davis lesson 17 with better organized code and all test passing
1 parent 004854d commit d018398

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/** Represents a account. */
77
abstract class BankAccount {
88

9-
protected final Set<Customer> owners;
9+
protected static Set<Customer> owners = null;
1010
protected final String accountNumber;
1111
protected double balance;
1212
protected boolean isActive;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class CheckingAccount extends BankAccount {
66

77
/**
8-
* Creates a new saving account.
8+
* Creates a new checking account.
99
*
1010
* @param accountNumber The account number.
1111
* @param owners The owners of the account.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/** Represents a money order. */
4+
public class MoneyOrder {
5+
6+
private final String orderNumber;
7+
private final double amount;
8+
private final BankAccount sourceAccount;
9+
private boolean isRedeemed = false;
10+
11+
/**
12+
* Creates a new money order and immediately withdraws funds.
13+
*
14+
* @param orderNumber The money order number.
15+
* @param amount The amount of the money order.
16+
* @param sourceAccount The account the money order is drawn from.
17+
*/
18+
public MoneyOrder(String orderNumber, double amount, BankAccount sourceAccount) {
19+
if (amount < 0) {
20+
throw new IllegalArgumentException("Money order amount must be positive");
21+
}
22+
this.orderNumber = orderNumber;
23+
this.amount = amount;
24+
this.sourceAccount = sourceAccount;
25+
26+
// Immediately withdraw funds
27+
sourceAccount.withdraw(amount);
28+
}
29+
30+
/** Checks if the money order has already been redeemed. */
31+
public boolean isRedeemed() {
32+
return isRedeemed;
33+
}
34+
35+
/**
36+
* Redeems the money order to another account.
37+
*
38+
* @param toAccount The account receiving the funds.
39+
*/
40+
public void redeem(BankAccount toAccount) {
41+
if (isRedeemed) {
42+
throw new IllegalStateException("Money order already redeemed");
43+
}
44+
toAccount.deposit(amount);
45+
isRedeemed = true;
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return orderNumber.hashCode();
51+
}
52+
53+
@Override
54+
public boolean equals(Object obj) {
55+
if (obj instanceof MoneyOrder other) {
56+
return orderNumber.equals(other.orderNumber);
57+
}
58+
return false;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "MoneyOrder{"
64+
+ "orderNumber='"
65+
+ orderNumber
66+
+ '\''
67+
+ ", amount="
68+
+ amount
69+
+ ", sourceAccount="
70+
+ sourceAccount.getAccountNumber()
71+
+ ", redeemed="
72+
+ isRedeemed
73+
+ '}';
74+
}
75+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/** A specialized type of BankAccount representing a source of funds. */
4+
public class SourceAccount extends BankAccount {
5+
6+
/**
7+
* Constructs a SourceAccount with the given account number and initial balance.
8+
*
9+
* @param accountNumber The account number.
10+
* @param initialBalance The starting balance.
11+
*/
12+
public SourceAccount(String accountNumber, double initialBalance) {
13+
super(accountNumber, owners, initialBalance);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
/** A specialized type of BankAccount representing a target (destination) for funds. */
4+
public class TargetAccount extends BankAccount {
5+
6+
/**
7+
* Constructs a TargetAccount with the given account number and initial balance.
8+
*
9+
* @param accountNumber The account number.
10+
* @param initialBalance The starting balance.
11+
*/
12+
public TargetAccount(String accountNumber, double initialBalance) {
13+
super(accountNumber, owners, initialBalance);
14+
}
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
class MoneyOrderTest {
12+
13+
private BankAccount sourceAccount;
14+
private BankAccount targetAccount;
15+
16+
@BeforeEach
17+
void setUp() {
18+
sourceAccount = new SourceAccount("123", 500);
19+
targetAccount = new TargetAccount("456", 100);
20+
}
21+
22+
@Test
23+
void testMoneyOrderWithdrawalOnCreation() {
24+
new MoneyOrder("MO-001", 200, sourceAccount);
25+
assertEquals(300, sourceAccount.getBalance(), 0.001);
26+
}
27+
28+
@Test
29+
void testMoneyOrderRedeem() {
30+
MoneyOrder mo = new MoneyOrder("MO-002", 150, sourceAccount);
31+
mo.redeem(targetAccount);
32+
33+
assertEquals(250, targetAccount.getBalance(), 0.001);
34+
assertTrue(mo.isRedeemed());
35+
}
36+
37+
@Test
38+
void testRedeemAlreadyRedeemedMoneyOrderThrows() {
39+
MoneyOrder mo = new MoneyOrder("MO-003", 100, sourceAccount);
40+
mo.redeem(targetAccount);
41+
42+
IllegalStateException exception =
43+
assertThrows(IllegalStateException.class, () -> mo.redeem(targetAccount));
44+
assertEquals("Money order already redeemed", exception.getMessage());
45+
}
46+
47+
@Test
48+
void testInsufficientFundsOnCreation() {
49+
BankAccount lowFundsAccount = new SourceAccount("789", 50);
50+
assertThrows(
51+
InsufficientFundsException.class, () -> new MoneyOrder("MO-004", 100, lowFundsAccount));
52+
}
53+
}

0 commit comments

Comments
 (0)