Question

In: Computer Science

ou must write tests for the following: You must write tests for the following (which may...

ou must write tests for the following:

You must write tests for the following (which may include Custom Exceptions):

  • BankAccount Class
    • Tests are written such that any deposit that is made greater than 10000 is not accepted.

    • Tests are written such that balance in the BankAccount does not go below 0.

    • Care should be taken for above tests at the time of Initial Deposit and at the time of Withdrawal and future Deposits.

    • Tests should be written such that the Bank AccountID only accepts a string of length 4 with first letter as a character followed by 3 integers, Eg., "A001", is a valid AccountID.

    • ==================================================================================================================

    • java code

      =============

      public class BankAccount {
              
              private String accountID;
              private double balance;
              
      
                 /**
                    Constructs a bank account with a zero balance
                    @param accountID - ID of the Account
                 */
                 public BankAccount(String accountID)
                 {   
                    balance = 0;
                    this.accountID = accountID;
                 }
      
                 /**
                    Constructs a bank account with a given balance
                    @param initialBalance the initial balance
                    @param accountID - ID of the Account
                 */
                 public BankAccount(double initialBalance, String accountID) 
                 {   
      
                              this.accountID = accountID;
                              balance = initialBalance;
                 }
                 
                 /**
                       * Returns the Account ID
                       * @return the accountID
                       */
                      public String getAccountID() {
                              return accountID;
                      }
                      
                      /**
                       * Sets the Account ID
                       * @param accountID
                       */
                      public void setAccountID(String accountID) {
                              this.accountID = accountID;
                      }
      
                 /**
                    Deposits money into the bank account.
                    @param amount the amount to deposit
                 */
                 public void deposit(double amount) 
                 {  
      
                              balance += amount;
                 }
      
                 /**
                    Withdraws money from the bank account.
                    @param amount the amount to withdraw
                    @return true if allowed to withdraw
                 */
                 public boolean withdraw(double amount) 
                 {   
                        boolean isAllowed = balance >= amount;
                        if (isAllowed)
                                balance -= amount;
                        return isAllowed;
                 }
      
                 /**
                    Gets the current balance of the bank account.
                    @return the current balance
                 */
                 public double getBalance()
                 {   
                    return balance;
                 }
                
              }
      

      ==========================================

Solutions

Expert Solution


import java.lang.*;
class MyException extends Exception
{
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
public class Main {
  
private String accountID;
private double balance;
  

/**
Constructs a bank account with a zero balance
@param accountID - ID of the Account
*/
public Main(String accountID)
{   
try {
if(checkaccount(accountID))
{
balance = 0;
this.accountID = accountID;
}
else
throw new MyException("Bank AccountID only accepts a string of length 4 with first letter as a character followed by 3 integers");
} catch(Exception e)
{

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

public boolean checkaccount(String accId)
{
if(accId.length()!=4)
return false;
else
{
if(!Character.isLetter(accId.charAt(0)))
return false;
else if(!(Character.isDigit(accId.charAt(1)) && Character.isDigit(accId.charAt(2)) && Character.isDigit(accId.charAt(3))))
return false;
}
return true;
  
}
  
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
@param accountID - ID of the Account
*/
public Main(double initialBalance, String accountID)
{   
try {
if(checkaccount(accountID))
{
balance = 0;
this.accountID = accountID;
}
else
throw new MyException("Bank AccountID only accepts a string of length 4 with first letter as a character followed by 3 integers");
} catch(Exception e)
{

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

try
{
  
  
if(initialBalance>10000)
throw new MyException("Deposit greater than 10000");
balance = initialBalance;
}
catch(Exception e)
{

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

/**
* Returns the Account ID
* @return the accountID
*/
public String getAccountID()
{
return accountID;
}
  
/**
* Sets the Account ID
* @param accountID
*/
public void setAccountID(String accountID)
{
this.accountID = accountID;
}

/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
try
{
if(amount>10000)
throw new MyException("Deposit greater than 10000");
  
balance += amount;
}
catch(Exception e)
{

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

/**
Withdraws money from the bank account.
@param amount the amount to withdraw
@return true if allowed to withdraw
*/
public boolean withdraw(double amount)
{   
boolean isAllowed = balance >= amount;
if(balance - amount <=0)
{
isAllowed=false;
System.out.println("Withdrwal not possible ");

}
if (isAllowed)
balance -= amount;
return isAllowed;
}

/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{   
return balance;
}
  
public static void main(String args[])
{
Main M=new Main(1000,"A122");
M.deposit(2000);
M.withdraw(5000);
}
}


Related Solutions

You must write tests for the following: all tests must be written in JUnit 5. Customer...
You must write tests for the following: all tests must be written in JUnit 5. Customer Class Checking that the customer number autoincrements with each new customer object created Order Class Checking that the order number autoincrements with each new order object created Adding a product that is already in the order Removing a product that is in the order - quantity less than the quantity in the order Removing a product that is in the order - quantity that...
All but which of the following tests must be met in order for a retirement plan...
All but which of the following tests must be met in order for a retirement plan to receive favorable tax treatment as a qualified plan? a. Percentage test b. Ratio test c. Participation test d. Average benefit test 14. An accounting firm has 125 employees. All employees are covered by the firm’s defined benefit plan except for 70 junior associates. Has the minimum participation requirement been met for the plan? a. Yes, both parts of the 50/40 test have been...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
You will write a program in Scheme which compares the accuracy of two medical tests for...
You will write a program in Scheme which compares the accuracy of two medical tests for diagnosing a particular disease. The input to your program will be a list of data about a collection of patients specifying Whether the patient has the disease Whether the result of test 1 was positive Whether the result of test 2 was positive The input format will be a sequence of entries of this form: A positive integer, which is the number of the...
Problem Scenario: Following is a problem description. For all hypothesis tests, you MUST state the statistical...
Problem Scenario: Following is a problem description. For all hypothesis tests, you MUST state the statistical test you are using and use the P-VALUE METHOD through Microsoft Excel to make your decision. Show all steps, calculations, and work. For confidence intervals there is a specific Excel tool for each interval.  Treat each part of the question as a separate problem -- we use the same data set but are answering different “research questions”. Many parts of cars are mechanically tested to...
Write at least a 1050-word paper in which you answer the following questions:. Your response must...
Write at least a 1050-word paper in which you answer the following questions:. Your response must address all aspects of each question, must include your personal opinions, ideas, or thoughts; and must provide examples in any situation where it is warranted. What is the typical IT implementation process? What are the roles and responsibilities involved in system implementation? Please give at least one reference
Write an accurate concluding statement for the following hypothesis tests. (a) You claim that the mean...
Write an accurate concluding statement for the following hypothesis tests. (a) You claim that the mean volume of all 12 ounce cans of Fizzy Pop is less than 12 ounces. After analyzing the data and performing a hypothesis test, you fail to reject the null hypothesis. (b) Fizzy Pop claims that most 12 ounce cans of Fizzy Pop contain more than 12 ounces. After analyzing the data and performing a hypothesis test, you reject the null hypothesis. (c) You claim...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: Border size? 2px, 5px, or 8px. Border Color? blue, red, green, and black. Border style? solid, dotted, dashed, and double. The HTML document...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: - Border size? 2px, 5px, or 8px. - Border Color? blue, red, green, and black. - Border style? solid, dotted, dashed, and double....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT