Skip to content

Commit e2b48dd

Browse files
author
“A1-4U2T1NN”
committed
feat: created abstract class 'BankAccount' that uses all base methods of 'CheckingAccount'; created 'SavingsAccount' class that is extended from 'BankAccount'; changed 'CheckingAccount' to be an extension of 'BankAccount';
1 parent 11135a9 commit e2b48dd

File tree

3 files changed

+149
-89
lines changed

3 files changed

+149
-89
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 BankAccount {
7+
protected final Set<Customer> owners;
8+
protected final String accountNumber;
9+
protected double balance;
10+
protected boolean isActive;
11+
12+
/**
13+
* Creates a new checking account.
14+
*
15+
* @param accountNumber The account number.
16+
* @param owners The owners of the account.
17+
* @param initialBalance The initial balance of the account.
18+
*/
19+
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
20+
this.accountNumber = accountNumber;
21+
this.owners = owners;
22+
this.balance = initialBalance;
23+
isActive = true;
24+
}
25+
26+
/**
27+
* Gets the account number.
28+
*
29+
* @return The account number.
30+
*/
31+
public String getAccountNumber() {
32+
return accountNumber;
33+
}
34+
35+
/**
36+
* Gets the owners of the account.
37+
*
38+
* @return The owners of the account.
39+
*/
40+
public Set<Customer> getOwners() {
41+
return owners;
42+
}
43+
44+
/**
45+
* Deposits funds into the account.
46+
*
47+
* @param amount The amount to deposit.
48+
*/
49+
public void deposit(double amount) throws IllegalStateException {
50+
if (isClosed()) {
51+
throw new IllegalStateException("Cannot deposit to a closed account");
52+
}
53+
if (amount <= 0) {
54+
throw new IllegalArgumentException("Deposit amount must be positive");
55+
}
56+
balance += amount;
57+
}
58+
59+
/**
60+
* Withdraws funds from the account.
61+
*
62+
* @param amount
63+
* @throws InsufficientFundsException
64+
*/
65+
public void withdraw(double amount) throws InsufficientFundsException {
66+
if (isClosed()) {
67+
throw new IllegalStateException("Cannot withdraw from a closed account");
68+
}
69+
if (amount <= 0) {
70+
throw new IllegalStateException("Withdrawal amount must be positive");
71+
}
72+
if (balance < amount) {
73+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
74+
}
75+
balance -= amount;
76+
}
77+
78+
/**
79+
* Gets the balance of the account.
80+
*
81+
* @return The balance of the account.
82+
*/
83+
public double getBalance() {
84+
return balance;
85+
}
86+
87+
/** Closes the account. */
88+
public void closeAccount() throws IllegalStateException {
89+
if (balance > 0) {
90+
throw new IllegalStateException("Cannot close account with a positive balance");
91+
}
92+
isActive = false;
93+
}
94+
95+
/**
96+
* Checks if the account is closed.
97+
*
98+
* @return True if the account is closed, otherwise false.
99+
*/
100+
public boolean isClosed() {
101+
return !isActive;
102+
}
103+
}

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

Lines changed: 2 additions & 89 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,88 +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;
16+
super(accountNumber, owners, initialBalance);
10417
}
10518

10619
@Override
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
/** Represents a checking account. */
6+
public class SavingsAccount extends BankAccount{
7+
8+
/**
9+
* Creates a new checking account.
10+
*
11+
* @param accountNumber The account number.
12+
* @param owners The owners of the account.
13+
* @param initialBalance The initial balance of the account.
14+
*/
15+
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
16+
super(accountNumber, owners, initialBalance);
17+
}
18+
19+
@Override
20+
public int hashCode() {
21+
return accountNumber.hashCode();
22+
}
23+
24+
@Override
25+
public boolean equals(Object obj) {
26+
if (obj instanceof CheckingAccount other) {
27+
return accountNumber.equals(other.accountNumber);
28+
}
29+
return false;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "CheckingAccount{"
35+
+ "accountNumber='"
36+
+ accountNumber
37+
+ '\''
38+
+ ", balance="
39+
+ balance
40+
+ ", isActive="
41+
+ isActive
42+
+ '}';
43+
}
44+
}

0 commit comments

Comments
 (0)