Skip to content

Commit 40abc44

Browse files
committed
lesson 17 mark2
1 parent a399102 commit 40abc44

File tree

11 files changed

+237
-244
lines changed

11 files changed

+237
-244
lines changed

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

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
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

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

1312
private final Map<UUID, Customer> customerById = new HashMap<>();
14-
private final Map<String, Account> accountByNumber = new HashMap<>();
13+
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
1514

1615
/**
17-
* Adds an account to the bank.
16+
* Adds a checking account to the bank.
1817
*
1918
* @param account The account to add.
2019
*/
21-
public void addAccount(Account account) {
20+
public void addAccount(CheckingAccount account) {
2221
accountByNumber.put(account.getAccountNumber(), account);
2322
account
2423
.getOwners()
@@ -35,8 +34,9 @@ public void addAccount(Account account) {
3534
* @return The unique set of accounts owned by the customer.
3635
*/
3736
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
38-
Customer customer = customerById.get(customerId);
39-
return customer != null ? customer.getAccounts() : Set.of();
37+
return customerById.containsKey(customerId)
38+
? customerById.get(customerId).getAccounts()
39+
: Set.of();
4040
}
4141

4242
/**
@@ -46,7 +46,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4646
* @param amount The amount to deposit.
4747
*/
4848
public void depositFunds(String accountNumber, double amount) {
49-
Account account = getAccountOrThrow(accountNumber);
49+
CheckingAccount account = getAccountOrThrow(accountNumber);
5050
account.deposit(amount);
5151
}
5252

@@ -55,10 +55,9 @@ public void depositFunds(String accountNumber, double amount) {
5555
*
5656
* @param accountNumber The account number.
5757
* @param check The check to deposit.
58-
* @throws Exception
5958
*/
60-
public void depositFunds(String accountNumber, Check check) throws Exception {
61-
Account account = getAccountOrThrow(accountNumber);
59+
public void depositFunds(String accountNumber, Check check) {
60+
CheckingAccount account = getAccountOrThrow(accountNumber);
6261
check.depositFunds(account);
6362
}
6463

@@ -67,10 +66,9 @@ public void depositFunds(String accountNumber, Check check) throws Exception {
6766
*
6867
* @param accountNumber
6968
* @param amount
70-
* @throws Exception
7169
*/
72-
public void withdrawFunds(String accountNumber, double amount) throws Exception {
73-
Account account = getAccountOrThrow(accountNumber);
70+
public void withdrawFunds(String accountNumber, double amount) {
71+
CheckingAccount account = getAccountOrThrow(accountNumber);
7472
account.withdraw(amount);
7573
}
7674

@@ -80,11 +78,11 @@ public void withdrawFunds(String accountNumber, double amount) throws Exception
8078
* @param accountNumber The account number.
8179
* @return The account.
8280
*/
83-
private Account getAccountOrThrow(String accountNumber) {
84-
Account account = accountByNumber.get(accountNumber);
81+
private CheckingAccount getAccountOrThrow(String accountNumber) {
82+
CheckingAccount account = accountByNumber.get(accountNumber);
8583
if (account == null || account.isClosed()) {
8684
throw new AccountNotFoundException("Account not found");
8785
}
8886
return account;
8987
}
90-
}
88+
}

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public class BusinessCheckingSavingsAccount extends CheckingAccount {
6+
public BusinessCheckingSavingsAccount(
7+
String accountNumber, Set<Customer> owners, double initialBalance) {
8+
super(accountNumber, owners, initialBalance);
9+
}
10+
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public void voidCheck() {
4343
/**
4444
* Deposits the check into an account.
4545
*
46-
* @param account2 The account to deposit the check into.
46+
* @param toAccount The account to deposit the check into.
4747
*/
48-
public void depositFunds(Account account2) {
48+
public void depositFunds(CheckingAccount toAccount) {
4949
if (isVoided) {
5050
throw new CheckVoidedException("Check is voided");
5151
}
5252
account.withdraw(amount);
53-
account2.deposit(amount);
53+
toAccount.deposit(amount);
5454
voidCheck();
5555
}
5656

@@ -79,9 +79,4 @@ public String toString() {
7979
+ account.getAccountNumber()
8080
+ '}';
8181
}
82-
83-
public double getAmount() {
84-
// TODO Auto-generated method stub
85-
throw new UnsupportedOperationException("Unimplemented method 'getAmount'");
86-
}
8782
}
Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,131 @@
11
package com.codedifferently.lesson17.bank;
22

3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
34
import java.util.Set;
45

5-
public class CheckingAccount extends Account {
6+
/** Represents a checking account. */
7+
public class CheckingAccount {
8+
9+
private final Set<Customer> owners;
10+
private final String accountNumber;
11+
protected 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+
*/
621
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
7-
super(accountNumber, owners, 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 CheckingAccount other) {
114+
return accountNumber.equals(other.accountNumber);
115+
}
116+
return false;
117+
}
118+
119+
@Override
120+
public String toString() {
121+
return "CheckingAccount{"
122+
+ "accountNumber='"
123+
+ accountNumber
124+
+ '\''
125+
+ ", balance="
126+
+ balance
127+
+ ", isActive="
128+
+ isActive
129+
+ '}';
8130
}
9-
}
131+
}

0 commit comments

Comments
 (0)