2011-04-15 58 views
0

我正在尝试编制Form Pay Estimator,它会计算单个员工的总工资,所欠税和净工资。出于某种原因,我的Pay()类中的CalculateTaxes方法没有返回正确的值。这是代码我在这里:我的代码没有返回正确的值

在我的工资()类:

{ 
     //declare variables 
     private static int dependants; 
     private static double grossPay; 

     //property that gets and sets the dependants 
     public int NumOfDependents 
     { 
      get 
      { 
       return dependants; 
      } 
      set 
      { 
       dependants = value; 
      } 
     } 

    //calculates the gross pay for the production worker employee, using their hours 
    //worked times their wage and overtime is calculated for the employee, if 
    //applicable 
    public double CalculateGrossPay(double hoursWorked, double wageRate) 
    { 
     grossPay = hoursWorked * wageRate; 
      if (hoursWorked > 40) 
      { 
       grossPay += (.5 * wageRate) * (hoursWorked - 40); 
      } 
      return grossPay; 
    } 

    //calculates the gross pay for a salesperson, using their hours worked times their 
    //wage rate and then commission is calculated for the employee, based on their 
    //sales amount 
    public double CalculateGrossPay(double hoursWorked, double wageRate, double  salesAmount) 
    { 
     grossPay = hoursWorked * wageRate; 
     if (salesAmount <= 10000) 
     { 
      grossPay += .02 * salesAmount; 
     } 
     else if (salesAmount > 10000) 
     { 
      grossPay += .04 * salesAmount; 
     } 
     return grossPay; 
    } 

    //calculates the taxes the employee has to pay 
    public static double CalculateTaxes() 
    { 
     int payCutoff = 100 + (100 * dependants); 

     if (grossPay <= payCutoff) 
     { 
      double taxesPaid = .1 * grossPay; 
      return taxesPaid; 
     } 
     else 
     { 
      double taxesPaid = (.1 * payCutoff) + (.2 * (grossPay - payCutoff)); 
      return taxesPaid; 
     } 
    } 
} 

在我的窗体类:

{ 
    public FormPayEstimator() 
    { 
     InitializeComponent(); 
    } 

    //closes the application 
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    //closes the application 
    private void buttonExit_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    //clears all text boxes, unchecks the salesperson check box, makes the sales amount 
    //text box and label invisible and sets the focus back to the hours worked text box 
    private void buttonClearForm_Click(object sender, EventArgs e) 
    { 
     textBoxDependants.Text = ""; 
     textBoxGrossPay.Text = ""; 
     textBoxHourlyWageRate.Text = ""; 
     textBoxHoursWorked.Text = ""; 
     textBoxNetPay.Text = ""; 
     textBoxTaxes.Text = ""; 
     checkBoxSalesperson.Checked = false; 
     textBoxSalesAmount.Visible = false; 
     labelSalesAmount.Visible = false; 
     textBoxHoursWorked.Focus(); 
    } 

    //displays information about the program 
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Pay Estimator - Version 1.0", "Pay Estimator", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 

    //if the user checks the salesperson check box the sales amount text box and label 
    //become visible to the user and it sets the focus to the sales amount text box 
    private void checkBoxSalesperson_CheckedChanged(object sender, EventArgs e) 
    { 
     textBoxSalesAmount.Visible = true; 
     labelSalesAmount.Visible = true; 
     textBoxSalesAmount.Focus(); 
    } 

    //displays the font dialog box and allows user to change their font 
    private void fontToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     fontDialog1.Font = textBoxHoursWorked.Font; 
     fontDialog1.Font = textBoxHourlyWageRate.Font; 
     fontDialog1.Font = textBoxDependants.Font; 
     fontDialog1.Font = textBoxGrossPay.Font; 
     fontDialog1.Font = textBoxTaxes.Font; 
     fontDialog1.Font = textBoxNetPay.Font; 
     fontDialog1.Font = textBoxSalesAmount.Font; 
     if (fontDialog1.ShowDialog() != DialogResult.Cancel) 
     { 
      textBoxHoursWorked.Font = fontDialog1.Font; 
      textBoxHourlyWageRate.Font = fontDialog1.Font; 
      textBoxDependants.Font = fontDialog1.Font; 
      textBoxGrossPay.Font = fontDialog1.Font; 
      textBoxTaxes.Font = fontDialog1.Font; 
      textBoxNetPay.Font = fontDialog1.Font; 
      textBoxSalesAmount.Font = fontDialog1.Font; 
     } 
    } 

    //displays the color dialog box and allows user to change thei font color 
    private void colorToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     colorDialog1.Color = textBoxHoursWorked.ForeColor; 
     colorDialog1.Color = textBoxHourlyWageRate.ForeColor; 
     colorDialog1.Color = textBoxDependants.ForeColor; 
     colorDialog1.Color = textBoxGrossPay.ForeColor; 
     colorDialog1.Color = textBoxTaxes.ForeColor; 
     colorDialog1.Color = textBoxNetPay.ForeColor; 
     colorDialog1.Color = textBoxSalesAmount.ForeColor; 
     if (colorDialog1.ShowDialog() != DialogResult.Cancel) 
     { 
      textBoxHoursWorked.ForeColor = fontDialog1.Color; 
      textBoxHourlyWageRate.ForeColor = fontDialog1.Color; 
      textBoxDependants.ForeColor = fontDialog1.Color; 
      textBoxGrossPay.ForeColor = fontDialog1.Color; 
      textBoxTaxes.ForeColor = fontDialog1.Color; 
      textBoxNetPay.ForeColor = fontDialog1.Color; 
      textBoxSalesAmount.ForeColor = fontDialog1.Color; 
     } 
    } 

    //calculates the users total gross pay, their taxes owed and their net pay 
    private void buttonCompute_Click(object sender, EventArgs e) 
    { 
     //declares variables 
     string inValue; 
     double hours, rate, dependants, salesAmount, grossPay, taxes, netPay; 

     //assigns variables to values user entered in the hours worked, hourly wage 
     //rate and dependants text boxes 
     inValue = textBoxHoursWorked.Text; 
     hours = double.Parse(inValue); 
     inValue = textBoxHourlyWageRate.Text; 
     rate = double.Parse(inValue); 
     inValue = textBoxDependants.Text; 
     dependants = int.Parse(inValue); 

     //creates an instance of the Pay class and runs the CalculateGrossPay method 
     //for the production workers 
     Pay p1 = new Pay(); 
     grossPay = p1.CalculateGrossPay(hours, rate); 

     //checks to see if the sales amount checkbox is checked and if it is, a value 
     //is assigned to the salesAmount text box, an instance of the pay class is 
     // createdand the CalculateGrossPay method for a salesperson is run 
     if (checkBoxSalesperson.Checked == true) 
     { 
      inValue = textBoxSalesAmount.Text; 
      salesAmount = double.Parse(inValue); 
      Pay p2 = new Pay(); 
      grossPay = p2.CalculateGrossPay(hours, rate, salesAmount); 
     } 
     //displays the answer in the Gross Pay text box 
     textBoxGrossPay.Text = String.Format("{0:c}", grossPay).ToString(); 

     //runs the CalculateTaxes method from the Pay class and displays the result in 
     //the taxes text box 
     taxes = Pay.CalculateTaxes(); 
     textBoxTaxes.Text = String.Format("{0:c}", taxes).ToString(); 

     //calculates the net pay for an employee and displays the result in the net pay 
     //text box 
     netPay = grossPay - taxes; 
     textBoxNetPay.Text = String.Format("{0:c}", netPay).ToString(); 
    } 
} 

}

