0

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.

3
  • Thanks to polymorphism and an implemented (overriden) toString method, you won't need to know the type of each instance. Commented Sep 30, 2013 at 23:35
  • If I don't need to know the type of each instance, how can I write a method that will go through the array and print out only the employees of type HourlyEmployee? Commented Sep 30, 2013 at 23:38
  • Just a slightly off-topic suggestion: If you are going to check employees.length to get the number of employees, then you don't need to track and implement currentEmployees. Storing the same information 2 different ways creates confusion as to whether they should be different. Worse, if something goes wrong and you fail to increment, now they do not match. I suggest doing it only one way. Commented Sep 30, 2013 at 23:49

2 Answers 2

2

If you really need to know a type of the object you can do this way:

if (employees[i] instanceof HourlyEmployee) {
// Hourly employee
}
Sign up to request clarification or add additional context in comments.

4 Comments

Going to try implementing that and see if that works
That worked, awesome. Now I can implement that for all of my object types, thank you!
@Neonjoe I have to point out that if you are adding all objects to the same array and every time you use the array, you need to do an instanceof check, that suggests the abstraction of using the parent class is not helpful. You could simply have an hourlyEmployees array, salaryEmployees array, etc.
I can't do that within the constraints of the assignment because I did not right the main method. Have to include everything in a way that the main method written by the instructor can follow.
0

This depends on what you want to do, but if you simply want to print out each object with toString(), simply use your current solution

if(employees[i] != null)
{
     System.out.print(employees[i]);
}

The System.out.print() method actually calls object.toString() where object is the argument you pass. In this case, the object referenced by employees[i]. You can therefore implement a toString() method in each of your Employee sub-classes. Late-binding and polymorphism will take care of calling the correct method depending on the type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.