在Java中创建和访问Array数据

时间:2016-11-29 19:48:11

标签: java arrays oop

我有一个教科书问题,我曾多次尝试过,但仍然无法使用这些说明:“

编写一个使用以下数组作为字段的Payroll类:

  • EMPLOYEEID。一组七个整数来保存员工识别号码。应使用以下数字初始化数组:
    5658845 4520125 7895122 8777541 8451277 1302850 7580489
  • 小时。一组七个整数,用于保存每位员工的工作小时数
  • payRate。每个员工每小时工资率的七个双倍数组
  • 工资。一系列七双,以保持每位员工的工资总额

    该类应通过下标关联每个数组中的数据。例如,hours数组的元素0中的数字应该是员工的工作小时数,该员工的标识号存储在employeeId数组的元素0中。同一员工的工资率应存储在payRate数组的元素0中。

除了适当的访问者和mutator方法之外,该类还应该有一个方法,该方法接受员工的标识号作为参数,并返回该员工的总薪酬。

在完整程序中展示班级,显示每个员工编号,并要求用户输入该员工的工时和工资率。然后它应显示每个员工的身份证号码和工资总额。

输入验证:不接受小时的负值或小于6.00的工资率。“

到目前为止,我有我的主要课程:

public class Payroll {
public static void main(String[] args){
    Pay work = new Pay();
    Scanner input = new Scanner(System.in);
    int[] hours = new hours[work.getLength()];

    for (int i=0; i<work.getLength(); ++i) {
        System.out.println("How many hours has Employee #"+work.getEmployeeId(i)+" worked?");
        input.nextInt() = hours[i];
        while (hours[i]<6){
            System.out.println("Error, inadequit value!");
            System.out.println("How many hours has Employee #"+work.getEmployeeId(i)+" worked?");
            input.nextInt() = hours[i];
        }

    }

}

我还有一个名为Pay的课程:

public class Pay {
private int[] employeeId;
//private int[] hours = new hours[employeeId.length];
//private int[] pay = new pay[employeeId.length];
//private int[] wage = new wage[employeeId.length];

public Pay() {
    employeeId = new int[]{5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
}
public int getLength(){
    return employeeId.length;
}

public int[] getEmployeeId(int id) {
    return employeeId[id];
}

在所有这些之后,我只是不知道下一步该往哪里去。请帮忙。

2 个答案:

答案 0 :(得分:0)

我将以最简单的方式而不是正确的方式回答这个问题,因为我假设你对编程有些新意。这是因为似乎没有强调阶级或任何真正的模块化。

您只想拥有4个大小为7的数组。

员工ID数组将由您设置。

当您提示用户输入信息时,您可以根据所设置员工的索引将其保存在正确的数组中。

总薪酬的方法只需要获取员工的身份证号码,在数组中找到他们的索引号码,并获得计算和返回总薪酬所需的信息。 (大概是工资总额是工资率*工作时间)

这是最简单的方法,单独的课程是不必要的。

答案 1 :(得分:0)

在面向对象的原则上,更好的方法是避免使用多个数组。而是创建一个将所有员工属性保存在一起的类作为对象。

public class Employee {
     private int id;
     private int hours;
     private double rate;

     // constructors

     // it should not have arguments such as id, hours or rate
     // because it is a method of this class and those attributes are 
     // implicitly assumed.
     public double getWage() {
        return hours * rate;
     }

     // toString method

     @Override
     public String toString() {
        return "Employee ID = " + id + "," + "hours = " .....
     }
}

工资可以预先计算并在施工时存储为另一个字段。或者在请求时计算,如上所述。

薪资类:

public class Payroll {

      private Employee[] employees;
      private int lastIndex = 0;

      // constructors
      // for example, one constructor can accept initial size
      public Payroll(int size) {
         employees = new Employee[7];
      };

      public addEmployee(Employee employee) {

           // need to check overflow before attempting to add

           // add employee
           employees [lastIndex ] = emplyee;
           lastIndex++;
      }

      // more methods, such remove etc


}

现在是驱动程序或应用程序类

public class Driver {


 public static void main(String[] args){
    Payroll payroll = new Payroll (7);

    // code to pupulate the Payroll with values

    for ( ) {
          // construct new Emplyee object
          // add object to Payroll

    }

    // Printing 
    for (Emplyee e: Payroll) {
        System.out.println(e.toString());
    }


}

}

请注意,应用程序或驱动程序类与Payroll分开,现在每个类只做一件事,而且只做那件事。请记住,Payroll类不关心填充自身或打印其内容等。