Thursday, April 2, 2015

Employee (Amended) This time it checks for shift number, pay rate, and employee number


#include <iostream>
#include <string>
using namespace std;

class Employee
{
private:
    string employeeName;
    int employeeNumber;
    string hireDate;
    //member function to validate the E number
    void checkEmployeeNumber()
    {
        if (employeeNumber < 0 ||employeeNumber > 9999 )
            throw invalidEmployeeNumber();
    }
public:
    //exception class
    class invalidEmployeeNumber
    {};
    //constructors
    Employee (string name, int number, string hDate)
    {
        //set variables
        employeeName = name;
        employeeNumber = number;
        hireDate = hDate;

        //check the employee number
        checkEmployeeNumber();
    }
    //mutators
    void setEmployeeName (string name)
    {
        employeeName=name;
    }
    void setEmployeeNumber (int number)
    {
        employeeNumber=number;
        checkEmployeeNumber();
    }
    void setHireDate (string hDate)
    {
        hireDate = hDate;
    }
    //accessors
    string getEmployeeName() const
    {
        return employeeName;
    }
    int getEmployeeNumber() const
    {
        return employeeNumber;
    }
    string getHireDate () const
    {
        return hireDate;
    }

};
class ProductionWorker:
    public Employee
    {
    private:
        int shift;
        double hourlyPayRate;
        //member function checking shift number
        void checkShift()
        {
            if
                (shift < 1 || shift > 2)
                    throw InvalidShift();
        }
        // checking pay rate
        void checkPayRate()
        {
            if
                (hourlyPayRate < 0)
                    throw InvalidPayRate();
        }

    public:
        //exception class
        class InvalidShift
        {};
        class InvalidPayRate
        {};
        //constructors
        ProductionWorker (string name, int number, string hDate, int shiftNum, double payRate):
                Employee (name, number, hDate)
                {
                    //set the member variables
                    shift = shiftNum;
                    hourlyPayRate = payRate;
                    //check the shift and pay rate
                    checkShift();
                    checkPayRate();
                }
        //mutators
        void setShift (int shiftNum)
        {
        shift = shiftNum;
        checkShift();
        }
        void setHourlyPayRate (double payRate)
        {
        hourlyPayRate = payRate;
        checkPayRate();
        }
        //accessors
        int getShift () const
        {
            return shift;
        }
        double getHourPayRate()
        {
            return hourlyPayRate;
        }
    };
    //prototype
    void testValues (string, int, string, int, double);

    int main()
    {
//test good data
       cout <<" Testing good data....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",1,550.00);
//test bad shift number
       cout <<"\n Testing bad shift number....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",9,550.00);
//test bad employee number
       cout <<"\n Testing bad employee number....\n";
       testValues("Pedro Colon", 91234, "12/01/2000",1,550.00);
//test bad pay rate
       cout <<"\n Testing  bad pay rate ....\n";
       testValues("Pedro Colon", 1234, "12/01/2000",1,-550.00);
       return 0;
    }
void testValues (string name, int number, string hDate, int shift, double payRate)
{
    try
            {
            ProductionWorker (name, number, hDate, shift, payRate);
            //good stuff
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\n Good data\n" ;
            }
        catch (ProductionWorker::invalidEmployeeNumber)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Employee Number encountered \n";
            }
        catch (ProductionWorker::InvalidShift)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Shift Number encountered \n";
            }
        catch (ProductionWorker::InvalidPayRate)
            {
            cout <<"Employee Name- "<< name<< " \nEmployee #- \t"<< number<< " \nHire Date- \t"<<hDate<<" \nShift # -\t "<<shift<< " \nPay Rate\t"<<payRate<<endl;
            cout<<"\nInvalid Pay Rate encountered \n";
            }
}

No comments:

Post a Comment