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.
Program.cs class Program { static void Main(string[] args).pdf
1. 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
2. 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(),
3. 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
4. {
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
5. {
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
6. 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
7. 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.
8. //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)
{
9. 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()
{
10. 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}",
11. 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