当我计算值,我当它仅仅是40美元时,得到了70美元的税收。谁能告诉我为什么会发生这种情况?

+6

呃,真的很长的代码。你是否通过调试器运行它以找出问题的根源? – 2011-04-15 03:47:06

回答

1

我认为你的一个问题是你从来没有设置静态属性Pay.NumOfDependents。很难说。你的代码非常混乱,混合静态属性等等。您最好更改这些静态属性和静态方法,以便它们是实例属性和方法。然后,在你的代码,你计算基于员工类型的付出,你可以写:

Pay p1 = new Pay(); 
// Here, set the number of dependents. 
p1.NumOfDependents = dependents; 
if (checkBoxSalesperson.Checked == true) 
{ 
    inValue = textBoxSalesAmount.Text; 
    salesAmount = double.Parse(inValue); 
    grossPay = p2.CalculateGrossPay(hours, rate, salesAmount); 
} 
else 
{ 
    grossPay = p1.CalculateGrossPay(hours, rate); 
} 

现在,当你要计算的税收,你可以写:

taxes = p1.CalculateTaxes(); 

一个清洁的设计你会把所有的相关属性(工作时间,销售金额等)放入Pay类中,然后打一个电话来计算工资总额,税金等。该方法将设置对象的属性,如taxesgrossPay等,然后你可以写:

// code to set properties here ... 
// now calculate everything 
p1.Calculate(); 
// and then access the computed properties 
textboxGrossPay.Text = string.Format("{0:c}", p1.grossPay); 
textboxTaxes.Text = string.Format("{0:c}", p1.taxes); 

的想法在这里它给予Pay对象实例所需的全部信息(工作时间,速度,销售金额,家属人数),然后就可以决定如何计算工资。这样,您的用户界面代码只需要关心从用户获取数据并显示计算结果。

1

我不打算调试的代码给你,但我有一个观察,可以帮助...

CalculateTaxes()似乎依赖于私人领域,如grossPay的价值。如果您没有按照正确的顺序调用您的方法,那么这些私有字段将不会被初始化,或者可能会从之前的运行中得到错误的值。依靠这样的副作用来正确计算通常是不好的做法。建议重写你的方法,不要依赖以前的私人领域的状态。

1

paycutoff应该是双精度型,或者在执行非整型计算时强制转换为双精度值。

这是单元测试的理想情况。

您可以使用一些数字对您的计算方法编写简单的函数调用,计算您期望的值,并查看它们是否匹配。

这也将有助于维护部分以及发展。

0

您声明grosspay为静态。总支付价值反映在每个对象中。你创建了p2并且调用了p2。CalculateGrossPay(hours,rate,salesAmount);它将覆盖对象p1的毛支付价值。当您拨打calculatetax()时,它会根据最新的毛支付价值估算。