I am trying to figure out how to display the total amount of: processed employees (I enter a new name and set of data when I click the "clear" button after calculating the previous person's data), the total gross pay, total deductions before taxes, total taxes, and the total net pay when I decide to click the "summary" button.
I think a while loop would be a good option since I do not know how many iterations would be needed as opposed to using a for loop. However, I am not too sure. I am not also sure if I should start the code in the calculate button method, or summary button method.
In addition to this, I want to display the total within a message box upon clicking the "summary" button, so I thought a list would be suitable. However, I am not too sure how to get strings like "Number of processed employees: ,Gross pay: , Total deductions: , Total Taxes: , Total net pay: " to be displayed alongside the results in the list provided (results in list are not accumulated at the moment).
So essentially, I am trying to gather and display the accumulated totals in a message box after clicking the summary button. Any help that would guide me towards solving this would be greatly appreciated.
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();
}
}
}