gammaman
May 6th, 2008, 08:59 AM
Hi, I need help writing a test application for my program. I have three files an Account Class which is a super class, and two sub classes, one being a checking account and the other a savings account. I need to write a test application to test the methods of the two subclasses. I am very new to java and I am a little confused about how to test all of the methods properly. Here are the three classes followed by what I have so far in the test application.
The super class
public class Account {
private String firstName;
private String lastName;
private double balance;
private Date estDate;
private static int count=0; //number of objects in memory
//constructor to initialize first and last name, Date object and balance
public Account(String first, String last, Date eDate, double bal) {
firstName = first;
lastName = last;
balance = bal;
estDate = eDate;
count++;
}
//method to add amount to balance
public void credit(double amount){
balance+=amount;
} //end of method credit
//method to subtract amount from balance
public boolean debit(double amount){
if (amount > balance)
{
//prevents user from overwithdrawing from their account
return false;
}
else
{
balance -= amount;
return true;
}
} //end of method debit
//displays the current balance in the account
public double getBalance() {
return balance;
}
//method to set the balance to the account
public void setBalance(double balance) {
this.balance = balance;
}
//method to get the number of accounts
public static int getCount() {
return count;
} //end of method getCount
//method to get the date using date object class
public Date getDate() {
return estDate;
} //end of method getDate
// method to format the output in the form of a String
public String toString(){
return String.format("%s,%s\t%s\t%5.2f\n",lastName,firstName,estDate,balance);
} //end of method toString
} //end of class Account
Subclass checking account
public class CheckingAccount extends Account{
private double transactionFee;
public CheckingAccount( String first, String last, Date edate,double bal, double fee){
super(first,last,edate,bal);
transactionFee=fee;
}
//set the credit
public void credit(double amount){
super.credit(amount);
chargeFee();
}
//set the debit
public boolean debit(double amount){
if(super.debit(amount)){
chargeFee();
return true;
}
return false;
}
//output the fee
public String toString(){
return String.format(super.toString() + "Fee:" + transactionFee);
}
private void chargeFee(){
setBalance(getBalance()-transactionFee);
System.out.println("Fee Charged" + transactionFee);
}
}
subclass savings account
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(String first,String last, Date edate,double bal, double rate){
super(first,last,edate,bal);
interestRate=rate;
}
//set the calculated interest
public double calculateInterest(){
return getBalance()*interestRate;
}
//print out the calculated interest
public String toString(){
return String.format(super.toString() +"Interest:" +calculateInterest());
}
}
The test application
public class AccountTest {
public static void main(String[] args){
Date aDate = new Date( 7, 24, 1949 );
Account account=new Account("Bob","Power",aDate,2000.0);
CheckingAccount cAccount=new CheckingAccount("John","Doe",aDate,5000.0,10.0);
SavingsAccount sAccount=new SavingsAccount("Sarah","Black",aDate,6000.0,0.05);
account.toString();
account.debit(1000);
account.toString();
account.credit(500);
System.out.format("Balance: %5.2f\n",account.getBalance());
account.setBalance(4000);
System.out.format("New Balance: %5.2f\n",account.getBalance());
System.out.format("Date: %10s\n",account.getDate());
cAccount.debit(2000);
cAccount.credit(200);
sAccount.debit(2000);
sAccount.credit(200);
System.out.format("Current interest: %5.2f\n",sAccount.calculateInterest());
}
}
The super class
public class Account {
private String firstName;
private String lastName;
private double balance;
private Date estDate;
private static int count=0; //number of objects in memory
//constructor to initialize first and last name, Date object and balance
public Account(String first, String last, Date eDate, double bal) {
firstName = first;
lastName = last;
balance = bal;
estDate = eDate;
count++;
}
//method to add amount to balance
public void credit(double amount){
balance+=amount;
} //end of method credit
//method to subtract amount from balance
public boolean debit(double amount){
if (amount > balance)
{
//prevents user from overwithdrawing from their account
return false;
}
else
{
balance -= amount;
return true;
}
} //end of method debit
//displays the current balance in the account
public double getBalance() {
return balance;
}
//method to set the balance to the account
public void setBalance(double balance) {
this.balance = balance;
}
//method to get the number of accounts
public static int getCount() {
return count;
} //end of method getCount
//method to get the date using date object class
public Date getDate() {
return estDate;
} //end of method getDate
// method to format the output in the form of a String
public String toString(){
return String.format("%s,%s\t%s\t%5.2f\n",lastName,firstName,estDate,balance);
} //end of method toString
} //end of class Account
Subclass checking account
public class CheckingAccount extends Account{
private double transactionFee;
public CheckingAccount( String first, String last, Date edate,double bal, double fee){
super(first,last,edate,bal);
transactionFee=fee;
}
//set the credit
public void credit(double amount){
super.credit(amount);
chargeFee();
}
//set the debit
public boolean debit(double amount){
if(super.debit(amount)){
chargeFee();
return true;
}
return false;
}
//output the fee
public String toString(){
return String.format(super.toString() + "Fee:" + transactionFee);
}
private void chargeFee(){
setBalance(getBalance()-transactionFee);
System.out.println("Fee Charged" + transactionFee);
}
}
subclass savings account
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(String first,String last, Date edate,double bal, double rate){
super(first,last,edate,bal);
interestRate=rate;
}
//set the calculated interest
public double calculateInterest(){
return getBalance()*interestRate;
}
//print out the calculated interest
public String toString(){
return String.format(super.toString() +"Interest:" +calculateInterest());
}
}
The test application
public class AccountTest {
public static void main(String[] args){
Date aDate = new Date( 7, 24, 1949 );
Account account=new Account("Bob","Power",aDate,2000.0);
CheckingAccount cAccount=new CheckingAccount("John","Doe",aDate,5000.0,10.0);
SavingsAccount sAccount=new SavingsAccount("Sarah","Black",aDate,6000.0,0.05);
account.toString();
account.debit(1000);
account.toString();
account.credit(500);
System.out.format("Balance: %5.2f\n",account.getBalance());
account.setBalance(4000);
System.out.format("New Balance: %5.2f\n",account.getBalance());
System.out.format("Date: %10s\n",account.getDate());
cAccount.debit(2000);
cAccount.credit(200);
sAccount.debit(2000);
sAccount.credit(200);
System.out.format("Current interest: %5.2f\n",sAccount.calculateInterest());
}
}