Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise, you will need to create a class named Account, this class rep

ID: 3813240 • Letter: F

Question

For this exercise, you will need to create a class named Account, this class represents a bank account This class has a constructor that sets the variable balance to 0. It also has two methods: deposit that takes a double and add that number to the balance, and withdraw that takes a double and removes that amount from the balance, both method should print the new balance. However, if the amount to withdraw is higher than the balance, you need to throw the NotEnoughMoneyException. You will need to create the NotEnoughMoneyException, it extends lllegalStateException. It has a method getAmount that returns the amount that was requested. getBalance that returns the balance at the time the exception was created and getMissingAmount that returns the amount of money needed to withdraw the requested amount. The exception should also invoke it's super constructor so that the method getMessage indicates that the amount cannot be withdrawn. When running the code you should get an output similar to this:

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author Namburi Ramesh
*/
public class Account {
private double balance; //variable to store balance
NotEnoughMoneyException nem; //variable to throw exception
public Account() {
balance=0; //initialise balance to 0
}
  
public void deposit(double amount){
balance=balance+amount; //add amount to existing balance
System.out.println("new balance="+balance+"$"); //print new balance
}
  
public void withdraw(double amount)throws NotEnoughMoneyException{
if(amount<=balance){
balance=balance-amount; //subtract amount from existing balance
System.out.println("new balance="+balance+"$"); //print new balance
}else{
nem=new NotEnoughMoneyException("you have not enough money to withdraw "+amount+"$", amount, balance);
throw nem;//throw exception if the requested amount is less than balance
}
}
  
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author Namburi Ramesh
*/
public class NotEnoughMoneyException extends IllegalStateException{

private double amount; //variable to store requsted amount at the time of exception
private double balance; //variable to store balance at the time exception occurs
  
public NotEnoughMoneyException(String s,double amount,double balance) {
super(s); //call super class constructor to initialise string that will be returned by getMessage method
this.amount=amount;
this.balance=balance;
}
  
public double getBalance(){
return balance;
}
  
public double getAmount(){
return amount;
}
  
public double getMissingAmount(){
return amount-balance;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote