Skip to content

Commit 8ab9a7c

Browse files
author
Meiko-S22
committed
feat: adds Savings Account and Business Checking Account and Tests
1 parent 61847f3 commit 8ab9a7c

File tree

7 files changed

+186
-11
lines changed

7 files changed

+186
-11
lines changed

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

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

3-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
43
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.UUID;
87

8+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
9+
910
/** Represents a bank ATM. */
1011
public class BankAtm {
1112

1213
private final Map<UUID, Customer> customerById = new HashMap<>();
13-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
14+
final Map<String, CheckingAccount> checkingAccountsByNumber = new HashMap<>();
15+
public Object accountByNumber;
16+
1417

1518
/**
1619
* Adds a checking account to the bank.
1720
*
1821
* @param account The account to add.
1922
*/
2023
public void addAccount(CheckingAccount account) {
21-
accountByNumber.put(account.getAccountNumber(), account);
24+
checkingAccountsByNumber.put(account.getAccountNumber(), account);
2225
account
2326
.getOwners()
2427
.forEach(
@@ -27,6 +30,8 @@ public void addAccount(CheckingAccount account) {
2730
});
2831
}
2932

33+
34+
3035
/**
3136
* Finds all accounts owned by a customer.
3237
*
@@ -79,10 +84,12 @@ public void withdrawFunds(String accountNumber, double amount) {
7984
* @return The account.
8085
*/
8186
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
83-
if (account == null || account.isClosed()) {
84-
throw new AccountNotFoundException("Account not found");
87+
CheckingAccount checkingAccount = checkingAccountsByNumber.get(accountNumber);
88+
if (checkingAccount != null && !checkingAccount.isClosed()) {
89+
throw new AccountNotFoundException("Account not foudn");
90+
}
91+
return checkingAccount;
8592
}
86-
return account;
93+
8794
}
88-
}
95+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public final class BusinessCheckingAccount extends CheckingAccount {
6+
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(
16+
"A BusinessCheckingAccount must have at least one business owner.");
17+
}
18+
}
19+
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) {
28+
return owners.stream().anyMatch(owner -> owner instanceof BusinessCustomer);
29+
}
30+
31+
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.UUID;
4+
5+
public class BusinessCustomer extends Customer {
6+
7+
public BusinessCustomer(UUID id, String name) {
8+
super(id, name);
9+
}
10+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
43
import java.util.Set;
54

5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
67
/** Represents a checking account. */
78
public class CheckingAccount {
89

9-
private final Set<Customer> owners;
10+
private static Set<Customer> owners;
1011
private final String accountNumber;
1112
private double balance;
1213
private boolean isActive;
@@ -20,7 +21,7 @@ public class CheckingAccount {
2021
*/
2122
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
2223
this.accountNumber = accountNumber;
23-
this.owners = owners;
24+
CheckingAccount.owners = owners;
2425
this.balance = initialBalance;
2526
isActive = true;
2627
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
6+
7+
8+
public class SavingsAccount extends CheckingAccount {
9+
10+
/**
11+
* Represents a savings account.
12+
*
13+
* <p>It is a subclass of CheckingAccount and inherits its properties and methods.
14+
*/
15+
16+
17+
18+
public SavingsAccount (String accountNumber, Set<Customer> owners, double initialBalance) {
19+
super(accountNumber, owners, initialBalance);
20+
}
21+
22+
public void depositFunds(Check check) {
23+
throw new UnsupportedOperationException("Cannot deposit check into a savings account");
24+
}
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.codedifferently.lesson17;
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
import java.util.UUID;
5+
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
import org.junit.jupiter.api.Test;
9+
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;
14+
public class BusinessCheckingAccountTest {
15+
@Test
16+
public void testAddBusinessCheckingAccountWithBusinessOwner() {
17+
18+
BankAtm atm = new BankAtm();
19+
20+
UUID customerId = UUID.randomUUID();
21+
22+
BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1");
23+
Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1");
24+
25+
26+
Set<Customer> owners = new HashSet<>();
27+
owners.add(businessCustomer);
28+
owners.add(personalCustomer);
29+
30+
31+
BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("12345", owners, 2000.00);
32+
atm.addAccount(businessAccount);
33+
assertNotNull((atm.findAccountsByCustomerId(customerId)));
34+
}
35+
36+
@Test
37+
public void testAddBusinessCheckingAccountWithoutBusinessOwner() {
38+
39+
BankAtm atm = new BankAtm();
40+
41+
42+
Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1");
43+
Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2");
44+
45+
46+
Set<Customer> owners = new HashSet<>();
47+
owners.add(personalCustomer1);
48+
owners.add(personalCustomer2);
49+
50+
51+
IllegalArgumentException exception =
52+
assertThrows(
53+
IllegalArgumentException.class,
54+
() -> {
55+
BusinessCheckingAccount businessAccount =
56+
new BusinessCheckingAccount("12345", owners, 2000.00);
57+
atm.addAccount(businessAccount);
58+
});
59+
}
60+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.codedifferently.lesson17.bank;
2+
import java.util.Set;
3+
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import org.junit.jupiter.api.Test;
7+
8+
9+
10+
public class SavingsAccountTest {
11+
@Test
12+
void testSavingsAccountCreation() {
13+
Set<Customer> owners = Set.of(new Customer(null, "John Doe"));
14+
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0);
15+
16+
assertEquals("123456789", savingsAccount.getAccountNumber());
17+
assertEquals(owners, savingsAccount.getOwners());
18+
assertEquals(1000.0, savingsAccount.getBalance());
19+
}
20+
21+
@Test
22+
void testDepositFunds() {
23+
Set<Customer> owners = Set.of(new Customer(null, "John Doe"));
24+
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0);
25+
26+
savingsAccount.deposit(500.0);
27+
assertEquals(1500.0, savingsAccount.getBalance());
28+
}
29+
30+
@Test
31+
void testDepositCheck() {
32+
Set<Customer> owners = Set.of(new Customer(null, "John Doe"));
33+
SavingsAccount savingsAccount = new SavingsAccount("123456789", owners, 1000.0);
34+
Check check = new Check(null, 200.0, savingsAccount);
35+
36+
assertThatExceptionOfType(UnsupportedOperationException.class)
37+
.isThrownBy(() -> savingsAccount.depositFunds(check))
38+
.withMessage("Cannot deposit check into a savings account");
39+
}
40+
}

0 commit comments

Comments
 (0)