当前位置: 首页 > 知识库问答 >
问题:

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

符功
2023-03-14

好的,这就是我目前的情况

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

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

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

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.

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

编辑:

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

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

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

共有1个答案

松英喆
2023-03-14

从表面上看,您需要做的只是将抽象类作为所有employee类的超类,然后声明数组以包含抽象类的类型。将希望所有employee类拥有的所有方法放入抽象类中。

 类似资料:
  • 问题内容: 我可以用来存储多种不同类型的数据(整数/字符串/等)?我来自PHP背景,可以将不同类型的数据存储到数组中,但是我不知道如何在Java中进行操作。 举个例子: 如何用Java做类似的事情? 问题答案: Java是一种强类型语言。在PHP或Javascript中,变量没有严格的类型。但是,在Java中,每个对象和基元都有严格的类型。您可以将多种类型的数据存储在Array中,但只能将其作为对

  • 我想根据两个属性按升序对数组进行排序。 我有以下数据数组,看起来像 我想把它排序成 我想根据属性按升序对数组进行排序,如果多个项目存在相同的,那么我想根据属性的对其进行排序。 我在这里面临的问题是,< code>subCode在字符串中,< code>code在数字中。我尝试使用< code>array.sort并解析整数形式的< code>subCode,但它返回了我不理解的不同数字。

  • 本文向大家介绍Javascript如何判断数据类型和数组类型,包括了Javascript如何判断数据类型和数组类型的使用技巧和注意事项,需要的朋友参考一下 这么基础的东西实在不应该再记录了,不过嘛,温故知新~就先从数据类型开始吧 js六大数据类型:number、string、object、Boolean、null、undefined string: 由单引号或双引号来说明,如"string" nu

  • 我有一个使用MVC模式开发的应用程序,现在我想对它的多个模型进行索引,这意味着每个模型都有不同的数据结构。 > 是使用多个索引更好,每个模型使用一个索引,还是在每个模型的同一索引中使用一个类型?我认为,这两种方法都需要不同的搜索查询。我刚开始做这个。 如果数据集是小的还是大的,这两个概念在性能上是否存在差异? 如果有人能为我推荐一些好的样本数据,我会自己测试第二个问题。

  •   MLlib既支持保存在单台机器上的本地向量和矩阵,也支持备份在一个或多个RDD中的分布式矩阵。本地向量和本地矩阵是简单的数据模型,作为公共接口提供。底层的线性代数操作通过Breeze和jblas提供。 在MLlib中,用于有监督学习的训练样本称为标注点(labeled point)。 1 本地向量(Local vector)   一个本地向量拥有从0开始的integer类型的索引以及doubl

  • NumPy 数字类型是dtype(数据类型)对象的实例,每个对象具有唯一的特征。 这些类型可以是,np.float32等。 数据类型对象描述了对应于数组的固定内存块的解释,取决于以下方面: 数据类型(整数、浮点或者 Python 对象) 数据大小 字节序(小端或大端) 在结构化类型的情况下,字段的名称,每个字段的数据类型,和每个字段占用的内存块部分。 如果数据类型是子序列,它的形状和数据类型。 字