简体   繁体   中英

Creating array with multiple data types that are then searchable and sortable by different types

Ok here is where I'm at currently

    public void addEmployee(int ty, String fn, String ln, char mi, char gen, int empNum, boolean ft, double p)
{
    if(ty == 1)
    {
        hourlyE = new HourlyEmployee();
        hourlyE.HourlyEmployee(fn, ln, mi, gen, empNum, ft, p);
    }
    else if(ty == 2)
    {
        salaryE = new SalaryEmployee();
        salaryE.SalaryEmployee(fn, ln, mi, gen, empNum, ft, p);
    }
    else if(ty == 3)
    {
        commE = new CommissionEmployee();
        commE.CommissionEmployee(fn, ln, mi, gen, empNum, ft, p);
    }
}

What this is doing is being controlled by another class. Once this determines the type of employee being created, it creates a new instance of said employee type. I already have all the classes written for each individual employee type.

The next step though, is I have to store the employee data in an array which can then later on be sortable by empNum or ln and fn. There is a menu that the user will be able to control all of this with. What I am having trouble with is figuring out how I can store all of the employee data in the same array and have it be searchable by index value as well.

Each employee will have int(Employee type), String(lastName), String(firstName), char(middleInitial), char(gender), int(employeeNumber), boolean(Full Time), double(Pay) as the data types corresponding to their entry.

Some of the other methods I will be implementing:

public void removeEmployee(int)
//Removes an Employee located at the given index from the Employee array. 
public void listAll()
//Lists all the current Employees. Outputs there are none if there are none. 
public void listHourly()
//Lists all the current HourlyEmployees. Outputs there are none if there are none. 
public void listSalary()
//Lists all the current SalaryEmployees. Outputs there are none if there are none. 
public void listCommission()
//Lists all the current CommissionEmployees. Outputs there are none if there are none. 
public void resetWeek()
//Resets the week for all Employees. Method written in each employee class.
public double calculatePayout()
//Returns the total weekly payout for all Employees. There is a weekly payout method in each employee class
public void removeRedundancies()
//Removes any duplicate Employees, keeping the Employee that appeared earlier in the array. 
public int getIndex(int)
//Given an Employee Number, returns the index of that Employee in the array, if the Employee doesn’t exist retuns -1. 
public void sortNumber()
//Sorts the Employees by Employee Number. 
public void sortName()
//Sorts the Employees by Name. Primarily by last name, secondary by first name. Don’t worry about middle initial. 
public void annualRaises()
//Applies annual raise to all current Employees. Each class has a method already written in it
public double holidayBonuses()
//Outputs and returns the total holiday bonus of all Employees. Each class has a method in it to calculate the holiday bonus
public void increaseHours(int, double)
//Increase the hours worked of the Employee at the given index by the given double amount. Will only be for the hourlyEmployee type. I have the method written there already.
public void increaseSales(int, double)
//Increase the sales of the Employee at the given index by the given double amount. Only for the salaryEmployee now. Again I already have the method written there.

Now I think as soon as I can figure out how to implement one of those methods, the rest will fall in to place pretty easily. The place I'm stuck at is how can I store all the employee data in an array that is sortable by multiple different data types.

EDIT:

In response to the first answer:

I did that first, I guess I'm confused on the implementation then. I made an abstract class Employee to start with. Then I created hourlyEmployee, salaryEmployee, and commissionEmployee that all extend Employee. What you're saying then is that I can then just store everything in the array as type Employee?

private Employee employees[];
private final int employeeMax = 100;
private int currentEmployees

    public EmployeeManager()
    {
        employees[] = new Employee[employeeMax];
        currentEmployees = 0;
    }

This is what I had at the beginning of my employeeManager class. Is that the route I should be going? I'm not very familiar with working with arrays. If this is the right direction, how would I go about storing my employees in the array, and then going back to it and pulling out just the employees that are hourly, salary, commission, and doing sorting based on name or employee number?

Also I should note this is a direct quote from my assignment sheet Sorting will be required, though we haven't discussed sorting in this class

By the looks of it, all you need to do is make an abstract class as a superclass for all the employee classes, then declare the array to contain types of the abstract class. Put all the methods you want all the employee classes to have into the abstract class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM