Saturday, January 20, 2018

Programming Challenge 15.3 - TeamLeader Class

Example Files: TeamLeaderClass.7z

Note: A change to the Employee and ProductionWorker class has been made. Both classes now contain a print function.

Employee.h


#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include <string>
using std::string;

#include <iostream>
using std::ostream;

class Employee
{
    private:
        string empName;                // Employee name
        int     empID;                      // Employee ID-number
        string empHireDate;           // Employee hire date

    public:   
        // Default constructor
        Employee()
        {
            empName = " ";
            empID = 0;
            empHireDate = " ";
        }

        // Parameterized constructor
        Employee(string name, int number, string hireDate)
        {
            empName = name;
            empID = number;
            empHireDate = hireDate;
        }

        // Mutator functions
        void setEmpName(string name)
        { empName = name; }

        void setEmpID(int number)
        { empID = number; }

        void setEmpHireDate(string hireDate)
        { empHireDate = hireDate; }

        // Accessor functions
        string getEmpName() const
        { return empName; }

        int getEmpID() const
        { return empID; }

        string getEmpDateHired() const
        { return empHireDate; }

        // Friend function
        friend ostream &operator << (ostream &, const Employee &);

        // Virtual function defined in Employee.cpp
        virtual void print(ostream &strm) const;
};

#endif

Employee.cpp


#include "Employee.h"

#include <iomanip>
using std::setw;

#include <iostream>
using std::left;
using std::right;

/* **********************************************************
            Overloaded << operator
    Makes a call to the virtual print function, and sends the
    output to the stream.
   ********************************************************** */

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

/* **********************************************************
               Employee::print() : ostream &
    This function sends formatted employee data to the output
    stream.
   ********************************************************** */

void Employee::print(ostream &strm) const
{
    strm << left << setw(12) << "Employee Name: "
            << right << empName << "\n"
            << left << setw(12) << "Employee ID:   "
            << right << empID << "\n"
            << left << setw(15) << "Hire Date: "
            << right << empHireDate << "\n\n";
}

ProductionWorker.h


#ifndef PRODUCTION_WORKER_H_
#define PRODUCTION_WORKER_H_

#include "Employee.h"

#include <iostream>
using std::ostream;

// ProductionWorker class derived from the Employee class
class ProductionWorker : public Employee
{
    protected:
        int    shiftNumber;                // The shift number (1 = day, 2 = night)
        double hourlyPayRate;            // The hourly pay rate

    public:
        // Default constructor
        ProductionWorker() : Employee()
        {
            shiftNumber = 0;
            hourlyPayRate = 0.0;
        }

        // Parameterized Constructor
        ProductionWorker(string name, int number, string hireDate, int shift, double payRate) :
                  Employee(name, number, hireDate)
        {
            shiftNumber = shift;
            hourlyPayRate = payRate;
        }

        // Mutator functions
        void setShift(int shift)
        { shiftNumber = shift; }

        void setHourlyPayrate(double payRate)
        { hourlyPayRate = payRate; }

        // Accessor functions
        int getShift() const
        { return shiftNumber; }

        double getHourlyPayRate() const
        { return hourlyPayRate; }

        void print(ostream &) const;
};

#endif

ProductionWorker.cpp


#include "ProductionWorker.h"

#include <iomanip>
using std::setprecision;
using std::setw;

#include <iostream>
using std::left;
using std::right;
using std::fixed;

/* **********************************************************
            ProductionWorker::print() : ostream & (overridden)
   ********************************************************** */

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

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

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

    strm << fixed << setprecision(2)
            << left << setw(14) << "Pay Rate: "
            << right << "$ " << hourlyPayRate << "\n";
}

TeamLeader.h


#ifndef TEAM_LEADER_H_
#define TEAM_LEADER_H_

#include "ProductionWorker.h"

// TeamLeader class derived from the ProductionWorker class
class TeamLeader : public ProductionWorker
{
    private:
        double monthlyBonus;                    // Monthly salary bonus
        int    attTrainingHours;                    // Attented training hours

    public:
        const int REQ_TRAINING_HOURS = 75;        // Required hours of traning p.A.

        // Constructor
        TeamLeader() : ProductionWorker()
        {
            monthlyBonus = 0.0;
            attTrainingHours = 0;
        }

        // Parameterized constructor
        TeamLeader(string name, int number, string hireDate, int shift, double payRate,
                      double bonus) :
        ProductionWorker(name, number, hireDate, shift, payRate)
        {
            monthlyBonus = bonus;
            attTrainingHours = 0;
        }

        // Mutator functions
        void setMonthlyBonus(double bonus)
        { monthlyBonus = bonus; }

        void setAttTrainingHours(int attHours)
        { attTrainingHours = attHours; }

        // Accessor functions
        double getMonthlyBonus() const
        { return monthlyBonus; }

        int getAttendedTrainingHours() const
        { return attTrainingHours; }

        int getRemainingTrainingHours() const;   

        // Overridden print function
        void print(ostream &) const;
};

#endif

TeamLeader.cpp


#include "TeamLeader.h"

#include <iomanip>
using std::setw;

#include <iostream>
using std::left;
using std::right;

/* **********************************************************
            TeamLeader::getRemainingTrainingHours()
    This function determines, how many hours of traning are
    left to take for a team leader, in order to fulfill the
    mandatory training requirements.
   ********************************************************** */

int TeamLeader::getRemainingTrainingHours() const
{
    int remainingHours = 0;

    return remainingHours = (REQ_TRAINING_HOURS - attTrainingHours);
}

/* **********************************************************
            TeamLeader::print() : ostream & (overridden)
   ********************************************************** */

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

    strm << left << setw(14) << "Bonus: "
            << right << "$ " << monthlyBonus << "\n\n"
            << "ANNUAL TRAINING REQUIREMENTS\n\n"
            << "Required:  " << REQ_TRAINING_HOURS << " hrs.\n"
            << "Attended:  " << attTrainingHours << " hrs.\n"
            << "Remaining: " << getRemainingTrainingHours() << " hrs.\n";   
}

TeamLeaderClass.cpp


 #include "TeamLeader.h"

#include <array>
using std::array;

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    // Create a TeamLeader object
    TeamLeader leader("Takahashi Hideki", 92358, "05/07/2014", 1, 39.70, 19.50);
   
    // Create an array of three ProductionWorker objects. Each employee is
    // part of this team leader's team.
    array <ProductionWorker, 3> worker
    {
        ProductionWorker("Hartwood Michael", 23727, "05/07/2016", 1, 14.70),   
        ProductionWorker("Talmar Arman", 54838, "03/03/2005", 1, 29.50),
        ProductionWorker("Clayborn Dolores", 54257, "05/04/2015", 1, 17.50)
    };

    cout << "TAKEUCHI MANUFACTURING CO. - TRACK LOADER ASSEMBLY TEAM\n\n";
    cout << "TEAM LEADER\n\n";
    leader.setAttTrainingHours(35);
    cout << leader;

    cout << "\n\nPRODUCTION TEAM\n\n";
    for (auto workerTeam : worker)
    {
        cout << workerTeam << "\n";
    }
   
    cout << "TAKEUCHI MANUFACTURING CO.\n"
          << "Unrivaled quality, products with a difference,\n"
          << "and fast development responding to the users' needs.";

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:




No comments:

Post a Comment