In: Computer Science
Create a C# Application. Create a class object called “Employee” which includes the following private variables:
firstN
lastN
idNum
wage: holds how much the person makes per hour
weekHrsWkd: holds how many total hours the person worked each
week
regHrsAmt: initialize to a fixed amount of 40 using
constructor.
regPay
otPay
After going over the regular hours, the employee gets 1.5x the
wage for each additional hour worked. Methods:
constructor
properties
CalcPay(): Calculate the regular pay and overtime pay.
Create an “EmployeeDemo” class. In the main function, the program should ask the user the number of employee in the company and create a 2-dimensional dynamic array (number of employee by 4 weeks). Then, the program should ask user to enter each employee’s information and the amount of hours they worked weekly.
The program shows a menu with employee name for user to choose
which employee to display the following information:
How much the person totally made
How much of paycheck is regular pay
How much of paycheck is overtime pay
Code Screenshots:
Sample Output:
Code to Copy:
using System;
//Define the class Employee()
class Employee
{
//Declare the
//required private variables.
private string firstN;
private string lastN;
private int idNum;
private double wage;
private double weekHrsWkd;
private double regHrsAmt;
private double regPay;
private double otPay;
//Define the constructor.
public Employee(string fname = "", string lname = "",
int id = -1, double sal = 0, double hrs = 0)
{
firstN = fname;
lastN = lname;
idNum = id;
wage = sal;
weekHrsWkd = hrs;
regHrsAmt = 40;
regPay = 0;
otPay = 0;
CalcPay();
}
//Define the required properties.
public int ID
{
get
{
return idNum;
}
}
public string Name
{
get
{
return lastN + ", " + firstN;
}
}
public double RegPay
{
get
{
return regPay;
}
}
public double OTPay
{
get
{
return otPay;
}
}
//Define the method CalcPay().
public void CalcPay()
{
//Declare the
//required variables.
double othrs, reghrs;
//If the employee has
//worked more than the Regular
//Hours, calculate
//the overtime hours.
if(weekHrsWkd > regHrsAmt)
{
reghrs = regHrsAmt;
othrs = weekHrsWkd - regHrsAmt;
}
//Otherwise, set the
//overtime hours to zero.
else
{
othrs = 0;
reghrs = weekHrsWkd;
}
//Calculate the
//regular and overtime pay.
regPay = wage * reghrs;
otPay = 1.5 * wage * othrs;
}
}
//Define the class EmployeeDemo.
class EmployeeDemo {
//Define the Main() method.
public static void Main (string[] args)
{
//Define a 2-D array to
//store the employees' data.
Employee [,] employees;
//Declare a variable to
//store the required variables.
int numEmp;
//Prompt the user to enter
//the number of employees.
Console.Write("Enter the number of employees: ");
//Read and store the input.
numEmp = Convert.ToInt32(Console.ReadLine());
//Allocate memory to the 2-D array.
employees = new Employee[numEmp, 4];
//Prompt the user to enter
//the details of the employees.
Console.WriteLine("Enter the details of the employees:");
for(int i=0; i<numEmp; i++)
{
string fname;
string lname;
int id;
double sal;
double hrs;
Console.WriteLine("Employee {0}:", (i+1));
Console.Write("Enter the first name: ");
fname = Console.ReadLine();
Console.Write("Enter the last name: ");
lname = Console.ReadLine();
Console.Write("Enter the id of the employee: ");
id = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the hourly pay: ");
sal = Convert.ToDouble(Console.ReadLine());
for(int j=0; j<4; j++)
{
Console.Write("Enter the hours for week {0}: ", j+1);
hrs = Convert.ToDouble(Console.ReadLine());
employees[i, j] = new Employee(fname, lname, id, sal, hrs);
}
}
Console.WriteLine("Choose one of the following employees: ");
for(int i=0; i<numEmp; i++)
{
Console.WriteLine("{0}. "+employees[i,0].Name, i+1);
}
Console.Write("Enter the choice (1 - {0}): ", numEmp);
int choice = Convert.ToInt32(Console.ReadLine()) - 1;
if(choice >= 0 && choice < numEmp)
{
double ot = 0;
double reg = 0;
double total = 0;
for(int i = 0; i<4; i++)
{
reg += employees[choice, i].RegPay;
ot += employees[choice, i].OTPay;
}
total = reg + ot;
Console.WriteLine("The details of the selected employee are as follows:");
Console.WriteLine("Total Pay: ${0}", total);
Console.WriteLine("Regular Pay: ${0}", reg);
Console.WriteLine("Overtime Pay: ${0}", ot);
}
}
}