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

Practice how to handle the errors caused by users\' inputs Throw one of the jave

ID: 3625536 • Letter: P

Question

Practice how to handle the errors caused by users' inputs Throw one of the jave Exception Download the inventory items class Add the following lines code to the top of class inventory item Write the controlling class named Exception Tester with the main method that includes the following tasks: Read the Description and the units id from the keyboard Create an object of inventory item by calling parameter constructor with description and unit id have read. Remember to add the code to handle the errors if users invalid description or unit id Display the item information Ask users the item information Ask users if they want to continue if yes, repeat tasks, 1 to 3 Create own exception Write an Exception class named invalidinputException where it will display message Write a class name SaavingAccount that has 4 fields: name, accountnum, balance and interestRate and the parameter constructor. In the parameter constructor, if the name is an empty String, the accountNum is 0 or negetiv, and the balance or interestRate is 0 or negetive throw invalidinputException Write a class name Exception Tester that read information and create a SavingAccount where you should handle error exception if users enter a blank name, a 0 or negetive for account number, balance and interest rate Also, give a chance to continue create other SavingAccount if users want to continue.

Explanation / Answer

/**

* NegativeStartingBalance exceptions are thrown by

* the BankAccount class when a negative starting

* balance is passed to the constructor.

*/

public class NegativeStartingBalance extends Exception

{

   /**

    * No-arg constructor

    */

   public NegativeStartingBalance()

   {

      super("Error: Negative starting balance");

   }

   /**

    * The following constructor accepts the amount

    * that was given as the starting balance.

    */

   public NegativeStartingBalance(double amount)

   {

      super("Error: Negative starting balance: " +

            amount);

   }

}

/**

* NegativeInterestRate exceptions are thrown by the

* BankAccount class when a negative interest rate is

* passed to the constructor.

*/

public class NegativeInterestRate extends Exception

{

   /**

    * No-arg constructor

    */

   public NegativeInterestRate()

   {

      super("Error: Negative interest rate");

   }

   /**

    * The following constructor accepts the amount that

    * was given as the interest rate.

    */

   public NegativeInterestRate(double amount)

   {

      super("Error: Negative interest rate: " + amount);

   }

}

/**

* savingAccount class

* This class simulates a bank account.

*/

public class savingAccount

{

     private String Name;

   private double balance;      // Account balance

   private double interestRate; // Interest rate

   private double interest;     // Interest earned

     /**

     * The constructor initializes the balance

     * and interestRatet fields with the values

     * passed to startBalance and intRate. The

     * interest field is assigned 0.0.

     */

   public savingAccount(String Nm,double startBalance,           double intRate)throws NegativeStartingBalance,                 NegativeInterestRate

   {

       Name=Nm;

        if (startBalance < 0)

         throw new NegativeStartingBalance(startBalance);

      if (intRate < 0)

         throw new NegativeInterestRate(intRate);

      balance = startBalance;

      interestRate = intRate;

      interest = 0.0;

   }

     /**

     * The deposit method adds the parameter

     * amount to the balance field.

     */

   public void deposit(double amount)

   {

      balance = balance + amount;

   }

     /**

     * The withdraw method subtracts the

     * parameter amount from the balance

     * field.

     */

   public void withdraw(double amount)

   {

      balance = balance - amount;

   }

     /**

     * The addInterest method adds the

     * interest for the month to balance.

     */

   public void addInterest()

   {

      interest = balance * interestRate;

      balance = balance + interest;

   }

     /**

     * The getBalance method returns the   

     * value in the balance field.

     */

   public double getBalance()

   {

      return balance;

   }

     /**

     * The getInterest method returns the   

     * value in the interest field.  

     */

   public double getInterest()

   {

      return interest;

   }

}

/**

* This program demonstrates how the /**

* savingAccount class

* This class simulates a bank account.

*/

public class savingAccount

{

     private String Name;

     private double balance;      // Account balance

     private double interestRate; // Interest rate

     private double interest;     // Interest earned

     /**

     * The constructor initializes the balance

     * and interestRatet fields with the values

     * passed to startBalance and intRate. The

     * interest field is assigned 0.0.

     */

     public savingAccount(String Nm,double startBalance,

          double intRate) throws NegativeStartingBalance,

          NegativeInterestRate

     {

          Name=Nm;

          if (startBalance < 0)

              throw new NegativeStartingBalance(startBalance);

          if (intRate < 0)

              throw new NegativeInterestRate(intRate);

          balance = startBalance;

          interestRate = intRate;

          interest = 0.0;

     }

     /**

     * The deposit method adds the parameter

     * amount to the balance field.

     */

     public void deposit(double amount)

     {

          balance = balance + amount;

     }

     /**

     * The withdraw method subtracts the

     * parameter amount from the balance

     * field.

     */

     public void withdraw(double amount)

     {

          balance = balance - amount;

     }

     /**

     * The addInterest method adds the

     * interest for the month to balance.

     */

     public void addInterest()

     {

          interest = balance * interestRate;

          balance = balance + interest;

     }

     /**

     * The getBalance method returns the   

     * value in the balance field.

     */

     public double getBalance()

     {

          return balance;

     }

     /**

     * The getInterest method returns the   

     * value in the interest field.  

     */

     public double getInterest()

     {

          return interest;

     }

}

* class constructor throws custom exceptions.

*/

public class AccountTest

{

   public static void main(String[] args)

   {

      // Force a NegativeStartingBalance exception.

      try

      {

         savingAccount account =

             new savingAccount("Reena",-1, 0.04);

      }

      catch(NegativeStartingBalance e)

      {

         System.out.println(e.getMessage());

      }

      catch(NegativeInterestRate e)

      {

         System.out.println(e.getMessage());

      }

      // Force a NegativeInterestRate exception.

      try

      {

         savingAccount account = new

                   savingAccount("Rachel",100, -0.04);

      }

      catch(NegativeStartingBalance e)

      {

         System.out.println(e.getMessage());

      }

      catch(NegativeInterestRate e)

      {

         System.out.println(e.getMessage());

      }

   }

}

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