Question
Using JUnit: Define a CheckingAccountTest class, which needs to contain the following test methods: testCreate():Test whether a CheckingAccount object has been correctly created and initialized
Using JUnit:
Define a CheckingAccountTest class, which needs to contain the following test methods:
testCreate():Test whether a CheckingAccount object has been correctly created and initialized
testWithdraw(): Test whether the withdraw() method is correctly doing withdraw on checking account
testCreateMonthlyStatement(): Test whether the createMonthlyStatement() method is correctly returning a string of the checking account information.
1. When you use assertEquals to compare two floating-point number, you should provide three parameters:
assertEquals (expected, actual, tolerance).
The third parameter is tolerance, generally we use 10^(-9) for double type and 10^(-5) for float type. You could use Math.power(10, -9) to get the value of 10^(-9). It means, when two floating-point number are very very close (difference less than 10^(-9)), they are considered as equal.
2. To test withdraw method completely, you should test three cases:
(1) balance < minimumBalance
(2) balance >= minimumBalance && amount > balance
(3) balance >= minimumBalance && amount <= balance
Therefore, you need two CheckingAccount objects, one with balance < minimumBalance, the other with balance>=minimumBalance. Since you have created one in setUp method, you just need to declare and create another one (as a local variable) in testWithdraw method. You need to call withdraw method using these two CheckingAccount objects separately. Use assertFalse and assertTrue to check the return value of calling. For the case (3), you should also use assertEquals to check the balance after withdraw.
3. Add getters in class BankAccount and class CheckingAccount if needed.
Bank Account Class ***
abstract public class BankAccount {
private int accountNumber;
private double balance;
public BankAccount(int number, double bal)
{
accountNumber = number;
balance = bal;
}
public int getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
public boolean withdraw(double amount)
{
if(balance - amount < 0)
return false;
else
{
balance = balance - amount;
return true;
}
}
public void deposit(double amount)
{
balance = balance + amount;
}
}
Checking Account Class***
public class CheckingAccount extends BankAccount{
private double minimumBalance;
public CheckingAccount(int acctNum, double balc, double minBalc)
{
super (acctNum, balc);
minimumBalance = minBalc;
}
public boolean withdraw(double amount)
{
if (getBalance() < minimumBalance)
return false;
else
return super.withdraw(amount);
}
public String createMonthlyStatement()
{
String statement = "Account number: " + getAccountNumber()
+ " Account balance: " + getBalance()
+ " Minimum balance: " + minimumBalance;
return statement;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started