Skip to content

Commit 89a467d

Browse files
author
Meiko-S22
committed
feat: implement BusinessCheckingAccount and SavingsAccount wth asscoiated tests
1 parent 8ab9a7c commit 89a467d

File tree

8 files changed

+170
-115
lines changed

8 files changed

+170
-115
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class BankAtm {
1313
private final Map<UUID, Customer> customerById = new HashMap<>();
1414
final Map<String, CheckingAccount> checkingAccountsByNumber = new HashMap<>();
1515
public Object accountByNumber;
16-
1716

1817
/**
1918
* Adds a checking account to the bank.
@@ -30,8 +29,6 @@ public void addAccount(CheckingAccount account) {
3029
});
3130
}
3231

33-
34-
3532
/**
3633
* Finds all accounts owned by a customer.
3734
*
@@ -85,11 +82,9 @@ public void withdrawFunds(String accountNumber, double amount) {
8582
*/
8683
private CheckingAccount getAccountOrThrow(String accountNumber) {
8784
CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber);
88-
if (checkingAccount != null && !checkingAccount.isClosed()) {
89-
throw new AccountNotFoundException("Account not foudn");
90-
}
91-
return checkingAccount;
85+
if (checkingAccount == null || checkingAccount.isClosed()) {
86+
throw new AccountNotFoundException("Account not found");
9287
}
93-
88+
return checkingAccount;
9489
}
95-
90+
}

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@
44

