Composition and Inheritance This is the culmination of everything you have learned up to this point. You will complete your Employee pay form by add
Composition and Inheritance
This is the culmination of everything you have learned up to this point. You will complete your Employee pay form by adding Benefits, Salary, and Hourly classes to the project. Then you will add these capabilities to your user interface. Read the Project Guide provided and the project video link for instructions and details to help guide you through your efforts. The final project is due next week (Module 8).
Course Project DeVry University College of Engineering and Information Sciences
Screenshot of program running:
Form code (only the code for the form, not program.cs):
,
Course Project DeVry University College of Engineering and Information Sciences
Course Number: CEIS209
Background
Part 2: Scenario
You have been hired as a consultant to create a basic Payroll System for a small business. The business wants to keep track of basic employee information and print paychecks for the employees every two weeks. Your client has some basic functionality in mind, such as the ability to add and remove employees, display basic employee information, and print paychecks. Maintenance is key! You realize that Object-Oriented programming will provide the most efficient application that is easy to maintain and update as needed. Let’s get started!
Week 7: Final Project – Composition and Inheritance
Objectives
1. Integrate Employee class
2. Data hiding
Introduction
So far, your application can keep track of basic employee information. You can add and remove employees. The basic information is displayed efficiently by your listbox. However, the company offers a benefit to their employees. The company wants the application to track these benefits since the benefits are considered part of the basic employee information. Since we are using object-oriented programming techniques, it will be easy to “attach” the benefits to the employees. The employee object will be contain the benefits object. In OOP speak, the Employee object is composed of a Benefits object.
Also, we cannot write paychecks because we do not know how to pay the employees! Since we only have one class, do the pay the employees a base salary or do we pay them an hourly rate or do pay them a consulting fee? It is impossible to pay the variety of employees using one single class. Therefore it is going to be a lot of work to write the Salary class and the Hourly class and any other employee classes that we need since we have to rewrite every single attribute and method. Right? NO! We can use Inheritance! With Inheritance, we create a base class and then extend this base class into subclasses. In other words, we create a Parent class and then create Child classes that inherit the attributes and behaviors from the Parent class. This way, we only have to write the attributes and behaviors that are unique to that Child class!
Since we are using object-oriented programming techniques, it will be easy to “extend” the base class into many different subclasses. We will only need to code the differences in the Child classes. Once we set up the Inheritance relationship, we can set up our ListBox to automatically show the Child properties by overriding the ToString( ) method. Inheritance is going to save us a massive amount of work!
Steps
1.
2. Since we changed the structure of our Employee class, we need to update project to accommodate the updated Employee objects.
a. Change your Input form to get the Benefits information as well
b. Update the AddButton_Click event method to get the additional information and create Employee objects using Composition. private void AddButton_Click(object sender, EventArgs e) { // add item to the employee listbox InputForm frmInput = new InputForm(); using (frmInput) { DialogResult result = frmInput.ShowDialog(); // see if input form was cancelled if (result == DialogResult.Cancel) return; // end method since user cancelled the input // get user's input and create an Employee object string fName = frmInput.FirstNameTextBox.Text; string lName = frmInput.LastNameTextBox.Text; string ssn = frmInput.SSNTextBox.Text; string date = frmInput.HireDateTextBox.Text; DateTime hireDate = DateTime.Parse(date); string healthIns = frmInput.HealthInsTextBox.Text; double lifeIns = Double.Parse( frmInput.LifeInsTextBox.Text ); int vacation = Int32.Parse( frmInput.VacationTextBox.Text ); Benefits benefits = new Benefits(healthIns, lifeIns, vacation); Employee emp = new Employee(fName, lName, ssn, hireDate, benefits); // add the Employee object to the employees listbox EmployeesListBox.Items.Add(emp); // write all Employee objects to the file WriteEmpsToFile(); } }
c. Update the WriteEmpsToFile( ) method to write all of the Employee information, including the Benefits information: private void WriteEmpsToFile() { // write all Employee objects to the file string fileName = "Employees.csv"; StreamWriter sw = new StreamWriter(fileName); for (int i = 0; i < EmployeesListBox.Items.Count; i++) { Employee temp = (Employee)EmployeesListBox.Items[i]; sw.WriteLine(temp.FirstName + ',' + temp.LastName + ',' + temp.SSN + ',' + temp.HireDate.ToShortDateString() + ',' + temp.BenefitsPackage.HealthInsurance + ',' + temp.BenefitsPackage.LifeInsurance + ',' + temp.BenefitsPackage.Vacation); } sw.Close(); // tell user that the records were written to the file MessageBox.Show("Employees were written to the file."); }
d. Update the ReadEmpsFromFile( ) method to read all of the Employee information, including the Benefits information: private void ReadEmpsFromFile() { // read all Employee objects from the file string fileName = "Employees.csv"; StreamReader sr = new StreamReader(fileName); using (sr) { while (sr.Peek() > -1) { // read the line and break it into parts based on commas string line = sr.ReadLine(); string[] parts = line.Split(','); string fName = parts[0]; string lName = parts[1]; string ssn = parts[2]; DateTime hireDate = DateTime.Parse(parts[3]); string healthIns = parts[4]; double lifeIns = Double.Parse(parts[5]); int vacation = Int32.Parse(parts[6]); // create the Employee object and add it to the listbox Benefits benefits = new Benefits(healthIns, lifeIns, vacation); Employee emp = new Employee(fName, lName, ssn, hireDate, benefits); EmployeesListBox.Items.Add(emp); } } }
3. How can we see the details for our Employee objects? We can set up the double-click event on the listbox!
a. Add the double-click event to the listbox
b. In the double-click event, get the selected Employee object
c. Display the Employee object’s information in the InputForm, which we will repurpose as an Update form
d. If the user clicks the Cancel button, end the method
e. If the user clicks the Submit/Update button:
i. Delete the selected Employee object
ii. Create a new Employee object using the updated information
iii. Add the new Employee object to the listbox
4. Next we will create a Salary class that inherits from the Employee class. This way, we automatically get all of the Employee attributes and behaviors. Add an annualSalary attribute to the Salary class. Set up the constructors to get all of the information, both the parent information and the child information. Pass the parent information to the parent’s constructor.
5.
Create an Hourly class that inherits from the Employee class. Add an hourlyRate attribute and an hoursWorked attribute. Set up the constructors to get all of the information, the parent information and the child information. Remember to pass the parent information to the parent’s constructor! Here is the UML class diagram showing the Inheritance relationship between the Employee parent class and the Salary and Hourly child classes:
1. Update your Input form to take Hourly employee information and Salary employee information. Use RadioButtons so the user can choose the Employee type. Then, get the appropriate information based on the employee type. Update your methods that read and write to the file to accommodate the child classes. We can use another field (“Type”) and then convert the line to the appropriate class type. On the other hand, if we convert our listbox items to a standard list, we can write the entire list to the file with one line of code! Technically, we can write the listbox items directly to the file, but that would cause a maintenance issue in the future. By using a standard list, we can change our form in the future with no issues at all! Convert the listbox items to a list that can work with Employee objects. We can use a foreach loop to quickly make this conversion. The foreach loop will get each object, one at a time, and apply the code to that object. It will apply your code to each object in the array or collection. In addition, the foreach loop automatically converts the objects to the data type that you give it! Update your WriteEmpsToFile( ) method: List<Employee> empList = new List<Employee>(); foreach (Employee emp in EmployeesListBox.Items) { empList.Add(emp); } Now, let’s use the BinaryFormatter to write the entire list to the file in one line! Create your pipe to the file. Then, create your BinaryFormatter as our “translator” that can translate you object into binary. Then, write the entire object to the file. Remember to close your pipe! // convert the listbox items to a generic list List<Employee> empList = new List<Employee>(); foreach (Employee emp in EmployeesListBox.Items) { empList.Add(emp); } // open a pipe to the file and create a translator FileStream fs = new FileStream(FILENAME, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); // write the generic list to the file formatter.Serialize(fs, empList); // close the pipe fs.Close(); We also need to update your ReadEmpsFromFile( ) method. Check to see if the file exists AND ALSO check to make sure the file has a length greater than zero. If it does, create a file stream from the file. Also, create your BinaryFormatter (“translator”). Read the list into a list reference. Finally, copy the Employee objects into your listbox items. // check to see if file exists if( File.Exists(FILENAME) && new FileInfo(FILENAME).Length > 0 ) { // create a pipe from the file and create the "translator" FileStream fs = new FileStream(FILENAME, FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); // read all Employee objects from the file List<Employee> list = (List<Employee>)formatter.Deserialize(fs); // close the pipe fs.Close(); // copy the Employee objects into our listbox foreach (Employee emp in list) EmployeesListBox.Items.Add(emp); }
On Your Own
· Add code for the print paychecks button. It should display in a MessageBox the employee information (hint: use ToString()) and the pay (use CalculatePay()). The result should look like this:
Final Project Deliverable
· Take a screenshot while your application is running. YOU MUST ENTER YOUR NAME AS ONE OF THE EMPLOYEES. Paste the screenshot to a Word document below your information. Finally, copy-paste your Form code – both main and input form to the Word document below the screenshot. Copy and paste the code for Employee.cs, Benefits.cs, Hourly.cs, Salary.cs. Save and close your Word document. Submit the Word document for your first Course Project deliverable.
Employee <<Stereotype>> parameter – firstName : string + ToString( ) : string – lastName : string – ssn : string – hireDate : DateTime + CalculatePay( ) : double Benefits <<Stereotype>> parameter – healthInsurance : string + ToString( ) : string – lifeInsurance : double – vacation : int – benefits : Benefits M1 M2 M3 M4
Employee <<Stereotype>> parameter # firstName : string + ToString( ) : string # lastName : string # ssn : string # hireDate : DateTime + CalculatePay( ) : double Benefits <<Stereotype>> parameter – healthInsurance : string + ToString( ) : string – lifeInsurance : double – vacation : int # benefits : Benefits M1 M2 M3 M4 Salary <<Stereotype>> parameter – annualSalary : double + ToString( ) : string M1 M2 M3 M4 Hourly <<Stereotype>> parameter – hourlyRate : float + ToString( ) : string – hoursWorked : float M1 M2 M3 M4 + CalculatePay( ) : double + CalculatePay( ) : double
1. Update your Input form to take Hourly employee information and Salary employee information. Use RadioButtons so the user can choose the Employee type. Then, get the appropriate information based on the employee type. Update your methods that read and write to the file to accommodate the child classes. We can use another field (“Type”) and then convert the line to the appropriate class type. On the other hand, if we convert our listbox items to a standard list, we can write the entire list to the file with one line of code! Technically, we can write the listbox items directly to the file, but that would cause a maintenance issue in the future. By using a standard list, we can change our form in the future with no issues at all! Convert the listbox items to a list that can work with Employee objects. We can use a foreach loop to quickly make this conversion. The foreach loop will get each object, one at a time, and apply the code to that object. It will apply your code to each object in the array or collection. In addition, the foreach loop automatically converts the objects to the data type that you give it! Update your WriteEmpsToFile( ) method: List<Employee> empList = new List<Employee>(); foreach (Employee emp in EmployeesListBox.Items) { empList.Add(emp); } Now, let’s use the BinaryFormatter to write the entire list to the file in one line! Create your pipe to the file. Then, create your BinaryFormatter as our “translator” that can translate you object into binary. Then, write the entire object to the file. Remember to close your pipe! // convert the listbox items to a generic list List<Employee> empList = new List<Employee>(); foreach (Employee emp in EmployeesListBox.Items) { empList.Add(emp); } // open a pipe to the file and create a translator FileStream fs = new FileStream(FILENAME, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); // write the generic list to the file formatter.Serialize(fs, empList); // close the pipe fs.Close(); We also need to update your ReadEmpsFromFile( ) method. Check to see if the file exists AND ALSO check to make sure the file has a length greater than zero. If it does, create a file stream from the file. Also, create your BinaryFormatter (“translator”). Read the list into a list reference. Finally, copy the Employee objects into your listbox items. // check to see if file exists if( File.Exists(FILENAME) && new FileInfo(FILENAME).Length > 0 ) { // create a pipe from the file and create the "translator" FileStream fs = new FileStream(FILENAME, FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); // read all Employee objects from the file List<Employee> list = (List<Employee>)formatter.Deserialize(fs); // close the pipe fs.Close(); // copy the Employee objects into our listbox foreach (Employee emp in list) EmployeesListBox.Items.Add(emp); }
On Your Own
· Add code for the print paychecks button. It should display in a MessageBox the employee information (hint: use ToString()) and the pay (use CalculatePay()). The result should look like this:
Final Project Deliverable
· Take a screenshot while your application is running. YOU MUST ENTER YOUR NAME AS ONE OF THE EMPLOYEES. Paste the screenshot to a Word document below your information. Finally, copy-paste your Form code – both main and input form to the Word document below the screenshot. Copy and paste the code for Employee.cs, Benefits.cs, Hourly.cs, Salary.cs. Save and close your Word document. Submit the Word document for your first Course Project deliverable.
Employee <<Stereotype>> parameter – firstName : string + ToString( ) : string – lastName : string – ssn : string – hireDate : DateTime + CalculatePay( ) : double Benefits <<Stereotype>> parameter – healthInsurance : string + ToString( ) : string – lifeInsurance : double – vacation : int – benefits : Benefits M1 M2 M3 M4
Employee <<Stereotype>> parameter # firstName : string + ToString( ) : string # lastName : string # ssn : string # hireDate : DateTime + CalculatePay( ) : double Benefits <<Stereotype>> parameter – healthInsurance : string + ToString( ) : string – lifeInsurance : double – vacation : int # benefits : Benefits M1 M2 M3 M4 Salary <<Stereotype>> parameter – annualSalary : double + ToString( ) : string M1 M2 M3 M4 Hourly <<Stereotype>> parameter – hourlyRate : float + ToString( ) : string – hoursWorked : float M1 M2 M3 M4 + CalculatePay( ) : double + CalculatePay( ) : double
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.