The error i'm getting is in the fillPayroll() method in the while loop where it says payroll.add(employee). The error says I can't invoke add() on an array type Person but the Employee class inherits from Person so I thought this would be possible. Can anyone clarify this for me?
import java.io.*;
import java.util.*;
public class Payroll
{
private int monthlyPay, tax;
private Person [] payroll = new Person [1];
//Method adds person to payroll array
public void add(Person person)
{
if(payroll[0] == null) //If array is empty, fill first element with person
{
payroll[payroll.length-1] = person;
}
else //Creates copy of payroll with new person added
{
Person [] newPayroll = new Person [payroll.length+1];
for(int i = 0;i<payroll.length;i++)
{
newPayroll[i] = payroll[i];
}
newPayroll[newPayroll.length] = person;
payroll = newPayroll;
}
}
public void fillPayroll()
{
try
{
FileReader fromEmployee = new FileReader ("EmployeeData.txt");
Scanner data = new Scanner(fromEmployee);
Employee employee = new Employee();
while (data.hasNextLine())
{
employee.readData(data.nextLine());
payroll.add(employee);
}
}
catch (FileNotFoundException e)
{
System.out.println("Error: File Not Found");
}
}
}