Skip to content

Commit 9cbe42b

Browse files
committed
lesson_17 homework mark0
1 parent 61847f3 commit 9cbe42b

File tree

8 files changed

+288
-164
lines changed

8 files changed

+288
-164
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
public abstract class Account {
7+
private final Set<Customer> owners;
8+
private final String accountNumber;
9+
protected double balance;
10+
protected boolean isActive;
11+
12+
/**
13+
* Constructs a new Account.
14+
*
15+
* @param accountNumber The account number.
16+
* @param owners The owners of the account.
17+
* @param initialBalance The starting balance.
18+
*/
19+
public Account(String accountNumber, Set<Customer> owners, double initialBalance) {
20+
this.accountNumber = accountNumber;
21+
this.owners = owners;
22+
this.balance = initialBalance;
23+
this.isActive = true;
24+
}
25+
26+
public String getAccountNumber() {
27+
return accountNumber;
28+
}
29+
30+
public Set<Customer> getOwners() {
31+
return owners;
32+
}
33+
34+
public void deposit(double amount) {
35+
if (isClosed()) {
36+
throw new IllegalStateException("Cannot deposit to a closed account");
37+
}
38+
if (amount <= 0) {
39+
throw new IllegalArgumentException("Deposit amount must be positive");
40+
}
41+
balance += amount;
42+
}
43+
44+
public void withdraw(double amount) throws InsufficientFundsException {
45+
if (isClosed()) {
46+
throw new IllegalStateException("Cannot withdraw from a closed account");
47+
}
48+
if (amount <= 0) {
49+
throw new IllegalStateException("Withdrawal amount must be positive");
50+
}
51+
if (balance < amount) {
52+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
53+
}
54+
balance -= amount;
55+
}
56+
57+
public double getBalance() {
58+
return balance;
59+
}
60+
61+
public void closeAccount() {
62+
if (balance > 0) {
63+
throw new IllegalStateException("Cannot close account with a positive balance");
64+
}
65+
isActive = false;
66+
}
67+
68+
public boolean isClosed() {
69+
return !isActive;
70+
}
71+
72+
@Override
73+
public int hashCode() {
74+
return accountNumber.hashCode();
75+
}
76+
77+
@Override
78+
public boolean equals(Object obj) {
79+
if (obj instanceof Account other) {
80+
return this.accountNumber.equals(other.accountNumber);
81+
}
82+
return false;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return getClass().getSimpleName() +
88+
"{" +
89+
"accountNumber='" + accountNumber + '\'' +
90+
", balance=" + balance +
91+
", isActive=" + isActive +
92+
'}';
93+
}
94+
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
public class BankAtm {
1111

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

1515
/**
16-
* Adds a checking account to the bank.
16+
* Adds a checking or savings account to the bank.
1717
*
1818
* @param account The account to add.
1919
*/
20-
public void addAccount(CheckingAccount account) {
20+
public void addAccount(Account account) {
21+
accountByNumber.put(account.getAccountNumber(), account);
22+
account
23+
.getOwners()
24+
.forEach(
25+
owner -> {
26+
customerById.put(owner.getId(), owner);
27+
});
28+
}
29+
30+
public void addAccount(SavingsAccount account) {
2131
accountByNumber.put(account.getAccountNumber(), account);
2232
account
2333
.getOwners()
@@ -46,7 +56,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4656
* @param amount The amount to deposit.
4757
*/
4858
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
59+
Account account = getAccountOrThrow(accountNumber);
5060
account.deposit(amount);
5161
}
5262

@@ -57,7 +67,7 @@ public void depositFunds(String accountNumber, double amount) {
5767
* @param check The check to deposit.
5868
*/
5969
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
70+
Account account = getAccountOrThrow(accountNumber);
6171
check.depositFunds(account);
6272
}
6373

@@ -68,7 +78,7 @@ public void depositFunds(String accountNumber, Check check) {
6878
* @param amount
6979
*/
7080
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
81+
Account account = getAccountOrThrow(accountNumber);
7282
account.withdraw(amount);
7383
}
7484

@@ -78,8 +88,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7888
* @param accountNumber The account number.
7989
* @return The account.
8090
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
91+
private Account getAccountOrThrow(String accountNumber) {
92+
Account account = accountByNumber.get(accountNumber);
8393
if (account == null || account.isClosed()) {
8494
throw new AccountNotFoundException("Account not found");
8595
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
import com.codedifferently.lesson17.bank.exceptions.InvalidBusinessAccountException;
6+
7+
public class BusinessCheckingAccount extends CheckingAccount {
8+
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
9+
super(accountNumber, owners, initialBalance);
10+
11+
boolean hasBusinessOwner = owners.stream().anyMatch(Customer::isBusiness);
12+
if (!hasBusinessOwner) {
13+
throw new InvalidBusinessAccountException("At least one owner must be a business.");
14+
}
15+
}
16+
}

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

Lines changed: 8 additions & 3 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 toAccount The account to deposit the check into.
46+
* @param account2 The account to deposit the check into.
4747
*/
48-
public void depositFunds(CheckingAccount toAccount) {
48+
public void depositFunds(Account account2) {
4949
if (isVoided) {
5050
throw new CheckVoidedException("Check is voided");
5151
}
5252
account.withdraw(amount);
53-
toAccount.deposit(amount);
53+
account2.deposit(amount);
5454
voidCheck();
5555
}
5656

@@ -79,4 +79,9 @@ 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+
}
8287
}
Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,9 @@
11
package com.codedifferently.lesson17.bank;
22

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

6-
/** Represents a checking account. */
7-
public class CheckingAccount {
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-
*/
5+
public class CheckingAccount extends Account {
216
public CheckingAccount(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 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-
+ '}';
7+
super(accountNumber, owners, initialBalance);
1308
}
131-
}
9+
}

0 commit comments

Comments
 (0)