Skip to content

Commit 24822ef

Browse files
committed
feat: add money order and savings account files & tests
1 parent b5b5d6c commit 24822ef

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
4+
import com.codedifferently.lesson17.bank.exceptions.MoneyOrderVoidedException;
5+
6+
/** Represents a check. */
7+
public class MoneyOrder {
8+
9+
private final String moneyOrderNumber;
10+
private final double amount;
11+
private final CheckingAccount account;
12+
private boolean isVoided = false;
13+
14+
/**
15+
* Creates a new check.
16+
*
17+
* @param moneyOrderNumber The check number.
18+
* @param amount The amount of the check.
19+
* @param account The account the check is drawn on.
20+
*/
21+
public MoneyOrder(String moneyOrderNumber, double amount, CheckingAccount account) {
22+
if (amount < 0) {
23+
throw new IllegalArgumentException("Money Order amount must be positive");
24+
}
25+
this.moneyOrderNumber = moneyOrderNumber;
26+
this.account = account;
27+
account.withdraw(amount);
28+
this.amount = amount ;
29+
}
30+
31+
/**
32+
* Gets the voided status of the check.
33+
*
34+
* @return True if the check is voided, and false otherwise.
35+
*/
36+
public boolean getIsVoided() {
37+
return isVoided;
38+
}
39+
40+
/** Voids the MoneyOrder. */
41+
public void voidMoneyOrder() {
42+
isVoided = true;
43+
}
44+
45+
/**
46+
* Deposits the money order into an account.
47+
*
48+
* @param toAccount The account to deposit the money order into.
49+
*/
50+
public void depositFunds(CheckingAccount toAccount) {
51+
if (isVoided) {
52+
throw new MoneyOrderVoidedException("Money order is voided");
53+
}
54+
account.withdraw(amount);
55+
toAccount.deposit(amount);
56+
voidMoneyOrder();
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return moneyOrderNumber.hashCode();
62+
}
63+
64+
@Override
65+
public boolean equals(Object obj) {
66+
if (obj instanceof MoneyOrder other) {
67+
return moneyOrderNumber.equals(other.moneyOrderNumber);
68+
}
69+
return false;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "Money Order{"
75+
+ "money Order Number='"
76+
+ moneyOrderNumber
77+
+ '\''
78+
+ ", amount="
79+
+ amount
80+
+ ", account="
81+
+ account.getAccountNumber()
82+
+ '}';
83+
}
84+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Represents a saving account. */
7+
public class SavingAccount {
8+
9+
private final Set<Customer> owners;
10+
private final String accountNumber;
11+
private double balance;
12+
private boolean isActive;
13+
14+
/**
15+
* Creates a new saving account.
16+
*
17+
* @param accountNumber The account number.
18+
* @param owners The owners of the account.
19+
* @param initialBalance The initial balance of the account.
20+
*/
21+
public SavingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
22+
this.accountNumber = accountNumber;
23+
this.owners = owners;
24+
this.balance = initialBalance;
25+
isActive = true;
26+
}
27+
28+
/**
29+
* Gets the account number.
30+
*
31+
* @return The account number.
32+
*/
33+
public String getAccountNumber() {
34+
return accountNumber;
35+
}
36+
37+
/**
38+
* Gets the owners of the account.
39+
*
40+
* @return The owners of the account.
41+
*/
42+
public Set<Customer> getOwners() {
43+
return owners;
44+
}
45+
46+
/**
47+
* Deposits funds into the account.
48+
*
49+
* @param amount The amount to deposit.
50+
*/
51+
public void deposit(double amount) throws IllegalStateException {
52+
if (isClosed()) {
53+
throw new IllegalStateException("Cannot deposit to a closed account");
54+
}
55+
if (amount <= 0) {
56+
throw new IllegalArgumentException("Deposit amount must be positive");
57+
}
58+
balance += amount;
59+
}
60+
61+
/**
62+
* Gets the balance of the account.
63+
*
64+
* @return The balance of the account.
65+
*/
66+
public double getBalance() {
67+
return balance;
68+
}
69+
70+
/** Closes the account. */
71+
public void closeAccount() throws IllegalStateException {
72+
if (balance > 0) {
73+
throw new IllegalStateException("Cannot close account with a positive balance");
74+
}
75+
isActive = false;
76+
}
77+
78+
/**
79+
* Checks if the account is closed.
80+
*
81+
* @return True if the account is closed, otherwise false.
82+
*/
83+
public boolean isClosed() {
84+
return !isActive;
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return accountNumber.hashCode();
90+
}
91+
92+
@Override
93+
public boolean equals(Object obj) {
94+
if (obj instanceof SavingAccount other) {
95+
return accountNumber.equals(other.accountNumber);
96+
}
97+
return false;
98+
}
99+
100+
@Override
101+
public String toString() {
102+
return "SavingAccount{"
103+
+ "accountNumber='"
104+
+ accountNumber
105+
+ '\''
106+
+ ", balance="
107+
+ balance
108+
+ ", isActive="
109+
+ isActive
110+
+ '}';
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codedifferently.lesson17.bank.exceptions;
2+
3+
public class MoneyOrderVoidedException extends RuntimeException {
4+
public MoneyOrderVoidedException() {}
5+
6+
public MoneyOrderVoidedException(String message){
7+
super(message);
8+
}
9+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8+
9+
10+
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
11+
import com.codedifferently.lesson17.bank.exceptions.MoneyOrderVoidedException;
12+
13+
class MoneyOrderTest {
14+
15+
private CheckingAccount account1;
16+
private CheckingAccount account2;
17+
private MoneyOrder classUnderTest;
18+
19+
@BeforeEach
20+
void setUp() {
21+
account1 = new CheckingAccount("123456789", null, 200.0);
22+
account2 = new CheckingAccount("987654321", null, 300.0);
23+
classUnderTest = new MoneyOrder("123456789", 50.0, account1);
24+
}
25+
26+
@Test
27+
void testInitializesProperly(){
28+
// Arrange
29+
double balanceAfterMoneyOrder = account1.getBalance();
30+
31+
// Act - happened upon money order creation
32+
33+
// Assert
34+
assertThat(balanceAfterMoneyOrder).isEqualTo(150.0);
35+
}
36+
37+
@Test
38+
void testDepositFunds() {
39+
// Act
40+
classUnderTest.depositFunds(account2);
41+
42+
// Assert
43+
assertThat(account1.getBalance()).isEqualTo(100.0);
44+
assertThat(account2.getBalance()).isEqualTo(350.0);
45+
}
46+
47+
@Test
48+
void testDepositFunds_MoneyOrderVoided() {
49+
// Arrange
50+
classUnderTest.voidMoneyOrder();
51+
52+
// Act & Assert
53+
assertThatExceptionOfType(MoneyOrderVoidedException.class)
54+
.isThrownBy(() -> classUnderTest.depositFunds(account2))
55+
.withMessage("Money order is voided");
56+
}
57+
58+
@Test
59+
void testConstructor_CantCreateCheckWithNegativeAmount() {
60+
// Act & Assert
61+
assertThatExceptionOfType(IllegalArgumentException.class)
62+
.isThrownBy(() -> new MoneyOrder("123456789", -50.0, account1))
63+
.withMessage("Money Order amount must be positive");
64+
}
65+
66+
@Test
67+
void testHashCode() {
68+
// Arrange
69+
MoneyOrder otherMoneyOrder = new MoneyOrder("123456789", 100.0, account1);
70+
71+
// Assert
72+
assertThat(classUnderTest.hashCode()).isEqualTo(otherMoneyOrder.hashCode());
73+
}
74+
75+
@Test
76+
void testEquals() {
77+
// Arrange
78+
MoneyOrder otherCheck = new MoneyOrder("123456789", 25.0, account1);
79+
MoneyOrder differentCheck = new MoneyOrder("987654321", 25.0, account1);
80+
81+
// Assert
82+
assertThat(classUnderTest.equals(otherCheck)).isTrue();
83+
assertThat(classUnderTest.equals(differentCheck)).isFalse();
84+
}
85+
86+
@Test
87+
void testToString() {
88+
// Assert
89+
assertThat(classUnderTest.toString())
90+
.isEqualTo("Money Order{money Order Number='123456789', amount=50.0, account=123456789}");
91+
}
92+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import java.util.UUID;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
class SavingAccountTest {
15+
16+
private SavingAccount classUnderTest;
17+
private Set<Customer> owners;
18+
19+
@BeforeEach
20+
void setUp() {
21+
owners = new HashSet<>();
22+
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
23+
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
24+
classUnderTest = new SavingAccount("123456789", owners, 100.0);
25+
}
26+
27+
@Test
28+
void getAccountNumber() {
29+
assertEquals("123456789", classUnderTest.getAccountNumber());
30+
}
31+
32+
@Test
33+
void getOwners() {
34+
assertEquals(owners, classUnderTest.getOwners());
35+
}
36+
37+
@Test
38+
void deposit() {
39+
classUnderTest.deposit(50.0);
40+
assertEquals(150.0, classUnderTest.getBalance());
41+
}
42+
43+
@Test
44+
void deposit_withNegativeAmount() {
45+
assertThatExceptionOfType(IllegalArgumentException.class)
46+
.isThrownBy(() -> classUnderTest.deposit(-50.0));
47+
}
48+
49+
@Test
50+
void getBalance() {
51+
assertEquals(100.0, classUnderTest.getBalance());
52+
}
53+
54+
@Test
55+
void closeAccount_withPositiveBalance() {
56+
assertThatExceptionOfType(IllegalStateException.class)
57+
.isThrownBy(() -> classUnderTest.closeAccount());
58+
}
59+
60+
@Test
61+
void equals() {
62+
SavingAccount otherAccount = new SavingAccount("123456789", owners, 200.0);
63+
assertEquals(classUnderTest, otherAccount);
64+
}
65+
66+
@Test
67+
void hashCodeTest() {
68+
SavingAccount otherAccount = new SavingAccount("123456789", owners, 200.0);
69+
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode());
70+
}
71+
72+
@Test
73+
void toStringTest() {
74+
String expected = "SavingAccount{accountNumber='123456789', balance=100.0, isActive=true}";
75+
assertEquals(expected, classUnderTest.toString());
76+
}
77+
}

0 commit comments

Comments
 (0)