Background
A company has three employee categories: salaried, wage and part-time. All employees have an employee ID number, name and SIN. Salaried employees have ID numbers starting with 0- 4, whereas wage employee's IDs start with 5-7 and part-time employee's IDs start with 8-9. All employees are paid weekly and payment for each employee category is calculated as follows:
- Salaried employees are paid a set salary each week.
- Wage employees' pay is calculated using hourly rate * work hours with overtime paid at time and a half for any hours worked over 40 in one week.
- Part-time employees' pay is calculated based on hourly rate * work hours with no overtime paid.
Instructions
1. Create the classes as shown in the diagram on the following page.
2. Place the employees.txt data file in the res folder.
3. Make an inheritance tree whereby Salaried, PartTime and Wages inherit from Employee.
4. Make an application with the following methods:
a. Fill a list with objects based on the supplied data file.
b. Calculate and return the average weekly pay for all employees.
c. Calculate and return the highest weekly pay for the wage employees, including the
name of the employee.
d. Calculate and return the lowest salary for the salaried employees, including the name
of the employee.
e. What percentage of the company's employees fall into each employee category?
This is the Text file:
634567:Fred Flintstone:34 Flintrock Way, Bedrock, BC:(345) 295-9076:678453234:June 15, 2000 BC:Pediatrics:27.85:44
217546:Samuel Ludlow III:2345 The Rich Man Way, RichVille, RC:(567) 324-9812:768956453:February 29, 1942:Collections Section:20500 86595:Bill Partley:11 Partway Road, Almost, NW:(111) 232-9876:876345987:July 10, 1966:Parts Stuff:20.55:18
0999999:Dr. Evil:Some Where in the Caribean:(999) 999-9999:98989898:February 29, 1977:World Chaos Care:1000000
9536476: Perky Volunteer:343 Volly Road, Volunteersalot:(212) 345-9876:867643782:October 14, 1947:Volunteer Organization:16.80:22 54153:BobBob Never:22 NeverReally Lane, Neverland: (234) 674-7865:98765678:September 3, 1988:Never Media:30.60:48 775342:Winnie Van Winnipeg:100 Aker Wood West, Winnipeg:(999) 555-8888:234777444:January 4, 2000:Forestry:28.45:38 1234987:Sam Packitaway:345 Stash Road, Piles, Alot:(660) 546-9867:999888777:March 19, 1958:Capital Acquisition:25675 5489867:Elle Driver: 32 Mountain Drive, Los Angeles:(765) 456-7766:765098345:October 5, 2000:International Sales:40.25:52
Wages:
internal class Wages : Employee
{
private double rate;
private double hours;
public double Rate
{
get { return rate; }
}
public double Hours
{
get { return hours; }
}
public override double Pay
{
get
{
double pay;
double rate = this.Rate;
double hours = this.Hours;
if (hours > 40)
{
double overtimeHours = hours - 40;
double overtimePay = overtimeHours * (rate * 1.5);
pay = rate * 40;
pay += overtimePay;
}
else
{
pay = rate * hours;
}
return pay;
}
}
public Wages(string id, string name, double rate, double hours)
{
this.id = id;
this.name = name;
this.rate = rate;
this.hours = hours;
}
Salaried:
internal class Salaried : Employee
{
private double salary;
public double Salary
{
get
{
return salary;
}
}
public override double Pay
{
get
{
return salary;
}
}
public Salaried(string id, string name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
PartTime:
internal class PartTime : Employee
{
private double rate;
private double hours;
public double Rate
{
get { return rate; }
}
public double Hours
{
get { return hours; }
}
public override double Pay
{
get
{
double rate = this.Rate;
double hours = this.Hours;
double pay = rate * hours;
return pay;
}
}
public PartTime(string id, string name, double rate, double hours)
{
this.id = id;
this.name = name;
this.rate = rate;
this.hours = hours;
}
Employee:
internal class Employee
{
protected string id;
protected string name;
protected string address;
protected string phone;
protected long sin;
protected string birthdate;
protected string department;
public string Id
{
get { return id; }
}
public string Name
{
get => name;
}
public virtual double Pay
{
get
{
return 0;
}
}
public Employee()
{
}
Program:
internal class Program
{
static void Main(string[] args)
{
string path = "employees.txt";
string[] lines = File.ReadAllLines(path);
List employees = new List();
foreach (string line in lines)
{
string[] parts = line.Split(':');
string id = parts[0];
string name = parts[1];
string firstDigit;
firstDigit = id.Substring(0, 1);
int firstDigitNum = int.Parse(firstDigit);
if (firstDigitNum >= 0 && firstDigitNum <= 4)
{
double salary = double.Parse(parts[7]);
Salaried salaried;
salaried = new Salaried(id, name, salary);
employees.Add(salaried);
}
else if (firstDigitNum >= 5 && firstDigitNum <= 7)
{
double rate = double.Parse(parts[7]);
double hours = double.Parse(parts[8]);
Wages waged = new Wages(id, name, rate, hours);
employees.Add(waged);
}
else if (firstDigitNum >= 8 && firstDigitNum <= 9)
{
double rate = double.Parse(parts[7]);
double hours = double.Parse(parts[8]);
PartTime partTime = new PartTime(id, name, rate, hours);
employees.Add(partTime);
}
}
//Average weekly pay
double averageWeeklyPay = CalcAverageWeeklyPay(employees);
Console.WriteLine(string.Format("Average weekly pay: {0:C2}", averageWeeklyPay));
//Lowert Paid Employment
Employee highestPaidWagedEmployee = FindHighestPaid(employees);
double highestWagedPay = highestPaidWagedEmployee.Pay;
Console.WriteLine("Highest waged pay: " + highestWagedPay.ToString("C2"));
Console.WriteLine("Highest waged employee: " + highestPaidWagedEmployee.Name);
//Lowert Paid Employment
Employee lowestPaidWagedEmployee = FindLowestPaid(employees);
double lowestWagedPay = lowestPaidWagedEmployee.Pay;
Console.WriteLine("Lowtest waged pay: " + lowestWagedPay.ToString("C2"));
Console.WriteLine("Lowest waged employee: " + lowestPaidWagedEmployee.Name);
}
private static double CalcAverageWeeklyPay(List employees)
{
double weeklyPaySum = 0;
foreach (Employee employee in employees)
{
if (employee is PartTime partTime)
{
double pay = partTime.Pay;
weeklyPaySum += pay;
}
else if (employee is Wages waged)
{
double pay = waged.Pay;
weeklyPaySum += pay;
}
else if (employee is Salaried salaried)
{
double pay = salaried.Pay;
weeklyPaySum += pay;
}
}
double averageWeeklyPay = weeklyPaySum / employees.Count;
return averageWeeklyPay;
}
private static Wages FindHighestPaid(List employees)
{
double highestWagedPay = 0;
Wages highestWagedEmployee = null;
foreach (Employee employee in employees)
{
if (employee is Wages waged)
{
double pay = waged.Pay;
if (pay > highestWagedPay)
{
highestWagedPay = pay;
highestWagedEmployee = waged;
}
}
}
return highestWagedEmployee;
}
private static Wages FindLowestPaid(List employees)
{
double lowestWagedPay = double.MaxValue;
Wages lowestWagedEmployee = null;
foreach (Employee employee in employees)
{
if (employee is Wages waged)
{
double pay = waged.Pay;
if (pay > lowestWagedPay)
{
lowestWagedPay = pay;
lowestWagedEmployee = waged;
}
}
}
return lowestWagedEmployee;
}
}