Skip to content

Commit e83db46

Browse files
committed
feat: created SavingsAccount.java and SavingsAccountTest.java, and modified BankAtm.java file
1 parent 1c93014 commit e83db46

File tree

3 files changed

+247
-1
lines changed

3 files changed

+247
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4343
* Deposits funds into an account.
4444
*
4545
* @param accountNumber The account number.
46+
* @param accountSNumber Savings account number.
4647
* @param amount The amount to deposit.
4748
*/
4849
public void depositFunds(String accountNumber, double amount) {
4950
CheckingAccount account = getAccountOrThrow(accountNumber);
5051
account.deposit(amount);
5152
}
52-
53+
public void depositSFunds(String accountSNumber, double amount) {
54+
SavingsAccount accountS = getAccountSOrThrow(accountSNumber);
55+
accountS.deposit(amount);
56+
}
5357
/**
5458
* Deposits funds into an account using a check.
5559
*
@@ -61,6 +65,8 @@ public void depositFunds(String accountNumber, Check check) {
6165
check.depositFunds(account);
6266
}
6367

68+
69+
6470
/**
6571
* Withdraws funds from an account.
6672
*
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Represents a checking account. */
7+
public class SavingsAccount {
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 savings 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 SavingsAccount(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+
* Withdraws funds from the account.
63+
*
64+
* @param amount
65+
* @throws InsufficientFundsException
66+
*/
67+
public void withdraw(double amount) throws InsufficientFundsException {
68+
if (isClosed()) {
69+
throw new IllegalStateException("Cannot withdraw from a closed account");
70+
}
71+
if (amount <= 0) {
72+
throw new IllegalStateException("Withdrawal amount must be positive");
73+
}
74+
if (balance < amount) {
75+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
76+
}
77+
balance -= amount;
78+
}
79+
80+
/**
81+
* Gets the balance of the account.
82+
*
83+
* @return The balance of the account.
84+
*/
85+
public double getBalance() {
86+
return balance;
87+
}
88+
89+
/** Closes the account. */
90+
public void closeAccount() throws IllegalStateException {
91+
if (balance > 0) {
92+
throw new IllegalStateException("Cannot close account with a positive balance");
93+
}
94+
isActive = false;
95+
}
96+
97+
/**
98+
* Checks if the account is closed.
99+
*
100+
* @return True if the account is closed, otherwise false.
101+
*/
102+
public boolean isClosed() {
103+
return !isActive;
104+
}
105+
106+
@Override
107+
public int hashCode() {
108+
return accountNumber.hashCode();
109+
}
110+
111+
@Override
112+
public boolean equals(Object obj) {
113+
if (obj instanceof SavingsAccount other) {
114+
return accountNumber.equals(other.accountNumber);
115+
}
116+
return false;
117+
}
118+
119+
@Override
120+
public String toString() {
121+
return "SavingsAccount{"
122+
+ "accountNumber='"
123+
+ accountNumber
124+
+ '\''
125+
+ ", balance="
126+
+ balance
127+
+ ", isActive="
128+
+ isActive
129+
+ '}';
130+
}
131+
}
132+
133+
134+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 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 classSTest;
18+
private Set<Customer> owners;
19+
20+
@BeforeEach
21+
void setSUp() {
22+
owners = new HashSet<>();
23+
owners.add(new Customer(UUID.randomUUID(), "Jay Doe"));
24+
owners.add(new Customer(UUID.randomUUID(), "Joy Smith"));
25+
classSTest = new SavingsAccount("123456789", owners, 100.0);
26+
}
27+
28+
@Test
29+
void getAccountNumber() {
30+
assertEquals("123456789", classSTest.getAccountNumber());
31+
}
32+
33+
@Test
34+
void getOwners() {
35+
assertEquals(owners, classSTest.getOwners());
36+
}
37+
38+
@Test
39+
void deposit() {
40+
classSTest.deposit(50.0);
41+
assertEquals(150.0, classSTest.getBalance());
42+
}
43+
44+
@Test
45+
void deposit_withNegativeAmount() {
46+
assertThatExceptionOfType(IllegalArgumentException.class)
47+
.isThrownBy(() -> classSTest.deposit(-50.0));
48+
}
49+
50+
@Test
51+
void withdraw() {
52+
classSTest.withdraw(50.0);
53+
assertEquals(50.0, classSTest.getBalance());
54+
}
55+
56+
@Test
57+
void withdraw_withNegativeAmount() {
58+
assertThatExceptionOfType(IllegalStateException.class)
59+
.isThrownBy(() -> classSTest.withdraw(-50.0))
60+
.withMessage("Withdrawal amount must be positive");
61+
}
62+
63+
@Test
64+
void withdraw_withInsufficientBalance() {
65+
assertThatExceptionOfType(InsufficientFundsException.class)
66+
.isThrownBy(() -> classSTest.withdraw(150.0))
67+
.withMessage("Account does not have enough funds for withdrawal");
68+
}
69+
70+
@Test
71+
void getBalance() {
72+
assertEquals(100.0, classSTest.getBalance());
73+
}
74+
75+
@Test
76+
void closeAccount_withPositiveBalance() {
77+
assertThatExceptionOfType(IllegalStateException.class)
78+
.isThrownBy(() -> classSTest.closeAccount());
79+
}
80+
81+
@Test
82+
void isClosed() {
83+
assertFalse(classSTest.isClosed());
84+
classSTest.withdraw(100);
85+
classSTest.closeAccount();
86+
assertTrue(classSTest.isClosed());
87+
}
88+
89+
@Test
90+
void equals() {
91+
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
92+
assertEquals(classSTest, otherAccount);
93+
}
94+
95+
@Test
96+
void hashCodeTest() {
97+
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
98+
assertEquals(classSTest.hashCode(), otherAccount.hashCode());
99+
}
100+
101+
@Test
102+
void toStringTest() {
103+
String expected = "SavingsAccount{accountNumber='123456789', balance=100.0, isActive=true}";
104+
assertEquals(expected, classSTest.toString());
105+
}
106+
}

0 commit comments

Comments
 (0)