-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckingAccount.java
More file actions
44 lines (39 loc) · 1.22 KB
/
CheckingAccount.java
File metadata and controls
44 lines (39 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* CheckingAccount class - demonstrates inheritance and method overriding.
*/
public class CheckingAccount extends BankAccount {
private double overdraftLimit;
/**
* Constructor.
*/
public CheckingAccount(String accountNumber, String pin, double initialBalance, double overdraftLimit) {
super(accountNumber, pin, initialBalance, "Checking");
this.overdraftLimit = overdraftLimit;
}
/**
* Overridden withdraw method to allow overdraft.
*/
@Override
public boolean withdraw(double amount) {
try {
if (amount <= 0) {
throw new IllegalArgumentException("Invalid amount");
}
// Allow withdrawal up to overdraft limit
if (amount > getBalance() + overdraftLimit) {
throw new InsufficientFundsException("Exceeds overdraft limit");
}
// If within overdraft, proceed with withdrawal
return super.withdraw(amount);
} catch (Exception e) {
System.err.println(e.getMessage());
return false;
}
}
/**
* Getter for overdraft limit.
*/
public double getOverdraftLimit() {
return overdraftLimit;
}
}