In: Computer Science
C++
It is common for an organization to have different categories of employees, such that one Employee class can not easily describe all types of Employees.In this project, you will code a basic Employee class, and then a number of classes which derive from the Employee class (inherit), adding Employee specifics. An application program which stores info for one company will have use objects of different types to model the all employees.
Part I: The Employee Class
First create the Employee class described below. This class will serve as a base class. All specific employee typed will be derived from this class. :
Employee |
-lastName:string -firstName:string -idNum:int |
+Employee() //default values assigned to all variables +Employee(int idNum, string lname, string fname) +void setLName(string) +void setFName(string) +void setIDNum(int) +string getLName() +sgtringgetFName() +intgetIDNum() +void displayName() +void display() |
Stick with the access specifiers on the diagram. Items denoted as + are public, - are private.
NOTE: There are NO protected member variables in this class
The constructors both initialize all member variables. Sets functions set the indicated member variable of the object, and get functions return the value of the indicated member variable of the object.
The displayName() function prints out the last name and ID only, and the display()function outputs the all employee information that is stored.
PART II: The Derived Classes
Salaried |
Hourly |
Commission |
||
-weeklySal:double |
-payperhr:double -hrsworked:double |
-basesalary:double -comAmount:double |
||
+Salaried(string lname, string fname) +Salaried(string lname, string fname, double sal) +void setSal(double nsal) +double getSal() +void displaySal() +void display() |
+Hourly(string lname, string fname) +Hourly(string lname, string fname, double newpay) +void setPay(double) +void setHrs(double nhr) +double getPay() +double getHrs() +void displayHrly(): +void display() |
+Commission(string lname, string fname) +Commission(string lname, string fname, double bsal) +void setBase(double npay) +void setCom(double nhr) +double getBase() +double getCom() +void displayBase() +void displayEmp() |
As you can see, Employee is a base class, which is extended by the Hourly, Salaried and Commissioned classes. The derived classes describe 3 ‘types’ of employee.
Each of the derived classes provides instance data necessary for determining an employee’s weekly earnings, and both set (mutator) and get(accessor) methods for these additional variables. A salaried employee just gets a prearranged salary per week. An hourly employee’s weekly salary is based on the hourly rate and number of hours worked that week. A commissioned employee gets a base pay per week, plus the amount of commission earned that week. Each of these classes provides two display functions, one that outputs info specific for that class, one that overrides the base class function display(), to output all info on the object (both base and derived class data).
The displaySal() function outputs the employee name, id and weekly salary.
The displayHrly() function outputs the employee name, id and hourly rate.
The displayBase() function outputs the employee name, id and base pay.
Once these classes are working and tested you are ready to begin part III.
Part III The Application
You are to write an application which will allow the user to enter all his/her employees and their data. This application will allow the user to print out information on employees.
(This project will be updated in a future project assignment, so be sure to stick to the specifications. This will make the update happen more smoothly)
This application is to store each of the three types of objects in it’s own vector. That is, you will have a vector<Salaried>, a vector<Hourly> and a vector<Commission>.
Your program is to provide the following options for the user:
* add a new salaried employee
* add a new hourly employee
* add a new commissioned employee
* list all employees with a particular last name
* output all information for all employees stored
* search for an employee with a particular id number
* output the name and salaries ONLY of all salaried employees
* output the name and hourly rate ONLY of all salaried employees
* output the name, id and base salary of all commissioned employees
You are to submit 9 files, Employee.h, Employee.cpp, Salaried.h, Salaried.cpp, Hourly.h, Hourly.cpp, Commission.h, Commission.cpp, and your main program file.
The idea is to use the abstract and polymorphism concepts to extend the functionalities of an Employee.
As asked in the above question, providing step-by-step codes for each class including Main class. Please find all the coding below with headings of class name.
------------------------------------------------------------------------------------------------------------------------------------
Let us make the basic Employee class as an abstract class inorder present the general functionality of an employee. The other classes that extend Employee class are Salaried, Commission and Hourly respectively as per the given inputs. included total 5 files along with EMPLOYEE creation in main.
So to start with Employee Class: Here is the tested fully functional code for Employee Class with given members:
1. EMPLOYEE
public abstract class Employee
{
private String firstName;
private String lastName;
private int idNum;
public Employee()
{
//initializing with default values
}
public Employee (int idNum, string lname, string fname)
{
firstName = fname;
lastName = lname;
idNum = idNum;
}
public void setFName ( String fname)
{
firstName = fname;
}
public String getFName ()
{
return firstName;
}
public void setLName ( String lname)
{
lastName = lname;
}
public String getLName ()
{
return lastName;
}
public void setIDNum ( int idNum)
{
idNum = idNum;
}
public int getIDNum ()
{
return idNum;
}
public void displayName()
{
return String.format( "%s %s\nEmployee id Number: %s",
getFName(), getLName() );
}
public void display()
{
return String.format( "%s %s\nEmployee id Number: %s",
getFName(), getLName(),getIDNum () );
}
Moving on to Derived classes:
2. SALARIED
public class Salaried extends Employee
{
private double weeklySal;
public Salaried( String lname, String fname)
{
super( lname, fname);
}
public Salaried( String lname, String fname, double sal )
{
super( lname, fname );
setSal( sal );
}
public void setSal( double nsal )
{
if (nsal >= 0.0 )
weeklySal = nsal;
else
throw new IllegalArgumentException(
"Weekly salary should be >= 0.0" );
}
public double getSal()
{
return weeklySal;
}
@Override
public double earnings()
{
return getSal();
}
public void display()
{
return String.format( "%s %s\nEmployee id Number: %s",
getFName(), getLName(),getIDNum () );
}
public void displaySal()
{
return String.format( "Salaried Employee: %s\n%s: $%,.2f",
super.display(), "Weekly salary" , getSal() );
}
}
3. HOURLY
public class Hourly extends Employee
{
private double payperhr;
private double hrsworked;
public Hourly( String lname, String fname)
{
super( lname, fname );
}
public Hourly( String lname, String fname,double newpay)
{
super( lname, fname );
setPay( newpay );
}
public void setPay( double newpay )
{
if ( newpay >= 0.0 )
payperhr = newpay;
else
throw new IllegalArgumentException(
"Hourly payperhr must be >= 0.0" );
}
public double getPay()
{
return payperhr;
}
public void setHrs( double nhr )
{
if ( (nhr>=0.0) && ( nhr <= 168.0 ) )
hrsworked = nhr;
else
throw new IllegalArgumentException(
"hrsworked worked must be >= 0.0 and <= 168.0" );
}
public double getHrs()
{
return hrsworked;
}
@Override
public double earnings()
{
if ( getHrs() <= 40 ) // no overtime
return getPay() * getHrs();
else
return 40 * getPay() + ( getHrs() - 40 ) * getPay() * 1.5 ;
}
public void display()
{
return String.format( "%s %s\nEmployee id Number: %s",
getFName(), getLName(),getIDNum () );
}
public void displayHrly()
{
return String.format( "Hourly employee: %s\n%s: $%,.2f; %s:
%,.2f",
super.display(), "hourly payperhr" , getPay(),
"hrsworked worked", getHrs() );
}
}
4. COMMISSION:
public class Commission extends Employee
{
private double basesalary;
private double comAmount;
public Commission( String lname, String fname)
{
super( lname, fname );
}
public Commission( String lname, String fname, double bsal)
{
super( lname, fname );
setBase( npay );
}
public void setCom( double nhr )
{
if ( nhr > 0.0 && nhr < 1.0 )
comAmount = nhr;
else
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0" );
}
public double getCom()
{
return comAmount;
}
public void setBase( double npay )
{
if ( npay >= 0.0 )
basesalary = npay;
else
throw new IllegalArgumentException(
"Base salary must be >= 0.0" );
}
public double getBase()
{
return basesalary;
}
@Override
public double earnings()
{
return getCom() * getBase();
}
public void display()
{
return String.format( "%s %s\nEmployee id Number: %s",
getFName(), getLName(),getIDNum () );
}
public void displayBase()
{
return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
"Commissioned employee", super.display(),
"base salary", getBase(),
"commission rate", getCom() );
}
}
5. MAIN
public class Main
{
public static void main( String[] args )
{
Employee employee = new Employee( 12345, "Pratibha", "Jogendra" );
Salaried salaried = new Salaried( "Pratibha", "Jogendra" ,800.00
);
Hourly hourly = new Hourly( "Datta", "Prakash" , 16.75);
Commission commission = new Commission("Kumar", "Gunadhya",
10000.00);
System.out.println( "Individual Employees :\n" );
System.out.printf( "%s\n%s: $%,.2f\n\n", salaried, "earned",
salaried.earnings() );
System.out.printf( "%s\n%s: $%,.2f\n\n", hourly, "earned",
hourly.earnings() );
System.out.printf( "%s\n%s: $%,.2f\n\n",commission, "earned",
commission.earnings() );
Employee[] employees = new Employee[ 4 ];
employees[ 0 ] = salaried;
employees[ 1 ] = hourly;
employees[ 2 ] = commission;
//Can display output using polymorphism
}
}