Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program.cs: class Program { static void Main(string[] args) { Console.WriteLine(

ID: 3634446 • Letter: P

Question

Program.cs:
class Program

{
static void Main(string[] args)
{
Console.WriteLine("Welcome the Employee Hierarchy Program");
Console.WriteLine("CIS247, Week 5 Lab");
Console.WriteLine("Name: Solution");
Console.WriteLine(" This program tests an Employee inheritance hierarchy ");
//Array to hold Employee type objects
Employee[] emp = new Employee[3];
//Initializing 3 values of array with 3 three types of classes
//Employee class object
emp[0] = new Employee("Joe", "Doe", 'M', 1, 10000.0, new Benefit("Partial", 1000, 2));
//Salaried Employee
emp[1] = new Salaried("Zoe", "Likoudis", 'F', 3, 20000.0, new Benefit("Full", 2000, 4), 1);
//Hourly Employee
emp[2] = new Hourly("Kate", "Perry", 'F', 0, 75, 25, new Benefit("Partial", 3000, 8), "part time");
//Displaying their data to the console
for (int i = 0; i < emp.Length; i++)
{
Console.WriteLine(" ***************** Display Employee's Data ***************** ");
Console.WriteLine(emp[i].ToString());
}
//Displaying number of emps
Console.WriteLine(" Total number of employees in Database: {0} ", Employee.GetNumberOfEmployees());
} //End of Main method
} //End of Program class
}//End of namespace

Employee.cs:
class Employee
{
//Private data members
protected string firstName;
protected string lastName;
protected char gender;
protected int dependents;
protected double annualSalary;
//Number of employees
private static int numEmployees = 0;
//Benefit object
protected Benefit benefit;
//Default Constructor
public Employee()
{
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
//updates number of employees
numEmployees++;
benefit = new Benefit("not given", 0, 0);
}//End of Default Constructor
//Argumented Constructor
public Employee(string first, string last, char gen, int dep, double salary, Benefit benefit)
{
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
//Updates number of employees
numEmployees++;
this.benefit = new Benefit(benefit.GetHealthInsurance(), benefit.GetLifeInsurance(), benefit.GetVacation());
}//End of Argumented Constructor
//Calculates weekly pay
public virtual double CalculatePay()
{
return annualSalary / 52;
}//End of CalculatePay method
//Displays all the data of the employee
public override string ToString()
{
return string.Format("Employee Type : {0} First Name : {1} Last Name : {2} Gender : {3} Dependents : {4} Annual Salary : {5:c} Weekly Pay : {6:c} {7}",
"GENERIC", FirstName, LastName, (Gender == 'M') ? "Male" : "Female", Dependents, AnnualSalary, CalculatePay(), benefit.ToString());
}//End of DisplayEmployee method
//Public properties
//Property for first name of the employee
public string FirstName
{
set
{
firstName = value;
}
get
{
return firstName;
}
}//End of FirstName method
//Property for last name of the employee
public string LastName
{
set
{
lastName = value;
}
get
{
return lastName;
}
}//End of LastName method
//Gender of the employee
public char Gender
{
set
{
gender = value;
}
get
{
return gender;
}
}//End of Gender method
//The dependents
public int Dependents
{
set
{
dependents = value;
}
get
{
return dependents;
}
}//End of the Dependents method
//Annual salary
public double AnnualSalary
{
set
{
annualSalary = value;
}
get
{
return annualSalary;
}
}//End of AnnualSalary method
//Additional setter method for dependents
public void SetDependents(int dep)
{
dependents = dep;
}//End of SetDependents method
//Additional setter method for annual salary
public void SetAnnualSalary(double salary)
{
annualSalary = salary;
} //End of SetAnnualSalary method
//Returns number of employees created
public static int GetNumberOfEmployees()
{
return numEmployees;
}//End of GetNumberOfEmployees method
//Overloaded method of setDependents with string parameter
public void SetDependents(string dep)
{
dependents = Convert.ToInt32(dep);
}//End of SetDependents method
//Overloaded method of setAnnualSalary with string parameter
public void SetAnnualSalary(string salary)
{
annualSalary = Convert.ToDouble(salary);
}//End of SetAnnualSalary method
} //End of Employee class
}//End of namespace

Benefit.cs:
class Benefit
{
//Data fields
private string healthInsurance;
private double lifeInsurance;
private int vacation;
//Default constructor
public Benefit()
{
}//End of Default constructor
//Argumented constructor
public Benefit(string health, double life, int v)
{
healthInsurance = health;
lifeInsurance = life;
vacation = v;
}//End of Argumented constructor
//Displays the data of the object
public override string ToString()
{
return string.Format("Health Insurance: {0} Life Insurance : {1:c} Vacation : {2}", healthInsurance, lifeInsurance, vacation);
}//End of ToString method
//Sets health insurance
public void SetHealthInsurance(string health)
{
healthInsurance = health;
}//End of SetHealthInsurance method
//Returns health insurance
public string GetHealthInsurance()
{
return healthInsurance;
}//End of GetHealthInsurance method
//sets life insurance
public void SetLifeInsurance(double life)
{
lifeInsurance = life;
}//End of SetLifeInsurance method
//Returns life insurance
public double GetLifeInsurance()
{
return lifeInsurance;
}//End of GetLifeInsurance method
//Sets vacation
public void SetVacation(int v)
{
vacation = v;
}//End of SetVacation method
//Returns vacation
public int GetVacation()
{
return vacation;
}//End of GetVacation method
}// End of Benefit class
}// End of namespace

