I'm currently writing a program that should read through a text file, create an array of different types of employees (each have their own subclass), then print out the information in a different format. I think I've got most of it working, but whenever I try to actually create the objects (at least I think that's what it is?), I'm getting a "Constructor is undefined" error. This is happening on all 7 of these objects. I will simply post one here (along with its subclass) so that you guys aren't overloaded with information, and hopefully I can figure out the rest from that.
Thanks for any help you can provide!
Driver class where I'm reading and creating objects (did not include the rest of the code following this) Error occurs at the "Emp[0]" line
import java.io.File;
import java.util.Scanner;
public class PayrollSystemTest2 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner input;
input = new Scanner(new File("EmployeePayrollInfo.txt"));
Employee[] Emp = new Employee[20];
while(input.hasNext())
{
String ID = input.nextLine();
if (ID.charAt(0) == 'S')
{
String first = input.nextLine();
String last = input.nextLine();
String ssn = input.nextLine();
Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
double salary = input.nextDouble();
Emp[0] = new SalariedEmployee(first, last, ssn, DayOfBirth, ID);
}
SalariedEmployee subclass
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn, Date DayOfBirth, String ID,
double salary )
{
super( first, last, ssn, DayOfBirth, ID); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
double baseSalary;
if ( salary >= 0.0 )
baseSalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0" );
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
@Override
public String toString()
{
return String.format( "salaried employee: %s\n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee
Again, thanks for any help you can provide.
Well yes - look at your constructor, including the clearly-inaccurate comment:
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn, Date DayOfBirth,
String ID, double salary)
Note how there are 6 parameters. Now here's how you're trying to call it:
Emp[0] = new SalariedEmployee(first, last, ssn, DayOfBirth, ID);
You're passing in 5 arguments. What happened to the salary?
As side notes:
dayOfBirth
and id
rather than DayOfBirth
and ID
double
for financial values such as a salary is a bad idea. Use BigDecimal
or keep an integer number of cents. (That's assuming you even need it down to the cent...)See more on this question at Stackoverflow