【问题标题】:Creating array with multiple data types that are then searchable and sortable by different types创建具有多种数据类型的数组,然后可按不同类型搜索和排序
【发布时间】:2013-09-30 02:29:52
【问题描述】:

好的,这就是我目前所在的位置

    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 课程开始时的内容。那是我应该走的路线吗?我对使用数组不是很熟悉。如果这是正确的方向,我将如何将我的员工存储在数组中,然后返回到它并仅提取每小时、薪水、佣金的员工,并根据姓名或员工编号进行排序?

另外我应该注意,这是我作业表中的直接引用需要排序,尽管我们还没有在本课程中讨论排序

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    看起来,您需要做的就是将抽象类作为所有员工类的超类,然后声明该数组以包含抽象类的类型。将您希望所有员工类具有的所有方法放入抽象类中。

    【讨论】:

    • 您的编辑看起来不错。但是,如果你使用这种结构,你实际上不能(嗯,你可以,但这有点困难)一旦他们在数组中,你就不能告诉他们具体是什么员工......事情是,你没有' t 真的告诉我们你想用这个数组做什么,所以我什至不确定这是否有必要。
    • 一般来说,拥有一个抽象类来实现员工的所有共同点(例如,他们都有姓名和编号)是个好主意。如果您确实需要能够单独查看特定类型的员工(例如您想列出所有小时工),您实际上需要以某种方式标记或存储它们;我个人对每种类型的员工都有一个 Set 或 Map (引用您存储在数组中的相同对象,ofc);您也可以考虑只使用三个单独的数组。
    • 我需要对数组做的事情是我在上面的其他方法的 cmets 中的内容。诸如根据员工编号对数组进行排序,根据数组索引删除员工,根据姓氏然后名字优先排序员工。列出每小时类型的所有员工,列出薪水类型的员工,列出佣金类型的员工。我意识到使用多个数组会更容易,但是分配定义的结构需要我使用单个数组。菜单功能由讲师编写,无法更改
    • 好的,请参阅我之前的评论以获取建议的解决方案。基本上,使用抽象超类,这样你就可以轻松地做只使用所有员工类型共享的信息的事情(比如按员工姓名或编号排序)。然后最重要的是,以某种方式保持不同的类型,例如拥有三个 Set,每个一个,引用您放入数组中的每个对象;显然,无论何时在数组中添加或删除某些内容时,您都需要维护这些集合。
    • 或者,另一种肮脏的解决方案(我不会这样做):查看 instanceof 关键字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多