使用数组作为表和员工小时和工资

时间:2018-02-02 16:42:36

标签: java arrays

我需要一个数组的帮助,该数组显示员工列表,他们的工作时间,工资,然后总计每个员工的工资总额,然后计算所有员工的工时,工资和员工总工资的小时数。我的代码到目前为止:

import java.util .*;
public class Employee
{
    public static void main(String[] args)
{
String wages[][] =
    {
        {"Emp", " " , "Hours", " ", "Wages", " ", "Total Pay"},
        {"Bobby", " ", "45", " ", "35"},
        {"Rick", " ", "15", " ",  "33"},
        {"Mike", " ", "66", " ", "50"},
        {"Jayme", " ", "15", " ",  "45"},
        {"Total", " ", " ", " ", " ", " "}
    };
  for (int i = 0; i < wages.length; i++)
  {
for (int j = 0; j < wages[i].length; j++)
{
    System.out.print(wages[i][j]);
}
System.out.println();

 }
          Integer H1 = Integer.valueOf(wages[1][2]);
          Integer H2 = Integer.valueOf(wages[2][2]);
          Integer H3 = Integer.valueOf(wages[3][2]);
          Integer H4 = Integer.valueOf(wages[4][2]);

          int sumH = 0;

          sumH = H1 + H2 + H3 + H4;


  System.out.println();
  System.out.println("The Sum of All Employee Hours is: " + sumH);



          Integer W1 = Integer.valueOf(wages[1][4]);
          Integer W2 = Integer.valueOf(wages[2][4]);
          Integer W3 = Integer.valueOf(wages[3][4]);
          Integer W4 = Integer.valueOf(wages[4][4]);

          double over = 0;
          double  totalPay1 = 0;

          if (H1 <= 40)
          {
        totalPay1 = W1 * H1;
          }
            else
              {

                 totalPay1 = (W1 * H1)  + ((H1- 40 ) * (W1 * 1.5));
               }


        double  totalPay2 = 0;

          if (H2 <= 40)
          {
        totalPay2 = W2 * H2;
          }
            else
              {

                 totalPay2 = (W2 * H2)  + ((H2) * (W2 * 1.5));
               }



        double  totalPay3 = 0;

          if (H3 <= 40)
          {
        totalPay3 = W3 * H3;
          }
            else
              {

                 totalPay3 = (W3 * H3)  + ((H3) * (W3 * 1.5));
               }




        double  totalPay4 = 0;

          if (H4 <= 40)
          {
        totalPay4 = W4 * H4;
          }
            else
              {

                 totalPay4 = (W4 * H4)  + ((H4) * (W4 * 1.5));
               }

      double grandTotal = 0;

      grandTotal = totalPay1 + totalPay2 + totalPay3 + totalPay4;

      System.out.printf("The total number of Pay is $%.2f.%n", grandTotal);

      //Bobby's totalPay1

          double bobbyTotal = (double) 0;

          bobbyTotal = totalPay1;

          System.out.printf("Bobby's total pay is $%.2f.%n", bobbyTotal);



        //Ricks Total
          double rickTotal = (double) 0;

          rickTotal = totalPay2;

          System.out.printf("Rick's total pay is $%.2f.%n", rickTotal);



            //Mikes Total
          double mikeTotal = (double) 0;

          mikeTotal = totalPay3;

          System.out.printf("Mike's total pay is $%.2f.%n", mikeTotal);



            //Jaymes Total
          double jaymeTotal = (double) 0;

          jaymeTotal = totalPay4;

          System.out.printf("Jayme's total pay is $%.2f.%n", jaymeTotal);
   }

 }

我需要能够打印每个员工的总数作为数组中的最后一列[x] [6] 我能够将它们打印出来,但我希望它看起来更像一张桌子。随之而来的是我的公式可能会被关闭,因为我认为它不会考虑加班时间。我认为我的IF ELSE声明可能有问题。

数组应该像

一样打印出来
Emp   Hours   Wage   Total
Bobby  45       35          $1662.50
Rick      15       33          $495.00
Mike     66        50          $3,950.00
Jayme   15       45         $675.00
Total      141    163         $6,782.50 

我得到的是:

Emp Hours Wages Total Pay
Bobby 45 35
Rick 15 33
Mike 66 50
Jayme 15 45
Total

The Sum of All Employee Hours is: 141
The total number of Pay is $11257.50.
Bobby's total pay is $1837.50.
Rick's total pay is $495.00.
Mike's total pay is $8250.00.
Jayme's total pay is $675.00

1 个答案:

答案 0 :(得分:0)

使用类而不是String []

我们发现每个员工都有名称,小时,工资,总值,而不是在String[]中表示,我们可以使用class

class Employee{

   private String name;
   private int hours;
   private int wage;
   private int total;

   public Employee(String name, int hours, int wage){
       this.name = name;
       this.hours = hours;
       this.wage = wage;
       this.total = hours * wage;
   }

   public String getName(){
       return name;
   }

   public int getHours(){
       return hours;
   }

   public int getWage(){
       return wage;
   }

   public int getTotal(){
      return total;
   }

   public String toString(){
       return String.format("%s %d %d %d", name, hours, wage, total);
   }
}

现在我们已经定义了Employee的外观,现在我们可以使用Employee

Employee放入List

public class EmployeeData{
    public static void main(String[] args){
       List<Employee> employees = new ArrayList<Employee>();

       Employee bobby = new Employee("Bobby", 45, 35);
       Employee rick = new Employee("Rick", 15, 33);
       Employee mike = new Employee("Mike", 66, 50);
       Employee jayme = new Employee("Jayme", 15, 45);

       employees.add(bobby);
       employees.add(rick);
       employees.add(mike);
       employees.add(jayme);

       //Now we have added all the employees, so we can just run through them
       System.out.println("Employee Hours Wage Total");
       for(Employee employee: employees){
          System.out.println(employee.toString());
       }
    }
}

现在您不必多次计算所有内容,但只需浏览一次列表,如果您想更改total,那么您可以在此处进行计算

public Employee(String name, int hours, int wage){
    this.name = name;
    this.hours = hours;
    this.wage = wage;
    this.total = hours * wage;
}