Hourly.cs:
class Hourly : Employee
{
//Constant values for minimum, maximum wage and hours
private const double MIN_WAGE = 10;
private const double MAX_WAGE = 75;
private const double MIN_HOURS = 0;
private const double MAX_HOURS = 50;
private double wage; // wage of employee
private double hours; // hours worked
private string category; // category
//Noargument constructor
public Hourly()
: base()
{
wage = MIN_WAGE;
hours = MIN_HOURS;
category = "Not Given";
}//End of Noargument constructor.
//3-argument constructor
public Hourly(double wage, double hours, string category)
: base()
{
setWage(wage);
setHours(hours);
setCategory(category);
}//End of 3-Argument constructor
//8-argument cinstructor
public Hourly(string first, string last, char gen, int dep, double wage, double hours, Benefit ben, string category)
: base(first, last, gen, dep, wage * 1300, ben)
{
setWage(wage);
setHours(hours);
setCategory(category);
}// End of 8-argument constructor
//Helper functions to set wage, hours and category
public void setWage(double wage)
{
if (wage >= MIN_WAGE && wage <= MAX_WAGE)
this.wage = wage;
else
this.wage = MIN_WAGE;
}//End of setWage
//Sets hours
public void setHours(double hours)
{
if (hours >= MIN_HOURS && hours <= MAX_HOURS)
this.hours = hours;
else
this.hours = MIN_HOURS;
}//End of setHours
//Sets category
public void setCategory(string category)
{
if (category == "temporary" || category == "part time" || category == "full time")
this.category = category;
else
this.category = "temporary";
}//End of setCategory
//Overrides the base class method calculatePay
public override double CalculatePay()
{
return wage * hours;
}//End of CalculatePay method
//Overrides the base class ToString to display data of the object
public override string ToString()
{
return string.Format("Employee Type : {0} First Name : {1} Last Name : {2} Gender : {3} Dependents : {4} Category : {5} Wage : {6:c} Hours : {7} Weekly Pay : {8:c} Annual Salary : {9:c} {10}",
GetType().Name.ToUpper(), FirstName, LastName, (Gender == 'M') ? "Male" : "Female", Dependents, category, wage, hours, CalculatePay(), AnnualSalary, benefit.ToString());
}//End of ToString method
}//End of Hourly class
}//End of namespace

