Wednesday, March 7, 2018

Programming Challenge 16.13 - Exceptions Project

Example Files: ExceptionsProject.7z

Employee.h


#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include <iostream>
#include <string>

class Employee
{
    private:
        int            employeeID;
        std::string employeeName;
        std::string employeeHireDate;

    public:
        class InvalidEmployeeID
        {
            private:   
                int invalidEmployeeID;

            public:
                InvalidEmployeeID(int invalidEmpID) : invalidEmployeeID(invalidEmpID)
                { }

                int getInvalidEmployeeID() const
                { return invalidEmployeeID; }
        };

        Employee(int, std::string, std::string);
        virtual ~Employee()
        { }

        // Mutator function
        void setEmployee(int, std::string, std::string);

        // Accessor function
        void invalidIDHandler() const;

        // Consider directly employing the overloaded operator and call it from the
        // Production worker class
        friend std::ostream &operator << (std::ostream &, const Employee &);
       
        virtual void print(std::ostream &) const;
};

#endif

Employee.cpp


#include "Employee.h"

#include <iomanip>

/* **********************************************************
            Employee::Employee() : int, string, string
            - Constructor
   ********************************************************** */

Employee::Employee(int empID, std::string empName, std::string empHired) :
    employeeID(empID), employeeName(empName), employeeHireDate(empHired)
{
    invalidIDHandler();
}

/* **********************************************************
            Employee::setEmployee() : int, string, string
    Sets the employee data.
   ********************************************************** */

void Employee::setEmployee(int empID, std::string empName, std::string empHired)
{
    employeeID = empID;
    employeeName = empName;
    employeeHireDate = empHired;

    invalidIDHandler();
}

/* **********************************************************
            Employee::invalidIDHandler()
    Evaluates the employeeID and throws an exception if found
    invalid.
   ********************************************************** */

void Employee::invalidIDHandler() const
{
    if (employeeID < 0 || employeeID > 9999)
    {
        throw InvalidEmployeeID(employeeID);
    }
}

/* **********************************************************
            Overloaded << function
   ********************************************************** */

std::ostream &operator << (std::ostream &strm, const Employee &obj)
{
    obj.print(strm);
    return strm;
}

/* **********************************************************
            Employee::print() : ostream &
    Outputs the employee data to screen.
   ********************************************************** */

void Employee::print(std::ostream &strm) const
{
    strm << std::left  << std::setw(12) << "Employee Name: "
          << std::right << employeeName << std::endl;
    strm << std::left << std::setw(15) << "Employee ID: "
          << std::right << employeeID << std::endl;
    strm << std::left  << std::setw(15) << "Hire Date: "
          << std::right << employeeHireDate << std::endl << std::endl;
}

ProductionWorker.h


#ifndef PRODUCTION_WORKER_H_
#define PRODUCTION_WORKER_H_

#include "Employee.h"

#include <iostream>
#include <string>

class ProductionWorker : public Employee
{
    private:
        int     shiftNumber;
        double hourlyPayrate;

    public:
        class InvalidPayrate
        {
            private:
                double invalidPayrate;

            public:
                InvalidPayrate(double invalidRate) : invalidPayrate(invalidRate)
                { }

                double getInvalidPayrate() const
                { return invalidPayrate; };
        };

        class InvalidShiftData
        {
            private:
                int invalidShiftID;

            public:
                InvalidShiftData(int invalidShift) : invalidShiftID(invalidShift)
                { }

                int getInvalidShiftID() const
                { return invalidShiftID; }
        };

        ProductionWorker(int, std::string, std::string, int, double);

        void setProductionWorker(int, double);

        void invalidShiftHandler() const;
        void invalidPayrateHandler() const;

        void print(std::ostream &) const override;
};

#endif

ProductionWorker.cpp


#include "ProductionWorker.h"

#include <iomanip>

/* **********************************************************
            ProductionWorker::ProductionWorker() : int, string,
            string, int, double - Constructor
   ********************************************************** */

ProductionWorker::ProductionWorker(int empID, std::string empName, std::string empHired,
                                              int shiftID, double payRate) :
                                              shiftNumber(shiftID),hourlyPayrate(payRate),
                                              Employee(empID, empName, empHired)
{
    invalidShiftHandler();
    invalidPayrateHandler();
}

