In: Computer Science
ou must write tests for the following:
You must write tests for the following (which may include Custom Exceptions):
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; } }
==========================================
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);
}
}