|
Example Application: Payroll Evaluation |
|
|
This application evaluates payroll, using the total time
worked in one or two weeks by an employee and his or her hourly salary. The
calculation takes overtime into account.
|
Application:
Creating the Application
|
|
- To start a new application, on the main menu, click File -> New Project (or File -> New Project)
- In the middle list, click Windows Application and set the name to
PayrollProcessing1
- Click OK
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
GroupBox |
|
|
Employee Identification |
|
Label |
|
|
&Employee Name: |
|
TextBox |
|
txtEmployeeName |
|
|
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 |
|
txtMonday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtTuesday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtWednesday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtThursday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtFriday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtSaturday1 |
0.00 |
TextAlign: Right |
TextBox |
|
txtSunday1 |
0.00 |
TextAlign: Right |
Label |
|
|
Second Week: |
|
TextBox |
|
txtMonday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtTuesday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtWednesday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtThursday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtFriday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtSaturday2 |
0.00 |
TextAlign: Right |
TextBox |
|
txtSunday2 |
0.00 |
TextAlign: Right |
GroupBox |
|
|
Payroll Processing |
|
Label |
|
|
Hours |
|
Label |
|
|
Amount |
|
Label |
|
btnCalculate |
Calculate |
AutoSize: False |
Label |
|
|
Regular |
|
TextBox |
|
txtRegularTime |
0.00 |
TextAlign: Right |
TextBox |
|
txtRegularAmount |
0.00 |
TextAlign: Right |
Label |
|
|
Net Pay: |
|
TextBox |
|
txtNetPay |
0.00 |
TextAlign: Right |
Label |
|
|
Overtime |
|
TextBox |
|
txtOvertime |
0.00 |
TextAlign: Right |
TextBox |
|
txtOvertimeAmount |
0.00 |
TextAlign: Right |
Label |
|
btnClose |
|
AutoSize: False |
|
- On the form, click the text box at the intersection of Time and
Regular
- In the Properties window, double-click ReadOnly to change its value
to True
- Do the same for the following text boxes: txtRegularAmount,
txtNetPay, txtOvertime, and txtOvertimeAmount
- Double-click the Close label and implement its Click event as
follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Return to the form
- On the form, click the txtEmployeeName text box
- In the Properties window, click AutoCompleteCustomSource and click
its ellipsis button
- In the String Collection Editor, enter the following names:
Micheline Hammond Paul Bertrand Yamaguchi Gertrude Monay
Ernestine Ngaleu Andy Barang Christophe Yuen Jean Michel Kankan
- Click OK
- Click AutoCompleteSource, then click the arrow of its combo box and
select CustomSource
- Click AutoCompleteMode, then click the arrow of its combo box and
select Accept
- On the form, double-click the Calculate label and implement its
event as follows:
private void btnCalculate_Click(object sender, EventArgs e)
{
double monday1 = 0.00, tuesday1 = 0.00, wednesday1 = 0.00,
thursday1 = 0.00, friday1 = 0.00, saturday1 = 0.00,
sunday1 = 0.00, monday2 = 0.00, tuesday2 = 0.00,
wednesday2 = 0.00, thursday2 = 0.00,
friday2 = 0.00, saturday2 = 0.00, sunday2 = 0.00;
double totalHoursWeek1, totalHoursWeek2;
double regHours1 = 0.00, regHours2 = 0.00,
ovtHours1 = 0.00, ovtHours2 = 0.00;
double regAmount1 = 0.00, regAmount2 = 0.00,
ovtAmount1 = 0.00, ovtAmount2 = 0.00;
double regularHours, overtimeHours;
double regularAmount, overtimeAmount, totalEarnings;
double hourlySalary = 0.00;
// Retrieve the hourly salary
try
{
hourlySalary = double.Parse(txtHourlySalary.Text);
}
catch (FormatException)
{
MessageBox.Show(
"The value you typed for the salary is invalid \n" +
"Please try again");
txtHourlySalary.Focus();
}
// Retrieve the value of each day worked
try
{
monday1 = double.Parse(txtMonday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtMonday1.Focus();
}
try
{
tuesday1 = double.Parse(txtTuesday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
this.txtTuesday1.Focus();
}
try
{
wednesday1 = double.Parse(txtWednesday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtWednesday1.Focus();
}
try
{
thursday1 = double.Parse(txtThursday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtThursday1.Focus();
}
try
{
friday1 = double.Parse(txtFriday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtFriday1.Focus();
}
try
{
saturday1 = double.Parse(txtSaturday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtSaturday1.Focus();
}
try
{
sunday1 = double.Parse(txtSunday1.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtSunday1.Focus();
}
try
{
monday2 = double.Parse(txtMonday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
this.txtMonday2.Focus();
}
try
{
tuesday2 = double.Parse(txtTuesday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
this.txtTuesday2.Focus();
}
try
{
wednesday2 = double.Parse(txtWednesday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
this.txtWednesday2.Focus();
}
try
{
thursday2 = double.Parse(txtThursday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtThursday2.Focus();
}
try
{
friday2 = double.Parse(txtFriday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtFriday2.Focus();
}
try
{
saturday2 = double.Parse(txtSaturday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtSaturday2.Focus();
}
try
{
sunday2 = double.Parse(txtSunday2.Text);
}
catch (FormatException)
{
MessageBox.Show("You typed an invalid value\n" +
"Please try again");
txtSunday2.Focus();
}
// Calculate the total number of hours for each week
totalHoursWeek1 = monday1 + tuesday1 + wednesday1 +
thursday1 + friday1 + saturday1 + sunday1;
totalHoursWeek2 = monday2 + tuesday2 + wednesday2 +
thursday2 + friday2 + saturday2 + sunday2;
// The overtime is paid time and half
double ovtSalary = hourlySalary * 1.5;
// If the employee worked under 40 hours, there is no overtime
if (totalHoursWeek1 < 40)
{
regHours1 = totalHoursWeek1;
regAmount1 = hourlySalary * regHours1;
ovtHours1 = 0.00;
ovtAmount1 = 0.00;
} // If the employee worked over 40 hours, calculate the overtime
else if (totalHoursWeek1 >= 40)
{
regHours1 = 40;
regAmount1 = hourlySalary * 40;
ovtHours1 = totalHoursWeek1 - 40;
ovtAmount1 = ovtHours1 * ovtSalary;
}
if (totalHoursWeek2 < 40)
{
regHours2 = totalHoursWeek2;
regAmount2 = hourlySalary * regHours2;
ovtHours2 = 0.00;
ovtAmount2 = 0.00;
}
else if (totalHoursWeek2 >= 40)
{
regHours2 = 40;
regAmount2 = hourlySalary * 40;
ovtHours2 = totalHoursWeek2 - 40;
ovtAmount2 = ovtHours2 * ovtSalary;
}
regularHours = regHours1 + regHours2;
overtimeHours = ovtHours1 + ovtHours2;
regularAmount = regAmount1 + regAmount2;
overtimeAmount = ovtAmount1 + ovtAmount2;
totalEarnings = regularAmount + overtimeAmount;
txtRegularTime.Text = regularHours.ToString("F");
txtOvertime.Text = overtimeHours.ToString("F");
txtRegularAmount.Text = regularAmount.ToString("F");
txtOvertimeAmount.Text = overtimeAmount.ToString("F");
txtNetPay.Text = totalEarnings.ToString("F");
}
- Execute the application to see the result
- Close the form and return to your programming environment
|
|