2015-10-16 62 views
0

我想弄清楚如何显示已处理员工的总数(在计算完成后单击“清除”按钮时输入一个新名称和一组数据以前的人的数据),当我决定点击“汇总”按钮时,总工资总额,税前扣除总额,总税额和总净工资。单击摘要按钮时计算并显示运行总数

我认为while循环将是一个很好的选择,因为我不知道需要多少次迭代,而不是使用for循环。但是,我不太确定。我不确定是否应该在计算按钮方法或摘要按钮方法中启动代码。

除此之外,我想在单击“摘要”按钮时在消息框内显示总数,所以我认为列表将是合适的。但是,我不太清楚如何获得字符串,如“所处理员工的数量:毛支付:总扣减额:总税额:总净支付额:”,并与所提供列表中的结果一起显示(列表中的结果目前没有积累)。

所以基本上,我试图收集并在点击摘要按钮后在消息框中显示累计总数。任何帮助,将指导我解决这个问题将不胜感激。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Project_2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     //list of all results... 
     List<decimal> results = new List<decimal>();  
     //constant variables for the tax rate and minimum wage 
     const decimal Tax_Rate = .25m; 
     const decimal minimum_wage = 9.25m; 


     //method that calculates the gross pay 
     private decimal gross_pay(decimal hours, decimal rate) 
     { 
      decimal gross_pay = 0m; 
      if (hours > 60) 
      { 
       decimal double_over_time = 20; 
       decimal over_time = hours - 60; 
       gross_pay = rate * 40 + (1.5m * rate) * over_time + (2.0m * rate) * double_over_time; 
      } 
      else if (hours > 40 && hours <= 60) 
      { 
       //calculates the over time pay. 
       decimal over_time = hours - 40; 
       gross_pay = rate * 40 + (1.5m * rate) * over_time; 
      } 
      else 
      { 
       gross_pay = hours * rate; 
      } 
      return gross_pay; 
     } 



     //method that calculates the tax deduction 
     private decimal Taxes(ref decimal hours, ref decimal rate) 

     { 
      decimal tax_deduction = (gross_pay(hours, rate) - Before_tax_deduction(ref hours, ref rate)) * Tax_Rate; 

      return tax_deduction; 

     } 

     decimal Before_tax_deduction(ref decimal hours, ref decimal rate) 
     { 
      decimal before_tax_deduction = 0m; 

      if (txtDeductionTextbox.Text == "D0") 
      { 
       before_tax_deduction = 0; 
      } 

      else if (txtDeductionTextbox.Text == "D1") 

      { 
       before_tax_deduction = 10; 
      } 


      else if (txtDeductionTextbox.Text == "D2") 
      { 
       before_tax_deduction = 30; 
      } 

      else if (txtDeductionTextbox.Text == "D3") 

      { 
       before_tax_deduction = 60; 
      } 

      return before_tax_deduction; 
     } 


     //calculates the net pay 
     private decimal net_pay(ref decimal hours, ref decimal rate) 

     { 
      decimal net_pay = 0m; 

      net_pay = gross_pay(hours, rate) - Before_tax_deduction(ref hours, ref rate) - Taxes(ref hours, ref rate); 

      return net_pay; 
     } 


     private void btnCalculate_Click(object sender, EventArgs e) 
     { 
      decimal hours = 0m; 
      decimal rate = 0m; 


      //try catch block for hours,rate, and name fields...checks to see if format is correct. 
      try 
      {    
       string name = Convert.ToString(txtNameTextbox.Text); 

        if (txtNameTextbox.Text == "") 

        { 
         MessageBox.Show("Please enter your name.", "Name is missing."); 
        } 

        if (txtDeductionTextbox.Text == "") 
        { 
         MessageBox.Show("Please enter a deduction code.", "Deduction code is missing."); 

        } 

        //converts the input of hours and rate to decimal, so it can be input into the textboxes. 
        hours = Convert.ToDecimal(txtHoursTextbox.Text); 
        rate = Convert.ToDecimal(txtRateTextbox.Text); 

        if (rate < Convert.ToDecimal(minimum_wage)) 
        { 
         MessageBox.Show("Hourly rate entered does not meet the minimum wage.", "Minimum Wage is missing"); 

        } 

        if (hours < 5 || hours >70) 
        { 
         MessageBox.Show("Hours must be between 5 and 70 hours.", "Entry Error."); 
        } 


        gross_pay(hours, rate); 

        net_pay(ref hours, ref rate); 

        Before_tax_deduction(ref hours, ref rate); 

       //displays the Name of employee 
        txtResultsTextbox.Text += ("Name: "); 
        txtResultsTextbox.Text += name.ToString(); 

        //displays the gross pay 
        txtResultsTextbox.Text += ("\r\nGross Pay: "); 
        txtResultsTextbox.Text += gross_pay(hours, rate).ToString("c"); 

        //displays the before tax deduction 
        txtResultsTextbox.Text += ("\r\nDeductions before taxes: "); 
        txtResultsTextbox.Text += Before_tax_deduction(ref hours, ref rate).ToString("c"); 

        // displays the 25% tax deduction 
        txtResultsTextbox.Text += ("\r\nTax: "); 
        txtResultsTextbox.Text += Taxes(ref hours, ref rate).ToString("c"); 

        //displays the net pay 
        txtResultsTextbox.Text += ("\r\nNet Pay: "); 
        txtResultsTextbox.Text += net_pay(ref hours, ref rate).ToString("c"); 

        //displays the hours worked 
        txtResultsTextbox.Text += ("\r\nHours: "); 
        txtResultsTextbox.Text += hours.ToString(); 

        //displays the rate of pay 
        txtResultsTextbox.Text += ("\r\nRate per hour: "); 
        txtResultsTextbox.Text += rate.ToString(); 

        txtNameTextbox.Focus(); 

       results.Add(gross_pay(hours, rate)); 
       results.Add(Before_tax_deduction(ref hours, ref rate)); 
       results.Add(Taxes(ref hours, ref rate)); 
       results.Add(net_pay(ref hours, ref rate)); 
       results.Add(hours); 
       results.Add(rate); 
      } 

      catch (Exception ex) 

      { 
       MessageBox.Show(ex.Message, "Hours and rate are missing"); 

      } 

     } 

     private void btnClear_Click(object sender, EventArgs e) 
     { 
      txtNameTextbox.Text = ""; 
      txtHoursTextbox.Text = ""; 
      txtRateTextbox.Text = ""; 
      txtDeductionTextbox.Text = ""; 
      txtResultsTextbox.Text = ""; 

      //focuses on the name text box after clearing the fields 
      txtNameTextbox.Focus(); 
     } 

     private void btnSummary_Click(object sender, EventArgs e) 
     { 
      string summary_results = ""; 

      //displays the results in a message box, but does not display the total or number or employees processed.. 
      foreach (decimal i in results) 
      { 
       summary_results += i.ToString("c") + "\n";   
      } 

      MessageBox.Show(summary_results , "Summary totals: ");  


     private void btnExit_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 

} 
+0

