Skip to content

Commit 1e6771f

Browse files
author
“Tezz03”
committed
Feat: Need more time
1 parent f76c59d commit 1e6771f

File tree

7 files changed

+242
-10
lines changed

7 files changed

+242
-10
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
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;
7+
import java.util.stream.Collectors;
8+
9+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
810

911
/** Represents a bank ATM. */
1012
public class BankAtm {
1113

1214
private final Map<UUID, Customer> customerById = new HashMap<>();
1315
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
16+
private final Map<String, BusinessAccount> accountByType = new HashMap<>();
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) {
24+
if (account == null) {
25+
throw new IllegalArgumentException("Account cannot be null");
26+
}
2127
accountByNumber.put(account.getAccountNumber(), account);
2228
account
2329
.getOwners()
@@ -33,9 +39,9 @@ public void addAccount(CheckingAccount account) {
3339
* @param customerId The ID of the customer.
3440
* @return The unique set of accounts owned by the customer.
3541
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
42+
public Set<Account> findAccountsByCustomerId(UUID customerId) {
3743
return customerById.containsKey(customerId)
38-
? customerById.get(customerId).getAccounts()
44+
? customerById.get(customerId).getAccounts().stream().collect(Collectors.toSet())
3945
: Set.of();
4046
}
4147

@@ -46,9 +52,15 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4652
* @param amount The amount to deposit.
4753
*/
4854
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
50-
account.deposit(amount);
51-
}
55+
Account account = getAccountOrThrow(accountNumber);
56+
57+
if (account instanceof CheckingAccount) {
58+
((CheckingAccount) account).deposit(amount);
59+
} else if (account instanceof BusinessAccount) {
60+
((BusinessAccount) account).deposit(amount);
61+
} else {
62+
throw new IllegalArgumentException("Unsupported account type");
63+
}
5264

5365
/**
5466
* Deposits funds into an account using a check.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
7+
public class BusinessAccount extends CheckingAccount {
8+
9+
/**
10+
* Creates a new business account.
11+
*
12+
* @param accountNumber The account number.
13+
* @param owners The owners of the account.
14+
* @param initialBalance The initial balance of the account.
15+
*/
16+
public BusinessAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
17+
super(accountNumber, owners, initialBalance);
18+
}
19+
20+
/**
21+
* Withdraws funds from the account.
22+
*
23+
* @param amount The amount to withdraw.
24+
* @throws InsufficientFundsException If there are insufficient funds in the account.
25+
*/
26+
@Override
27+
public void withdraw(double amount) throws InsufficientFundsException {
28+
if (amount > getBalance()) {
29+
throw new InsufficientFundsException("Insufficient funds in the account.");
30+
}
31+
super.withdraw(amount);
32+
}
33+
34+
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@ public class Check {
99
private final double amount;
1010
private final CheckingAccount account;
1111
private boolean isVoided = false;
12+
private final SavingsAccount savings;
1213

1314
/**
1415
* Creates a new check.
1516
*
1617
* @param checkNumber The check number.
1718
* @param amount The amount of the check.
18-
* @param account The account the check is drawn on.
19+
* @param account The account the check is drawn on. Because account is represents checking it could also represent savings.so if
20+
* we call saving account saving we should be able to throw the exception on the check.
1921
*/
20-
public Check(String checkNumber, double amount, CheckingAccount account) {
21-
if (amount < 0) {
22+
public Check(String checkNumber, double amount, CheckingAccount account, SavingsAccount savings) {
23+
if (checkNumber == null || checkNumber.isEmpty()) {
24+
throw new IllegalArgumentException("Check number cannot be null or empty");
25+
}
26+
if (amount < 0 ) {
2227
throw new IllegalArgumentException("Check amount must be positive");
2328
}
2429
this.checkNumber = checkNumber;
2530
this.amount = amount;
2631
this.account = account;
32+
this.savings = null;
2733
}
2834

2935
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6+
// Removed redundant or incorrect import
7+
8+
9+
/** Represents a savings account, which does not support check writing. */
10+
public class SavingsAccount extends CheckingAccount {
11+
12+
/**
13+
* Creates a new savings account.
14+
*
15+
* @param accountNumber The account number.
16+
* @param owners The owners of the account.
17+
* @param initialBalance The initial balance of the account.
18+
*/
19+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
20+
super(accountNumber, owners, initialBalance);
21+
}
22+
23+
/**
24+
* Withdraws funds from the account.
25+
*
26+
* @param amount The amount to withdraw.
27+
* @throws InsufficientFundsException If there are insufficient funds in the account.
28+
*/
29+
@Override
30+
public void withdraw(double amount) throws InsufficientFundsException {
31+
if (amount > getBalance()) {
32+
throw new InsufficientFundsException("Insufficient funds in the account.");
33+
}
34+
// Use a protected setter or a method in parent class if balance is private
35+
super.withdraw(amount); // Use super if parent class has the logic
36+
}
37+
38+
/**
39+
* Disallows writing checks from a savings account.
40+
*
41+
* @param check The amount to write on the check.
42+
* @throws UnsupportedOperationException Always, since savings accounts don't support checks.
43+
*/
44+
public void writeCheck(Check check) {
45+
throw new UnsupportedOperationException("Savings accounts can't write checks.");
46+
}
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
public class SavingAccount {
4+
5+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.UUID;
6+
7+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import com.codedifferently.lesson17.bank.SavingsAccount;
15+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
16+
17+
public class SavingsAccountTest {
18+
19+
20+
private SavingsAccount classUnderTest;
21+
private Set<Customer> owners;
22+
23+
@BeforeEach
24+
void setUp() {
25+
owners = new HashSet<>();
26+
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
27+
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
28+
classUnderTest = new SavingsAccount("123456789", owners, 100.0);
29+
}
30+
31+
@Test
32+
void getAccountNumber() {
33+
assertEquals("123456789", classUnderTest.getAccountNumber());
34+
}
35+
36+
@Test
37+
void getOwners() {
38+
assertEquals(owners, classUnderTest.getOwners());
39+
}
40+
41+
@Test
42+
void deposit() {
43+
classUnderTest.deposit(50.0);
44+
assertEquals(150.0, classUnderTest.getBalance());
45+
}
46+
47+
@Test
48+
void deposit_withNegativeAmount() {
49+
assertThatExceptionOfType(IllegalArgumentException.class)
50+
.isThrownBy(() -> classUnderTest.deposit(-50.0));
51+
}
52+
53+
@Test
54+
void withdraw() {
55+
classUnderTest.withdraw(50.0);
56+
assertEquals(50.0, classUnderTest.getBalance());
57+
}
58+
59+
@Test
60+
void withdraw_withNegativeAmount() {
61+
assertThatExceptionOfType(IllegalStateException.class)
62+
.isThrownBy(() -> classUnderTest.withdraw(-50.0))
63+
.withMessage("Withdrawal amount must be positive");
64+
}
65+
66+
@Test
67+
void withdraw_withInsufficientBalance() {
68+
assertThatExceptionOfType(InsufficientFundsException.class)
69+
.isThrownBy(() -> classUnderTest.withdraw(150.0))
70+
.withMessage("Account does not have enough funds for withdrawal");
71+
}
72+
73+
@Test
74+
void getBalance() {
75+
assertEquals(100.0, classUnderTest.getBalance());
76+
}
77+
78+
@Test
79+
void closeAccount_withPositiveBalance() {
80+
assertThatExceptionOfType(IllegalStateException.class)
81+
.isThrownBy(() -> classUnderTest.closeAccount());
82+
}
83+
84+
@Test
85+
void isClosed() {
86+
assertFalse(classUnderTest.isClosed());
87+
classUnderTest.withdraw(100);
88+
classUnderTest.closeAccount();
89+
assertTrue(classUnderTest.isClosed());
90+
}
91+
92+
@Test
93+
void equals() {
94+
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
95+
assertEquals(classUnderTest, otherAccount);
96+
}
97+
98+
@Test
99+
void hashCodeTest() {
100+
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
101+
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode());
102+
}
103+
104+
@Test
105+
void toStringTest() {
106+
String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}";
107+
assertEquals(expected, classUnderTest.toString());
108+
}
109+
110+
@Test
111+
void writeCheck() {
112+
// Arrange
113+
Check check = new Check("987654321", 50.0, classUnderTest, classUnderTest);
114+
115+
// Act
116+
classUnderTest.writeCheck(check);
117+
118+
// Assert
119+
assertEquals(50.0, classUnderTest.getBalance());
120+
assertTrue(check.getIsVoided());
121+
122+
}
123+
}
124+
125+
126+
127+

0 commit comments

Comments
 (0)