55
public final class BusinessCheckingAccount extends CheckingAccount {
66

7-
/**
8-
* Represents a business checking account.
9-
*
10-
* <p>It is a subclass of CheckingAccount and inherits its properties and methods.
11-
*/
12-
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
13-
super(accountNumber, owners, initialBalance);
14-
if (!hasBusinessOwner(owners)) {
15-
throw new IllegalArgumentException(
7+
/**
8+
* Represents a business checking account.
9+
*
10+
* <p>It is a subclass of BankAccount and inherits its properties and methods.
11+
*/
12+
public BusinessCheckingAccount(
13+
String accountNumber, Set<Customer> owners, double initialBalance) {
14+
super(accountNumber, owners, initialBalance);
15+
if (!hasBusinessOwner(owners)) {
16+
throw new IllegalArgumentException(
1617
"A BusinessCheckingAccount must have at least one business owner.");
1718
}
1819
}
1920

20-
/**
21-
* Checks if the owners of the account include at least one business customer.
22-
*
23-
* @param owners The owners of the account.
24-
* @return true if there is a business owner, false otherwise.
25-
*/
26-
private
27-
boolean hasBusinessOwner(Set<Customer> owners) {
21+
/**
22+
* Checks if the owners of the account include at least one business customer.
23+
*
24+
* @param owners The owners of the account.
25+
* @return true if there is a business owner, false otherwise.
26+
*/
27+
private boolean hasBusinessOwner(Set<Customer> owners) {
2828
return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer);
2929
}
30-
31-
3230
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ public class BusinessCustomer extends Customer {
77
public BusinessCustomer(UUID id, String name) {
88
super(id, name);
99
}
10-
}
10+
}

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import java.util.Set;
4-
53
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
65

76
/** Represents a checking account. */
87
public class CheckingAccount {
9-
108
private static Set<Customer> owners;
119
private final String accountNumber;
1210
private double balance;
1311
private boolean isActive;
1412

15-
/**
16-
* Creates a new checking account.
17-
*
18-
* @param accountNumber The account number.
19-
* @param owners The owners of the account.
20-
* @param initialBalance The initial balance of the account.
21-
*/
2213
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
2314
this.accountNumber = accountNumber;
24-
CheckingAccount.owners = owners;
15+
this.owners = owners;
2516
this.balance = initialBalance;
2617
isActive = true;
2718
}
@@ -44,6 +35,23 @@ public Set<Customer> getOwners() {
4435
return owners;
4536
}
4637

38+
/**
39+
* Gets the balance of the account.
40+
*
41+
* @return The balance of the account.
42+
*/
43+
public double getBalance() {
44+
return balance;
45+
}
46+
47+
/** Closes the account. */
48+
public void closeAccount() throws IllegalStateException {
49+
if (balance > 0) {
50+
throw new IllegalStateException("Cannot close account with positive balance");
51+
}
52+
isActive = false;
53+
}
54+
4755
/**
4856
* Deposits funds into the account.
4957
*
@@ -72,29 +80,12 @@ public void withdraw(double amount) throws InsufficientFundsException {
7280
if (amount <= 0) {
7381
throw new IllegalStateException("Withdrawal amount must be positive");
7482
}
75-
if (balance < amount) {
83+
if (amount > balance) {
7684
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
7785
}
7886
balance -= amount;
7987
}
8088

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-
9889
/**
9990
* Checks if the account is closed.
10091
*
Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,115 @@
11
package com.codedifferently.lesson17.bank;
22

3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
34
import java.util.Set;
45

6+
public class SavingsAccount {
7+
private final Set<Customer> owners;
8+
private final String accountNumber;
9+
private double balance;
10+
private boolean isActive;
511

12+
/**
13+
* Represents a savings account.
14+
*
15+
* <p>It is a subclass of BankAccount and inherits its properties and methods.
16+
*/
17+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
18+
this.accountNumber = accountNumber;
19+
this.owners = owners;
20+
this.balance = initialBalance;
21+
isActive = true;
22+
}
623

24+
/**
25+
* Gets the account number.
26+
*
27+
* @return The account number.
28+
*/
29+
public String getAccountNumber() {
30+
return accountNumber;
31+
}
732

8-
public class SavingsAccount extends CheckingAccount {
9-
1033
/**
11-
* Represents a savings account.
34+
* Gets the owners of the account.
35+
*
36+
* @return The owners of the account.
37+
*/
38+
public Set<Customer> getOwners() {
39+
return owners;
40+
}
41+
42+
/**
43+
* Gets the balance of the account.
44+
*
45+
* @return The balance of the account.
46+
*/
47+
public double getBalance() {
48+
return balance;
49+
}
50+
51+
/**
52+
* Deposits funds into the account.
1253
*
13-
* <p>It is a subclass of CheckingAccount and inherits its properties and methods.
54+
* @param amount The amount to deposit.
1455
*/
56+
public void deposit(double amount) throws IllegalStateException {
57+
if (isClosed()) {
58+
throw new IllegalStateException("Cannot deposit to a closed account");
59+
}
60+
if (amount <= 0) {
61+
throw new IllegalArgumentException("Deposit amount must be positive");
62+
}
63+
balance += amount;
64+
}
1565

66+
/**
67+
* Withdraws funds from the account.
68+
*
69+
* @param amount
70+
* @throws InsufficientFundsException
71+
*/
72+
public void withdraw(double amount) throws InsufficientFundsException {
73+
if (isClosed()) {
74+
throw new IllegalStateException("Cannot withdraw from a closed account");
75+
}
76+
if (amount <= 0) {
77+
throw new IllegalStateException("Withdrawal amount must be positive");
78+
}
79+
}
1680

81+
/**
82+
* Checks if the account is closed.
83+
*
84+
* @return True if the account is closed, otherwise false.
85+
*/
86+
public boolean isClosed() {
87+
return !isActive;
88+
}
1789

18-
public SavingsAccount (String accountNumber, Set<Customer> owners, double initialBalance) {
19-
super(accountNumber, owners, initialBalance);
90+
@Override
91+
public int hashCode() {
92+
return accountNumber.hashCode();
93+
}
94+
95+
@Override
96+
public boolean equals(Object obj) {
97+
if (obj instanceof SavingsAccount other) {
98+
return accountNumber.equals(other.accountNumber);
99+
}
100+
return false;
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return "CheckingAccount{"
106+
+ "accountNumber='"
107+
+ accountNumber
108+
+ '\''
109+
+ ", balance="
110+
+ balance
111+
+ ", isActive="
112+
+ isActive
113+
+ '}';
20114
}
21-
22-
public void depositFunds(Check check) {
23-
throw new UnsupportedOperationException("Cannot deposit check into a savings account");
24-
}
25115
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void setUp() {
2727
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
2828
customer1.addAccount(account1);
2929
customer1.addAccount(account2);
30+
customer2.addAccount(account1);
3031
customer2.addAccount(account2);
3132
classUnderTest.addAccount(account1);
3233
classUnderTest.addAccount(account2);
@@ -74,7 +75,7 @@ void testDepositFunds_Check() {
7475
classUnderTest.depositFunds("987654321", check);
7576

7677
// Assert
77-
assertThat(account1.getBalance()).isEqualTo(0);
78+
assertThat(account1.getBalance()).isEqualTo(0.0);
7879
assertThat(account2.getBalance()).isEqualTo(300.0);
7980
}
8081

Original file line numberDiff line numberDiff line change
@@ -1,60 +1,52 @@
1-
package com.codedifferently.lesson17;
2-
import java.util.HashSet;
3-
import java.util.Set;
4-
import java.util.UUID;
1+
package com.codedifferently.lesson17.bank;
52

63
import static org.junit.jupiter.api.Assertions.assertNotNull;
74
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import java.util.UUID;
89
import org.junit.jupiter.api.Test;
910

10-
import com.codedifferently.lesson17.bank.BankAtm;
11-
import com.codedifferently.lesson17.bank.BusinessCheckingAccount;
12-
import com.codedifferently.lesson17.bank.BusinessCustomer;
13-
import com.codedifferently.lesson17.bank.Customer;
1411
public class BusinessCheckingAccountTest {
15-
@Test
12+
@Test
1613
public void testAddBusinessCheckingAccountWithBusinessOwner() {
17-
14+
1815
BankAtm atm = new BankAtm();
1916

2017
UUID customerId = UUID.randomUUID();
2118

2219
BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1");
2320
Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1");
2421

25-
2622
Set<Customer> owners = new HashSet<>();
2723
owners.add(businessCustomer);
2824
owners.add(personalCustomer);
2925

30-
3126
BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("12345", owners, 2000.00);
3227
atm.addAccount(businessAccount);
3328
assertNotNull((atm.findAccountsByCustomerId(customerId)));
3429
}
3530

3631
@Test
3732
public void testAddBusinessCheckingAccountWithoutBusinessOwner() {
38-
33+
3934
BankAtm atm = new BankAtm();
4035

41-
4236
Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1");
4337
Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2");
4438

45-
4639
Set<Customer> owners = new HashSet<>();
4740
owners.add(personalCustomer1);
4841
owners.add(personalCustomer2);
4942

50-
5143
IllegalArgumentException exception =
52-
assertThrows(
44+
assertThrows(
5345
IllegalArgumentException.class,
5446
() -> {
5547
BusinessCheckingAccount businessAccount =
5648
new BusinessCheckingAccount("12345", owners, 2000.00);
5749
atm.addAccount(businessAccount);
5850
});
59-
}
51+
}
6052
}

0 commit comments

Comments
 (0)