Hi all,
I have created an abstract class Named Employee and one Sub-Class named FullTimeEmployee. In my abstract class "Employee", i have defined two constructors one without parameter and one with two parameter, both used to intializee first name and last name. My aim is to print payroll for employee in a file.
My problem is, my program works fine that is i get the desired output in file when i have two constructors in the sub-class FullTimeEmployee, but when i add another that is third constructor in the subclass, i get the firstname and second name in the file from the first constructor (without parameter) of abstract eventhough i call the second one.First constructor i just intialised the names to sample value.
Output i get when having two constructors in subclass is
daniel
creig
10000
2350
output i get when having three constructors in subclass is
Null
Null
10000
2350
Please explain this behaviour, i have posted the subclass with three constructors 1. no para 2. first,lastname 3.basesalry, hours worked. if i have two constructors in sub class like 1. no para 2. first,last,basesalary,hoursworked and call the constructor accordingly it works perfect. why ?
public abstract class Employee {
/**
* @param args
*/
private String firstName;
private String lastName;
public Employee (){
firstName = "test";
lastName = "testone";
}
public Employee(String inFirstName, String inLastName){
firstName = inFirstName;
lastName = inLastName;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String inFirstName){
firstName = inFirstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String inLastName){
lastName = inLastName;
}
public abstract double compensation();
public abstract String paystub();
public String toString(){
return paystub();
}
}
Sub Class
import java.text.DecimalFormat;
public class FullTimeEmp extends Employee {
private double baseSalary;
private int hoursWorked;
public FullTimeEmp(){
super();
baseSalary = 0.0;
hoursWorked = 0;
}
public FullTimeEmp(String inFirstName, String inLastName,double)
{
super(inFirstName,inLastName);
baseSalary = inBaseSalary;
hoursWorked = inHoursWorked;
}
public FullTimeEmp(double inBaseSalary, int inHoursWorked)
{
baseSalary = inBaseSalary;
hoursWorked = inHoursWorked;
}
@Override
public double compensation() {
// TODO Auto-generated method stub
//return 0;
return (baseSalary + (1.5 * (80 - hoursWorked)));
}
@Override
public String paystub() {
// TODO Auto-generated method stub
//return null;
double payroll;
String salary;
DecimalFormat currencyFormat = new DecimalFormat("0.00");
payroll = compensation();
salary = " Heartland Cars of America" +"\n\n FulltimeEmployee" + "\n\n" + "\t\t\t\tfirst name : " + super.getFirstName() + "\n" + "\t\t\t\tLast name : " + super.getLastName() + "\n" + "\t\t\t\tBase Salary : " + baseSalary + "\n" + "\t\t\t\tHours worked : " + hoursWorked + "\n" + "\t\t\t\tSalary : " + currencyFormat.format(payroll)+"\n\n";
return salary;
}
}
Application Program
import java.io.*;
import java.util.*;
public class Payroll {
/**
* @param args
*/
public static void main(String[] args)throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
Scanner scannedInfo = new Scanner(new File("D:\\apple.dat"));
PrintWriter output = new PrintWriter(new FileWriter("D:\\orange.dat"));
Employee reference ;
String firstName;
String lastName;
Double baseSalary;
int hoursWorked;
String choice;
while (scannedInfo.hasNext())
{
choice = scannedInfo.next();
switch(choice){
case "F":
case "f":
firstName = scannedInfo.next();
lastName = scannedInfo.next();
baseSalary = scannedInfo.nextDouble();
hoursWorked = scannedInfo.nextInt();
reference = new FullTimeEmp(firstName,lastName);
reference = new FullTimeEmp(baseSalary,hoursWorked);
//reference = new FullTimeEmp(baseSalary,hoursWorked);
output.print(reference.paystub());
break;
default :
break;
}
}
scannedInfo.close();
output.close();
}
}