Object Orientated Payroll Program

Object Orientated Payroll Program

Solution 

p1.cpp 

#include <string>

#include <iostream>

using namespace std;

// define the Employee classes

class Employee

{

private:

string name;

int number;  // employee number

string hire; // the hire date

public:

// constructors

Employee(string name, int number, string hire)

: name(name), number(number), hire(hire)

{}

Employee() : name(“”), number(0), hire(“”) {}

// accessor and mutator functions

stringgetName()     const { return name; }

intgetNumber()      const { return number; }

stringgetHireDate() const { return hire; }

voidsetName(const string &value)     { name = value; }

voidsetNumber(constint value)       { number = value; }

voidsetHireDate(const string &value) { hire = value; }

};

// inherent class defination

classProductionWorker : public Employee

{

private:

int shift;

doublehourlyRate; // hourly pay rate

public:

// constructors

ProductionWorker(string name, int number, string hire, int shift, double hourlyRate)

: Employee(name, number, hire), shift(shift), hourlyRate(hourlyRate)

{}

ProductionWorker() : Employee(), shift(1), hourlyRate(0.0) {}

// accessor and mutator functions

intgetShift()         const { return shift; }

doublegetHourlyRate() const { return hourlyRate; }

voidsetShift(int value)         { shift = value; }

voidsetHourlyRate(double value) { hourlyRate = value; }

};

// define ShiftSuppervisor class

classShiftSupervisor : public Employee

{

private:

doubleannualSalary;

doubleannualBonus;  // annual production bonus

public:

// constructors

ShiftSupervisor(string name, int number, string hire, double annualSalary, double annualBonus)

: Employee(name, number, hire), annualSalary(annualSalary), annualBonus(annualBonus)

{}

ShiftSupervisor() : Employee(), annualSalary(0.0), annualBonus(0.0) {}

// accessor and mutator functions

doublegetAnnualSalary() const { return annualSalary; }

doublegetAnnualBonus()  const { return annualBonus; }

voidsetAnnualSalary(double value) { annualSalary = value; }

voidsetAnnualBonus(double value)  { annualBonus = value; }

};

// TeamLeader class

classTeamLeader : public ProductionWorker

{

private:

doublemonthlyBonus;  // fixed monthly bonus

intrequiredHours;    // minimal required training hours

inttrainedHours;     // number of hours the team leader has attended

public:

TeamLeader(string name, int number, string hire, int shift, double hourlyRate,

doublemonthlyBonus, intrequiredHours, inttrainedHours)

: ProductionWorker(name, number, hire, shift, hourlyRate),

monthlyBonus(monthlyBonus), requiredHours(requiredHours), trainedHours(trainedHours)

{}

TeamLeader() : ProductionWorker(), monthlyBonus(0), requiredHours(0), trainedHours(0) {}

// accessor and mutator functions

doublegetMonthlyBonus() const { return monthlyBonus; }

intgetRequiredHours()   const { return requiredHours; }

intgetTrainedHours()    const { return trainedHours; }

voidsetMonthlyBonus(double value) { monthlyBonus = value; }

voidsetRequiredHours(int value)   { requiredHours = value; }

voidsetTrainedHours(int value)    { trainedHours = value; }

};

int main() {

// Q1 use the ProductionWorker class

ProductionWorkerp(“Ben J”, 1, “2010-03-12”, 2, 100);

cout<< “Production Worker: ” <<p.getName() << ” [” <<p.getNumber() << “]” <<endl

<< “Hired Date “<<p.getHireDate() <<endl

<< (p.getShift() == 1 ? “Day Shift Hourly Rate @” : “Night Shift Hourly Rate @”)

<<p.getHourlyRate() <<endl<<endl;

// Q2 use ShiftSuppervisor class

ShiftSupervisors(“Steven K”, 2, “2008-01-01”, 60000, 20000);

cout<< “Shift Suppervisor: ” <<s.getName() << ” [” <<s.getNumber() << “]” <<endl

<< “Hired Date “<<s.getHireDate() <<endl

<< “Annual Salary @” <<s.getAnnualSalary() <<endl

<< “Annual Production Bonus @” <<s.getAnnualBonus() <<endl<<endl;

// Q3 use TeamLeader

TeamLeadert(“Bryan M”, 3, “2009-08-11”, 1, 120, 2000, 10, 3);

cout<< “Team Leader: ” <<t.getName() << ” [” <<t.getNumber() << “]” <<endl

<< “Hired Date “<<t.getHireDate() <<endl

<< (t.getShift() == 1 ? “Day Shift Hourly Rate @” : “Night Shift Hourly Rate @”)

<<t.getHourlyRate() <<endl

<< “Monthly Bonus @” <<t.getMonthlyBonus() <<endl

<< “Required Minimum Training “<<t.getRequiredHours() << ” hours” <<endl

<< “Trained “<<t.getTrainedHours() << ” hours”

<<endl<<endl;

} 

p2.cpp 

#include <string>

#include <iostream>

using namespace std;

// define the Employee classes

class Employee

{

private:

string name;

int number;  // employee number

string hire; // the hire date

public:

classInvalidEmployeeNumber {};

// constructors

Employee(string name, int number, string hire)

: name(name), number(number), hire(hire)

{

if (number < 0 || number > 9999) {

throwInvalidEmployeeNumber();

}

}

Employee() : name(“”), number(0), hire(“”) {}

// accessor and mutator functions

stringgetName()     const { return name; }

intgetNumber()      const { return number; }

stringgetHireDate() const { return hire; }

voidsetName(const string &value)     { name = value; }

voidsetNumber(constint value)       {

if (value < 0 || value > 9999) {

throwInvalidEmployeeNumber();

}

number = value;

}

voidsetHireDate(const string &value) { hire = value; }

};

// inherent class defination

classProductionWorker : public Employee

{

private:

int shift;

doublehourlyRate; // hourly pay rate

public:

classInvalidShift {};

classInvalidPayRate {};

// constructors

ProductionWorker(string name, int number, string hire, int shift, double hourlyRate)

: Employee(name, number, hire), shift(shift), hourlyRate(hourlyRate)

{

if (shift != 1 && shift != 2) {

throwInvalidShift();

}

if (hourlyRate< 0) {

throwInvalidPayRate();

}

}

ProductionWorker() : Employee(), shift(1), hourlyRate(0.0) {}

// accessor and mutator functions

intgetShift()         const { return shift; }

doublegetHourlyRate() const { return hourlyRate; }

voidsetShift(int value) {

if (value != 1 && value != 2) {

throwInvalidShift();

}

shift = value;

}

voidsetHourlyRate(double value) {

if (value < 0) {

throwInvalidPayRate();

}

hourlyRate = value;

}

};

int main() {

// demonstrate the use of exceptions

try {

ProductionWorkerp1(“Ben J”, -1, “2010-03-12”, 2, 100);

} catch (Employee::InvalidEmployeeNumber&e) {

cout<< “Employee::InvalidEmployeeNumber is caught!” <<endl;

}

try {

ProductionWorkerp1(“Ben J”, 9999, “2010-03-12”, 0, 100);

} catch (ProductionWorker::InvalidShift&e) {

cout<< “Employee::InvalidShift is caught!” <<endl;

}

try {

ProductionWorkerp1(“Ben J”, 9999, “2010-03-12”, 1, -10);

} catch (ProductionWorker::InvalidPayRate&e) {

cout<< “Employee::InvalidPayRate is caught!” <<endl;

}

return 0;

}