创建一个SIZE = 10个员工对象的数组

时间:2015-05-15 02:36:12

标签: java

是否有人可以指导我如何创建一系列这些员工的正确方向?数组设置为常量SIZE = 10;这是我的员工类和我尝试过的数组的驱动程序。此外,我知道大多数输出​​将是空白的(员工姓名,身份证等)因为我已经知道如何写它但到目前为止还没有。还有" 1"在班级名称"员工1"只是因为我已经在员工下保存了另一个文件。你可能很有可能告诉java的新手。谢谢

   class Employee1{

  //variables
 private  String name;
 private double grossPay;

   // This is the constructor of the class Employee
   public Employee1(String EmpName)
   {
      name = EmpName;
   }
    //calculates gross pay and returns 
    public double weeklyPay(double hoursWorked, double hourlyRate)
    { 

     double timeAndHalf = (hourlyRate/2.0)+hourlyRate;
     double dblOvtHours;
     double dblOvtPay;
     double regHours;
     double ovtHours;

    if (hoursWorked <= 40) 
        {
        grossPay = hoursWorked*hourlyRate;
        }

        else if (hoursWorked > 40 && hoursWorked <= 60)
        {
        ovtHours = hoursWorked-40; 
        regHours = 40;
        grossPay = (ovtHours*timeAndHalf) + (regHours*hourlyRate);
        }

        else if (hoursWorked > 60)
        {
         ovtHours = 20; 
         regHours = 40;
         dblOvtHours = hoursWorked - 60;
         dblOvtPay = hourlyRate * 2;
         grossPay = (dblOvtPay*dblOvtHours) + (timeAndHalf * ovtHours) 
     +(regHours * hourlyRate);
        }

           return grossPay; 
   }/////////////////////////////////////////////////



   /* Print the Employee details */
   public String toString()
   {
       return "Employee Report\n" + "Name :" + "\nID number :" 
+ "\nHours Worked" + "\nHourly Rate : " +"\nGross pay: " + grossPay ;
   }
 }

我的司机班:

import java.util.Scanner;
public class EmployeeDriver{

public static void main(String args[]){

   // Invoking methods for each object created
  final double hourlyRatef = 10.25;
  double hoursWorkedf, wPay; 
  double grossPayf = 0.0;

  Scanner input = new Scanner(System.in);

  System.out.print("Please enter the number of hours work: ");

  hoursWorkedf = input.nextDouble();


 //array that does not work //

  Employee1 emp = new Employee1();
  emp[0] = new Employee (); 

  /* invoke weeklyPay() method */
  grossPayf=  emp.weeklyPay(hoursWorkedf,hourlyRatef);

  // invoke printEmployee() method
  System.out.println (emp.toString());

  }
}

4 个答案:

答案 0 :(得分:4)

您正在做的是创建单个对象,而不是数组。数组看起来像这样:

final int SIZE = 10;
Employee1[] emp = new Employee1[SIZE];

然后,必须像这样实例化数组的每个成员:

emp[0] = new Employee1();

答案 1 :(得分:3)

public static final int SIZE = 10;

public static void main(String[] args) {
    Employee1[] employees = new Employee1[SIZE];
}

答案 2 :(得分:2)

根据Java doc

An array is a container object that holds a fixed number of values of a single type.

Array of length 10 (0-9)

在您的情况下,您要实例化一个对象(Employee1 emp)并将其设置为索引0。其他索引怎么样?你需要运行一个循环并向用户询问新员工并将其插入适当的索引(0-> 1 ->2等等)。

您的构造函数也接受您应提供的员工姓名,并以toString方法打印。我做了一些更改,最终的代码如下:

public class Employee1 {
    //variables
    private  String name;
    private double grossPay;

    // This is the constructor of the class Employee
    public Employee1(String EmpName)
    {
        name = EmpName;
    }
    //calculates gross pay and returns
    public double weeklyPay(double hoursWorked, double hourlyRate)
    {

        double timeAndHalf = (hourlyRate/2.0)+hourlyRate;
        double dblOvtHours;
        double dblOvtPay;
        double regHours;
        double ovtHours;

        if (hoursWorked <= 40)
        {
            grossPay = hoursWorked*hourlyRate;
        }

        else if (hoursWorked > 40 && hoursWorked <= 60)
        {
            ovtHours = hoursWorked-40;
            regHours = 40;
            grossPay = (ovtHours*timeAndHalf) + (regHours*hourlyRate);
        }

        else if (hoursWorked > 60)
        {
            ovtHours = 20;
            regHours = 40;
            dblOvtHours = hoursWorked - 60;
            dblOvtPay = hourlyRate * 2;
            grossPay = (dblOvtPay*dblOvtHours) + (timeAndHalf * ovtHours)
                    +(regHours * hourlyRate);
        }

        return grossPay;
    }/////////////////////////////////////////////////



    /* Print the Employee details */
    public String toString()
    {
        return "Employee Report\n" + "Name :" + name + "\nID number :"
                + "\nHours Worked" + "\nHourly Rate : " +"\nGross pay: " + grossPay ;
    }
}

主要是:

public static void main(String[] args) {
    final double hourlyRatef = 10.25;
    double hoursWorkedf, wPay;
    double grossPayf = 0.0;

    Scanner input = new Scanner(System.in);
    System.out.println("How many employees you want to enter: ");
    final int empSize = input.nextInt();
    Employee1[] employees = new Employee1[empSize];

    for (int i = 0; i <empSize; i++) {
        System.out.print("Please enter the number of hours work: ");
        hoursWorkedf = input.nextDouble();
        employees[0] = new Employee1("John");
        grossPayf =  employees[0].weeklyPay(hoursWorkedf,hourlyRatef);
        System.out.println (employees[0].toString());
    }
}

注意:我只进行了最少的更改以使程序正常运行。您可以在代码中改进其他各种内容。该程序运行如下:

How many employees you want to enter: 2 Please enter the number of hours work: 11 Employee Report Name :John ID number : Hours Worked Hourly Rate : Gross pay: 112.75 Please enter the number of hours work: 10 Employee Report Name :John ID number : Hours Worked Hourly Rate : Gross pay: 102.5

答案 3 :(得分:0)

新代码

 public static void main(String[] args) {

final int SIZE=10;
final double hourlyRatef = 10.25;
double hoursWorkedf, wPay;
double grossPayf = 0.0;
String name = "Void";

Scanner input = new Scanner(System.in);
// System.out.println("How many employees you want to enter: ");
// final int empSize = input.nextInt();
Employee1[] employees = new Employee1[SIZE];

for (int i = 0; i <SIZE; i++) 
{
    System.out.print("Please enter the number of hours work: ");
    hoursWorkedf = input.nextDouble();
    System.out.print("Please enter employee name: ");
    employees[i] = new Employee1(name);
    grossPayf =  employees[i].weeklyPay(hoursWorkedf,hourlyRatef);
    System.out.println (employees[i].toString());