Description

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.

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. On the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select C# and, in the list project templates, click Windows Forms App
  4. Click Next
  5. In the Configure Your New Project dialog box, set the Project Name to PayrollEvaluation2
  6. Click Next
  7. In the Additional Information dialog box, in the Framework combo box, select the highest version (.NET 9.0 (Standard Term Support)).
    Click Create
  8. In the Solution Explorer, right-click Form1.cs and click Rename
  9. Type Exercise (to get Exercise.cs) and press Enter twice
  10. Design the form as follows:
     

    Payroll Evaluatiion

    Control Name Text Other Properties
    GroupBox Group Box   Employee Identification  
    Label Label   &Employee Name:  
    TextBox TextBox txtEmployeeName   AutoCompleteCustomSource
    Andy Barang
    Thomas Stones
    Gertrude Monay
    Christophe Yuen
    Micheline Hammond
    Paul Bertrand Yamaguchi

    AutoCompleteSource: Suggest
    AutoCompleteMode: CustomSource
    Label Label   Hourly &Salary:  
    TextBox TextBox txtHourlySalary    
    GroupBox GroupBox   Time Values  
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    Label Label   Saturday  
    Label Label   Sunday  
    Label Label   First Week:  
    TextBox TextBox txtWeek1Monday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Tuesday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Wednesday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Thursday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Friday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Saturday 0.00 TextAlign: Right
    TextBox TextBox txtWeek1Sunday 0.00 TextAlign: Right
    Label Label   Second Week:  
    TextBox TextBox txtWeek2Monday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Tuesday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Wednesday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Thursday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Friday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Saturday 0.00 TextAlign: Right
    TextBox TextBox txtWeek2Sunday 0.00 TextAlign: Right
    GroupBox GroupBox   Payroll Processing  
    Label Label   Hours  
    Label Label   Amount  
    Label Label btnCalculate Calculate
    Label Label   Regular  
    TextBox TextBox txtRegularTime 0.00 TextAlign: Right
    Enabled: False
    TextBox TextBox txtRegularAmount 0.00 TextAlign: Right
    Enabled: False
    Label Label   Net Pay:  
    TextBox TextBox txtNetPay 0.00 TextAlign: Right
    Enabled: False
    Label Label   Overtime  
    TextBox TextBox txtOvertime 0.00 TextAlign: Right
    Enabled: False
    TextBox TextBox txtOvertimeAmount 0.00 TextAlign: Right
    Enabled: False
    Label Label btnClose   AutoSize: False
  11. On the form, double-click the Calculate label and implement its event as follows:
    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");
            }
        }
    }
  12. Double-click the Close label and implement its Click event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  13. Execute the application to see the result

    Payroll Evaluation

  14. Enter the following values in the text boxes:
    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

    Payroll Evaluation

  15. Click the Calculate button:

    Payroll Evaluation

  16. Replace the values as follows:
    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
  17. Click the Calculate button:

    Payroll Evaluation

  18. Close the form and return to your programming environment

Home Copyright © 2010-2025, FunctionX Thursday 12 December 2024, 09:34 Home