Saturday, September 9, 2017

Programming Challenge 13.11 - Payroll

Example Files: Payroll.h
                          Payroll.cpp
                          PayrollDemo.cpp

Payroll.h


#ifndef PAYROLL_H
#define PAYROLL_H

class Payroll
{
    private:
        double payRate;        // The hourly pay rate
        float  hrsWorked;        // The hours worked by an employee
        double grossPay;        // The gross pay

    public:
        Payroll()                // Default constructor
        {
            payRate = 0.0;
            hrsWorked = 0.0;
            grossPay = 0.0;
        }

        void setHrsWorked(float hrs);               
        void setPayRate(double pRate);           
        void calcGrossPay();                           

        float getHoursWorked() const        // Returns the hours worked
        { return hrsWorked; }

        double getGrossPay() const            // Returns the gross pay
        { return grossPay; }
};
#endif


Payroll.cpp


/* Payroll.h - Implementation file for the Payroll class. */

#include <iostream>
#include "Payroll.h"

/* **********************************************************
            Payroll::setHrsWorked(accepts a float)
    This function sets the hours worked. If a value passed to
    is within range it is copied to the hrsWorked member. If
    the value for hours worked is too high, hrsWorked is set 
    to 60 hrs, else if the value is lower than or equal to 0,
    hrsWorked is set to 5.0.
   ********************************************************** */

void Payroll::setHrsWorked(float hrs)
{
    enum HoursWorked { MIN_HRS = 5, MAX_HRS = 60 };

    if (hrs >= MIN_HRS && hrs <= MAX_HRS)
    {
        hrsWorked = hrs;
    }
    else if (hrs > MAX_HRS)
    {
        std::cout << "\nValue for hours worked too high.\n"
                   << "Setting default value: " << MAX_HRS << "hrs.\n\n";
        hrsWorked = MAX_HRS;
    }
    else if (hrs <= MIN_HRS)
    { std::cout << "\nValue for hours worked too low.\n"
                << "Setting default value: " << MIN_HRS << " hrs.\n\n";
        hrsWorked = MIN_HRS;
    }
}

/* **********************************************************
            Payroll::setPayRate(accepts a double)
    This function sets the pay rate. If a value passed to it
    is within range, it is copied to the payRate member, else
    a default value of 2.50 is set.
   ********************************************************** */

void Payroll::setPayRate(double pRate)
{
    const double MIN_WAGE = 2.50;

    if (pRate >= MIN_WAGE)
    {
        payRate = pRate;
    }
    else
    {
        std::cout << "\nValue for pay rate can't be lower than " << MIN_WAGE << "\n"
                   << "Setting default pay rate: $ " << MIN_WAGE << "\n";
        payRate = MIN_WAGE;
    }
}

/* **********************************************************
            Payroll::calcGrossPay()
    Calculates the gross pay earned by an employee and stores
    the result in the grossPay member variable.
   ********************************************************** */

void Payroll::calcGrossPay()
{
    grossPay = hrsWorked * payRate;
}

PayrollDemo.cpp


/* PayrollDemo.cpp - This program demonstrates the Payroll class. */

#include <iomanip>
#include <iostream>
#include <array>
#include <vector>
#include "Payroll.h"

using std::cin;
using std::cout;
using std::array;

constexpr int NUM_EMPLOYEES = 7;

void getData(array<Payroll, NUM_EMPLOYEES> &);
void displayPData(array<Payroll, NUM_EMPLOYEES> const);

int main()
{
    array<Payroll, NUM_EMPLOYEES> employee{};        // Array of Payroll objects
   
    cout << "ISHIKAWA FRUIT COMPANY - PAYROLL SYSTEM\n\n";

    getData(employee);
    displayPData(employee);

    cout << "\nISHIKAWA FRUIT COMPANY - Your number one fruit supplier!\n";

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

/* **********************************************************
    getData(accepts an array of Payroll objects)
    This function asks the user for the number of hours worked
    by each employee and the hourly pay rate, then calculates
    the gross pay.
   ********************************************************** */

void getData(array<Payroll, NUM_EMPLOYEES> &employee)
{
    float     hours = 0;
    double payRate = 0.0;

    cout << "Enter the hours worked by " << NUM_EMPLOYEES
          << " employees and their hourly rates.\n";

    for (int index = 0; index < NUM_EMPLOYEES; index++)
    {
        cout << "\nHours worked by employee #"
              << (index + 1) << ": ";
        cin >> hours;

        employee[index].setHrsWorked(hours);

        cout << "Hourly pay rate for employee #"
            << (index + 1) << ": ";
        cin >> payRate;

        employee[index].setPayRate(payRate);
        employee[index].calcGrossPay();
    }
}

/* **********************************************************
   displayPData(accepts an array of Payroll objects)
    This function displays the amount of gross pay each
    employee earned during the week.
   ********************************************************** */

void displayPData(array<Payroll, NUM_EMPLOYEES> const employee)
{
    cout << std::fixed << std::showpoint << std::setprecision(2);
    cout << "\nHere is the gross pay for each employee:\n\n";
   
    for (int index = 0; index < NUM_EMPLOYEES; index++)
    {
        cout << "Employee #" << (index + 1) << " worked " << std::setw(5) << employee[index].getHoursWorked() << " hrs. "
              << ": $ " << std::setw(24) << employee[index].getGrossPay() << "\n";
    }
}


Example Output:




No comments:

Post a Comment