我不知道如何将小时数输入提取到我的总工资计算方法中



我被布置了一个家庭作业问题来编写一些代码,然后进行单元测试以确保基本的工资计算器正常运行。 大部分代码都是我只需要完成获得总工资、获得税收和获得净工资的方法。 然而,当我运行该程序时,我的总工资、税款和净工资都为零。 我认为我的总工资方法有问题。

我将把我正在尝试的代码放在下面

class Program
{
/* The Main() is not tested and is separate from the 
* unit tests. Technically, Main() is not needed for 
* unit tests. */
static void Main(string[] args)
{
const int WeeksInPayPeriod = 4;
List<decimal> hours = new List<decimal>();
decimal hourlyRate;
// Prompt and read hourly rate of pay (same for all weeks)
Console.Write("Hourly Rate: ");
hourlyRate = Convert.ToDecimal(Console.ReadLine());
// Loop to prompt for and read hours for each week
for (int i = 0; i < WeeksInPayPeriod; i++)
{
Console.Write("Enter hours for week {0}: ", i + 1);
decimal h = Convert.ToDecimal(Console.ReadLine());
hours.Add(h);
}
// Create Pay object using parameterized constructor
PayCalculator totalPay = new PayCalculator(hours, hourlyRate);
// WriteLine() causes object's ToString() to be called
Console.WriteLine(totalPay);
}
}
public class PayCalculator
{
// list holds hours by week in pay period
private List<decimal> hours;
// hourly rate is the same for whole pay period
private decimal hourlyRate;
private decimal grossPay = 0;
private decimal tax = 0;
private decimal netPay = 0;
decimal reg_time = 40.00M;
decimal ot = 1.5M;
// Parameterized constructor. 
public PayCalculator(List<decimal> hours, decimal hourlyRate)
{
this.hours = hours;
this.hourlyRate = hourlyRate;
}
/* Calculates the gross pay for pay period with overtime 
* paid at time and half for hours over 40 in a week. */
public decimal GetGrossPay()
{
decimal grossPay = 0;
foreach (decimal h in hours)
{
if (hours[0] <= reg_time)
{
grossPay += hours[0] * hourlyRate;
}
else
{
grossPay += (reg_time * hourlyRate) + ((hours[0] - reg_time) * ot);
}
}
/*========================================
* Complete code to calculate gross pay
*========================================*/
return grossPay;
}
/* Calculate tax: 15% on first $600.00, 20% on 
any amount over $600.00 */
public decimal GetTax()
{
decimal tax = 0;
decimal surplus = 600M;
if (grossPay <= surplus)
{
tax = grossPay * .15M;
}
else
{
tax = (surplus * .15M) + ((grossPay - surplus) * .2M);
}
/*========================================
* Complete code to calculate gross tax
*========================================*/
return tax;
}
public decimal GetNetPay()
{
decimal netPay = 0;
netPay = grossPay - tax;
/*========================================
* Complete code to calculate net pay
*========================================*/
return netPay;
}
/*
* ToString() returns pay info in string formatted like:
* Gross pay: 830.00, Tax: 136.00, Net pay: 694.00
*/
override
public string ToString()
{
return string.Format("Gross pay: {0:F2}, Tax: {1:F2}, Net pay: {2:F2}",
this.grossPay, this.tax, this.netPay);
}
}

这些方法不会改变PayCalculator的属性(它们只是将值返回为零)。

假设这里的最终目标是使 ToString() 显示除零以外的其他内容,您应该让方法将值分配给属性。例如,使用 GetGrossPay():

/* Calculates the gross pay for pay period with overtime 
* paid at time and half for hours over 40 in a week. */
public decimal GetGrossPay()
{
decimal grossPay = 0;
foreach (decimal h in hours)
{
if (hours[0] <= reg_time)
{
grossPay += hours[0] * hourlyRate;
}
else
{
grossPay += (reg_time * hourlyRate) + ((hours[0] - reg_time) * ot);
}
}
/*========================================
* Complete code to calculate gross pay
*========================================*/
this.grossPay = grossPay;
return grossPay;
}

注意添加的行

this.grossPay = grossPay;

如果不需要返回的值,则可以将方法返回类型更改为 void 并删除返回行。

最新更新