Skip to content

Commit a399102

Browse files
committed
lesson 17 homework mark1
1 parent 9cbe42b commit a399102

File tree

5 files changed

+109
-187
lines changed

5 files changed

+109
-187
lines changed

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
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<>();
1314
private final Map<String, Account> accountByNumber = new HashMap<>();
1415

1516
/**
16-
* Adds a checking or savings account to the bank.
17+
* Adds an account to the bank.
1718
*
1819
* @param account The account to add.
1920
*/
@@ -27,26 +28,15 @@ public void addAccount(Account account) {
2728
});
2829
}
2930

30-
public void addAccount(SavingsAccount account) {
31-
accountByNumber.put(account.getAccountNumber(), account);
32-
account
33-
.getOwners()
34-
.forEach(
35-
owner -> {
36-
customerById.put(owner.getId(), owner);
37-
});
38-
}
39-
4031
/**
4132
* Finds all accounts owned by a customer.
4233
*
4334
* @param customerId The ID of the customer.
4435
* @return The unique set of accounts owned by the customer.
4536
*/
4637
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
47-
return customerById.containsKey(customerId)
48-
? customerById.get(customerId).getAccounts()
49-
: Set.of();
38+
Customer customer = customerById.get(customerId);
39+
return customer != null ? customer.getAccounts() : Set.of();
5040
}
5141

5242
/**
@@ -65,8 +55,9 @@ public void depositFunds(String accountNumber, double amount) {
6555
*
6656
* @param accountNumber The account number.
6757
* @param check The check to deposit.
58+
* @throws Exception
6859
*/
69-
public void depositFunds(String accountNumber, Check check) {
60+
public void depositFunds(String accountNumber, Check check) throws Exception {
7061
Account account = getAccountOrThrow(accountNumber);
7162
check.depositFunds(account);
7263
}
@@ -76,8 +67,9 @@ public void depositFunds(String accountNumber, Check check) {
7667
*
7768
* @param accountNumber
7869
* @param amount
70+
* @throws Exception
7971
*/
80-
public void withdrawFunds(String accountNumber, double amount) {
72+
public void withdrawFunds(String accountNumber, double amount) throws Exception {
8173
Account account = getAccountOrThrow(accountNumber);
8274
account.withdraw(amount);
8375
}
@@ -95,4 +87,4 @@ private Account getAccountOrThrow(String accountNumber) {
9587
}
9688
return account;
9789
}
98-
}
90+
}
Lines changed: 4 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,10 @@
11
package com.codedifferently.lesson17.bank;
22

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

6-
/** Represents a savings account. */
7-
public class SavingsAccount {
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 savings 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-
*/
5+
public class SavingsAccount extends Account {
216
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
22-
this.accountNumber = accountNumber;
23-
this.owners = owners;
24-
this.balance = initialBalance;
25-
isActive = true;
26-
}
27-
28-
/**
29-
* Gets the account number.
30-
*
31-
* @return The account number.
32-
*/
33-
public String getAccountNumber() {
34-
return accountNumber;
35-
}
36-
37-
/**
38-
* Gets the owners of the account.
39-
*
40-
* @return The owners of the account.
41-
*/
42-
public Set<Customer> getOwners() {
43-
return owners;
44-
}
45-
46-
/**
47-
* Deposits funds into the account.
48-
*
49-
* @param amount The amount to deposit.
50-
*/
51-
public void deposit(double amount) throws IllegalStateException {
52-
if (isClosed()) {
53-
throw new IllegalStateException("Cannot deposit to a closed account");
54-
}
55-
if (amount <= 0) {
56-
throw new IllegalArgumentException("Deposit amount must be positive");
57-
}
58-
balance += amount;
59-
}
60-
61-
/**
62-
* Withdraws funds from the account.
63-
*
64-
* @param amount
65-
* @throws InsufficientFundsException
66-
*/
67-
public void withdraw(double amount) throws InsufficientFundsException {
68-
if (isClosed()) {
69-
throw new IllegalStateException("Cannot withdraw from a closed account");
70-
}
71-
if (amount <= 0) {
72-
throw new IllegalStateException("Withdrawal amount must be positive");
73-
}
74-
if (balance < amount) {
75-
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
76-
}
77-
balance -= amount;
78-
}
79-
80-
/**
81-
* Gets the balance of the account.
82-
*
83-
* @return The balance of the account.
84-
*/
85-
public double getBalance() {
86-
return balance;
87-
}
88-
89-
/** Closes the account. */
90-
public void closeAccount() throws IllegalStateException {
91-
if (balance > 0) {
92-
throw new IllegalStateException("Cannot close account with a positive balance");
93-
}
94-
isActive = false;
95-
}
96-
97-
/**
98-
* Checks if the account is closed.
99-
*
100-
* @return True if the account is closed, otherwise false.
101-
*/
102-
public boolean isClosed() {
103-
return !isActive;
104-
}
105-
106-
@Override
107-
public int hashCode() {
108-
return accountNumber.hashCode();
109-
}
110-
111-
@Override
112-
public boolean equals(Object obj) {
113-
if (obj instanceof SavingsAccount other) {
114-
return accountNumber.equals(other.accountNumber);
115-
}
116-
return false;
117-
}
118-
119-
@Override
120-
public String toString() {
121-
return "SavingsAccount{"
122-
+ "accountNumber='"
123-
+ accountNumber
124-
+ '\''
125-
+ ", balance="
126-
+ balance
127-
+ ", isActive="
128-
+ isActive
129-
+ '}';
7+
super(accountNumber, owners, initialBalance);
1308
}
131-
}
9+
10+
}

0 commit comments

Comments
 (0)