Question: Build an InsufficientFundsException class . Create a new File called InsufficientFunds.java . Have this class extend from the Exception class. Also add to your Account
Build an InsufficientFundsException class. Create a new File called InsufficientFunds.java. Have this class extend from the Exception class. Also add to your Account class. Make it so that when the withdraw method attempt to set the balance below zero(the withdraw() method is called, and the amount the user is trying to withdraw is greater than the available balance), the InsufficientFundsException will be thrown
Lastly, in your Account classs main method, use try-catch blocks to catch this InsufficientFundsException when you call the withdraw() method.
Account.java -/
public class Account { public String owner; public double balance; public int accntnumber;
public Account(int acctnumber, String Owner, double balance) { this.owner = owner; this.balance = balance; this.accntnumber = acctnumber; } public Account() { this.owner = ""; this.balance = 0; this.accntnumber = 0; } public void getBalance() { System.out.println("Your current balance is " + this.balance); } public void setBalance(int b) { this.balance = b; } public void deposit(double amount) { this.balance = this.balance + amount; } public void display() { System.out.println("Owner: " + this.owner); System.out.println("Balance: " + this.balance); System.out.println("Account Number: " + this.accntnumber); } public void withdraw(double amount) { if (amount > this.balance) { System.out.println("Sorry, you have insufficient funds!"); } else { this.balance = this.balance - amount; } } }
/
Testexception/
public class TestExceptions { public static void main(String[] args) { int myArr = new int[10]; int x, y, z; x = 0; y = 10; try { z = y / x; } catch (ArithmeticException e) { System.out.println(e); } myArr[10] = 0; } }
/
Testexception2 /
public class TestExceptions2 { public static void main(String[] args) { int myArr = new int[10]; int x, y, z; x = 0; y = 10; if (x != 0) { z = y / x; } else { System.out.println("Divide by zero Exception"); } myArr[10] = 0; } }
/
Example of test
Accountest /
public class AccountTest2 { public static void main(String[] args) { Account a1; a1 = new Account(2222, "Frank", 500.00); a1.deposit(100.00); a1.withdraw(900.00); a1.display(); } }
/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
