Introduction

In this introductory lesson on C#, 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 C#.
    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:
    using static System.Console;
    
    WriteLine("Payroll Evaluation");
    WriteLine("===================================================");
    WriteLine("Enter the following pieces of information");
    WriteLine("---------------------------------------------------");
    WriteLine("Employee Information");
    WriteLine("---------------------------------------------------");
    Write("First Name:        ");
    string? firstName = ReadLine();
    Write("Last Name:         ");
    string? lastName = ReadLine();
    Write("Hourly Salary:     ");
    double hourlySalary = double.Parse(ReadLine()!);
    WriteLine("===================================================");
    WriteLine("Time worked - First Week");
    WriteLine("---------------------------------------------------");
    
    Write("Monday:            ");
    double wk1Monday = double.Parse(ReadLine()!);
    
    Write("Tuesday:           ");
    double wk1Tuesday = double.Parse(ReadLine()!);
    
    Write("Wednesday:         ");
    double wk1Wednesday = double.Parse(ReadLine()!);
    
    Write("Thursday:          ");
    double wk1Thursday = double.Parse(ReadLine()!);
    
    Write("Friday:            ");
    double wk1Friday = double.Parse(ReadLine()!);
    WriteLine("===================================================");
    WriteLine("Time worked - Second Week");
    WriteLine("---------------------------------------------------");
    
    Write("Monday:            ");
    double wk2Monday = double.Parse(ReadLine()!);
    
    Write("Tuesday:           ");
    double wk2Tuesday = double.Parse(ReadLine()!);
    
    Write("Wednesday:         ");
    double wk2Wednesday = double.Parse(ReadLine()!);
    
    Write("Thursday:          ");
    double wk2Thursday = double.Parse(ReadLine()!);
    
    Write("Friday:            ");
    double wk2Friday = double.Parse(ReadLine()!);
    
    double wk1TimeWorked = wk1Monday + wk1Tuesday + wk1Wednesday + wk1Thursday + wk1Friday;
    double wk2TimeWorked = wk2Monday + wk2Tuesday + wk2Wednesday + wk2Thursday + wk2Friday;
    
    double wk1RegularTime = 40.0;
    double wk1OverTime = wk1TimeWorked - 40.0;
    double wk1RegularPay = hourlySalary * 40.0;
    double wk1OvertimePay = hourlySalary * 1.5 * wk1OverTime;
    
    double wk2RegularTime = 40.0;
    double wk2OverTime = wk2TimeWorked - 40.0;
    double wk2RegularPay = hourlySalary * 40.0;
    double wk2OvertimePay = hourlySalary * 1.5 * wk2OverTime;
    
    if(wk1TimeWorked <= 40.0)
    {
        wk1RegularTime = wk1TimeWorked;
        wk1OverTime = 0.0;
        wk1RegularPay = hourlySalary * wk1TimeWorked;
        wk1OvertimePay = 0.00;
    }
    
    if(wk2TimeWorked <= 40.0)
    {
        wk2RegularTime = wk2TimeWorked;
        wk2OverTime = 0.0;
        wk2RegularPay = hourlySalary * wk2TimeWorked;
        wk2OvertimePay = 0.00;
    }
    
    double wk1NetPay = wk1RegularPay + wk1OvertimePay;
    double wk2NetPay = wk2RegularPay + wk2OvertimePay;
    double netPay = wk1NetPay + wk2NetPay;
    
    WriteLine("===================================================");
    WriteLine("Payroll Evaluation");
    WriteLine("===================================================");
    WriteLine("Employee Information");
    WriteLine("---------------------------------------------------");
    WriteLine($"Full Name:     {firstName} {lastName}");
    WriteLine($"Hourly Salary: {hourlySalary:f}");
    WriteLine("===================================================");
    WriteLine("Time Worked Summary");
    WriteLine("--------+---------+-----------+----------+---------");
    WriteLine(" Monday | Tuesday | Wednesday | Thursday | Friday");
    WriteLine("--------+---------+-----------+----------+---------");
    WriteLine("{0,6:f}  | {1,6:f}  | {2,7:f}   | {3,7:f}  | {4,5:f}",
                              wk1Monday, wk1Tuesday, wk1Wednesday, wk1Thursday, wk1Friday);
    WriteLine("--------+---------+-----------+----------+---------");
    WriteLine("{0,6:f}  | {1,6:f}  | {2,7:f}   | {3,7:f}  | {4,5:f}",
                              wk2Monday, wk2Tuesday, wk2Wednesday, wk2Thursday, wk2Friday);
    WriteLine("========+=========+===========+==========+=========");
    WriteLine("                   Pay Summary");
    WriteLine("---------------------------------------------------");
    WriteLine("                                Time      Pay");
    WriteLine("===================================================");
    WriteLine("    First Week     Regular:{0,9:f}{1,12:f}", wk1RegularTime, wk1RegularPay);
    WriteLine("---------------------------------------------------");
    WriteLine("                   Overtime:{0,8:f}{1,12:f}", wk1OverTime, wk1OvertimePay);
    WriteLine("---------------------------------------------------");
    WriteLine("                   Weekly Pay:{0,18:f}", wk1NetPay);
    WriteLine("===================================================");
    WriteLine("    Second Week    Regular:{0,9:f}{1,12:f}", wk2RegularTime, wk2RegularPay);
    WriteLine("---------------------------------------------------");
    WriteLine("                   Overtime:{0,8:f}{1,12:f}", wk2OverTime, wk2OvertimePay);
    WriteLine("---------------------------------------------------");
    WriteLine("                   Weekly Pay:{0,18:f}", wk2NetPay);
    WriteLine("===================================================");
    WriteLine("                   Net Pay:{0,21:f}", netPay);
    WriteLine("===================================================");;
  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:        Michael
    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:     Michael 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
    ---------------------------------------------------
        First Week     Regular:    37.50     1067.25
    ---------------------------------------------------
                       Overtime:    0.00        0.00
    ---------------------------------------------------
                       Weekly Pay:           1067.25
    ===================================================
        Second Week    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. Press C to close the window and return to your programming environment
  12. Close your programming environment

Home Copyright © 2001-2025, FunctionX Sunday 22 December 2024, 15:36 Home