Salaried.cs:
class Salaried : Employee
{
//Constant values for minmum and maximum management levels
private const int MIN_MANAGEMENT_LEVEL = 0;
private const int MAX_MANAGEMENT_LEVEL = 3;
//Bonus percentage
private const double BONUS_PERCENT = 10.0;
//Employee management level
private int managementLevel;
//No argument constructor
public Salaried()
: base()
{
managementLevel = MIN_MANAGEMENT_LEVEL;
}
//7-ArgumentException constructor
public Salaried(string first, string last, char gen, int dep, double salary, Benefit benefit, int manLevel)
: base(first, last, gen, dep, salary, benefit)
{
setManagementLevel(manLevel);
}
//2-Argument constructor
public Salaried(double salary, int manLevel)
: base()
{
AnnualSalary = salary;
setManagementLevel(manLevel);
}
//Helper method to set management level
public void setManagementLevel(int manLevel)
{
if (manLevel >= MIN_MANAGEMENT_LEVEL && manLevel <= MAX_MANAGEMENT_LEVEL)
managementLevel = manLevel;
else
managementLevel = MIN_MANAGEMENT_LEVEL;
}//End of setManagementLevel method
//Overrides the base class method calculatePay
public override double CalculatePay()
{
double weekPay = AnnualSalary / 52;
return weekPay + weekPay * (managementLevel * BONUS_PERCENT / 100);
}//End of CalculatePay method
//Overrides the base class ToString to display data of the object
public override string ToString()
{
return string.Format("Employee Type : {0} First Name : {1} Last Name : {2} Gender : {3} Dependents : {4} Management Lvl. : {5} Annual Salary : {6:c} Weekly Pay : {7:c} {8}",
GetType().Name.ToUpper(), FirstName, LastName, (Gender == 'M') ? "Male" : "Female", Dependents, managementLevel, AnnualSalary, CalculatePay(), benefit.ToString());
}//End of ToString method
}//End of Salaried class
}//End of namespace

STEP 3: Modify the Employee Class:
Make the Employee class abstract.
Define CalculatePay() as an abstract method.

Scenario and Summary: 1.We are going to create an abstract Employee class and an abstract CalculatePay() method. The abstract Employee class will prevent a programmer from creating an object based on Employee. Only objects based on Salaried and Hourly will be allowed. The abstract CalculatePay() method in Employee will force the child classes to implement CalculatePay(). 2.We are going to implement Polymorphism and dynamic binding by creating generalized methods that accept generalized Employee objects to collect input, and display information. In the Main method we will pass derived objects of the Employee class into these generalized methods. STEP 1: Understand the UML Diagram: Notice in the updated UML diagram, that the Employee class is designated as abstract by having the class name Employee italicized. Also, the CalculatePay method is also italicized, which means that it is a pure virtual function and needs to be implemented in the derived classes.

Explanation / Answer

Dear,

// Program.cs (main program)

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" This program tests an Employee inheritance hierarchy ");           
            // arrsy to hold Employee type objects
            Employee []employeeList = new Employee[2];           
            // Salaried Employee
            employeeList[0] = new Salaried();
            // Hourly Employee
            employeeList[1] = new Hourly();

            // Collecting employees information
            Utilities.CollectEmployeeInformation(employeeList[0]);
            Utilities.CollectEmployeeInformation(employeeList[1]);

            // Displaying employees information
            Utilities.DisplayEmployeeInformation(employeeList[0]);
            Utilities.DisplayEmployeeInformation(employeeList[1]);
          
            //displaying number of emps
            Console.WriteLine(" Total number of Employees in Database : {0} ", Employee.GetNumberOfEmployees());
            Console.WriteLine(" Thank you for using the Abstract Employee Inheritance Hierarchy ");
        }
    }

I hope this will helps you...