Skip to content

Commit af41cfa

Browse files
committed
feat:adds savings account and savings account test along with account class and made an update to checking account in lesson 17
1 parent 3939fb5 commit af41cfa

File tree

4 files changed

+222
-1
lines changed

4 files changed

+222
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
public interface Account {
7+
String getAccountNumber();
8+
9+
Set<Customer> getOwners();
10+
11+
void deposit(double amount);
12+
13+
void withdraw(double amount) throws InsufficientFundsException;
14+
15+
double getBalance();
16+
17+
void closeAccount();
18+
19+
boolean isClosed();
20+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Set;
55

66
/** Represents a checking account. */
7-
public class CheckingAccount extends BankAccount{
7+
public class CheckingAccount implements Account {
88

99
private final Set<Customer> owners;
1010
private final String accountNumber;
@@ -30,6 +30,7 @@ public CheckingAccount(String accountNumber, Set<Customer> owners, double initia
3030
*
3131
* @return The account number.
3232
*/
33+
@Override
3334
public String getAccountNumber() {
3435
return accountNumber;
3536
}
@@ -39,6 +40,7 @@ public String getAccountNumber() {
3940
*
4041
* @return The owners of the account.
4142
*/
43+
@Override
4244
public Set<Customer> getOwners() {
4345
return owners;
4446
}
@@ -48,6 +50,7 @@ public Set<Customer> getOwners() {
4850
*
4951
* @param amount The amount to deposit.
5052
*/
53+
@Override
5154
public void deposit(double amount) throws IllegalStateException {
5255
if (isClosed()) {
5356
throw new IllegalStateException("Cannot deposit to a closed account");
@@ -64,6 +67,7 @@ public void deposit(double amount) throws IllegalStateException {
6467
* @param amount
6568
* @throws InsufficientFundsException
6669
*/
70+
@Override
6771
public void withdraw(double amount) throws InsufficientFundsException {
6872
if (isClosed()) {
6973
throw new IllegalStateException("Cannot withdraw from a closed account");
@@ -82,11 +86,13 @@ public void withdraw(double amount) throws InsufficientFundsException {
8286
*
8387
* @return The balance of the account.
8488
*/
89+
@Override
8590
public double getBalance() {
8691
return balance;
8792
}
8893

8994
/** Closes the account. */
95+
@Override
9096
public void closeAccount() throws IllegalStateException {
9197
if (balance > 0) {
9298
throw new IllegalStateException("Cannot close account with a positive balance");
@@ -99,6 +105,7 @@ public void closeAccount() throws IllegalStateException {
99105
*
100106
* @return True if the account is closed, otherwise false.
101107
*/
108+
@Override
102109
public boolean isClosed() {
103110
return !isActive;
104111
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
public class SavingsAccount implements Account {
7+
8+
private final Set<Customer> owners;
9+
private final String accountNumber;
10+
private double balance;
11+
private boolean isActive;
12+
13+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
14+
this.accountNumber = accountNumber;
15+
this.owners = owners;
16+
this.balance = initialBalance;
17+
this.isActive = true;
18+
}
19+
20+
@Override
21+
public String getAccountNumber() {
22+
return accountNumber;
23+
}
24+
25+
@Override
26+
public Set<Customer> getOwners() {
27+
return owners;
28+
}
29+
30+
@Override
31+
public void deposit(double amount) {
32+
if (isClosed()) {
33+
throw new IllegalStateException("Cannot deposit to a closed account");
34+
}
35+
if (amount <= 0) {
36+
throw new IllegalArgumentException("Deposit amount must be positive");
37+
}
38+
balance += amount;
39+
}
40+
41+
@Override
42+
public void withdraw(double amount) throws InsufficientFundsException {
43+
if (isClosed()) {
44+
throw new IllegalStateException("Cannot withdraw from a closed account");
45+
}
46+
if (amount <= 0) {
47+
throw new IllegalArgumentException("Withdrawal amount must be positive");
48+
}
49+
if (balance < amount) {
50+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
51+
}
52+
balance -= amount;
53+
}
54+
55+
@Override
56+
public double getBalance() {
57+
return balance;
58+
}
59+
60+
@Override
61+
public void closeAccount() {
62+
if (balance > 0) {
63+
throw new IllegalStateException("Cannot close account with a positive balance");
64+
}
65+
isActive = false;
66+
}
67+
68+
@Override
69+
public boolean isClosed() {
70+
return !isActive;
71+
}
72+
73+
@Override
74+
public boolean equals(Object obj) {
75+
if (this == obj) {
76+
return true;
77+
}
78+
if (obj == null || getClass() != obj.getClass()) {
79+
return false;
80+
}
81+
SavingsAccount other = (SavingsAccount) obj;
82+
return accountNumber.equals(other.accountNumber)
83+
&& Double.compare(other.balance, balance) == 0
84+
&& isActive == other.isActive
85+
&& owners.equals(other.owners);
86+
}
87+
88+
@Override
89+
public int hashCode() {
90+
int result = accountNumber.hashCode();
91+
result = 31 * result + Double.hashCode(balance);
92+
result = 31 * result + Boolean.hashCode(isActive);
93+
result = 31 * result + owners.hashCode();
94+
return result;
95+
}
96+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.assertTrue;
6+
7+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
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+
public class SavingsAccountTest {
15+
16+
private SavingsAccount savingsAccount;
17+
private Set<Customer> accountOwners;
18+
19+
@BeforeEach
20+
void setUp() {
21+
accountOwners = new HashSet<>();
22+
accountOwners.add(new Customer(UUID.randomUUID(), "John Doe"));
23+
savingsAccount = new SavingsAccount("987654321", accountOwners, 200.0);
24+
}
25+
26+
@Test
27+
void shouldReturnAccountNumber() {
28+
assertEquals("987654321", savingsAccount.getAccountNumber());
29+
}
30+
31+
@Test
32+
void shouldReturnAccountOwners() {
33+
assertEquals(accountOwners, savingsAccount.getOwners());
34+
}
35+
36+
@Test
37+
void shouldDepositFundsSuccessfully() {
38+
savingsAccount.deposit(100.0);
39+
assertEquals(300.0, savingsAccount.getBalance());
40+
}
41+
42+
@Test
43+
void shouldThrowExceptionForNegativeDeposit() {
44+
assertThatExceptionOfType(IllegalArgumentException.class)
45+
.isThrownBy(() -> savingsAccount.deposit(-20.0));
46+
}
47+
48+
@Test
49+
void shouldWithdrawFundsSuccessfully() {
50+
savingsAccount.withdraw(50.0);
51+
assertEquals(150.0, savingsAccount.getBalance());
52+
}
53+
54+
@Test
55+
void shouldThrowExceptionForNegativeWithdrawalAmount() {
56+
assertThatExceptionOfType(IllegalArgumentException.class)
57+
.isThrownBy(() -> savingsAccount.withdraw(-10.0))
58+
.withMessage("Withdrawal amount must be positive");
59+
}
60+
61+
@Test
62+
void shouldThrowExceptionForInsufficientBalance() {
63+
assertThatExceptionOfType(InsufficientFundsException.class)
64+
.isThrownBy(() -> savingsAccount.withdraw(500.0))
65+
.withMessage("Account does not have enough funds for withdrawal");
66+
}
67+
68+
@Test
69+
void shouldReturnBalance() {
70+
assertEquals(200.0, savingsAccount.getBalance());
71+
}
72+
73+
@Test
74+
void shouldThrowExceptionWhenClosingAccountWithPositiveBalance() {
75+
assertThatExceptionOfType(IllegalStateException.class)
76+
.isThrownBy(() -> savingsAccount.closeAccount());
77+
}
78+
79+
@Test
80+
void shouldBeClosedAfterWithdrawAndClose() {
81+
savingsAccount.withdraw(200.0);
82+
savingsAccount.closeAccount();
83+
assertTrue(savingsAccount.isClosed());
84+
}
85+
86+
@Test
87+
void shouldCheckEqualityAndHashCode() {
88+
Set<Customer> owners = new HashSet<>();
89+
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
90+
91+
SavingsAccount account1 = new SavingsAccount("987654321", owners, 200.0);
92+
SavingsAccount account2 = new SavingsAccount("987654321", owners, 200.0);
93+
94+
assertEquals(account1, account2);
95+
96+
assertEquals(account1.hashCode(), account2.hashCode());
97+
}
98+
}

0 commit comments

Comments
 (0)