【问题标题】:Calculating and displaying running total when summary button is clicked单击摘要按钮时计算并显示运行总计
【发布时间】:2015-10-16 18:53:45
【问题描述】:

我试图弄清楚如何显示以下总金额:已处理的员工(我在计算前一个人的数据后单击“清除”按钮时输入了一个新名称和一组数据),总工资,当我决定点击“汇总”按钮时,税前扣除总额、税前总额和净工资总额。

我认为 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();
        }
    }

}

【问题讨论】:

  • 嗯,在不显式使用循环的情况下从 List&lt;decimal&gt; 获取总数的一种方法是使用 LINQ:results.Sum()。不过,目前还不清楚你被困在哪里。你试过什么?您是在问如何将值相加吗?如何设置标签的文字?你的代码已经在做这样的事情了……

标签: c# arrays list logic cumulative-sum


【解决方案1】:

请看我的帖子,我用代码和屏幕截图解释了如何在 c# 中计算总和。

Display running total

【讨论】:

    【解决方案2】:

    如果我理解您想要正确执行的操作。您需要添加一个变量来跟踪计算结果的总数。

    如果我们有一个全局变量

    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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2022-06-11
      • 2019-10-27
      相关资源
      最近更新 更多