Skip to content

Commit a6bac14

Browse files
feat:created files SavingAccount, Account, and SavingAccountTest, and the test for getAccountNumber passed
1 parent 1c93014 commit a6bac14

File tree

8 files changed

+289
-18
lines changed

8 files changed

+289
-18
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public interface Account {
6+
String getAccountNumber();
7+
8+
Set<Customer> getOwners();
9+
10+
void deposit(double amount);
11+
12+
void withdraw(double amount);
13+
14+
double getBalance();
15+
16+
void closeAccount();
17+
18+
boolean isClosed();
19+
}

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

Lines changed: 13 additions & 9 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
/**
1616
* Adds a checking 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()
@@ -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<Account> 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+
Account account = getAccountOrThrow(accountNumber);
5050
account.deposit(amount);
5151
}
5252

@@ -57,8 +57,12 @@ public void depositFunds(String accountNumber, double amount) {
5757
* @param check The check to deposit.
5858
*/
5959
public void depositFunds(String accountNumber, Check check) {
60-
CheckingAccount account = getAccountOrThrow(accountNumber);
61-
check.depositFunds(account);
60+
Account account = getAccountOrThrow(accountNumber);
61+
if (account instanceof CheckingAccount) {
62+
check.depositFunds(account);
63+
} else {
64+
System.out.println("Not a Checking Account");
65+
}
6266
}
6367

6468
/**
@@ -68,7 +72,7 @@ public void depositFunds(String accountNumber, Check check) {
6872
* @param amount
6973
*/
7074
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
75+
Account account = getAccountOrThrow(accountNumber);
7276
account.withdraw(amount);
7377
}
7478

