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

In Java, Write a class BankAccount with following member variables and methods:

ID: 643559 • Letter: I

Question

In Java,

Write a class BankAccount with following member variables and methods:

String id, int balance: represent the account's ID, and the balance in the account.

void depositMoney(int deposit): adds deposit amount to the balance.

void withdrawMoney(int withdraw): subtracts withdraw amount from the balance.

int getBalance(): returns the present balance in the account.

You have to write your own exception class NegativeBalanceException to ensure that the balance can never go negative. An attempt to make the balance less than 0 will throw an exception with the message

Explanation / Answer

/**
* The BankAccount class that have two instance variables
* to set id number and balance.
* The three possible cases to throw user defined exceptions are
* At the constructor when the balance is set.
* Check if balance is negative then throw NegativeBalanceException with a message
* that "Balance cannot be negative."
* The deposit method, when negative deposit amount is added
* to the balance. So call throw NegativeBalanceException with a message
* that "Balance cannot be negative."
* In withdraw method,when the subtracting withdraw amount
* from balance, negative balance may occur.
* So call throw NegativeBalanceException with a message
* that "Balance cannot be negative."
* */

//BankAccount.java
public class BankAccount
{
   //instance variables of class BankAccount
   private String id;
   private int balance;
  
   //Constructor that sets the id and balance
   public BankAccount(String id, int balance) throws NegativeBalanceException
   {
       this.id=id;
      
       //case 1: For negative balance to occur
       if(balance<0)
           throw new NegativeBalanceException("Balance cannot be negative.");
       else          
           this.balance=balance;
   }
  
   /*Adds deposit amount to the balance.*/
   void depositMoney(int deposit) throws NegativeBalanceException  
   {
       //case 2: For negative balance to occur
       if(deposit<0)
           throw new NegativeBalanceException("Balance cannot be negative.");
       else          
           this.balance=deposit;
   }
   /*Subtracts withdraw amount from the balance.*/
   void withdrawMoney(int withdraw) throws NegativeBalanceException
   {
       //case 3: For negative balance to occur
       if((balance-withdraw)<0)
           throw new NegativeBalanceException("Balance cannot be negative.");
       else          
           this.balance=balance-withdraw;
          
   }
   /*Returns the present balance in the account*/
   int getBalance()
   {
       return balance;
   }
  
  
}//end of BankAccount class


-----------------------------------------------------------------------------------------------


//NegativeBalanceException.java that extends the Exception
//class
public class NegativeBalanceException extends Exception
{
   public NegativeBalanceException(String message)
   {
       //calls super class Exception with a string
       //message
       super(message);
   }
}


---------------------------------------------------------------------------------------------------------


//TestBankAccount.java
public class TestBankAccount
{
   public static void main(String[] args) throws NegativeBalanceException
   {
      
       //Test case 1:
       //Throws exception for negative balance
       //Create a BankAccount class with negative balance
       BankAccount bankAccount= new BankAccount("ABC-Bank-777", -1000);
      
      
      
      
   }
}


-----------------------------------------------------------------------------------------------

Sample output:

Exception in thread "main" NegativeBalanceException: Balance cannot be negative.
   at BankAccount.<init>(BankAccount.java:34)
   at TestBankAccount.main(TestBankAccount.java:13)

------------------------------------------------------------------------------------------------------------------------

//Test program for test case 2:

//TestBankAccount.java
public class TestBankAccount
{
   public static void main(String[] args) throws NegativeBalanceException
   {


       //test case 2:
       //Create a BankAccount class with balance
       BankAccount bankAccount= new BankAccount("ABC-Bank-777", 1000);
       //Throws exception for negative balance
       bankAccount.depositMoney(-100);


   }
}

Sample output:

Exception in thread "main" NegativeBalanceException: Balance cannot be negative.
   at BankAccount.depositMoney(BankAccount.java:44)
   at TestBankAccount.main(TestBankAccount.java:15)

------------------------------------------------------------------------------------------------------------------------

//Test program for test case 3:
//TestBankAccount.java
public class TestBankAccount
{
   public static void main(String[] args) throws NegativeBalanceException
   {
       //Test case 3:
       //Create a BankAccount class with balance
       BankAccount bankAccount= new BankAccount("ABC-Bank-777", 1000);
       //Throws exception when withdraw money is more than balace amount
       bankAccount.withdrawMoney(5000);
   }
}


------------------------------------------------------------------------------------------------------------------

Sample output:

Exception in thread "main" NegativeBalanceException: Balance cannot be negative.
   at BankAccount.withdrawMoney(BankAccount.java:53)
   at TestBankAccount.main(TestBankAccount.java:13)


Hope this helps you.....

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