1 / 17

Case Study - Employee

Case Study - Employee. In this case study, we will design a class hierarchy for the employees of a firm using inheritance. In this design, we will see how to create classes and how to set up inheritance relations among these classes. We will use abstract classes.

pisces
Download Presentation

Case Study - Employee

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Case Study - Employee • In this case study, we will design a class hierarchy for the employees of a firm using inheritance. • In this design, we will see how to create classes and how to set up inheritance relations among these classes. • We will use abstract classes. • We will use polymorphic references, and polymorphic method invocation. COP 3330 Object Oriented Programming

  2. Case Study – Problem Definition • A firm has employees. • An employee can be a volunteer or a paid employee. • Each employee has a name and an address. • We should be able to get a string representation of an employee. • Each employee should be associated with an earning method to evaluate his/her monthly earnings (whether 0 or not, this method must be available). • A volunteer is not paid (his/her earnings is zero). • A paid employee has to have a social security number. • A paid employee can be an executive, a monthly-employee, or a hourly-employee. • A monthly-employee has a monthly salary. • An executive has a monthly salary and a bonus for that month. • A hourly-employee earnings depends on how many hours he/she worked on that month (there is a wage per hour for him/her). COP 3330 Object Oriented Programming

  3. A Class Hierarchy Employee Volunteer PaidEmployee Executive MonthlyEmployee HourlyEmployee • The inheritance relations among classes. • Now let us look at the details of each class. COP 3330 Object Oriented Programming

  4. Class Details Employee class: • This class has to have two fields: name and address • Its constructor must initialize these fields. • Its toString should return a string representation of these fields. • It should enforce that its subclasses have to have an earnings method.This means that it has to have an abstract earnings method. This class must be an abstract class. Volunteer class: • This class will be a sub-class of Employee class. • Since we do not need another information about a volunteer, we do not need to define new fields in this class. • Its toString should return a string representation indicating that this employee is a volunteer. • Its earnings method should return 0 since a volunteer is not paid. • Since earnings method is implemented in this class, it can be a normal class. COP 3330 Object Oriented Programming

  5. Class Details (cont.) PaidEmployee class: • This class will be a sub-class of Employee class. • Since we need a social security number for a paid employee, this class should define a new field: socialSecurityNumber. • Its toString should return a string representation of these three fields.. • Since this method does not implement earnings method, it should be abstract. Executive class: • This class will be a sub-class of PaidEmployee class. • Since we need a monthly salary and a bonus for an executive, this class should define the fields: salary and bonus. • Its toString should return a string representation indicating that this employee is an executive. • Its earnings method should return the summation of salary and bonus. • Since we may change the bonus of an executive, this class should include setBonus method. • Since earnings method is implemented in this class, it can be a normal class. COP 3330 Object Oriented Programming

  6. Class Details (cont.) MonthlyEmployee class: • This class will be a sub-class of PaidEmployee class. • Since we need a monthly salary for a monthly-employee, this class should define the field: salary. • Its toString should return a string representation indicating that this employee is a monthly-employee. • Its earnings method should return the salary. • Since earnings method is implemented in this class, it can be a normal class. HourlyEmployee class: • This class will be a sub-class of PaidEmployee class. • This class should define the fields: wage and hours. • Its toString should return a string representation indicating that this employee is a hourly-employee. • Its earnings method should return the multiplication of wage and hours. • Since we may add hours for a hourly-employee, this class should include addHours method. • Since earnings method is implemented in this class, it can be a normal class. COP 3330 Object Oriented Programming

  7. Other Classes Personnel class: • This class will declare an Employee array to hold all employees of a firm. • It should also include payday method to print paychecks for employees. EmployeeTest class: • A driver class. • It will create a Personnel object, and will call payday method with this object. COP 3330 Object Oriented Programming

  8. Employee Class public abstract class Employee { protected String eName; protected String eAddress; // Set up an employee using the specified information public Employee(String name, String address) { eName=name; eAddress=address; } // Return a string containg the basic employee information public String toString() { return ( "Name: " + eName + "\n" + "Address: " + eAddress ) ; } // All subclasses of Employee class must define earnings method public abstract double earnings() ; } COP 3330 Object Oriented Programming

  9. Volunteer Class public class Volunteer extends Employee { // Set up a volunteer using the specified information public Volunteer(String name, String address) { super(name,address); } // Return a string containing the paid employee information public String toString() { return ( "Volunteer\n" + super.toString()); } // Volunteers are not paid public double earnings() { return 0.0; } } COP 3330 Object Oriented Programming

  10. PaidEmployee Class public abstract class PaidEmployee extends Employee { protected String socialSecurityNumber; // Set up a paid employee using the specified information public PaidEmployee(String name, String address, String ssn) { super(name,address); socialSecurityNumber=ssn; } // Return a string containing the paid employee information public String toString() { return ( super.toString() + "\nSSN: " + socialSecurityNumber) ; } } COP 3330 Object Oriented Programming

  11. Executive Class public class Executive extends PaidEmployee { private double salary; private double bonus; // Set up an executive using the specified information public Executive(String name, String address, String ssn, double salary) { super(name,address,ssn); this.salary=salary; bonus=0.0; } // Set up the bonus for the executive public void setBonus(double bonus) { this.bonus=bonus; } // Return a string containing the Executive information public String toString() { return ( "Executive\n" + super.toString() + "\nSalary: " + salary + "\nBonus: " + bonus); } // Executives earn a monthly salary plus a bonus public double earnings() { return salary+bonus; } } COP 3330 Object Oriented Programming

  12. MonthlyEmployee Class public class MonthlyEmployee extends PaidEmployee { private double salary; // Set up a monthly employee using the specified information public MonthlyEmployee(String name, String address, String ssn, double salary) { super(name,address,ssn); this.salary=salary; } // Return a string containing the monthly employee information public String toString() { return ( "Monthly Employee\n" + super.toString() + "\nSalary: " + salary ); } // Monthly employees earn a monthly salary public double earnings() { return salary; } } COP 3330 Object Oriented Programming

  13. HourlyEmployee Class public class HourlyEmployee extends PaidEmployee { private double wage; // wage per hour private int hours; // hours worked for the month // Set up a hourly employee using the specified information public HourlyEmployee(String name,String address,String ssn,double wage){ super(name,address,ssn); this.wage=wage; hours=0; } // Add hours public void addHours(int hours) { this.hours += hours; } // Return a string containing the hourly employee information public String toString() { return ( "Hourly Employee\n" + super.toString() + "\nWage Per Hour: " + wage + "\nHours Worked: " + hours); } // Get hourly employee's earnings public double earnings() { return wage*hours; } } COP 3330 Object Oriented Programming

  14. Personnel Class public class Personnel { Employee[] employeeList; // Holds polymorphic references, it can hold objects of sublasses of Employee // Set up the employee list public Personnel() { employeeList = new Employee[5]; employeeList[0] = new Volunteer("Sam Clark","123 Alafaya Trail"); employeeList[1] = new Executive("Bill Brown","124 Alafaya Trail","666-66-6666",7500); employeeList[2] = new MonthlyEmployee("John Goodman","125 Alafaya Trail","777-77-7777",5000); employeeList[3] = new MonthlyEmployee("Jill Woody","126 Alafaya Trail","888-88-8888",4000); employeeList[4] = new HourlyEmployee("Tom Jones","127 Alafaya Trail","999-99-9999",9.50); ((Executive)employeeList[1]).setBonus(1200); // Downcasting ((HourlyEmployee)employeeList[4]).addHours(150); ((HourlyEmployee)employeeList[4]).addHours(60); } COP 3330 Object Oriented Programming

  15. Personnel Class (cont.) // Pays all employees public void payday() { double amount; for (int i=0; i<employeeList.length; i++) { System.out.println(employeeList[i]); amount=employeeList[i].earnings(); // polymorphic method invocation if (amount==0.0) System.out.println("Thanks!"); else System.out.println("Paid: " + amount); System.out.println("----------------------------------------"); } } } COP 3330 Object Oriented Programming

  16. EmployeeTest Class public class EmployeeTest { // Creates the personnel of a firm and pays them public static void main (String[] args) { Personnel staff = new Personnel(); staff.payday(); } } COP 3330 Object Oriented Programming

  17. Output of The Program COP 3330 Object Oriented Programming

More Related