/* **********************************************************
            ProductionWorker::setProductionWorker() int, double
    Sets the production worker data.
   ********************************************************** */

void ProductionWorker::setProductionWorker(int shiftID, double payRate)
{
    shiftNumber = shiftID;
    hourlyPayrate = payRate;

    invalidShiftHandler();
    invalidPayrateHandler();
}

/* **********************************************************
            ProductionWorker::shiftErrorHandler()
    Evaluates the shiftNumber and throws an exception if found
    invalid.
   ********************************************************** */

void ProductionWorker::invalidShiftHandler() const
{
    if (shiftNumber < 0 || shiftNumber > 2)
    {
        throw InvalidShiftData(shiftNumber);
    }
}

/* **********************************************************
            ProductionWorker::payRateErrorHandler()
    Evaluates the hourlyPayRate and throws an exception if
    found invalid.
   ********************************************************** */

void ProductionWorker::invalidPayrateHandler() const
{
    if (hourlyPayrate < 0.0)
    {
        throw InvalidPayrate(hourlyPayrate);
    }
}

/* **********************************************************
            ProductionWorker::print() : ostream &
    Outputs the production worker data to screen.
   ********************************************************** */

void ProductionWorker::print(std::ostream &strm) const
{
    Employee::print(strm);

    strm << "Shift #" << shiftNumber << ": ";

    shiftNumber == 1 ?
        strm << std::setw(16) << std::right << "Day-Shift"   << std::endl :
        strm << std::setw(16) << std::right << "Night-Shift" << std::endl;

    strm << std::fixed << std::setprecision(2) << std::fixed;
    strm << std::left  << std::setw(14) << "Pay Rate: "
          << std::right << "$ " << hourlyPayrate << std::endl;
}

ExceptionProject.cpp


#include "ProductionWorker.h"

#include <iostream>

void badValueCheck(int, std::string, std::string, int, double);

int main()
{
    std::cout << "TAKEUCHI MANUFACTURING CO. - EMPLOYEE DATA VALIDATION\n\n";
    std::cout << "The following tests will now be carried out:\n\n";

    std::cout << "Test 1: Employee Number\n";
    badValueCheck(99921, "Flanders Moll", "17/04/2013", 2, 86.80);
    std::cout << std::endl;

    std::cout << "Test 2: Shift ID\n";
    badValueCheck(1241, " Kakuta", "12/03/2015", -7, 85.99);
    std::cout << std::endl;

    std::cout << "Test 3: Hourly Payrate\n";
    badValueCheck(9411, "Vishneva Diana", "12/03/2015", 1, -190.55);
    std::cout << std::endl;

    std::cout << "Test 4: Correct Data\n";
    badValueCheck(4350, "Hiroshi Sato", "15/05/2005", 2, 99.75);
    std::cout << std::endl;

    std::cout << "You are now leaving the Database System ...\n\n";
    std::cout << "TAKEUCHI MANUFACTURING CO.\n"
             << "Unrivaled quality, products with a difference,\n"
             << "and fast development responding to the users' needs.";

    std::cin.get();
    return 0;
}

/* **********************************************************
            badValueCheck() : int, string, string, int, double
    This function tests various data items for validity. If an
    item is invalid, it is caught, and an error message is
    output. Otherwise the production worker data is output.
   ********************************************************** */

void badValueCheck(int empID, std::string empName, std::string empHired,
                         int shiftID, double payRate)
{
    try
    {
        ProductionWorker temp(empID, empName, empHired, shiftID, payRate);

        std::cout << temp;
    }
    catch (ProductionWorker::InvalidEmployeeID badEmployeeID)
    {
        std::cout << "Error! Invalid Employee ID: "
                     << badEmployeeID.getInvalidEmployeeID() << std::endl;
    }
    catch (ProductionWorker::InvalidShiftData badShiftID)
    {
        std::cout << "Error! Invalid Shift ID: "
                     << badShiftID.getInvalidShiftID() << std::endl;
    }
    catch (ProductionWorker::InvalidPayrate badPayrate)
    {
        std::cout << "Error! Invalid Hourly Payrate: $ "
                     << badPayrate.getInvalidPayrate() << std::endl;
    }
}

Example Output:



No comments:

Post a Comment