Introduction

We are continuing to learn programming in the F# computer language. In this exercise, we will calculate the salary for daily time worked for two weeks.

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 F#
  4. In the list project templates, click Console Application (.NET Framework)
  5. Click Next
  6. In the Configure Your New Project dialog box, set the Project Name to PayrollEvaluation2
    In the Framework combo box, select the highest version (.NET Framework 4.8).
    Click Create
  7. In the Solution Explorer, right-click Dependencies and Add Assembly Reference...
  8. In the Reference Manager dialog box, make sure Assemblies is selected in the left frame.
    In the middle list, click the check box of System.Windows.Forms to select it
  9. Click OK
  10. On the main menu of Microsoft Visual Studio, click Project and click PayrollEvaluation2 Properties
  11. In the Output Type combo box, select Windows Application
  12. Click the Program.fs tab and replace the text in the Code Editor with the following:
    // Indicate the libraries that will be used for this project
    open System
    open System.Drawing
    open System.Windows.Forms
    
    // Create a form by declaring a Form variable
    let exercise : Form = new Form()
    
    (* The form will contain three group boxes that act as sections.
     * Therefore, declare a GroupBox variable for each section. *)
    let gpxEmployeeIdentification : GroupBox = new GroupBox()
    let gbxCalculation : GroupBox = new GroupBox()
    let gbxTimeValues : GroupBox = new GroupBox()
    
    // Suspend the layout of each group box
    gpxEmployeeIdentification.SuspendLayout()
    gbxCalculation.SuspendLayout()
    gbxTimeValues.SuspendLayout()
    
    // Also suspend the layout of the main form
    exercise.SuspendLayout()
    exercise.AutoScaleDimensions <- new SizeF(10F, 25F)
    exercise.AutoScaleMode <- AutoScaleMode.Font
    exercise.ClientSize <- new Size(1033, 589)
    exercise.StartPosition <- FormStartPosition.CenterScreen
    exercise.Text <- "Payroll Evaluation"
    
    // Specify the characteristics of the Employee Identification group box
    gpxEmployeeIdentification.Location <- new Point(24, 27)
    gpxEmployeeIdentification.Size <- new Size(985, 110)
    gpxEmployeeIdentification.TabIndex <- 3
    gpxEmployeeIdentification.TabStop <- false
    gpxEmployeeIdentification.Text <- "Employee Identification"
    
    // Create a label for the name of the employee
    let lblEmployeeName : Label = new Label()
    lblEmployeeName.AutoSize <-  true
    lblEmployeeName.Location <- new Point(27, 50)
    lblEmployeeName.Size <- new Size(146, 25)
    lblEmployeeName.TabIndex <- 0
    lblEmployeeName.Text <- "&Employee Name:"
    gpxEmployeeIdentification.Controls.Add(lblEmployeeName)
    
    // Create a text box for the employee name
    let txtEmployeeName : TextBox = new TextBox()
    txtEmployeeName.Location <- new Point(179, 44)
    txtEmployeeName.Size <- new Size(272, 31)
    txtEmployeeName.TabIndex <- 1
    gpxEmployeeIdentification.Controls.Add(txtEmployeeName)
    
    // Create a label for the hourly salary
    let lblHourlySalary : Label = new Label()
    lblHourlySalary.AutoSize <-  true
    lblHourlySalary.Location <- new Point(490, 50)
    lblHourlySalary.Size <- new Size(121, 25)
    lblHourlySalary.TabIndex <- 2
    lblHourlySalary.Text <- "Hourly &Salary:"
    gpxEmployeeIdentification.Controls.Add(lblHourlySalary)
    
    // Create a text box for the hourly salary
    let txtHourlySalary : TextBox = new TextBox()
    txtHourlySalary.Location <- new Point(627, 47)
    txtHourlySalary.Size <- new Size(95, 31)
    txtHourlySalary.TabIndex <- 3
    txtHourlySalary.Text <- "0.00"
    txtHourlySalary.TextAlign <- HorizontalAlignment.Right
    gpxEmployeeIdentification.Controls.Add(txtHourlySalary)
    
    // Specify the characteristics of the Time Values section
    gbxTimeValues.Location <- new Point(24, 161)
    gbxTimeValues.Size <- new Size(985, 186)
    gbxTimeValues.TabIndex <- 4
    gbxTimeValues.TabStop <- false
    gbxTimeValues.Text <- "Time Values"
    
    // Create the labels for each day.
    // Monday, for the first and second week
    let lblMonday : Label = new Label()
    lblMonday.AutoSize <-  true
    lblMonday.Location <- new Point(179, 29)
    lblMonday.Size <- new Size(78, 25)
    lblMonday.TabIndex <- 0
    lblMonday.Text <- "Monday"
    gbxTimeValues.Controls.Add(lblMonday)
    
    // Tuesday, for the first and second week
    let lblTuesday : Label = new Label()
    lblTuesday.AutoSize <-  true
    lblTuesday.Location <- new Point(291, 29)
    lblTuesday.Size <- new Size(77, 25)
    lblTuesday.TabIndex <- 1
    lblTuesday.Text <- "Tuesday"
    gbxTimeValues.Controls.Add(lblTuesday)
    
    // Wednesday, for the first and second week
    let lblWednesday : Label = new Label()
    lblWednesday.AutoSize <-  true
    lblWednesday.Location <- new Point(405, 29)
    lblWednesday.Size <- new Size(104, 25)
    lblWednesday.TabIndex <- 2
    lblWednesday.Text <- "Wednesday"
    gbxTimeValues.Controls.Add(lblWednesday)
    
    // Thursday, for the first and second week
    let lblThursday : Label = new Label()
    lblThursday.AutoSize <-  true
    lblThursday.Location <- new Point(515, 29)
    lblThursday.Size <- new Size(84, 25)
    lblThursday.TabIndex <- 3
    lblThursday.Text <- "Thursday"
    gbxTimeValues.Controls.Add(lblThursday)
    
    // Friday, for the first and second week
    let lblFriday : Label = new Label()
    lblFriday.AutoSize <-  true
    lblFriday.Location <- new Point(627, 29)
    lblFriday.Size <- new Size(60, 25)
    lblFriday.TabIndex <- 4
    lblFriday.Text <- "Friday"
    gbxTimeValues.Controls.Add(lblFriday)
    
    // Saturday, for the first and second week
    let lblSaturday : Label = new Label()
    lblSaturday.AutoSize <-  true
    lblSaturday.Location <- new Point(739, 29)
    lblSaturday.Size <- new Size(82, 25)
    lblSaturday.TabIndex <- 5
    lblSaturday.Text <- "Saturday"
    gbxTimeValues.Controls.Add(lblSaturday)
    
    // Sunday, for the first and second week
    let lblSunday : Label = new Label()
    lblSunday.AutoSize <-  true
    lblSunday.Location <- new Point(851, 29)
    lblSunday.Size <- new Size(71, 25)
    lblSunday.TabIndex <- 6
    lblSunday.Text <- "Sunday"
    gbxTimeValues.Controls.Add(lblSunday)
    
    // Creatw a label for the times worked for the first week
    let lblFirstWeek : Label = new Label()
    lblFirstWeek.AutoSize <-  true
    lblFirstWeek.Location <- new Point(27, 74)
    lblFirstWeek.Size <- new Size(97, 25)
    lblFirstWeek.TabIndex <- 7
    lblFirstWeek.Text <- "First Week:"
    gbxTimeValues.Controls.Add(lblFirstWeek)
    
    // Create a text box for the time worked on Monday of the first week
    let txtWeek1Monday : TextBox = new TextBox()
    txtWeek1Monday.Location <- new Point(179, 71)
    txtWeek1Monday.Size <- new Size(106, 31)
    txtWeek1Monday.TabIndex <- 8
    txtWeek1Monday.Text <- "0.00"
    txtWeek1Monday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Monday)
    
    // Create a text box for the time worked on Tuesday of the first week
    let txtWeek1Tuesday : TextBox = new TextBox()
    txtWeek1Tuesday.Location <- new Point(291, 71)
    txtWeek1Tuesday.Size <- new Size(106, 31)
    txtWeek1Tuesday.TabIndex <- 9
    txtWeek1Tuesday.Text <- "0.00"
    txtWeek1Tuesday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Tuesday)
    
    // Create a text box for the time worked on Wednesday of the first week
    let txtWeek1Wednesday : TextBox = new TextBox()
    txtWeek1Wednesday.Location <- new Point(403, 71)
    txtWeek1Wednesday.Size <- new Size(106, 31)
    txtWeek1Wednesday.TabIndex <- 10
    txtWeek1Wednesday.Text <- "0.00"
    txtWeek1Wednesday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Wednesday)
    
    // Create a text box for the time worked on Thursday of the first week
    let txtWeek1Thursday : TextBox = new TextBox()
    txtWeek1Thursday.Location <- new Point(515, 71)
    txtWeek1Thursday.Size <- new Size(106, 31)
    txtWeek1Thursday.TabIndex <- 11
    txtWeek1Thursday.Text <- "0.00"
    txtWeek1Thursday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Thursday)
    
    // Create a text box for the time worked on Friday of the first week
    let txtWeek1Friday : TextBox = new TextBox()
    txtWeek1Friday.Location <- new Point(627, 71)
    txtWeek1Friday.Size <- new Size(106, 31)
    txtWeek1Friday.TabIndex <- 12
    txtWeek1Friday.Text <- "0.00"
    txtWeek1Friday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Friday)
    
    // Create a text box for the time worked on Saturday of the first week
    let txtWeek1Saturday : TextBox = new TextBox()
    txtWeek1Saturday.Location <- new Point(739, 71)
    txtWeek1Saturday.Size <- new Size(106, 31)
    txtWeek1Saturday.TabIndex <- 13
    txtWeek1Saturday.Text <- "0.00"
    txtWeek1Saturday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Saturday)
    
    // Create a text box for the time worked on Sunday of the first week
    let txtWeek1Sunday : TextBox = new TextBox()
    txtWeek1Sunday.Location <- new Point(851, 71)
    txtWeek1Sunday.Size <- new Size(106, 31)
    txtWeek1Sunday.TabIndex <- 14
    txtWeek1Sunday.Text <- "0.00"
    txtWeek1Sunday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek1Sunday)
    
    // Creatw a label for the times worked for the second week
    let lblSecondWeek : Label = new Label()
    lblSecondWeek.AutoSize <-  true
    lblSecondWeek.Location <- new Point(27, 130)
    lblSecondWeek.Size <- new Size(123, 25)
    lblSecondWeek.TabIndex <- 15
    lblSecondWeek.Text <- "Second Week:"
    gbxTimeValues.Controls.Add(lblSecondWeek)
    
    // Create a text box for the time worked on Monday of the second week
    let txtWeek2Monday : TextBox = new TextBox()
    txtWeek2Monday.Location <- new Point(179, 127)
    txtWeek2Monday.Size <- new Size(106, 31)
    txtWeek2Monday.TabIndex <- 16
    txtWeek2Monday.Text <- "0.00"
    txtWeek2Monday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Monday)
    
    // Create a text box for the time worked on Tuesday of the second week
    let txtWeek2Tuesday : TextBox = new TextBox()
    txtWeek2Tuesday.Location <- new Point(291, 127)
    txtWeek2Tuesday.Size <- new Size(106, 31)
    txtWeek2Tuesday.TabIndex <- 17
    txtWeek2Tuesday.Text <- "0.00"
    txtWeek2Tuesday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Tuesday)
    
    // Create a text box for the time worked on Wednesday of the second week
    let txtWeek2Wednesday : TextBox = new TextBox()
    txtWeek2Wednesday.Location <- new Point(403, 127)
    txtWeek2Wednesday.Size <- new Size(106, 31)
    txtWeek2Wednesday.TabIndex <- 18
    txtWeek2Wednesday.Text <- "0.00"
    txtWeek2Wednesday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Wednesday)
    
    // Create a text box for the time worked on Thursday of the second week
    let txtWeek2Thursday : TextBox = new TextBox()
    txtWeek2Thursday.Location <- new Point(515, 127)
    txtWeek2Thursday.Size <- new Size(106, 31)
    txtWeek2Thursday.TabIndex <- 19
    txtWeek2Thursday.Text <- "0.00"
    txtWeek2Thursday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Thursday)
    
    // Create a text box for the time worked on Friday of the second week
    let txtWeek2Friday : TextBox = new TextBox()
    txtWeek2Friday.Location <- new Point(627, 127)
    txtWeek2Friday.Size <- new Size(106, 31)
    txtWeek2Friday.TabIndex <- 20
    txtWeek2Friday.Text <- "0.00"
    txtWeek2Friday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Friday)
    
    // Create a text box for the time worked on Saturday of the second week
    let txtWeek2Saturday : TextBox = new TextBox()
    txtWeek2Saturday.Location <- new Point(739, 127)
    txtWeek2Saturday.Size <- new Size(106, 31)
    txtWeek2Saturday.TabIndex <- 21
    txtWeek2Saturday.Text <- "0.00"
    txtWeek2Saturday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Saturday)
    
    // Create a text box for the time worked on Sunday of the second week
    let txtWeek2Sunday : TextBox = new TextBox()
    txtWeek2Sunday.Location <- new Point(851, 127)
    txtWeek2Sunday.Size <- new Size(106, 31)
    txtWeek2Sunday.TabIndex <- 22
    txtWeek2Sunday.Text <- "0.00"
    txtWeek2Sunday.TextAlign <- HorizontalAlignment.Right
    gbxTimeValues.Controls.Add(txtWeek2Sunday)
    
    // Specify the characteristics of the Calculation section
    gbxCalculation.Location <- new Point(24, 369)
    gbxCalculation.Size <- new Size(985, 192)
    gbxCalculation.TabIndex <- 5
    gbxCalculation.TabStop <- false
    gbxCalculation.Text <- "Calculation"
    
    // Create a button that will be used to handle the calculations
    let btnCalculate : Button = new Button()
    btnCalculate.Location <- new Point(27, 70)
    btnCalculate.Size <- new Size(258, 73)
    btnCalculate.TabIndex <- 0
    btnCalculate.Text <- "Calculate"
    btnCalculate.UseVisualStyleBackColor <- true
    gbxCalculation.Controls.Add(btnCalculate)
    
    // The controls in the following section will be used to display the pay summary
    // Create a label that displays Time
    let lblTime : Label = new Label()
    lblTime.AutoSize <-  true
    lblTime.Location <- new Point(414, 35)
    lblTime.Size <- new Size(50, 25)
    lblTime.TabIndex <- 2
    lblTime.Text <- "Time"
    gbxCalculation.Controls.Add(lblTime)
    
    // Create a label that displays Pay Amount
    let lblPayAmount : Label = new Label()
    lblPayAmount.AutoSize <-  true
    lblPayAmount.Location <- new Point(525, 35)
    lblPayAmount.Size <- new Size(78, 25)
    lblPayAmount.TabIndex <- 4
    lblPayAmount.Text <- "Pay Amt"
    gbxCalculation.Controls.Add(lblPayAmount)
    
    // Create a label that displays Regular Time
    let lblRegular : Label = new Label()
    lblRegular.AutoSize <-  true
    lblRegular.Location <- new Point(324, 76)
    lblRegular.Size <- new Size(71, 25)
    lblRegular.TabIndex <- 1
    lblRegular.Text <- "Regular"
    gbxCalculation.Controls.Add(lblRegular)
    
    // Create a text box that will the regular time
    let txtRegularTime : TextBox = new TextBox()
    txtRegularTime.Enabled <- false
    txtRegularTime.Location <- new Point(414, 73)
    txtRegularTime.Size <- new Size(95, 31)
    txtRegularTime.TabIndex <- 3
    txtRegularTime.Text <- "0.00"
    txtRegularTime.TextAlign <- HorizontalAlignment.Right
    gbxCalculation.Controls.Add(txtRegularTime)
    
    // Create a text box that will the regular pay
    let txtRegularPay : TextBox = new TextBox()
    txtRegularPay.Enabled <- false
    txtRegularPay.Location <- new Point(525, 73)
    txtRegularPay.Size <- new Size(95, 31)
    txtRegularPay.TabIndex <- 5
    txtRegularPay.Text <- "0.00"
    txtRegularPay.TextAlign <- HorizontalAlignment.Right
    gbxCalculation.Controls.Add(txtRegularPay)
    
    // Create a label that displays Overtime
    let lblOvertime : Label = new Label()
    lblOvertime.AutoSize <-  true
    lblOvertime.Size <- new Size(85, 25)
    lblOvertime.TabIndex <- 6
    lblOvertime.Text <- "Overtime"
    lblOvertime.Location <- new Point(324, 122)
    gbxCalculation.Controls.Add(lblOvertime)
    
    // Create a text box that will the amount of overtime worked
    let txtOverTime : TextBox = new TextBox()
    txtOverTime.Enabled <- false
    txtOverTime.Location <- new Point(414, 119)
    txtOverTime.Size <- new Size(95, 31)
    txtOverTime.TabIndex <- 7
    txtOverTime.Text <- "0.00"
    txtOverTime.TextAlign <- HorizontalAlignment.Right
    gbxCalculation.Controls.Add(txtOverTime)
    
    // Create a text box that will the Overtime pay
    let txtOvertimePay : TextBox = new TextBox()
    txtOvertimePay.Enabled <- false
    txtOvertimePay.Location <- new Point(525, 119)
    txtOvertimePay.Size <- new Size(95, 31)
    txtOvertimePay.TabIndex <- 8
    txtOvertimePay.Text <- "0.00"
    txtOvertimePay.TextAlign <- HorizontalAlignment.Right
    gbxCalculation.Controls.Add(txtOvertimePay)
    
    // Create a label that displays Net Pay
    let lblNetPay : Label = new Label()
    lblNetPay.AutoSize <-  true
    lblNetPay.Location <- new Point(772, 73)
    lblNetPay.Size <- new Size(71, 25)
    lblNetPay.TabIndex <- 9
    lblNetPay.Text <- "Regular"
    gbxCalculation.Controls.Add(lblNetPay)
    
    // Create a text box that will the net pay
    let txtNetPay : TextBox = new TextBox()
    txtNetPay.Enabled <- false
    txtNetPay.Location <- new Point(862, 70)
    txtNetPay.Size <- new Size(95, 31)
    txtNetPay.TabIndex <- 10
    txtNetPay.Text <- "0.00"
    txtNetPay.TextAlign <- HorizontalAlignment.Right
    gbxCalculation.Controls.Add(txtNetPay)
    
    // This function is used to perform the calculations related to the payroll
    let  btnCalculateClick(e) =
        // Declare the variables that will get the values of the time worked from the time sheet
        let mutable week1Monday : double = 0.00
        let mutable week1Tuesday  = 0.00
        let mutable week1Wednesday = 0.00
        let mutable week1Thursday  = 0.00
        let mutable week1Friday   = 0.00
        let mutable week1Saturday  = 0.00
        let mutable week1Sunday    = 0.00
        let mutable week2Monday   = 0.00
        let mutable week2Ttuesday  = 0.00
        let mutable week2Wednesday = 0.00
        let mutable week2Thursday = 0.00
        let mutable week2Friday    = 0.00
        let mutable week2Saturday  = 0.00
        let mutable week2Sunday   = 0.00
    
        // Declare a value for the hourly salary
        let mutable hourlySalary : double = 0.00
    
        // Get the hourly salary. Use exception handling in case the user types a bad value.
        try
            hourlySalary <- float txtHourlySalary.Text
        with    
            | :? FormatException -> MessageBox.Show("The value you typed for the salary is invalid. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
    
        (* Get the value for each day worked.
         * Use exception handling for each text box in case the user types a bad value.
         * Get the value worked for Monday of the first week. *)
        try
            week1Monday <- float txtWeek1Monday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Monday of the first week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
        
        // Get the value worked for Tuesday of the first week
        try
            week1Tuesday <- float txtWeek1Tuesday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Tuesday of the first week. " +
                                                   "Please try again.", "Payroll Evaluation",
                                                   MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
        
        // Get the value worked for Wednesday of the first week
        try
            week1Wednesday <- float txtWeek1Wednesday.Text
        with
            | :? FormatException ->  MessageBox.Show("You typed an invalid value for Wednesday of the first week. " +
                                                     "Please try again.", "Payroll Evaluation",
                                                     MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
        
        // Get the value worked for Thursday of the first week
        try
            week1Thursday <- float txtWeek1Thursday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Thursday of the first week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
    
        // Get the value worked for Friday of the first week
        try
            week1Friday <- float txtWeek1Friday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Firday of the first week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
    
        // Get the value worked for Saturday of the first week
        try
            week1Saturday <- float txtWeek1Saturday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Saturday of the first week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Sunday of the first week
        try
            week1Sunday <- float txtWeek1Sunday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Sunday of the first week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                
        // Get the value worked for Monday of the second week
        try
            week2Monday <- float txtWeek2Monday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Monday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Tuesday of the second week
        try
            week2Ttuesday <- float txtWeek2Tuesday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Tuesday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Wednesday of the second week
        try
            week2Wednesday <- float txtWeek2Wednesday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Wednesday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Thursday of the second week
        try
            week2Thursday <- float txtWeek2Thursday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Thursday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Friday of the second week
        try
            week2Friday <- float txtWeek2Friday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Friday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Saurday of the second week
        try
            week2Saturday <- float txtWeek2Saturday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Saturday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
                                                    
        // Get the value worked for Sunday of the second week
        try
            week2Sunday <- float txtWeek2Sunday.Text
        with
            | :? FormatException -> MessageBox.Show("You typed an invalid value for Sunday of the second week. " +
                                                    "Please try again.", "Payroll Evaluation",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Information) |> ignore
    
        // Calculate the total time worked for the first week
        let week1TotalTime = week1Monday   + week1Tuesday  + week1Wednesday +
                             week1Thursday + week1Friday   + week1Saturday  + week1Sunday;
        // Calculate the total time worked for the second week
        let week2TotalTime = week2Monday   + week2Ttuesday + week2Wednesday +
                             week2Thursday + week2Friday   + week2Saturday  + week2Sunday;
    
        // The overtime is paid time and half
        let ovtSalary = hourlySalary * 1.5;
    
        // Declare the variables that will be involved in the calculations
        let mutable week1RegularTime = 0.00
        let mutable week2RegularTime = 0.00
        let mutable week1OverTime    = 0.00
        let mutable week2OverTime    = 0.00
        let mutable week1RegularPay  = 0.00
        let mutable week2RegularPay  = 0.00
        let mutable week1OvertimePay = 0.00
        let mutable week2OvertimePay = 0.00
    
        // If the employee worked below 40 hours, there is no overtime
        if week1TotalTime < 40.00 then
            week1RegularTime <- week1TotalTime
            week1RegularPay  <- hourlySalary * week1RegularTime
            week1OverTime    <- 0.00
            week1OvertimePay <- 0.00
        elif week1TotalTime >= 40.00 then // If the employee worked over 40 hours, calculate the overtime
            week1RegularTime <- 40.00
            week1RegularPay  <- hourlySalary * 40.00
            week1OverTime    <- week1TotalTime - 40.00
            week1OvertimePay <- week1OverTime * ovtSalary
        
        // Use the same logic for the second week and perform the calculations
        if week2TotalTime < 40 then
            week2RegularTime <- week2TotalTime
            week2RegularPay  <- hourlySalary * week2RegularTime
            week2OverTime    <- 0.00
            week2OvertimePay <- 0.00
        elif week2TotalTime >= 40.00 then
            week2RegularTime <- 40.00
            week2RegularPay  <- hourlySalary * 40.00
            week2OverTime    <- week2TotalTime - 40.00
            week2OvertimePay <- week2OverTime * ovtSalary
    
        // To get the total regular time, add the regular times of the first and the second week
        let regularTime  = week1RegularTime + week2RegularTime
        // To get the total overtime, add the ovetimes of the first and the second week
        let overTime     = week1OverTime + week2OverTime
        // To get the total regular pay, add the regular pays of the first and the second week
        let regularPay   = week1RegularPay + week2RegularPay
        // To get the total overtime pay, add the overtime pays of the first and the second week
        let overtimePay  = week1OvertimePay + week2OvertimePay
                
        // To get the total pay, add the net pays of the first and the second week
        let netPay       = regularPay + overtimePay
    
        // Display the results as a pay summary
        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")
    
    // Pass the calculation function to the Click event of the Calculate button
    btnCalculate.Click.Add(btnCalculateClick)
    
    // Create a button that will be used to close the form
    let btnClose : Button = new Button()
    btnClose.Location <- new Point(772, 119)
    btnClose.Size <- new Size(185, 34)
    btnClose.TabIndex <- 11
    btnClose.Text <- "Close"
    btnClose.UseVisualStyleBackColor <- true
    let btnCloseClick(e) =
        exercise.Close()
    btnClose.Click.Add(btnCloseClick)
    gbxCalculation.Controls.Add(btnClose)
    
    exercise.Controls.Add(gbxCalculation)
    exercise.Controls.Add(gbxTimeValues)
    exercise.Controls.Add(gpxEmployeeIdentification)
    
    gbxCalculation.ResumeLayout(false)
    gbxCalculation.PerformLayout()
    gbxTimeValues.ResumeLayout(false)
    gbxTimeValues.PerformLayout()
    gpxEmployeeIdentification.ResumeLayout(false)
    gpxEmployeeIdentification.PerformLayout()
    exercise.ResumeLayout(false)
    
    Application.Run(exercise)
  13. To execute the application, on the main menu, click Debug and click Start Without Debudding:

    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