2011-04-15 79 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()時,它會根據最新的毛支付價值估算。