WinForms C# - Example Application: Payroll Evaluation - 2 Weeks
WinForms C# - Example Application: Payroll Evaluation - 2 Weeks
Description
We are learning to program in C#. In this exercise, we will evaluate the salary of an employee on a two-week basis. We will calculate the regular time, the overtime, the regular pay, the overtime pay, and the net pay.
Practical Learning: Creating the Application

| Control | Name | Text | Other Properties | |
| GroupBox | Employee Identification | |||
| Label | &Employee Name: | |||
| TextBox | txtEmployeeName | AutoCompleteCustomSource Andy Barang Thomas Stones Gertrude Monay Christophe Yuen Micheline Hammond Paul Bertrand Yamaguchi AutoCompleteSource: Suggest AutoCompleteMode: CustomSource |
||
| Label | Hourly &Salary: | |||
| TextBox | txtHourlySalary | |||
| GroupBox | Time Values | |||
| Label | Monday | |||
| Label | Tuesday | |||
| Label | Wednesday | |||
| Label | Thursday | |||
| Label | Friday | |||
| Label | Saturday | |||
| Label | Sunday | |||
| Label | First Week: | |||
| TextBox | txtWeek1Monday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Tuesday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Wednesday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Thursday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Friday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Saturday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek1Sunday | 0.00 | TextAlign: Right | |
| Label | Second Week: | |||
| TextBox | txtWeek2Monday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Tuesday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Wednesday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Thursday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Friday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Saturday | 0.00 | TextAlign: Right | |
| TextBox | txtWeek2Sunday | 0.00 | TextAlign: Right | |
| GroupBox | Payroll Processing | |||
| Label | Hours | |||
| Label | Amount | |||
| Label | btnCalculate | Calculate | ||
| Label | Regular | |||
| TextBox | txtRegularTime | 0.00 | TextAlign: Right Enabled: False |
|
| TextBox | txtRegularAmount | 0.00 | TextAlign: Right Enabled: False |
|
| Label | Net Pay: | |||
| TextBox | txtNetPay | 0.00 | TextAlign: Right Enabled: False |
|
| Label | Overtime | |||
| TextBox | txtOvertime | 0.00 | TextAlign: Right Enabled: False |
|
| TextBox | txtOvertimeAmount | 0.00 | TextAlign: Right Enabled: False |
|
| Label | btnClose | AutoSize: False | ||
namespace PayrollEvaluation2
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double week1Monday = 0.00, week1Tuesday = 0.00, week1Wednesday = 0.00,
week1Thursday = 0.00, week1Friday = 0.00, week1Saturday = 0.00,
week1Sunday = 0.00, week2Monday = 0.00, week2Ttuesday = 0.00,
week2Wednesday = 0.00, week2Thursday = 0.00, week2Friday = 0.00,
week2Saturday = 0.00, week2Sunday = 0.00;
double hourlySalary = 0.00;
// Get the hourly salary. Use exception handling in case the user types a bad value.
try
{
hourlySalary = double.Parse(txtHourlySalary.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you typed for the salary is invalid. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtHourlySalary.Focus();
}
/* Get the value for each work dayworked.
* . Use exception handling for each text box in case the user types a bad value.*/
try
{
week1Monday = double.Parse(txtWeek1Monday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Monday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Monday.Focus();
}
try
{
week1Tuesday = double.Parse(txtWeek1Tuesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Tuesday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtWeek1Tuesday.Focus();
}
try
{
week1Wednesday = double.Parse(txtWeek1Wednesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Wednesday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Wednesday.Focus();
}
try
{
week1Thursday = double.Parse(txtWeek1Thursday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Thursday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Thursday.Focus();
}
try
{
week1Friday = double.Parse(txtWeek1Friday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Firday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Friday.Focus();
}
try
{
week1Saturday = double.Parse(txtWeek1Saturday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Saturday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Saturday.Focus();
}
try
{
week1Sunday = double.Parse(txtWeek1Sunday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Sunday of the first week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek1Sunday.Focus();
}
try
{
week2Monday = double.Parse(txtWeek2Monday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Monday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Monday.Focus();
}
try
{
week2Ttuesday = double.Parse(txtWeek2Tuesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Tuesday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Tuesday.Focus();
}
try
{
week2Wednesday = double.Parse(txtWeek2Wednesday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Wednesday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Wednesday.Focus();
}
try
{
week2Thursday = double.Parse(txtWeek2Thursday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Thursday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Thursday.Focus();
}
try
{
week2Friday = double.Parse(txtWeek2Friday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Friday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Friday.Focus();
}
try
{
week2Saturday = double.Parse(txtWeek2Saturday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Saturday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Saturday.Focus();
}
try
{
week2Sunday = double.Parse(txtWeek2Sunday.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value for Sunday of the second week. " +
"Please try again.", "Payroll Evaluation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtWeek2Sunday.Focus();
}
// Calculate the total number of hours for each week
double week1TotalTime = week1Monday + week1Tuesday + week1Wednesday +
week1Thursday + week1Friday + week1Saturday + week1Sunday;
double week2TotalTime = week2Monday + week2Ttuesday + week2Wednesday +
week2Thursday + week2Friday + week2Saturday + week2Sunday;
// The overtime is paid time and half
double ovtSalary = hourlySalary * 1.5;
double week1RegularTime = 0.00, week2RegularTime = 0.00,
week1OverTime = 0.00, week2OverTime = 0.00;
double week1RegularPay = 0.00, week2RegularPay = 0.00,
week1OvertimePay = 0.00, week2OvertimePay = 0.00;
// If the employee worked below 40 hours, there is no overtime
if (week1TotalTime < 40)
{
week1RegularTime = week1TotalTime;
week1RegularPay = hourlySalary * week1RegularTime;
week1OverTime = 0.00;
week1OvertimePay = 0.00;
} // If the employee worked over 40 hours, calculate the overtime
else if (week1TotalTime >= 40)
{
week1RegularTime = 40;
week1RegularPay = hourlySalary * 40;
week1OverTime = week1TotalTime - 40;
week1OvertimePay = week1OverTime * ovtSalary;
}
if (week2TotalTime < 40)
{
week2RegularTime = week2TotalTime;
week2RegularPay = hourlySalary * week2RegularTime;
week2OverTime = 0.00;
week2OvertimePay = 0.00;
}
else if (week2TotalTime >= 40)
{
week2RegularTime = 40;
week2RegularPay = hourlySalary * 40;
week2OverTime = week2TotalTime - 40;
week2OvertimePay = week2OverTime * ovtSalary;
}
double regularTime = week1RegularTime + week2RegularTime;
double overTime = week1OverTime + week2OverTime;
double regularPay = week1RegularPay + week2RegularPay;
double overtimePay = week1OvertimePay + week2OvertimePay;
double netPay = regularPay + overtimePay;
txtRegularTime.Text = regularTime.ToString("F");
txtOverTime.Text = overTime.ToString("F");
txtRegularPay.Text = regularPay.ToString("F");
txtOvertimePay.Text = overtimePay.ToString("F");
txtNetPay.Text = netPay.ToString("F");
}
}
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
Employee Name: Gertrude Monay
Hourly Salary: 28.46
First Week
Monday: 8
Tuesday: 7.5
Wednesday: 6
Thursday: 7.5
Friday: 6.5
Saturday: 0
Sunday: 0
Second Week
Monday: 7
Tuesday: 8
Wednesday: 6
Thursday: 6
Friday: 8
Saturday: 0
Sunday: 0


Employee Name: Thomas Stones
Hourly Salary: 31.68
First Week
Monday: 8
Tuesday: 10
Wednesday: 9
Thursday: 8.5
Friday: 8
Saturday: 0
Sunday: 0
Second Week
Monday: 9
Tuesday: 8
Wednesday: 10.5
Thursday: 9
Friday: 8.5
Saturday: 0
Sunday: 0
|
|
|||
| Home | Copyright © 2010-2026, FunctionX | Thursday 12 December 2024, 09:34 | Home |
|
|
|||