Skip to content

Commit e250514

Browse files
committed
feat: added functional requirement 1- SavingsAccount
file and SavingsAccountTest file- set up class inheritance- tests passed- coverage 87%
1 parent 2e32759 commit e250514

File tree

2 files changed

+236
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)