Skip to content

Commit b33bd24

Browse files
committed
feat: Lesson17HW implement SavingsAccount
1 parent 1c93014 commit b33bd24

File tree

7 files changed

+429
-61
lines changed

7 files changed

+429
-61
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Represents a bank account interface that defines common account operations. */
7+
public interface Account {
8+
9+
/**
10+
* Gets the account number.
11+
*
12+
* @return The account number.
13+
*/
14+
String getAccountNumber();
15+
16+
/**
17+
* Gets the owners of the account.
18+
*
19+
* @return The owners of the account.
20+
*/
21+
Set<Customer> getOwners();
22+
23+
/**
24+
* Deposits funds into the account.
25+
*
26+
* @param amount The amount to deposit.
27+
* @throws IllegalStateException If the account is closed.
28+
* @throws IllegalArgumentException If the amount is not positive.
29+
*/
30+
void deposit(double amount) throws IllegalStateException, IllegalArgumentException;
31+
32+
/**
33+
* Withdraws funds from the account.
34+
*
35+
* @param amount The amount to withdraw.
36+
* @throws InsufficientFundsException When there are not enough funds.
37+
* @throws IllegalStateException If the account is closed or amount is not positive.
38+
*/
39+
void withdraw(double amount) throws InsufficientFundsException, IllegalStateException;
40+
41+
/**
42+
* Gets the balance of the account.
43+
*
44+
* @return The balance of the account.
45+
*/
46+
double getBalance();
47+
48+
/**
49+
* Closes the account.
50+
*
51+
* @throws IllegalStateException If the account cannot be closed.
52+
*/
53+
void closeAccount() throws IllegalStateException;
54+
55+
/**
56+
* Checks if the account is closed.
57+
*
58+
* @return True if the account is closed, otherwise false.
59+
*/
60+
boolean isClosed();
61+
}

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
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 an 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) {
2121
accountByNumber.put(account.getAccountNumber(), account);
2222
account
2323
.getOwners()
@@ -27,13 +27,31 @@ public void addAccount(CheckingAccount account) {
2727
});
2828
}
2929

30+
/**
31+
* Adds a checking account to the bank.
32+
*
33+
* @param account The checking account to add.
34+
*/
35+
public void addAccount(CheckingAccount account) {
36+
addAccount((Account) account);
37+
}
38+
39+
/**
40+
* Adds a savings account to the bank.
41+
*
42+
* @param account The savings account to add.
43+
*/
44+
public void addAccount(SavingsAccount account) {
45+
addAccount((Account) account);
46+
}
47+
3048
/**
3149
* Finds all accounts owned by a customer.
3250
*
3351
* @param customerId The ID of the customer.
3452
* @return The unique set of accounts owned by the customer.
3553
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
54+
public Set<Account> findAccountsByCustomerId(UUID customerId) {
3755
return customerById.containsKey(customerId)
3856
? customerById.get(customerId).getAccounts()
3957
: Set.of();
@@ -46,7 +64,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4664
* @param amount The amount to deposit.
4765
*/
4866
public void depositFunds(String accountNumber, double amount) {
49-
CheckingAccount account = getAccountOrThrow(accountNumber);
67+
Account account = getAccountOrThrow(accountNumber);
5068
account.deposit(amount);
5169
}
5270

@@ -57,18 +75,25 @@ public void depositFunds(String accountNumber, double amount) {
5775
* @param check The check to deposit.
5876
*/
5977
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
61-
check.depositFunds(account);
78+
Account account = getAccountOrThrow(accountNumber);
79+
80+
// Check deposits only work with checking accounts
81+
if (!(account instanceof CheckingAccount)) {
82+
throw new IllegalArgumentException("Check deposits are only allowed for checking accounts");
83+
}
84+
85+
CheckingAccount checkingAccount = (CheckingAccount) account;
86+
check.depositFunds(checkingAccount);
6287
}
6388

6489
/**
6590
* Withdraws funds from an account.
6691
*
67-
* @param accountNumber
68-
* @param amount
92+
* @param accountNumber The account number.
93+
* @param amount The amount to withdraw.
6994
*/
7095
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
96+
Account account = getAccountOrThrow(accountNumber);
7297
account.withdraw(amount);
7398
}
7499

