好的,这就是我目前所在的位置
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 type), String(lastName), String(firstName), char(middleInitial), char(gender), int(employeeNumber), boolean(Full Time), 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。然后我创建了hourlyEmployee、salaryEmployee 和commissionEmployee,它们都扩展了Employee。你的意思是我可以将数组中的所有内容存储为 Employee 类型?
private Employee employees[];
private final int employeeMax = 100;
private int currentEmployees
public EmployeeManager()
{
employees[] = new Employee[employeeMax];
currentEmployees = 0;
}
这就是我在employeeManager 课程开始时所拥有的。那是我应该走的路线吗?我对使用数组不是很熟悉。如果这是正确的方向,我将如何将我的员工存储在数组中,然后返回到它并仅提取每小时、薪水、佣金的员工,并根据姓名或员工编号进行排序?
另外我应该注意这是我的作业表中的直接引用需要排序,尽管我们还没有在本课中讨论排序