创建具有多种数据类型的数组,然后可以按不同类型进行搜索和排序

时间:2013-09-30 02:29:53

标签: java arrays

好的,这是我目前的地方

    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);
    }
}

这样做是由另一个班级控制的。一旦确定了正在创建的员工类型,它就会创建所述员工类型的新实例。我已经为每个员工类型编写了所有课程。

下一步是,我必须将员工数据存储在一个数组中,然后可以通过empNum或ln和fn对其进行排序。有一个菜单,用户将能够控制所有这些。我遇到的问题是弄清楚如何将所有员工数据存储在同一个数组中,并且可以通过索引值进行搜索。

每个员工都将int(Employee类型),String(lastName),String(firstName),char(middleInitial),char(性别),int(employeeNumber),boolean(全职),double(Pay)作为与其条目对应的数据类型。

我将实施的其他一些方法:

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.

现在我想,只要我能弄清楚如何实现其中一种方法,其余的就会很容易地落实到位。我坚持的地方是如何将所有员工数据存储在可由多种不同数据类型排序的数组中。

编辑:

回答第一个答案:

我先做了那个,我想我对这个实现感到困惑。我创建了一个抽象类Employee。然后我创建了所有扩展Employee的hourlyEmployee,salaryEmployee和commissionEmployee。那么你所说的是我可以将数据中的所有内容存储为类型Employee?

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

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

这是我在employeeManager类开头的内容。这是我应该去的路线吗?我对使用数组不是很熟悉。如果这是正确的方向,我将如何将我的员工存储在阵列中,然后返回到它并根据姓名或员工编号提取每小时,薪水,佣金和排序的员工? / p>

另外我应该注意这是我的作业单中的直接引用需要排序,虽然我们没有讨论过这个类的排序

1 个答案:

答案 0 :(得分:0)

从它的外观来看,您需要做的就是将一个抽象类作为所有雇员类的超类,然后声明该数组包含抽象类的类型。将所有员工类所需的所有方法放入抽象类中。