Skip to content

Commit a0e1cd0

Browse files
author
“Tezz03”
committed
fix:update rest of files
1 parent 05b9e0a commit a0e1cd0

File tree

11 files changed

+505
-258
lines changed

11 files changed

+505
-258
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 BankAccount {
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 checking 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+
*/
21+
// Constructor
22+
public BankAccount(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 BankAccount other) {
115+
return accountNumber.equals(other.accountNumber);
116+
}
117+
return false;
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return "BankAccount{"
123+
+ "accountNumber='"
124+
+ accountNumber
125+
+ '\''
126+
+ ", balance="
127+
+ balance
128+
+ ", isActive="
129+
+ isActive
130+
+ '}';
131+
}
132+
}

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/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{"

0 commit comments

Comments
 (0)