Skip to content

Commit b0099ab

Browse files
committed
feat: adds savingsaccount and businesscheckingaccount class modified other classes
1 parent 4ecefe4 commit b0099ab

File tree

9 files changed

+265
-121
lines changed

9 files changed

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

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

Lines changed: 8 additions & 8 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
/**
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(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

@@ -57,7 +57,7 @@ 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);
60+
BankAccount account = getAccountOrThrow(accountNumber);
6161
check.depositFunds(account);
6262
}
6363

@@ -68,7 +68,7 @@ public void depositFunds(String accountNumber, Check check) {
6868
* @param amount
6969
*/
7070
public void withdrawFunds(String accountNumber, double amount) {
71-
CheckingAccount account = getAccountOrThrow(accountNumber);
71+
BankAccount account = getAccountOrThrow(accountNumber);
7272
account.withdraw(amount);
7373
}
7474

@@ -78,8 +78,8 @@ public void withdrawFunds(String accountNumber, double amount) {
7878
* @param accountNumber The account number.
7979
* @return The account.
8080
*/
81-
private CheckingAccount getAccountOrThrow(String accountNumber) {
82-
CheckingAccount account = accountByNumber.get(accountNumber);
81+
private BankAccount getAccountOrThrow(String accountNumber) {
82+
BankAccount account = accountByNumber.get(accountNumber);
8383
if (account == null || account.isClosed()) {
8484
throw new AccountNotFoundException("Account not found");
8585
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
package com.codedifferently.lesson17.bank;
22

3-
public class SavingAccount {
4-
5-
}
3+
public class BusinessCheckingAccount {}

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

Lines changed: 3 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(BankAccount 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

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

Lines changed: 2 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package com.codedifferently.lesson17.bank;
22

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

65
/** 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;
6+
public class CheckingAccount extends BankAccount {
137

148
/**
159
* Creates a new checking account.
@@ -19,101 +13,7 @@ public class CheckingAccount {
1913
* @param initialBalance The initial balance of the account.
2014
*/
2115
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;
16+
super(accountNumber, owners, initialBalance);
11717
}
11818

11919
@Override

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

Lines changed: 3 additions & 3 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<BankAccount> accounts = new HashSet<>();
1313

1414
/**
1515
* Creates a new customer.
@@ -45,7 +45,7 @@ public String getName() {
4545
*
4646
* @param account The account to add.
4747
*/
48-
public void addAccount(CheckingAccount account) {
48+
public void addAccount(BankAccount 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<BankAccount> getAccounts() {
5858
return accounts;
5959
}
6060

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 java.util.Set;
4+
5+
public class SavingsAccount extends BankAccount {
6+
7+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
8+
super(accountNumber, owners, initialBalance);
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return "SavingsAccount{"
14+
+ "accountNumber='"
15+
+ accountNumber
16+
+ '\''
17+
+ ", balance="
18+
+ balance
19+
+ ", isActive="
20+
+ isActive
21+
+ '}';
22+
}
23+
}

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<BankAccount> 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<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
5454

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

0 commit comments

Comments
 (0)