Professional Documents
Culture Documents
ArrayList;
import java.util.List;
class BankAccount {
private String accountNumber;
private double balance;
@Override
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > 0 && getBalance() + overdraftLimit >= amount) {
double overdraft = amount - getBalance();
if (overdraft > 0) {
System.out.println("Overdraft: $" + overdraft);
balance = 0.0;
throw new InsufficientFundsException("Insufficient funds for
withdrawal. Used overdraft.");
} else {
super.withdraw(amount);
}
} else {
throw new InsufficientFundsException("Insufficient funds for
withdrawal.");
}
}
}
accounts.add(savingsAccount);
accounts.add(checkingAccount);
savingsAccount.deposit(1000);
checkingAccount.deposit(500);
try {
savingsAccount.addInterest();
savingsAccount.withdraw(200);
savingsAccount.withdraw(2000); // This will throw an exception
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}
try {
checkingAccount.withdraw(1200);
checkingAccount.withdraw(2000); // This will throw an exception
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}