好了,一个办法让从'名单'没有明确使用循环与LINQ总:'results.Sum()'。但是,你卡在哪里并不是很清楚。你有什么尝试?你在问如何将数值加在一起?如何设置标签的文字?你的代码已经在做这种事情了...... – David

回答

0

如果我明白你想要做的正确。您需要添加的是一个跟踪计算结果总计的变量。

如果我们有一个全局变量

int totalEmployeesProcessed = 0; 

那么每个计算按钮被点击,你就只需要像

totalEmployeesProcessed +=1; 

这将增加一个时间变量。 现在,当点击摘要按钮时,您应该能够轻松使用此变量。

MessageBox.Show("Total Employees:" + totalEmployeesProcessed, "Summary"); 

如果您确实想使用列表,我很可能会创建一个雇员类或类似的东西,可以容纳我需要的数据。然后,计算当创建一个雇员,并将其添加到对象列表中的一些相关的代码可能包括:

//create a class 
public class Employee 
{ 
    string Name {get;set;} 
    decimal GrossPay {get;set;} 
    decimal NetPay {get;set;} 
    //etc... 
} 
//Create an instance of employee 
private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    Employee tempEmployee = new Employee(); 
    tempEmployee.Name = txtName.Text; 
    tempEmployee.GrossPay = gross_pay; 
    tempEmployee.NetPay = net_pay; 
    EmployeeList.Add(tempEmployee); 

} 
//Get totals from List<Employees> 
private void btnSummary_Click(object sender, EventArgs e) 
{ 
    Employee TotalEmployee = new Employee(); 
    for (int i = 0; i < EmployeeList.Count; i++ 
    { 
     TotalEmployee.GrossPay += EmployeeList[i].GrossPay; 
     TotalEmployee.NetPay += EmployeeList[i].NetPay; 
    } 

} 
1

请参阅我的文章中,我解释如何在C#中的运行总的代码和屏幕截图。

Display running total

相关问题