合并员工小时数时无法恢复变量错误

时间:2018-01-21 10:09:55

标签: java class methods

我正在尝试接受一个Employee对象并合并所述员工的小时数。 例如,如果e1有小时:[8,8,8,2,0,0,0],e2有小时:[0,0,0,4,10,0,0]。然后,当e1.mergeEmployee(e2)被执行时,e1将有几个小时:[8,8,8,6,10,0,0]。

但是,我无法进入代码的那一步,因为我在代码中得到了解决变量错误的信息:

public class Employee {

private double[] hours;
private String name;
private double payRate;

public Employee() {
    hours = new double[7];
}

public Employee(String name, double payRate) {
    hours = new double[7];
    this.name = name;
    this.payRate = payRate;

}

public double getHours(int day) {
    return hours[day];

}

public void setHours(int day, double num) {
    hours[day] = num;
}

public String getName() {
    return name;
}

public double getPayRate() {
    return payRate;
}

public int getNumDaysWorked() {
    int count = 0;
    for(int i = 0; i < hours.length; i++) {
        if(hours[i] > 0) {
            count++;
        }
    }
    return count;
}

public double getTotalHours() {
    int count = 0;
    for(int i = 0; i < hours.length; i++) {
        count += hours[i];
    }
    return count;
}

public double getWeekdayHours() {
    int count = 0;
    for(int i = 0; i < 5; i++) {
        count += hours[i];
    }
    return count;
}

public double getWeekendHours() {
    int count = 0;
    for(int i = 5; i <7; i++) {
        count += hours[i];
    }
    return count;
}

public void newWeek() {
    for(int i = 0; i < hours.length; i++) {
        hours[i] = 0;
    }
    //Arrays.fill(array,0.0); ??? 
}

public double getPay() {
    double totalPay = 0.0;
    double timeHalf = (payRate * 1.5);
    double timeDouble = (payRate * 2.0);
    double bonus = 50.00;

    if(getNumDaysWorked() == 7) {
        totalPay += bonus;
    }

    if(getWeekendHours() > 0) {
        totalPay += (getWeekendHours() * timeDouble);
    }

    if(getWeekdayHours() > 40) {
        totalPay += (payRate * 40) + ((getWeekdayHours() - 40)*timeHalf);
    }
    else {
        totalPay += (getWeekdayHours() * payRate);
    }

    return totalPay;
}

public void mergeEmployee(Employee emp) {
    if(this.getHours(day).equals(emp.getHours(day))) {    

    }
}
}

错误发生在mergeEmployee类的if语句中。 我脑子里已经死了,所以如果我错过了一些容易和大的东西,我会道歉。

1 个答案:

答案 0 :(得分:0)

你没有声明的变量&#39; day&#39;在该功能的范围内。我认为你要做的是在for循环中使用它,如下所示:

for(int day=0; day<7; day++) {
    if(this.getHours(day) == emp.getHours(day)) {
        // do stuff
    }
}

此外,在您的示例中,您似乎只是将两个数组的第n个元素添加到一起,所以为什么不这样做:

for(int day=0; day<7; day++) {
    this.setHours(day, this.getHours(day) + emp.getHours(day)); 
}