C# Console Application: Payroll Evaluation - 1 Week
C# Console Application: Payroll Evaluation - 1 Week
Introduction
In this introductory lesson on C#, we will use a conditional statement to validate a condition and perform some simple calculations.
Practical Learning: Creating the Application
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 hSalary = double.Parse(ReadLine()!);
WriteLine("---------------------------------------------------");
WriteLine("Time worked");
WriteLine("---------------------------------------------------");
Write("Monday: ");
double monday = double.Parse(ReadLine()!);
Write("Tuesday: ");
double tuesday = double.Parse(ReadLine()!);
Write("Wednesday: ");
double wednesday = double.Parse(ReadLine()!);
Write("Thursday: ");
double thursday = double.Parse(ReadLine()!);
Write("Friday: ");
double friday = double.Parse(ReadLine()!);
double timeWorked = monday + tuesday + wednesday + thursday + friday;
double regTime = timeWorked;
double overtime = 0.00;
double regPay = hSalary * timeWorked;
double overPay = 0.00;
if(timeWorked is > 40.00)
{
regTime = 40.00;
overtime = timeWorked - 40.00;
regPay = hSalary * 40.00;
overPay = hSalary * 1.50 * overtime;
}
double netPay = regPay + overPay;
WriteLine("===================================================");
WriteLine("Payroll Evaluation");
WriteLine("===================================================");
WriteLine("Employee Information");
WriteLine("---------------------------------------------------");
WriteLine($"Full Name: {firstName} {lastName}");
WriteLine($"Hourly Salary: {hSalary: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}",
monday, tuesday, wednesday, thursday, friday);
WriteLine("========+=========+===========+==========+=========");
WriteLine(" Pay Summary");
WriteLine("---------------------------------------------------");
WriteLine(" Time Pay");
WriteLine("---------------------------------------------------");
WriteLine(" Regular:{0,9:f}{1,12:f}", regTime, regPay);
WriteLine("---------------------------------------------------");
WriteLine(" Overtime:{0,8:f}{1,12:f}", overtime, overPay);
WriteLine("===================================================");
WriteLine(" Net Pay:{0,21:f}", netPay);
WriteLine("===================================================");| First Name: | Michael |
| Last Name: | Carlock |
| Hourly Salary: | 28.46 |
| Monday | 7 |
| Tuesday: | 8 |
| Wednesday: | 6.5 |
| Thursday: | 8.5 |
| Friday: | 7.5 |
Payroll Evaluation
---------------------------------------------------
Enter the following pieces of information
---------------------------------------------------
Employee Information
---------------------------------------------------
First Name: Michael
Last Name: Carlock
Hourly Salary: 28.46
---------------------------------------------------
Time worked
---------------------------------------------------
Monday: 7
Tuesday: 8
Wednesday: 6.5
Thursday: 8.5
Friday: 7.5
===================================================
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
========+=========+===========+==========+=========
Pay Summary
---------------------------------------------------
Time Pay
---------------------------------------------------
Regular: 37.50 1067.25
---------------------------------------------------
Overtime: 0.00 0.00
===================================================
Net Pay: 1067.25
===================================================
Press any key to close this window . . .Using an Opposite Conditional Statement
In the previous section, we use a Greater Than operator to check a condition. That operator has an opposite, which is the Less Than Or Equal operator. We will uase apply as an example.
Practical Learning: Using an Opposite Conditional Statement
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 hSalary = double.Parse(ReadLine()!);
WriteLine("---------------------------------------------------");
WriteLine("Time worked");
WriteLine("---------------------------------------------------");
Write("Monday: ");
double monday = double.Parse(ReadLine()!);
Write("Tuesday: ");
double tuesday = double.Parse(ReadLine()!);
Write("Wednesday: ");
double wednesday = double.Parse(ReadLine()!);
Write("Thursday: ");
double thursday = double.Parse(ReadLine()!);
Write("Friday: ");
double friday = double.Parse(ReadLine()!);
double timeWorked = monday + tuesday + wednesday + thursday + friday;
double regTime = 40.00;
double overtime = timeWorked - 40.00;
double regPay = hSalary * 40.00;
double overPay = hSalary * 1.50 * overtime;
if(timeWorked is <= 40.00)
{
regTime = timeWorked;
overtime = 0.00;
regPay = hSalary * timeWorked;
overPay = 0.00;
}
double netPay = regPay + overPay;
WriteLine("===================================================");
WriteLine("Payroll Evaluation");
WriteLine("===================================================");
WriteLine("Employee Information");
WriteLine("---------------------------------------------------");
WriteLine($"Full Name: {firstName} {lastName}");
WriteLine($"Hourly Salary: {hSalary: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}",
monday, tuesday, wednesday, thursday, friday);
WriteLine("========+=========+===========+==========+=========");
WriteLine(" Pay Summary");
WriteLine("---------------------------------------------------");
WriteLine(" Time Pay");
WriteLine("---------------------------------------------------");
WriteLine(" Regular:{0,9:f}{1,12:f}", regTime, regPay);
WriteLine("---------------------------------------------------");
WriteLine(" Overtime:{0,8:f}{1,12:f}", overtime, overPay);
WriteLine("===================================================");
WriteLine(" Net Pay:{0,21:f}", netPay);
WriteLine("===================================================");| First Name: | Catherine |
| Last Name: | Busbey |
| Hourly Salary: | 24.37 |
| Monday | 9.5 |
| Tuesday: | 8 |
| Wednesday: | 10.5 |
| Thursday: | 9 |
| Friday: | 10.5 |
Payroll Evaluation
---------------------------------------------------
Enter the following pieces of information
---------------------------------------------------
Employee Information
---------------------------------------------------
First Name: Catherine
Last Name: Busbey
Hourly Salary: 24.37
---------------------------------------------------
Time worked
---------------------------------------------------
Monday: 9.5
Tuesday: 8
Wednesday: 10.5
Thursday: 9
Friday: 10.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 | 10.50
========+=========+===========+==========+=========
Pay Summary
---------------------------------------------------
Time Pay
---------------------------------------------------
Regular: 40.00 974.80
---------------------------------------------------
Overtime: 7.50 274.16
===================================================
Net Pay: 1248.96
===================================================
Press any key to close this window . . .|
|
|||
| Home | Copyright © 2001-2026, FunctionX | Saturday 14 December 2024, 18:55 | Home |
|
|
|||