Skip to content

Commit 1065d42

Browse files
committed
feat: add saving account for Bank ATM HW (incomplete)
1 parent 61847f3 commit 1065d42

File tree

2 files changed

+238
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)