我正在尝试对Form Pay Estimator进行编程,并计算单个员工的总工资,欠税和净工资。出于某种原因,我的Pay()类中的CalculateTaxes方法没有返回正确的值。这是我在这里的代码:
在我的Pay()课程中:
{
//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;
}
}
}
在我的Form类中:
{
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美元的税金。谁能告诉我为什么会这样?
答案 0 :(得分:1)
我认为您的一个问题是您从未设置静态属性Pay.NumOfDependents
。这很难说。您的代码非常混乱,混合静态属性等等。您最好更改这些静态属性和静态CalculateTaxes
方法,以便它们是实例属性和方法。然后,在您根据员工类型计算薪酬的代码中,您可以写:
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
课程,然后拨打一个电话来计算总工资,税金等。会在taxes
,grossPay
等对象上设置属性。然后你可以写:
// 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 :(得分:1)
我不打算为你调试代码,但我确实有一个可能有帮助的观察......
CalculateTaxes()似乎依赖于私有字段的值,例如grossPay。如果您没有按正确的顺序调用方法,那么这些私有字段将不会被初始化,或者可能具有前一次运行中的错误值。依靠这样的副作用进行正确计算通常是不好的做法。建议重写您的方法,不要依赖以前的私有字段状态。
答案 2 :(得分:1)
paycutoff应为double,或在进行非整数计算时转换为double。
这是单元测试的理想情况。
您可以使用一些数字为计算方法编写简单的函数调用,计算您期望的值,并查看它们是否匹配。
它也有助于维护部分和开发。
答案 3 :(得分:0)
你宣称grosspay是静态的。 grosspay的价值反映在每个对象中。你创建了p2并调用了p2.CalculateGrossPay(hours,rate,salesAmount);它将覆盖对象p1的grosspay值。当你调用calculatetax()时,它是根据最新的总支付价值估算的。