Skip to content

Commit 9c6bf25

Browse files
author
“Tezz03”
committed
fix:lesson_17
1 parent 1e6771f commit 9c6bf25

File tree

11 files changed

+282
-300
lines changed

11 files changed

+282
-300
lines changed

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

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

3+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
34
import java.util.HashMap;
45
import java.util.Map;
56
import java.util.Set;
67
import java.util.UUID;
78
import java.util.stream.Collectors;
89

9-
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
10-
1110
/** Represents a bank ATM. */
1211
public class BankAtm {
1312

1413
private final Map<UUID, Customer> customerById = new HashMap<>();
15-
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
16-
private final Map<String, BusinessAccount> accountByType = new HashMap<>();
14+
private final Map<String, BankAccount> accountByNumber = new HashMap<>();
1715

1816
/**
19-
* Adds a checking account to the bank.
17+
* Adds an account to the bank.
2018
*
2119
* @param account The account to add.
2220
*/
23-
public void addAccount(CheckingAccount account) {
24-
if (account == null) {
25-
throw new IllegalArgumentException("Account cannot be null");
26-
}
21+
public void addAccount(BankAccount account) {
2722
accountByNumber.put(account.getAccountNumber(), account);
2823
account
2924
.getOwners()
@@ -39,7 +34,7 @@ public void addAccount(CheckingAccount account) {
3934
* @param customerId The ID of the customer.
4035
* @return The unique set of accounts owned by the customer.
4136
*/
42-
public Set<Account> findAccountsByCustomerId(UUID customerId) {
37+
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
4338
return customerById.containsKey(customerId)
4439
? customerById.get(customerId).getAccounts().stream().collect(Collectors.toSet())
4540
: Set.of();
@@ -52,24 +47,18 @@ public Set<Account> findAccountsByCustomerId(UUID customerId) {
5247
* @param amount The amount to deposit.
5348
*/
5449
public void depositFunds(String accountNumber, double amount) {
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-
}
50+
BankAccount account = getAccountOrThrow(accountNumber);
51+
account.deposit(amount);
52+
}
6453

6554
/**
6655
* Deposits funds into an account using a check.
6756
*
6857
* @param accountNumber The account number.
6958
* @param check The check to deposit.
7059
*/
71-
public void depositFunds(String accountNumber, Check check) {
72-
CheckingAccount account = getAccountOrThrow(accountNumber);
60+
public void depositFunds(String accountNumber, Check check) throws Exception {
61+
BankAccount account = getAccountOrThrow(accountNumber);
7362
check.depositFunds(account);
7463
}
7564

@@ -79,8 +68,8 @@ public void depositFunds(String accountNumber, Check check) {
7968
* @param accountNumber
8069
* @param amount
8170
*/
82-
public void withdrawFunds(String accountNumber, double amount) {
83-
CheckingAccount account = getAccountOrThrow(accountNumber);
71+
public void withdrawFunds(String accountNumber, double amount) throws Exception {
72+
BankAccount account = getAccountOrThrow(accountNumber);
8473
account.withdraw(amount);
8574
}
8675

@@ -90,8 +79,8 @@ public void withdrawFunds(String accountNumber, double amount) {
9079
* @param accountNumber The account number.
9180
* @return The account.
9281
*/
93-
private CheckingAccount getAccountOrThrow(String accountNumber) {
94-
CheckingAccount account = accountByNumber.get(accountNumber);
82+
private BankAccount getAccountOrThrow(String accountNumber) {
83+
BankAccount account = accountByNumber.get(accountNumber);
9584
if (account == null || account.isClosed()) {
9685
throw new AccountNotFoundException("Account not found");
9786
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException;
4+
import java.util.Set;
5+
6+
/**
7+
* Creates a business account.
8+
*
9+
* @param accountNumber The account number.
10+
* @param owners The owners of the account.
11+
* @param initialBalance The initial balance of the account.
12+
*/
13+
public class BusinessCheckingAccount extends CheckingAccount {
14+
public BusinessCheckingAccount(
15+
String accountNumber, Set<Customer> owners, double initialBalance) {
16+
super(accountNumber, owners, initialBalance);
17+
18+
boolean hasBusinessOwner = owners.stream().anyMatch(customer -> customer.isBusiness());
19+
if (!hasBusinessOwner) {
20+
throw new InvalidBusinessAccountException("At least one owner must be a business.");
21+
}
22+
}
23+
}

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,27 @@
22

33
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
44

5-
/** Represents a check. */
65
public class Check {
76

87
private final String checkNumber;
98
private final double amount;
109
private final CheckingAccount account;
1110
private boolean isVoided = false;
12-
private final SavingsAccount savings;
1311

1412
/**
1513
* Creates a new check.
1614
*
1715
* @param checkNumber The check number.
1816
* @param amount The amount of the check.
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.
17+
* @param account The account the check is drawn on.
2118
*/
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 ) {
19+
public Check(String checkNumber, double amount, CheckingAccount account) {
20+
if (amount < 0) {
2721
throw new IllegalArgumentException("Check amount must be positive");
2822
}
2923
this.checkNumber = checkNumber;
3024
this.amount = amount;
3125
this.account = account;
32-
this.savings = null;
3326
}
3427

3528
/**
@@ -50,8 +43,9 @@ public void voidCheck() {
5043
* Deposits the check into an account.
5144
*
5245
* @param toAccount The account to deposit the check into.
46+
* @throws Exception
5347
*/
54-
public void depositFunds(CheckingAccount toAccount) {
48+
public void depositFunds(BankAccount toAccount) throws Exception {
5549
if (isVoided) {
5650
throw new CheckVoidedException("Check is voided");
5751
}

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

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import java.util.Set;
44

5-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
6-
75
/** Represents a checking account. */
8-
public class CheckingAccount {
6+
public class CheckingAccount extends BankAccount {
97

108
private final Set<Customer> owners;
119
private final String accountNumber;
@@ -20,103 +18,13 @@ public class CheckingAccount {
2018
* @param initialBalance The initial balance of the account.
2119
*/
2220
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
21+
super(accountNumber, owners, initialBalance);
2322
this.accountNumber = accountNumber;
2423
this.owners = owners;
2524
this.balance = initialBalance;
2625
isActive = true;
2726
}
2827

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 CheckingAccount other) {
115-
return accountNumber.equals(other.accountNumber);
116-
}
117-
return false;
118-
}
119-
12028
@Override
12129
public String toString() {
12230
return "CheckingAccount{"

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77
/** Represents a customer of the bank. */
88
public class Customer {
99

10+
static boolean isBusinessStatic() {
11+
throw new UnsupportedOperationException("Not supported yet.");
12+
}
13+
1014
private final UUID id;
1115
private final String name;
12-
private final Set<CheckingAccount> accounts = new HashSet<>();
16+
private final Set<BankAccount> accounts = new HashSet<>();
17+
private final boolean isBusiness;
1318

1419
/**
1520
* Creates a new customer.
1621
*
1722
* @param id The ID of the customer.
1823
* @param name The name of the customer.
1924
*/
20-
public Customer(UUID id, String name) {
25+
public Customer(UUID id, String name, boolean isBusiness) {
2126
this.id = id;
2227
this.name = name;
28+
this.isBusiness = isBusiness;
2329
}
2430

2531
/**
@@ -41,11 +47,20 @@ public String getName() {
4147
}
4248

4349
/**
44-
* Adds a checking account to the customer.
50+
* Adds a checking account to the customer. Checks if this is a business
4551
*
46-
* @param account The account to add.
52+
* @return True if this is a business, otherwise false.
4753
*/
48-
public void addAccount(CheckingAccount account) {
54+
public boolean isBusiness() {
55+
return isBusiness;
56+
}
57+
58+
/**
59+
* Adds an account to the customer.
60+
*
61+
* @param checkingAccount2 The account to add.
62+
*/
63+
public void addAccount(BankAccount account) {
4964
accounts.add(account);
5065
}
5166

@@ -54,7 +69,7 @@ public void addAccount(CheckingAccount account) {
5469
*
5570
* @return The unique set of accounts owned by the customer.
5671
*/
57-
public Set<CheckingAccount> getAccounts() {
72+
public Set<BankAccount> getAccounts() {
5873
return accounts;
5974
}
6075

@@ -63,13 +78,13 @@ public int hashCode() {
6378
return id.hashCode();
6479
}
6580

66-
@Override
81+
/*@Override
6782
public boolean equals(Object obj) {
6883
if (obj instanceof Customer other) {
6984
return id.equals(other.id);
7085
}
7186
return false;
72-
}
87+
} */
7388

7489
@Override
7590
public String toString() {

0 commit comments

Comments
 (0)