I am currently working on an assignment where I am creating an employee database.
Where I'm at right now, is I created an abstract class Employee. From that I have subclasses HourlyEmployee, SalaryEmployee, and CommissionEmployee.
For the assignment when I create a new employee of any type, it all needs to be stored in an array. Then later on I have to be able to list out the employees based on which type. The ty int being passed to addEmployee() is coming from my main method in another class where the menu is asking you for a choice 1-3 for the type of employee you want.
public class EmployeeManager{
private Employee[] employees;
private final int employeeMax = 100;
private int currentEmployees;
public EmployeeManager()
{
employees = new Employee[employeeMax];
currentEmployees = 0;
}
public void addEmployee(int ty, String fn, String ln, char mi, char gen, int empNum, boolean ft, double p)
{
if(ty == 1)
{
employees[currentEmployees] = new HourlyEmployee(fn, ln, mi, gen, empNum, ft, p);
currentEmployees++;
}
else if(ty == 2)
{
employees[currentEmployees] = new SalaryEmployee(fn, ln, mi, gen, empNum, ft, p);
currentEmployees++;
}
else if(ty == 3)
{
employees[currentEmployees] = new CommissionEmployee(fn, ln, mi, gen, empNum, ft, p);
currentEmployees++;
}
}
Now this works fine for adding the employees to the array, however what it is missing is a way that identifies which type of employee it is within the array. Later on I have to implement methods that will print out all employees of type Hourly, Salary, or Commission. How could I identify the employees within the array so that later on I can print a list of all the employees of that type?
public void listAll()
{
if(currentEmployees == 0)
{
System.out.println("\nNo Employees.");
}
else
{
for (int i=0; i < employees.length; i++)
{
if(employees[i] != null)
{
System.out.print(employees[i]);
}
}
}
}
That is my method that prints out all employees of any type. Within each class I have a toString() method written that formats the output the way I need it.
toStringmethod, you won't need to know the type of each instance.