@@ -78,8 +82,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7882
* @param accountNumber The account number.
7983
* @return The account.
8084
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
85+
private Account getAccountOrThrow(String accountNumber) {
86+
Account account = accountByNumber.get(accountNumber);
8387
if (account == null || account.isClosed()) {
8488
throw new AccountNotFoundException("Account not found");
8589
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void voidCheck() {
4545
*
4646
* @param toAccount The account to deposit the check into.
4747
*/
48-
public void depositFunds(CheckingAccount toAccount) {
48+
public void depositFunds(Account toAccount) {
4949
if (isVoided) {
5050
throw new CheckVoidedException("Check is voided");
5151
}

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

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

66
/** Represents a checking account. */
7-
public class CheckingAccount {
8-
7+
public class CheckingAccount implements Account {
98
private final Set<Customer> owners;
109
private final String accountNumber;
1110
private double balance;
@@ -30,6 +29,7 @@ public CheckingAccount(String accountNumber, Set<Customer> owners, double initia
3029
*
3130
* @return The account number.
3231
*/
32+
@Override
3333
public String getAccountNumber() {
3434
return accountNumber;
3535
}
@@ -39,6 +39,7 @@ public String getAccountNumber() {
3939
*
4040
* @return The owners of the account.
4141
*/
42+
@Override
4243
public Set<Customer> getOwners() {
4344
return owners;
4445
}
@@ -48,6 +49,7 @@ public Set<Customer> getOwners() {
4849
*
4950
* @param amount The amount to deposit.
5051
*/
52+
@Override
5153
public void deposit(double amount) throws IllegalStateException {
5254
if (isClosed()) {
5355
throw new IllegalStateException("Cannot deposit to a closed account");
@@ -64,6 +66,7 @@ public void deposit(double amount) throws IllegalStateException {
6466
* @param amount
6567
* @throws InsufficientFundsException
6668
*/
69+
@Override
6770
public void withdraw(double amount) throws InsufficientFundsException {
6871
if (isClosed()) {
6972
throw new IllegalStateException("Cannot withdraw from a closed account");
@@ -82,11 +85,13 @@ public void withdraw(double amount) throws InsufficientFundsException {
8285
*
8386
* @return The balance of the account.
8487
*/
88+
@Override
8589
public double getBalance() {
8690
return balance;
8791
}
8892

8993
/** Closes the account. */
94+
@Override
9095
public void closeAccount() throws IllegalStateException {
9196
if (balance > 0) {
9297
throw new IllegalStateException("Cannot close account with a positive balance");
@@ -99,6 +104,7 @@ public void closeAccount() throws IllegalStateException {
99104
*
100105
* @return True if the account is closed, otherwise false.
101106
*/
107+
@Override
102108
public boolean isClosed() {
103109
return !isActive;
104110
}

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

Lines changed: 4 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,11 +41,11 @@ 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

@@ -54,7 +54,7 @@ public void addAccount(CheckingAccount account) {
5454
*
5555
* @return The unique set of accounts owned by the customer.
5656
*/
57-
public Set<CheckingAccount> getAccounts() {
57+
public Set<Account> getAccounts() {
5858
return accounts;
5959
}
6060

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
/** Represents a savings account */
7+
public class SavingAccount implements Account {
8+
private final Set<Customer> owners;
9+
private final String accountNumber;
10+
private double balance;
11+
private boolean isActive;
12+
13+
/**
14+
* Creates a new savings account.
15+
*
16+
* @param accountNumber The account number.
17+
* @param owners The owners of the account.
18+
* @param initialBalance The initial balance of the account.
19+
*/
20+
public SavingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
21+
this.accountNumber = accountNumber;
22+
this.owners = owners;
23+
this.balance = balance;
24+
isActive = true;
25+
}
26+
27+
/**
28+
* Gets the account number.
29+
*
30+
* @return The account number.
31+
*/
32+
@Override
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+
@Override
43+
public Set<Customer> getOwners() {
44+
return owners;
45+
}
46+
47+
/**
48+
* Deposits funds into the account
49+
*
50+
* @param amount The amount to deposit.
51+
*/
52+
@Override
53+
public void deposit(double amount) throws IllegalStateException {
54+
if (isClosed()) {
55+
throw new IllegalStateException("Cannot deposit to a closed account");
56+
}
57+
if (amount <= 0) {
58+
throw new IllegalArgumentException("Deposit amount must be positive");
59+
}
60+
balance += amount;
61+
}
62+
63+
/**
64+
* Withdraws funds from the account.
65+
*
66+
* @param amount
67+
* @throws InsufficientFundsException
68+
*/
69+
@Override
70+
public void withdraw(double amount) throws InsufficientFundsException {
71+
if (isClosed()) {
72+
throw new IllegalStateException("Cannot withdraw from a closed account");
73+
}
74+
if (amount <= 0) {
75+
throw new IllegalStateException("Withdrawal amount must be positive");
76+
}
77+
if (balance < amount) {
78+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
79+
}
80+
balance -= amount;
81+
}
82+
83+
/**
84+
* Gets the balance of the account.
85+
*
86+
* @return The balance of the account.
87+
*/
88+
@Override
89+
public double getBalance() {
90+
return balance;
91+
}
92+
93+
/** Closes the account. */
94+
@Override
95+
public void closeAccount() throws IllegalStateException {
96+
if (balance > 0) {
97+
throw new IllegalStateException("Cannot close account with a positive balance");
98+
}
99+
isActive = false;
100+
}
101+
102+
/**
103+
* Checks if the account is closed.
104+
*
105+
* @return True if the account is closed, otherwise false.
106+
*/
107+
@Override
108+
public boolean isClosed() {
109+
return !isActive;
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return accountNumber.hashCode();
115+
}
116+
117+
@Override
118+
public boolean equals(Object obj) {
119+
if (obj instanceof SavingAccount other) {
120+
return accountNumber.equals(other.accountNumber);
121+
}
122+
return false;
123+
}
124+
125+
@Override
126+
public String toString() {
127+
return "SavingAccount{"
128+
+ "accountNumber='"
129+
+ accountNumber
130+
+ '\''
131+
+ ", balance="
132+
+ balance
133+
+ ", isActive="
134+
+ isActive
135+
+ '}';
136+
}
137+
}

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/BankAtmTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ void testAddAccount() {
4343
classUnderTest.addAccount(account3);
4444

4545
// Assert
46-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
46+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
4747
assertThat(accounts).containsOnly(account3);
4848
}
4949

5050
@Test
5151
void testFindAccountsByCustomerId() {
5252
// Act
53-
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
53+
Set<Account> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5454

5555
// Assert
5656
assertThat(accounts).containsOnly(account1, account2);

0 commit comments

Comments
 (0)