Skip to content

Commit a6f21fa

Browse files
committed
feat: adds SavingsAccount Tests
1 parent 04be46f commit a6f21fa

File tree

5 files changed

+104
-8
lines changed

5 files changed

+104
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/** Represents a business checking account */
6+
public class BusinessCheckingAccount extends CheckingAccount {
7+
8+
/**
9+
* Creates a new business checking account
10+
*
11+
* @param accountNumber The accounnt number.
12+
* @param owners The owners of the account.
13+
* @param initialBalance The initial balance of the account.
14+
*/
15+
public BusinessCheckingAccount(
16+
String accountNumber, Set<Customer> owners, double initialBalance) {
17+
super(accountNumber, owners, initialBalance);
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "BusinessCheckingAccount{"
23+
+ "accountNumber='"
24+
+ getAccountNumber()
25+
+ '\''
26+
+ ", balance="
27+
+ getBalance()
28+
+ ", isActive="
29+
+ isActive()
30+
+ '}';
31+
}
32+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Check {
77

88
private final String checkNumber;
99
private final double amount;
10-
private final Account account;
10+
private final CheckingAccount account;
1111
private boolean isVoided = false;
1212

1313
/**
@@ -17,7 +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, Account account) {
20+
public Check(String checkNumber, double amount, CheckingAccount account) {
2121
if (amount < 0) {
2222
throw new IllegalArgumentException("Check amount must be positive");
2323
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Customer {
1919
/**
2020
* Creates a new customer.
2121
*
22-
* @param id The ID of the customer.
22+
* @param id The ID of the customer.
2323
* @param name The name of the customer.
2424
* @param type The type of customer (individual or business)
2525
*/
@@ -32,11 +32,11 @@ public Customer(UUID id, String name, CustomerType type) {
3232
/**
3333
* Creates an new individual customer.
3434
*
35-
* @param id The ID of the customer.
35+
* @param id The ID of the customer.
3636
* @param name The name of the customer.
3737
* @param type The type of custimer (individual or business)
3838
*/
39-
public Customer(UUID id, String name, Customer type) {
39+
public Customer(UUID id, String name) {
4040
this.id = id;
4141
this.name = name;
4242
this.type = CustomerType.INDIVIDUAL;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ void testAddAccount() {
4343
classUnderTest.addAccount(account3);
4444

4545
// Assert
46-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
46+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
4747
assertThat(accounts).containsOnly(account3);
4848
}
4949

5050
@Test
5151
void testFindAccountsByCustomerId() {
5252
// Act
53-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
53+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5454

5555
// Assert
5656
assertThat(accounts).containsOnly(account1, account2);
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
11
package com.codedifferently.lesson17.bank;
22

3-
class CheckingAccountTest {}
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 com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import java.util.UUID;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
class SavingsAccountTest {
16+
17+
private SavingsAccount account;
18+
private Set<Customer> owners;
19+
20+
@BeforeEach
21+
void setUp() {
22+
owners = new HashSet<>();
23+
owners.add(new Customer(UUID.randomUUID(), "Yuji Nishida"));
24+
owners.add(new Customer(UUID.randomUUID(), "Masahiro Sekita"));
25+
26+
account = new SavingsAccount("123456789", owners, 100.0);
27+
}
28+
29+
@Test
30+
void deposit() {
31+
account.deposit(100.0);
32+
assertEquals(200.0, account.getBalance());
33+
}
34+
35+
@Test
36+
void withdraw() {
37+
account.withdraw(25.0);
38+
assertEquals(75.0, account.getBalance());
39+
}
40+
41+
@Test
42+
void withdraw_withNegativeAmount() {
43+
assertThatExceptionOfType(IllegalStateException.class)
44+
.isThrownBy(() -> account.withdraw(-25.0))
45+
.withMessage("Withdrawal amount must be positive");
46+
}
47+
48+
@Test
49+
void withdraw_withInsufficientBalance() {
50+
assertThatExceptionOfType(InsufficientFundsException.class)
51+
.isThrownBy(() -> account.withdraw(200.0))
52+
.withMessage("Account does not have enough funds for withdrawal");
53+
}
54+
55+
@Test
56+
void closeAccount_withPositiveBalance() {
57+
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> account.closeAccount());
58+
}
59+
60+
@Test
61+
void isClosed() {
62+
assertFalse(account.isClosed());
63+
account.withdraw(100.0);
64+
account.closeAccount();
65+
assertTrue(account.isClosed());
66+
}
67+
}

0 commit comments

Comments
 (0)