Reputation: 3
I am building a form that allows the user to input an employee's name, pay-rate, and hours worked for a pay period. Once that info is entered into the three text-boxes, the user clicks a button that calculates gross pay, social security withheld, medicare withheld, state taxes withheld, Federal taxes withheld, and net pay. This info is output to a label. I want the user to be able to enter multiple employees, and have this information added to previous input (if the first employee had a gross pay of $2,000.00 and the second employee had a gross pay of $1,000, the total gross pay is $3,000, etc.) How might I go about accomplishing this?
Current code:
public Employees()
{
InitializeComponent();
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
String name;
decimal hours, payrate, grosspay;
Boolean error = false;
name = NameBox.Text;
if (NameBox.Text == string.Empty)
{
MessageBox.Show("You must enter a full name into the name textbox.");
error = true;
NameBox.Focus();
}
hours = decimal.Parse(HoursWorkedBox.Text);
payrate = decimal.Parse(HourRateBox.Text);
if (hours < 0 || payrate < 0)
{
MessageBox.Show("Cannot have negative hours/hourly rate.");
error = true;
HoursWorkedBox.Focus();
}
if (error == false)
{
decimal netpay, sswithheld, mcwithheld, statetaxwithheld, fedtaxwithheld;
grosspay = hours * payrate;
sswithheld = grosspay * .061m;
mcwithheld = grosspay * .0143m;
statetaxwithheld = 0;
fedtaxwithheld = 0;
if (grosspay > 0 && grosspay < 600)
{
statetaxwithheld = grosspay * .02m;
fedtaxwithheld = grosspay * .05m;
}
else if (grosspay >= 600 && grosspay < 1100)
{
statetaxwithheld = grosspay * .04m;
fedtaxwithheld = grosspay * .10m;
}
else if (grosspay >= 1100 && grosspay < 1600)
{
statetaxwithheld = grosspay * .06m;
fedtaxwithheld = grosspay * .15m;
}
else if (grosspay >= 1600 && grosspay < 2100)
{
statetaxwithheld = grosspay * .06m;
fedtaxwithheld = grosspay * .20m;
}
else if (grosspay >= 2100 && grosspay < 3100)
{
statetaxwithheld = grosspay * .06m;
fedtaxwithheld = grosspay * .25m;
}
else if (grosspay > 3100)
{
statetaxwithheld = grosspay * .06m;
fedtaxwithheld = grosspay * .30m;
}
netpay = grosspay - fedtaxwithheld - statetaxwithheld - sswithheld - mcwithheld;
OutputLbl.Text = ("Employee name: " + name + "\nGross pay: " + grosspay + "\nFederal Tax Withheld : " + fedtaxwithheld + "\nState Tax Withheld: " + statetaxwithheld + "\nSocial Security Withheld: " + sswithheld + "\nMedicare Withheld: " + mcwithheld + "\nNet Pay: " + netpay);
}
}
Upvotes: 0
Views: 82
Reputation: 6749
There are many ways but just off the top of my head I would make a Controller possibly or the likes of and have it store a list of Employee classes where the Employee class has all the information for each employee. Then you can find the running total of all employees from the Controller (EmployeeController maybe) by simply adding up the gross from the list.
Quick and ugly example (Edit Employee and EmployeeController to fit your needs):
public class Employee
{
public string Name { get; set; }
public double Pay { get; set; }
}
public class EmployeeController
{
private static EmployeeController employeeController = new EmployeeController();
private { Employees = new List<Employee>(); }
public static EmployeeController Instance => employeeController;
public List<Employee> Employees { get; set; }
public double TotalPay
{
get
{
var totalPay = 0d;
foreach (var employee in Employees)
totalPay += employee.Pay;
return totalPay;
}
}
}
Then you can use it like so...
public void SomeMethod()
{
var newEmployee = new Employee()
{
Name = textboxName.Text,
Pay = double.Parse(textboxPay.Text)
}
EmployeeController.Instance.Employees.Add(newEmployee);
labelEmployeePay.Text = newEmployee.Pay;
labelTotalPay.Text = EmployeeController.Instance.TotalPay;
}
Upvotes: 1