员工类无法正确计算

时间:2015-03-23 18:03:19

标签: java class joptionpane

对于任务,员工有一个身份证号码,每小时工资,并且工作一定的小时数。我需要从这两个值计算总收入,然后根据支付扣缴,在我的代码中我相信所有的东西,但是当我测试我的程序时,我只能计算所赚的工资,扣缴和净工资的价值为0.我将非常感谢您对此问题的任何帮助,谢谢。

// an employee has an ID, get paid an amout hourly and work an amount of hours

// tax is withheld depending on gross pay

public class Employee

{

//withholding calculation

public Employee(String empId, double hrsWrk, double hrPay)
{
    employeeId = empId;
    hoursWorked = hrsWrk;
    hourlyPay = hrPay;

}

// access methods
public String getEmployeeId()
{
    return employeeId;
}

public double getHoursWorked()
{
    return hoursWorked;
}

public double getHourlyPay()
{
    return hourlyPay;
}

public double getWithholding()
{
    return withholding;
}

public double getIncome()
{
    double income = hourlyPay * hoursWorked;
    return income;
}

public double getNetPay()
{
    double netPay = income - withholding;
    return netPay;
}



// mutator methods
public void setId(String empId)
{
    employeeId = empId;
}

public void setHoursWorked(double hrsWrk)
{
    hoursWorked = hrsWrk;
}

public void setHourlyPay(double hrPay)
{
    hourlyPay = hrPay;
}

//withholding calculator based on income
public void calcWithholding()
{
    if(income <= 0)
    {
        withholding = 0.0;
    }

    else if(income >0 && income <= 300.0)
        withholding = income*10.0/100;

    else if(income >= 300.01 && income <= 400.0)
        withholding = income*12/100;

    else if(income >= 400.01 && income <= 500.0)
        withholding = income*15/10;

    else
        withholding = income*20/100;

        System.out.println("withholding is " + withholding);
}

public void displayWithholding()
{


    calcWithholding();
    System.out.println("Employee " + employeeId + " your income is " + getIncome() + " per week\n you have to pay " + getWithholding());
    System.out.println("Employee " + employeeId + " your net income is " + getNetPay());
}


//instance fields
private String employeeId;
private double hoursWorked;
private double hourlyPay;
private double withholding;
private double income;
private double netPay;
}


here is the test program---------------------------------------------

    import javax.swing.JOptionPane;


public class EmployeeTest
{
public static void main(String[] args)
{



    String employeeId = JOptionPane.showInputDialog("Please enter your Employee ID");


    String input = JOptionPane.showInputDialog("Enter your hourly wage");
    double hourlyPay = Double.parseDouble(input);


    input = JOptionPane.showInputDialog("How many hours have you worked this week?");
    double hoursWorked = Double.parseDouble(input);


    Employee richard = new Employee(employeeId, hoursWorked, hourlyPay);
    richard.displayWithholding();
    System.exit(0);
}


}

1 个答案:

答案 0 :(得分:0)

在calcWithHolding之后你才调用getIncome,所以在计算过程中收入变量总是为零。

当前的方法依赖于调用某些访问器的副作用,这通常被认为是一种不好的方法。

我会建议以下其中一项

  1. 确保在构造函数中完成计算(或称为...)
  2. 分开您的顾虑,以免数据存储,对象构建,数据访问者和计算之间产生混淆。
  3. 延迟初始化,以便仅在从未访问过计算值时计算它们。