Skip to content

Commit 0e0ec62

Browse files
author
Evan Philakhong
committed
feat: adds Account superclass for CheckingAccount
1 parent 9762cfd commit 0e0ec62

File tree

2 files changed

+110
-107
lines changed

2 files changed

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

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
3+
import lombok.Getter;
4+
45
import java.util.Set;
56

67
/** 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;
8+
@Getter
9+
public class CheckingAccount extends Account {
1310

1411
/**
1512
* Creates a new checking account.
1613
*
17-
* @param accountNumber The account number.
18-
* @param owners The owners of the account.
14+
* @param accountNumber The account number.
15+
* @param owners The owners of the account.
1916
* @param initialBalance The initial balance of the account.
2017
*/
2118
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;
19+
super(accountNumber, owners, initialBalance);
11720
}
11821

11922
@Override
12023
public String toString() {
12124
return "CheckingAccount{"
12225
+ "accountNumber='"
123-
+ accountNumber
26+
+ getAccountNumber()
12427
+ '\''
12528
+ ", balance="
126-
+ balance
29+
+ getBalance()
12730
+ ", isActive="
128-
+ isActive
31+
+ isActive()
12932
+ '}';
13033
}
13134
}

0 commit comments

Comments
 (0)