我正在试图弄清楚如何显示处理过的员工总数(我在计算上一个人的数据后单击“清除”按钮时输入新名称和数据集),总工资总额,我决定点击“摘要”按钮时扣除税前总税金,总税金和总净工资。
我认为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 :(得分:1)
请参阅我的帖子,我解释了如何使用代码和屏幕截图在c#中运行总计。
答案 1 :(得分: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;
}
}