Skip to content

Commit 2e45933

Browse files
feat: adds functionality for SavingsAccount and BusinessCheckingAccount and updated/added tests
1 parent a51414e commit 2e45933

File tree

12 files changed

+299
-150
lines changed

12 files changed

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

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

Lines changed: 13 additions & 11 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, BankAccount> 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(BankAccount account) {
2121
accountByNumber.put(account.getAccountNumber(), account);
2222
account
2323
.getOwners()
@@ -33,7 +33,7 @@ public void addAccount(CheckingAccount account) {
3333
* @param customerId The ID of the customer.
3434
* @return The unique set of accounts owned by the customer.
3535
*/
36-
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
36+
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
3737
return customerById.containsKey(customerId)
3838
? customerById.get(customerId).getAccounts()
3939
: Set.of();
@@ -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-
CheckingAccount account = getAccountOrThrow(accountNumber);
49+
BankAccount account = getAccountOrThrow(accountNumber);
5050
account.deposit(amount);
5151
}
5252

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

@@ -66,9 +67,10 @@ public void depositFunds(String accountNumber, Check check) {
6667
*
6768
* @param accountNumber
6869
* @param amount
70+
* @throws Exception
6971
*/
70-
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
72+
public void withdrawFunds(String accountNumber, double amount) throws Exception {
73+
BankAccount account = getAccountOrThrow(accountNumber);
7274
account.withdraw(amount);
7375
}
7476

@@ -78,8 +80,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7880
* @param accountNumber The account number.
7981
* @return The account.
8082
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
83+
private BankAccount getAccountOrThrow(String accountNumber) {
84+
BankAccount account = accountByNumber.get(accountNumber);
8385
if (account == null || account.isClosed()) {
8486
throw new AccountNotFoundException("Account not found");
8587
}
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::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/Check.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Check {
77

88
private final String checkNumber;
99
private final double amount;
10-
private final CheckingAccount account;
10+
private final BankAccount account;
1111
private boolean isVoided = false;
1212

1313
/**
@@ -17,7 +17,10 @@ public class Check {
1717
* @param amount The amount of the check.
1818
* @param account The account the check is drawn on.
1919
*/
20-
public Check(String checkNumber, double amount, CheckingAccount account) {
20+
public Check(String checkNumber, double amount, BankAccount account) {
21+
if (account instanceof SavingsAccount) {
22+
throw new IllegalArgumentException("Cannot write a check from a savings account");
23+
}
2124
if (amount < 0) {
2225
throw new IllegalArgumentException("Check amount must be positive");
2326
}
@@ -44,8 +47,9 @@ public void voidCheck() {
4447
* Deposits the check into an account.
4548
*
4649
* @param toAccount The account to deposit the check into.
50+
* @throws Exception
4751
*/
48-
public void depositFunds(CheckingAccount toAccount) {
52+
public void depositFunds(BankAccount toAccount) throws Exception {
4953
if (isVoided) {
5054
throw new CheckVoidedException("Check is voided");
5155
}

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

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

66
/** 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;
7+
public class CheckingAccount extends BankAccount {
138

149
/**
1510
* Creates a new checking account.
@@ -19,43 +14,7 @@ public class CheckingAccount {
1914
* @param initialBalance The initial balance of the account.
2015
*/
2116
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;
17+
super(accountNumber, owners, initialBalance);
5918
}
6019

6120
/**
@@ -76,56 +35,4 @@ public void withdraw(double amount) throws InsufficientFundsException {
7635
}
7736
balance -= amount;
7837
}
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-
+ '}';
130-
}
13138
}

0 commit comments

Comments
 (0)