i L A B O V E R V I E W Scenario and Summary The objective of the lab is to modi
ID: 3637457 • Letter: I
Question
i L A B O V E R V I E W
Scenario and Summary
The objective of the lab is to modify the Employee class to demonstrate composition where a containing class (Employee) contains another class (Benefit). An employee typically has benefits, so we will make the following changes:
Create a Benefit class.
Integrate the Benefit class into the Employee class.
Separate the files in the project into Presentation and Logic tier folders
Deliverables
Due this week:
Capture the console output window and paste it into a Word Document.
Zip the project folder.
Put the zip file and screen shots (Word document) in the Dropbox.
i L A B S T E P S
STEP 1: Understand the UML Diagram
Analyze and understand the object UML diagram, which models the structure of the program.
There are no design changes to the Presentation Tier from the previous project and InputUtilities and ApplicationsUtilities classes are used without modification (except for changing the Application Information).
A new class called Benefits that holds the health insurance company, life insurance amount, and vacation days. There are constant attributes defined for each of the default values and the minimum and maximum values. This composition relationhip is specified in the UML class diagram as a solid diamond connecting to the Emploee class.
The Benefits class shall contain properties that contain get and set methods for each of the attributes in the Benefits class, and each attribute value shall be properly validated in the properties.
The Employee class contains a new attribute called benefit that is of type Benefits. There will be a property in the Employee class that can set and get the benefit attribute.
Each constructor of Employee class will need to instansiate the benefit attribute.
STEP 2: Create the Project
You will want to use the Week 3 project as the starting point for the lab. To do this, you will want to create a new project by following these steps:
Create a new project named "CIS247_WK4_Lab_LASTNAME". An empty project will then be created.
Delete the default Program.cs file that is created.
Now that we are beginning to add more classes to our projects the Solution Explorer can become difficult to organize so you will create folders to hold the Presentation Tier and Logic Tier Files in order to organize the project. One thing to remember, even though we only have a few files in our project, a professional program will have 100's if not 1000's of files in a project so you will want to get practice in organizing your project files in a logical folder heirarchy and we will use the Tiered Architecture structure shown in the UML Class diagram for the folder structure. You will find that creating folders within MS Visual Studio is very similiar to creating folders in Windows Explorer. Follow these directions to create the folders:
Select the project and then right click
Select Add
Select New Folder
Enter the name of the folder
Add the following three folders to your project (1) Presentation Tier, (2) Logic Tier, and (3) Utilities.
You are going to add the files from the previous week lab to the project just as you did before, but now you add the existing files to the corresponding folder
Select the Logic Tier folder, right click and select Add then Existing Item, navigate to your previous week's project and select the InputUtitilies.cs and Program.cs files and click add. These two files will then be added to the Presentation. [Hint: you can also drag and drop the files directly from Windows Explorer directly into the corresponding folder in your project!]
Add the previous week's Employee.cs file to the Logic Tier folder.
Add the ApplicationUtilities.cs file to the Utilities folder.
Your solution explorer should look similiar to the following (note: you can rename any folder by selecting the folder, right click, and then Rename just like you do in Windows).
The namespaces for the classes should all be "Employee", but you should verify that the namespaces for all the classes are the same.
Update the program information in the ApplicationUtilities.DisplayApplicationInformation method to reflect your name, current lab, and program description.
Build and execute the project.
STEP 3: Create the Benefits Class
Using the Benefit class diagram as a guide, build the Benefit class.
Create a property for each of the listed private attributes and validate the provided value using the following rules:
If the insurance company provided is empty or null then set the healthInsuranceCompany to DEFAULT_HEALTH_INSURANCE
If the provided life insurance value is between the MIN_LIFE_INSURANCE and MAX_LIFE_INSURANCE (inclusive) then set lifeInsuranceAmount to the provided value; if the provided value is less than MIN_LIFE_INSURANCE set the lifeInsuranceAmount to MIN_LIFE_INSURANCE; else if provided value is greater than MAX_LIFE_INSURANCE; set thelifeInsuranceAmount to MAX_LIFE_INSURANCE.
If the provided vacation days value is between the MIN_VACATION and MAX_VACATION (inclusive) the set the vacationDays to the provided value; if the provided value is less than MIN_VACATION set the vacationDays to MIN_VACATION; else if provided value is greater than MAX_VACATION set the vacationDays value to MAX_VACATION.
In the parameterized constructor, set the attributes so that the properties are used, which ensures that attributes are validated prior to be set.
Create an overridden ToString method that collects and formats the attribute information for the benefit object. Ensure to display life insurance amount in currency format.
STEP 4: Modify the Employee Class
Using the Employee class diagram as a guide, modify the Employee class
Add a private attribute called "benefit" to the employee class of type Benefits
Create a public Benefit property that returns the benefit attribute. In the set method of the property, if the provided value is null then re-instantiate the benefit variable; otherwise, set the provided value to the benefit variable. [Hint: to check if a object is null use the syntax "if (object != null)"]
In the default constructor, instantiate the benefit variable using the Benefits default constructor
In the parameterized constructor, add a benefit argument of type Benefits, and then set the value of this parameter to the Benefit property (using the property will ensure that any null benefit object is correctly instansiated.)
Modify the ToString method to the Employee class, by adding a call to the Benefits ToString methods at the end of the Employee ToString method.
STEP 5: Modify the Main Method
In the previous labs you learned how to access an object/class methods and properties using the DOT notation. For example, to access the calculatePay method of an employee object you used a statement similiar to:
employee1.CalculateWeeklyPay(modifiedSalary)
Notice that the Employee class now has a public Benefit object inside it. This means that you can access the set methods of the Benefit object using the transitive notation:
containingObject.containedObject.methodName()
or
containingObject.containedObject.PropertyName
That is to access the members of contained object, you start at the containing object, then "transit" to the contained object, then to the contained objects members.
As an example, to set the life insurance amount of an employee object, the statement would look something like:
employee1.Benefit.LifeInsuranceAmount = 100000;
Notice, the containing object is "employee1", the contained object is "Benefit", and the property of Benefit we are accessing is LifeInsuranceAmount.
The code in the previous week's project performed the following operations
Display the program information.
Create an Employee object using the default constructor.
Prompt for and then set the first name, last name, gender, dependents, and annual salary. Remember to use the appropriate methods in the InputUtilties class to prompt for and retreive the values.
Display the employee information.
After the first employee information is provided, display the number of employees created.
Prompt the user to provide an updated annual salary for employee1, retrieve the value and invoke the overloaded CalculateWeeklyPay, and then display only the updated weekly pay.
Create a second Employee object using the multi-argument constructor using data of your choosing that is of the correct type for each input.
Display the Employee information for the second employee object.
Create a third Employee object using the parameterized constructor setting each of the attributes with the following values: "Sue", "Smith", 'F', 15, 500000.0
Display the employee information for the third Employee object and verify that the dependents and annual salary values have been set to the maximum values by the properties. If not, make sure you change the parameterized constructor to use the properties to set the attributes.
Display the number of employees created.
Terminate the application
Once your code is working and implements the previous week's operations, modify the code to implement the following new requirements (updated code should implement all previous requirements except as noted below).
After you collect the information for the first employee object, prompt for and collect the Health Insurance Company, the LifeInsuranceAmount, and the number of vacation days.
Display the updated employee 1 information
Display the number of employees created.
Create a new, standalone benefits object using the multi-argument constructor using data of your choosing that is of the correct type for each input.
Modify the second employee object instantiation and add the newly created benefit object to the constructor call.
Display the updated employee 2 information
Display the number of employees created.
Create a new, standalone benefits object using the multi-argument constructor using the following invalid data "" (empty string), 10000000, -10
Modify the third employee object instantiation and add the newly created benefit object to the constructor call.
Display the updated employee 3 information and verify that the default values for the benefit object have been correctly set.
Display the number of employees created.
STEP 5: Compile and Test
When done, compile and execute your code. Debug errors until your code is error-free. Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild. The following shows some sample output, but your output may look different.
Image Description
STEP 6: Submit Deliverables
Capture the output window and paste it into a Word Document.
Put the zip file and screen shots (Word document) in the Dropbox.
Answers (1)
Answered by Anonymous 52 minutes ago
// Newly created Interface
// IEmployee.cs
//IEmployee interface
interface IEmployee
{
//calculate the pay of employee
double CalculatePay();
}
// Newly Created Class
// Benefit.cs
class Benefit
{
// data fields
private string healthInsurance;
private double lifeInsurance;
private int vacation;
//default constructor
public Benefit()
{
}
//argumented constructor
public Benefit(string health, double life, int v)
{
healthInsurance = health;
lifeInsurance = life;
vacation = v;
}
//displays the data of the object
public void DisplayBenefits()
{
Console.WriteLine("Health Insurance: {0}", healthInsurance);
Console.WriteLine("Life Insurance: {0}", lifeInsurance);
Console.WriteLine("Vacation: {0}", vacation);
}
//sets health insurance
public void SetHealthInsurance(string health)
{
healthInsurance = health;
}
//returns health insurance
public string GetHealthInsurance()
{
return healthInsurance;
}//sets life insurance
public void SetLifeInsurance(double life)
{
lifeInsurance = life;
}//returns life insurance
public double GetLifeInsurance()
{
return lifeInsurance;
}//sets vacation
public void SetVacation(int v)
{
vacation = v;
}//returns vacation
public int GetVacation()
{
return vacation;
}
}// end of Benefit class
// Employee.cs
class Employee : IEmployee
{
//Private data members
private string firstName;
private string lastName;
private char gender;
private int dependents;
private double annualSalary;
//number of employees
private static int numEmployees = 0;
//benift object
public 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 double CalculatePay()
{
return annualSalary / 52;
}//End of CalculatePay method
//Displays all the data of the employee
public void DisplayEmployee()
{
Console.WriteLine("***************** Employee Information *****************");
Console.WriteLine("First Name: {0}", firstName);
Console.WriteLine("Last Name: {0}", lastName);
Console.WriteLine("Gender: {0}", gender);
Console.WriteLine("Dependents: {0}", dependents);
Console.WriteLine("Annual Salary: {0:c}", annualSalary);
Console.WriteLine("Weekly Pay: {0:c}", CalculatePay());
benefit.DisplayBenefits();
}//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 dependets
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
// Program.cs (main class)
class Program
{
static void Main(string[] args)
{
//Instantiating employee object using default constructor
Employee emp = new Employee();
//Gets the employee information from the console
Console.Write("Enter first name: ");
string first = Console.ReadLine();
Console.Write("Enter last name: ");
string last = Console.ReadLine();
Console.Write("Gender: ");
char gen = Console.ReadLine().First();
Console.Write("Dependents: ");
string dep = Console.ReadLine();
Console.Write("Annual Salary: $");
string salary = Console.ReadLine();
Console.Write("Enter health insurance (Full or Partial): ");
string health = Console.ReadLine();
Console.Write("Enter life insurance :$ ");
double life = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter vacation: ");
int vacation = Convert.ToInt32(Console.ReadLine());
//Sets the read information to the emp object
emp.FirstName = first;
emp.LastName = last;
emp.Gender = gen;
//setting dependets using overloaded method SetDependents
//that accepts string type argument
emp.SetDependents(dep);
//setting salary using overloaded method SetAnnualSalary
//that accepts string type argument
emp.SetAnnualSalary(salary);
emp.benefit.SetHealthInsurance(health);
emp.benefit.SetLifeInsurance(life);
emp.benefit.SetVacation(vacation);
//Displaying object data to the console.
emp.DisplayEmployee();
Console.WriteLine("Total employees : {0} ", Employee.GetNumberOfEmployees());
Benefit benefit1 = new Benefit("Full", 1000, 5);
//Creates another employee object using argumented construtor
Employee emp2 = new Employee("Mary", "Noia", 'F', 5, 24000.0, benefit1);
//Displaying object data to the console.
emp2.DisplayEmployee();
Console.WriteLine("Total employees : {0} ", Employee.GetNumberOfEmployees());
}
}
** In your response there is nothing listed in the employee constructor. Is this common practice? **
Explanation / Answer
// Newly created Interface // IEmployee.cs //IEmployee interface interface IEmployee { //calculate the pay of employee double CalculatePay(); } // Newly Created Class // Benefit.cs class Benefit { // data fields private string healthInsurance; private double lifeInsurance; private int vacation; //default constructor public Benefit() { } //argumented constructor public Benefit(string health, double life, int v) { healthInsurance = health; lifeInsurance = life; vacation = v; } //displays the data of the object public void DisplayBenefits() { Console.WriteLine("Health Insurance: {0}", healthInsurance); Console.WriteLine("Life Insurance: {0}", lifeInsurance); Console.WriteLine("Vacation: {0}", vacation); } //sets health insurance public void SetHealthInsurance(string health) { healthInsurance = health; } //returns health insurance public string GetHealthInsurance() { return healthInsurance; }//sets life insurance public void SetLifeInsurance(double life) { lifeInsurance = life; }//returns life insurance public double GetLifeInsurance() { return lifeInsurance; }//sets vacation public void SetVacation(int v) { vacation = v; }//returns vacation public int GetVacation() { return vacation; } }// end of Benefit class // Employee.cs class Employee : IEmployee { //Private data members private string firstName; private string lastName; private char gender; private int dependents; private double annualSalary; //number of employees private static int numEmployees = 0; //benift object public 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 double CalculatePay() { return annualSalary / 52; }//End of CalculatePay method //Displays all the data of the employee public void DisplayEmployee() { Console.WriteLine("***************** Employee Information *****************"); Console.WriteLine("First Name: {0}", firstName); Console.WriteLine("Last Name: {0}", lastName); Console.WriteLine("Gender: {0}", gender); Console.WriteLine("Dependents: {0}", dependents); Console.WriteLine("Annual Salary: {0:c}", annualSalary); Console.WriteLine("Weekly Pay: {0:c}", CalculatePay()); benefit.DisplayBenefits(); }//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 dependets 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 // Program.cs (main class) class Program { static void Main(string[] args) { //Instantiating employee object using default constructor Employee emp = new Employee(); //Gets the employee information from the console Console.Write("Enter first name: "); string first = Console.ReadLine(); Console.Write("Enter last name: "); string last = Console.ReadLine(); Console.Write("Gender: "); char gen = Console.ReadLine().First(); Console.Write("Dependents: "); string dep = Console.ReadLine(); Console.Write("Annual Salary: $"); string salary = Console.ReadLine(); Console.Write("Enter health insurance (Full or Partial): "); string health = Console.ReadLine(); Console.Write("Enter life insurance :$ "); double life = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter vacation: "); int vacation = Convert.ToInt32(Console.ReadLine()); //Sets the read information to the emp object emp.FirstName = first; emp.LastName = last; emp.Gender = gen; //setting dependets using overloaded method SetDependents //that accepts string type argument emp.SetDependents(dep); //setting salary using overloaded method SetAnnualSalary //that accepts string type argument emp.SetAnnualSalary(salary); emp.benefit.SetHealthInsurance(health); emp.benefit.SetLifeInsurance(life); emp.benefit.SetVacation(vacation); //Displaying object data to the console. emp.DisplayEmployee(); Console.WriteLine("Total employees : {0} ", Employee.GetNumberOfEmployees()); Benefit benefit1 = new Benefit("Full", 1000, 5); //Creates another employee object using argumented construtor Employee emp2 = new Employee("Mary", "Noia", 'F', 5, 24000.0, benefit1); //Displaying object data to the console. emp2.DisplayEmployee(); Console.WriteLine("Total employees : {0} ", Employee.GetNumberOfEmployees()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.