Introduction

In this introductory lesson on F#, we will use a conditional statement to validate a condition and perform some simple calculations.

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. In 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#.
    In the list of projects templates, select Console App (it should be selected already)
  4. Click Next
  5. In the Configure Your New Project dialog box, change the Project Name to PayrollEvaluation2 and set the Location of your choice
  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. Change the document as follows:
    printfn "Payroll Evaluation"
    printfn "-------------------------------------------------------"
    printfn "Enter the following pieces of information"
    printfn "-------------------------------------------------------"
    printfn "Employee Information"
    printfn "-------------------------------------------------------"
    printf "First Name:      ";
    let firstName : string = stdin.ReadLine()
    printf "Last Name:       "
    let lastName : string = stdin.ReadLine()
    printf "Hourly Salary:   "
    let hSalary = float(stdin.ReadLine())
    printfn "======================================================="
    printfn "Time worked - First Week"
    printfn "-------------------------------------------------------"
    
    printf "Monday:          "
    let wk1Monday    = float(stdin.ReadLine())
    
    printf "Tuesday:         "
    let wk1Tuesday   = float(stdin.ReadLine())
    
    printf "Wednesday:       "
    let wk1Wednesday = float(stdin.ReadLine())
    
    printf "Thursday:        "
    let wk1Thursday  = float(stdin.ReadLine())
    
    printf "Friday:          "
    let wk1Friday    = float(stdin.ReadLine())
    printfn "======================================================="
    printfn "Time worked - Second Week"
    printfn "-------------------------------------------------------"
    
    printf "Monday:          "
    let wk2Monday    = float(stdin.ReadLine())
    
    printf "Tuesday:         "
    let wk2Tuesday   = float(stdin.ReadLine())
    
    printf "Wednesday:       "
    let wk2Wednesday = float(stdin.ReadLine())
    
    printf "Thursday:        "
    let wk2Thursday  = float(stdin.ReadLine())
    
    printf "Friday:          "
    let wk2Friday    = float(stdin.ReadLine())
    
    let wk1TimeWorked = wk1Monday + wk1Tuesday + wk1Wednesday + wk1Thursday + wk1Friday
    let wk2TimeWorked = wk2Monday + wk2Tuesday + wk2Wednesday + wk2Thursday + wk2Friday
    
    let mutable wk1RegTime  = wk1TimeWorked
    let mutable wk1Overtime = 0.00
    let mutable wk1RegPay   = hSalary * wk1TimeWorked
    let mutable wk1OverPay  = 0.00
            
    if wk1TimeWorked     > 40.00 then
        wk1RegTime          <- 40.00
        wk1Overtime         <- wk1TimeWorked - 40.00
        wk1RegPay           <- hSalary * 40.00
        wk1OverPay          <- hSalary * 1.50 * wk1Overtime
    
    let mutable wk2RegTime  = wk2TimeWorked
    let mutable wk2Overtime = 0.00
    let mutable wk2RegPay   = hSalary * wk2TimeWorked
    let mutable wk2OverPay  = 0.00
            
    if wk2TimeWorked     > 40.00 then
        wk2RegTime          <- 40.00
        wk2Overtime         <- wk2TimeWorked - 40.00
        wk2RegPay           <- hSalary * 40.00
        wk2OverPay          <- hSalary * 1.50 * wk2Overtime
    
    let wk1NetPay           = wk1RegPay + wk1OverPay
    let wk2NetPay           = wk2RegPay + wk2OverPay
    let netPay              = wk1NetPay + wk2NetPay
    
    printfn "======================================================="
    printfn "Payroll Evaluation"
    printfn "======================================================="
    printfn "Employee Information"
    printfn "-------------------------------------------------------"
    printfn "Full Name:     %s %s" firstName lastName
    printfn "Hourly Salary: %0.2f" hSalary
    printfn "======================================================="
    printfn "Time Worked Summary"
    printfn "--------+---------+-----------+----------+-------------"
    printfn " Monday | Tuesday | Wednesday | Thursday |  Friday"
    printfn "--------+---------+-----------+----------+-------------"
    printfn "%6.2f  | %6.2f  | %7.2f   | %7.2f  | %6.2f" wk1Monday wk1Tuesday wk1Wednesday wk1Thursday wk1Friday
    printfn "--------+---------+-----------+----------+-------------"
    printfn "%6.2f  | %6.2f  | %7.2f   | %7.2f  | %6.2f" wk2Monday wk2Tuesday wk2Wednesday wk2Thursday wk2Friday
    printfn "========+=========+===========+==========+============="
    printfn "                                 Pay Summary"
    printfn "-------------------------------------------------------"
    printfn "                                 Time      Pay"
    printfn "-------------------------------------------------------"
    printfn "          Week 1 - Regular:%8.2f %9.2f" wk1RegTime wk1RegPay
    printfn "-------------------------------------------------------"
    printfn "                   Overtime:%7.2f %9.2f" wk1Overtime wk1OverPay
    printfn "-------------------------------------------------------"
    printfn "                   Weekly Pay:%16.2f" wk1NetPay
    printfn "======================================================="
    printfn "          Week 2 - Regular:%8.2f %10.2f" wk2RegTime wk2RegPay
    printfn "-------------------------------------------------------"
    printfn "                   Overtime:%7.2f %10.2f" wk2Overtime wk2OverPay
    printfn "-------------------------------------------------------"
    printfn "                   Weekly Pay:%16.2f" wk2NetPay
    printfn "======================================================="
    printfn "                   Net Pay:%19.2f" netPay
    printfn "======================================================="
  9. To execute the project, on the main menu, click Debug -> Start Without Debugging
  10. When requested, type each of the following values and press Enter each time:
    First Name:    Michael
    Last Name:     Carlock
    Hourly Salary: 28.46
    Week 1
    Monday:         7
    Tuesday:        8
    Wednesday:      6.5
    Thursday:       8.5
    Friday:         7.5
    Week 2
    Monday:         9.5
    Tuesday:        8.5
    Wednesday:     10.5
    Thursday:       9
    Friday:         8
    
    Here are are the results:
    Payroll Evaluation
    -------------------------------------------------------
    Enter the following pieces of information
    -------------------------------------------------------
    Employee Information
    -------------------------------------------------------
    First Name:      Catherine
    Last Name:       Carlock
    Hourly Salary:   28.46
    =======================================================
    Time worked - First Week
    -------------------------------------------------------
    Monday:          7
    Tuesday:         8
    Wednesday:       6.5
    Thursday:        8.5
    Friday:          7.5
    =======================================================
    Time worked - Second Week
    -------------------------------------------------------
    Monday:          9.5
    Tuesday:         8.5
    Wednesday:       10.5
    Thursday:        9
    Friday:          8
    =======================================================
    Payroll Evaluation
    =======================================================
    Employee Information
    -------------------------------------------------------
    Full Name:     Catherine Carlock
    Hourly Salary: 28.46
    =======================================================
    Time Worked Summary
    --------+---------+-----------+----------+-------------
     Monday | Tuesday | Wednesday | Thursday |  Friday
    --------+---------+-----------+----------+-------------
      7.00  |   8.00  |    6.50   |    8.50  |   7.50
    --------+---------+-----------+----------+-------------
      9.50  |   8.50  |   10.50   |    9.00  |   8.00
    ========+=========+===========+==========+=============
                                     Pay Summary
    -------------------------------------------------------
                                     Time      Pay
    -------------------------------------------------------
              Week 1 - Regular:   37.50   1067.25
    -------------------------------------------------------
                       Overtime:   0.00      0.00
    -------------------------------------------------------
                       Weekly Pay:         1067.25
    =======================================================
              Week 2 - Regular:   40.00    1138.40
    -------------------------------------------------------
                       Overtime:   5.50     234.79
    -------------------------------------------------------
                       Weekly Pay:         1373.20
    =======================================================
                       Net Pay:            2440.45
    =======================================================
    
    Press any key to close this window . . .
  11. Return to your programming environment and, to execute the project again, on the main menu, click Debug -> Start Without Debugging
  12. When requested, type each of the following values and press Enter each time:
    First Name:    Catherine
    Last Name:     Busbey
    Hourly Salary: 24.37
    Week 1
    Monday:         9.5
    Tuesday:        8
    Wednesday:     10.5
    Thursday:       9
    Friday:         8.5
    Week 2
    Monday:         8
    Tuesday:        7.5
    Wednesday:      8
    Thursday:       6.5
    Friday:         7.5
    Here are are the results:
    Payroll Evaluation
    -------------------------------------------------------
    Enter the following pieces of information
    -------------------------------------------------------
    Employee Information
    -------------------------------------------------------
    First Name:      Catherine
    Last Name:       Busbey
    Hourly Salary:   24.37
    =======================================================
    Time worked - First Week
    -------------------------------------------------------
    Monday:          9.5
    Tuesday:         8
    Wednesday:       10.5
    Thursday:        9
    Friday:          8.5
    =======================================================
    Time worked - Second Week
    -------------------------------------------------------
    Monday:          8
    Tuesday:         7.5
    Wednesday:       8
    Thursday:        6.5
    Friday:          7.5
    =======================================================
    Payroll Evaluation
    =======================================================
    Employee Information
    -------------------------------------------------------
    Full Name:     Catherine Busbey
    Hourly Salary: 24.37
    =======================================================
    Time Worked Summary
    --------+---------+-----------+----------+-------------
     Monday | Tuesday | Wednesday | Thursday |  Friday
    --------+---------+-----------+----------+-------------
      9.50  |   8.00  |   10.50   |    9.00  |   8.50
    --------+---------+-----------+----------+-------------
      8.00  |   7.50  |    8.00   |    6.50  |   7.50
    ========+=========+===========+==========+=============
                                     Pay Summary
    -------------------------------------------------------
                                     Time      Pay
    -------------------------------------------------------
              Week 1 - Regular:   40.00    974.80
    -------------------------------------------------------
                       Overtime:   5.50    201.05
    -------------------------------------------------------
                       Weekly Pay:         1175.85
    =======================================================
              Week 2 - Regular:   37.50     913.88
    -------------------------------------------------------
                       Overtime:   0.00       0.00
    -------------------------------------------------------
                       Weekly Pay:          913.88
    =======================================================
                       Net Pay:            2089.73
    =======================================================
    
    Press any key to close this window . . .
  13. Press T to close the window and return to your programming environment

Home Copyright © 2001-2025, FunctionX Saturday 21 December 2024, 10:15 Home