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 PayrollEvaluation1
  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   Work Week:  
    TextBox TextBox txtMonday 0.00 TextAlign: Right
    TextBox TextBox txtTuesday 0.00 TextAlign: Right
    TextBox TextBox txtWednesday 0.00 TextAlign: Right
    TextBox TextBox txtThursday 0.00 TextAlign: Right
    TextBox TextBox txtFriday 0.00 TextAlign: Right
    TextBox TextBox txtSaturday 0.00 TextAlign: Right
    TextBox TextBox txtSunday 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 PayrollEvaluation1
    {
        public partial class Exercise : Form
        {
            public Exercise()
            {
                InitializeComponent();
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                double monday = 0.00, tuesday = 0.00, wednesday = 0.00,
                       thursday = 0.00, friday = 0.00, saturday = 0.00, sunday = 0.00;
                double regularTime = 0.00, overTime = 0.00;
                double regularPay = 0.00, overtimePay = 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
                {
                    monday = double.Parse(txtMonday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Monday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtMonday.Focus();
                }
                try
                {
                    tuesday = double.Parse(txtTuesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Tuesday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtTuesday.Focus();
                }
                try
                {
                    wednesday = double.Parse(txtWednesday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Wednesday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtWednesday.Focus();
                }
                try
                {
                    thursday = double.Parse(txtThursday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Thursday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtThursday.Focus();
                }
    
                try
                {
                    friday = double.Parse(txtFriday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Firday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtFriday.Focus();
                }
    
                try
                {
                    saturday = double.Parse(txtSaturday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Saturday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSaturday.Focus();
                }
    
                try
                {
                    sunday = double.Parse(txtSunday.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("You typed an invalid value for Sunday. " +
                                    "Please try again.", "Payroll Evaluation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSunday.Focus();
                }
    
                // Calculate the total amount of time the employee worked
                double totalTime = monday + tuesday + wednesday +
                                 thursday + friday + saturday + sunday;
    
                // The overtime is paid time and half
                double ovtSalary = hourlySalary * 1.5;
    
                // If the employee worked below 40 hours, there is no overtime
                if (totalTime < 40)
                {
                    regularTime = totalTime;
                    regularPay  = hourlySalary * regularTime;
                    overTime    = 0.00;
                    overtimePay = 0.00;
                } // If the employee worked over 40 hours, calculate the overtime
                else if (totalTime >= 40)
                {
                    regularTime = 40;
                    regularPay  = hourlySalary * 40;
                    overTime    = totalTime - 40;
                    overtimePay = overTime * ovtSalary;
                }
    
                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. Type a letter in one of the text boxes and click Calculate

    Payroll Evaluation

  15. Enter the following values in the text boxes:
    Employee Name: Gertrude Monay
    Hourly Salary: 28.46
    Work Week
        Monday:    8
        Tuesday:   7.5
        Wednesday: 6
        Thursday:  7.5
        Friday:    6.5
        Saturday:  0
        Sunday:    0

    Payroll Evaluation

  16. Click the Calculate button:

    Payroll Evaluation

  17. Replace the values as follows:
    Employee Name: Thomas Stones
    Hourly Salary: 31.68
    Work Week
        Monday:    8
        Tuesday:   10.5
        Wednesday: 9
        Thursday:  8
        Friday:    8
        Saturday:  0
        Sunday:    0
  18. Click the Calculate button:

    Payroll Evaluation

  19. Close the form and return to your programming environment

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