类数组 - 从for循环中的部门收集数据(两种不同类型)

时间:2016-06-06 03:37:43

标签: java algorithm class

我写了下面的练习,但是我有点问题。 在我将工人添加到数组中后,我想要计算所有工人的工资。

功能有什么问题?我如何从员工薪水中拿出变量?

这个函数(进入Department类):

我遇到编译器错误,我尝试运行calculateSalary函数因此,通过I的循环的每次迭代都被视为员工的工资和工资结束时的金额。

ionic $ module.js:338
    throw err;
          ^
Error: Cannot find module 'escape-string-regexp'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/kel/Documents/Code/gogenieapp-genie-side/node_modules/chalk/index.js:2:26)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)

工人阶级:

for (int i = 0; i < arrWork.length; i++) {

    Compile error>> this.calculateSalary(arrWork[i]); 

        Compile error>> sumSalary+=arrWork[i];
    }

}

系类:

public class Worker {

private String id;
private String name;
private int hourly_wage;
public  int num_hours;


//constructor
public Worker(String id,String name,int hourly_wage,int num_hours){
this.id = id;
this.name = name;
this.hourly_wage = hourly_wage;
this.num_hours = num_hours;
}

public Worker(){

}

//copy constructor
public Worker(Worker other){
    this.id = other.id;
    this.name = other.name;
    this.hourly_wage = other.hourly_wage;
    this.num_hours = other.num_hours;
}

public String getID(){
    return this.id;
}
public void setID(String id){
    this.id = id;
}
public String getNAME(){
    return this.name;
}
public void setNAME(String name){
    this.name = name;
}
public int getHW(){
    return this.hourly_wage;
}
public void setHW(int hourly_wage){
    this.hourly_wage = hourly_wage;;
}
public int getNH(){
    return this.num_hours;
}
public void setNH(int num_hours){
    this.num_hours = num_hours;
}



public void addHours(int n){
    this.num_hours+=n;
}   
public void clearHours(){
    this.num_hours=0;
}
public void calculateSalary(int n){
    double sum=0;
    if(n<=this.num_hours){
        sum=n*this.hourly_wage;
        System.out.println("The salary is: "  + sum);
    }
    else if(n>this.num_hours){
        sum= n - this.num_hours;
        sum= sum * (this.hourly_wage * 1.2);
        sum+= this.num_hours*this.hourly_wage;
        System.out.println("The salary is: "  + sum);
    }
}

public String toString(){
    return "NAME: "+this.name+", ID: "+this.id+", HOURLY WAGE: "+ this.hourly_wage+
    " ,NUM HOURS: "+this.num_hours;
}

}

主要:

public class Department extends Worker {
private String D_name;
private int A_budget;
private Worker[] arrWork;
private int Index;
public int MAX_WORKERS;

public Department(int max_workers){
    this.MAX_WORKERS = max_workers;
    arrWork= new Worker[this.MAX_WORKERS];
    Index=0;
}

public String getD_NAME(){
    return this.D_name;
}
public void setD_NAME(String D_name){
    this.D_name = D_name;
}
public int getA_BUDGET(){
    return this.A_budget;
}
public void setA_BUDGET(int A_budget){

    this.A_budget = A_budget;
}

public void addEmploy(String id,String name,int hourly_wage,
        int num_hours){
    arrWork[Index] = new Worker(id,name,hourly_wage,num_hours);
    Index++;
}
public void calculateDepartmentSalary(){
    int sumSalary=0;
    for (int i = 0; i < arrWork.length; i++) {

    }
}

public void print(){
    for (int i = 0; i < arrWork.length; i++) {
        System.out.println(arrWork[i]);
    }
}

}

感谢&#39; S

3 个答案:

答案 0 :(得分:0)

我不确定编译器错误,除非您告诉我编译器错误消息。 但是我在你的代码中观察到的另一件事:如果代码应该完全计算工资,那么calculateSalary(int n)中的方法Worker class应该返回工资。目前它没有返回任何内容(无效)。然后在下面的for循环中,你应该在for循环的每次迭代中添加工人的返回工资。

public double calculateSalary(int n){
    double sum=0;
    if(n<=this.num_hours){
        sum=n*this.hourly_wage;
        System.out.println("The salary is: "  + sum);
    }
    else if(n>this.num_hours){
        sum= n - this.num_hours;
        sum= sum * (this.hourly_wage * 1.2);
        sum+= this.num_hours*this.hourly_wage;
        System.out.println("The salary is: "  + sum);
    }
    return sum;
}

修改for循环,完全计算工人的工资:

 double sumSalary = 0;

 for (int i = 0; i < arrWork.length; i++) {
    sumSalary += this.calculateSalary(arrWork[i].getNH()); 
 }

答案 1 :(得分:0)

用以下更改替换上面的for循环,我也编辑了previos的答案。提供的 arrWork Worker 类型的数组。

arrWork[i] --> arrWork[i].getNH()

我认为现在应该可以了。

 for (int i = 0; i < arrWork.length; i++) {
    sumSalary += this.calculateSalary(arrWork[i].getNH()); 
 }

答案 2 :(得分:0)

对于NullPointerException ...

在下面的代码中,您将使用max_workers = 5创建department对象。 它将初始化长度为5的数组,现在您只在数组中添加了3个worker,因此最后两个条目仍然为null。我认为这是NullPointerException的原因。

Department d1 = new Department(5);

d1.setD_NAME("sapir");
d1.setA_BUDGET(20000);

d1.addEmploy("123", "liran", 25, 80);
d1.addEmploy("234", "yosi", 29, 70);
d1.addEmploy("456", "david", 26, 90);

d1.print();
d1.calculateDepartmentSalary();