@@ -78,8 +103,8 @@ public void withdrawFunds(String accountNumber, double amount) {
78103
* @param accountNumber The account number.
79104
* @return The account.
80105
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
106+
private Account getAccountOrThrow(String accountNumber) {
107+
Account account = accountByNumber.get(accountNumber);
83108
if (account == null || account.isClosed()) {
84109
throw new AccountNotFoundException("Account not found");
85110
}

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Set;
55

66
/** Represents a checking account. */
7-
public class CheckingAccount {
7+
public class CheckingAccount implements Account {
88

99
private final Set<Customer> owners;
1010
private final String accountNumber;
@@ -25,29 +25,17 @@ public CheckingAccount(String accountNumber, Set<Customer> owners, double initia
2525
isActive = true;
2626
}
2727

28-
/**
29-
* Gets the account number.
30-
*
31-
* @return The account number.
32-
*/
28+
@Override
3329
public String getAccountNumber() {
3430
return accountNumber;
3531
}
3632

37-
/**
38-
* Gets the owners of the account.
39-
*
40-
* @return The owners of the account.
41-
*/
33+
@Override
4234
public Set<Customer> getOwners() {
4335
return owners;
4436
}
4537

46-
/**
47-
* Deposits funds into the account.
48-
*
49-
* @param amount The amount to deposit.
50-
*/
38+
@Override
5139
public void deposit(double amount) throws IllegalStateException {
5240
if (isClosed()) {
5341
throw new IllegalStateException("Cannot deposit to a closed account");
@@ -58,12 +46,7 @@ public void deposit(double amount) throws IllegalStateException {
5846
balance += amount;
5947
}
6048

61-
/**
62-
* Withdraws funds from the account.
63-
*
64-
* @param amount
65-
* @throws InsufficientFundsException
66-
*/
49+
@Override
6750
public void withdraw(double amount) throws InsufficientFundsException {
6851
if (isClosed()) {
6952
throw new IllegalStateException("Cannot withdraw from a closed account");
@@ -77,28 +60,20 @@ public void withdraw(double amount) throws InsufficientFundsException {
7760
balance -= amount;
7861
}
7962

80-
/**
81-
* Gets the balance of the account.
82-
*
83-
* @return The balance of the account.
84-
*/
63+
@Override
8564
public double getBalance() {
8665
return balance;
8766
}
8867

89-
/** Closes the account. */
68+
@Override
9069
public void closeAccount() throws IllegalStateException {
9170
if (balance > 0) {
9271
throw new IllegalStateException("Cannot close account with a positive balance");
9372
}
9473
isActive = false;
9574
}
9675

97-
/**
98-
* Checks if the account is closed.
99-
*
100-
* @return True if the account is closed, otherwise false.
101-
*/
76+
@Override
10277
public boolean isClosed() {
10378
return !isActive;
10479
}

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Customer {
99

1010
private final UUID id;
1111
private final String name;
12-
private final Set<CheckingAccount> accounts = new HashSet<>();
12+
private final Set<Account> accounts = new HashSet<>();
1313

1414
/**
1515
* Creates a new customer.
@@ -41,20 +41,38 @@ public String getName() {
4141
}
4242

4343
/**
44-
* Adds a checking account to the customer.
44+
* Adds an account to the customer.
4545
*
4646
* @param account The account to add.
4747
*/
48-
public void addAccount(CheckingAccount account) {
48+
public void addAccount(Account account) {
4949
accounts.add(account);
5050
}
5151

52+
/**
53+
* Adds a checking account to the customer.
54+
*
55+
* @param account The checking account to add.
56+
*/
57+
public void addAccount(CheckingAccount account) {
58+
addAccount((Account) account);
59+
}
60+
61+
/**
62+
* Adds a savings account to the customer.
63+
*
64+
* @param account The savings account to add.
65+
*/
66+
public void addAccount(SavingsAccount account) {
67+
addAccount((Account) account);
68+
}
69+
5270
/**
5371
* Gets the accounts owned by the customer.
5472
*
5573
* @return The unique set of accounts owned by the customer.
5674
*/
57-
public Set<CheckingAccount> getAccounts() {
75+
public Set<Account> getAccounts() {
5876
return accounts;
5977
}
6078

0 commit comments

Comments
 (0)