Skip to content

Commit 3939fb5

Browse files
committed
feat: added BankAccount file and modified checking account class.
1 parent ba5326d commit 3939fb5

File tree

2 files changed

+116
-1
lines changed

2 files changed

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Set;
55

66
/** Represents a checking account. */
7-
public class CheckingAccount {
7+
public class CheckingAccount extends BankAccount{
88

99
private final Set<Customer> owners;
1010
private final String accountNumber;

0 commit comments

Comments
 (0)