C# You have to create two classes. The first class “Employee” which included: Th
ID: 3572164 • Letter: C
Question
C#
You have to create two classes. The first class “Employee” which included: The private variables:
• first name
• last name
• id number (Format: A#####)
• Wage • hoursWorked
The hoursWorked variable holds how many total hours the person worked. When the hoursWorked is over 40 hours, it will be considered as over time. Wage holds how much the person makes per hour. After passing the 40 hours, they get 1.5 x the wage for each additional hour worked. The functions:
• Constructors
• Properties
• GetGrossPay: calculate the gross pay for the employee (Be careful with the over time)
The second class will be “EmployeeDemo” class with the main function. In the main function, Read the data from the “employeeinfo.txt” file and save the data in the array of (Employee) objects. In the file, the first number is the total number of employees and each employee’s information.
After import the data, display a menu for user to choose:
1. Display all employees
2. Add New employee
3. Quit the program (save the information to “employeeinfo.txt” file)
employee.txt file
Explanation / Answer
/// place your employeeinfo.txt in C: or change the path in code accordingly to test.
using System;
class Employee{
//Private variables
private string firstName,lastName, idNumber;
private double wage,hoursWorked;
//Constructor
public Employee(){
}
public void setData(string infirstName,string inlastName,string inIdNumber, double inWage, double inHoursWorked){
firstName=infirstName;
lastName=inlastName;
idNumber=inIdNumber;
wage=inWage;
hoursWorked=inHoursWorked;
}
public double GetGrossPay(){
double perHour=wage/hoursWorked;
if (hoursWorked>40)
return wage+(1.5*perHour*(hoursWorked-40));
else
return wage;
}
public void displayEmployee(){
double gp=GetGrossPay();
Console.WriteLine(firstName+","+lastName+","+idNumber+","+wage+","+hoursWorked+","+gp);
}
}
public class EmployeeDemo
{
public static void Main()
{
int counter = 0,emp_c=0;
Employee[] empdata= new Employee[0];
string line,emp="";
// Open the file
System.IO.StreamReader file = new System.IO.StreamReader(@"c:employeeinfo.txt");
// Read the file and display it line by line.
while((line = file.ReadLine()) != null)
{
emp=emp+","+line;
if(counter==0){
Array.Resize(ref empdata, empdata.Length+Int32.Parse(line));
emp="";
}
if(counter%5==0 && counter>0){
string[] strArr = null;
strArr = emp.Split(',');
empdata[emp_c].setData(strArr[0]
,strArr[1]
,strArr[2]
,double.Parse(strArr[3])
,double.Parse(strArr[4]));
emp_c++;
emp="";
}
counter++;
}
file.Close();
for (int i = 0; i < empdata.Length; i++)
{
empdata[i].displayEmployee();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.