You have been given a code snippet that models a bank account in Java. The BankAccount
class has a constructor that takes an initial balance as a parameter, and two methods: withdraw
and deposit
for making withdrawals and deposits, respectively. The withdraw
method should throw a BankAccountException
if the withdrawal amount is greater than the current balance.
Your task is to complete the withdraw
method and implement a BankAccountException
class that extends the Exception
class.
public class BankAccount {
private double balance;
// Constructor
public BankAccount(double initialBalance) {
balance = initialBalance;
}
// Method for withdrawing money
public void withdraw(double amount) {
// TODO: Add exception handling code here
}
// Method for depositing money
public void deposit(double amount) {
balance += amount;
}
}
Implement the BankAccountException
class that extends the Exception
class. This exception should have a constructor that takes a message as a parameter, and passes the message to the parent Exception
class.
Modify the withdraw
method in the BankAccount
class to throw a BankAccountException
if the withdrawal amount is greater than the current balance.
If an exception is thrown, print the message "Withdrawal amount exceeds available balance" to the console.
Write the complete code for the BankAccountException
class and modify the withdraw
method in the BankAccount
class according to the instructions.
Note: You do not have to implement any input/output logic or a main method. Just write the required classes and methods.
public class BankAccountException extends Exception {
public BankAccountException(String message) {
super(message);
}
}
public class BankAccount {
private double balance;
// Constructor
public BankAccount(double initialBalance) {
balance = initialBalance;
}
// Method for withdrawing money
public void withdraw(double amount) throws BankAccountException {
if (amount > balance) {
throw new BankAccountException("Withdrawal amount exceeds available balance");
}
balance -= amount;
}
// Method for depositing money
public void deposit(double amount) {
balance += amount;
}
}
To implement the BankAccountException
class, we extend the Exception
class using the extends
keyword. The exception has a constructor that takes a String
parameter message
, and passes the message
to the parent Exception
class using the super
keyword.
In the withdraw
method of the BankAccount
class, we add the throws BankAccountException
declaration, indicating that this method can throw a BankAccountException
.
We check if the amount
is greater than the balance
inside the withdraw
method. If it is, we throw a new instance of BankAccountException
with the message "Withdrawal amount exceeds available balance" using the throw
keyword.
If the amount
is less than or equal to the balance
, we subtract the amount
from the balance
.
By implementing the BankAccountException
class and modifying the withdraw
method as mentioned, we can handle the case where the withdrawal amount exceeds the available balance and throw the custom exception accordingly.