Thursday, June 15, 2017

Programming Challenge 11.15 - Multipurpose Payroll

/* Multipurpose Payroll - This program calculates the pay for either an
   hourly paid worker or a salaried worker. Hourly paid workers are paid
    their hourly pay rate times the number of hours worked. Salaried workers
    are paid their regular salary plus any bonus they may have earned. The
    program declares two structures for the following data:

        * Hourly Paid                                        * Salaried
          HoursWorked                                          Salary
          HourlyRate                                          Bonus

    The program also declares a union with two members. Each member is a
    structure variable: one for the hourly paid worker and another for the
    salaried worker.

    The program asks the user whether he or she is calculating the pay for
    an hourly paid worker or a salaried worker. Regardless of which the user
    selects, the appropriate members of the union will be used to store the
    data that will be used to calculate the pay.

    Input Validation: No negative numbers are accepted. No values greater
    than 80 for HoursWorked are accepted. */

#include "Utility.h"

struct HourlyPaid
{
    int    hoursWorked;        /* Number of hours worked */
    double hourlyRate;        /* The hourly pay rate      */
};

struct Salaried
{
    double salary;                /* The employee's salary   */
    double bonus;                /* Bonus for overtime work */
};

union EmployeeType
{
    HourlyPaid worker;        /* HourlyPaid structure variable */
    Salaried staffMember;    /* Salaried structure variable   */
};

void menu();
void getHourlyPaid(EmployeeType &);
void getSalaried(EmployeeType &);
void calcHourlyGP(EmployeeType);
void calcSalariedGP(EmployeeType);

int main()
{
    menu();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: menu

    This function offers a basic menu from which the user is
    asked to enter data either an hourly employed worker or a
    staff member. Once finished entering data for one of the
    employee types, the user can either repeat the process for
    another employee, or to quit the program.
   ********************************************************** */

void menu()
{
    EmployeeType employee;

    char payType = ' ';
    char next = ' ';
   
    do
    {
        cout << "\n\n\tOnigiri House - Payroll System\n\n\n"
              << "\tEnter employee type:\n\n"
              << "\t[H] for hourly paid workers\n"
              << "\t[S] for salaried employees\n\n"
              << "\tPay Type: ";
        cin >> payType;

        payType = toupper(payType);

        while (payType != 'H' && payType != 'S')
        {
            cout << "\tPay Type: ";
            cin >> payType;
        }

        if (payType == 'H')
        {
            getHourlyPaid(employee);
            calcHourlyGP(employee);
        }
        else
        {
            getSalaried(employee);
            calcSalariedGP(employee);
        }

        cout << "\n\n\tSelect\n\n"
              << "\t[N] for Next employee\n"
              << "\t[Q] to Quit\n"
              << "\n\tEnter Choice: ";
        cin >> next;

        next = toupper(next);

        while (next != 'N' && next != 'Q')
        {
            cout << "\n\n\tSelect\n\n"
                  << "\t[N] for Next employee\n"
                  << "\t[Q] to Quit\n"
                  << "\n\tEnter Choice: ";
            cin >> next;

            next = toupper(next);
        }

        if (next == 'Q')
        {
            cout << "\n\tOnigiri House - Your number one address for Onigiri, "
                     "Bento & Miso Soup\n\n";
        }
    } while (next != 'Q');
}

/* **********************************************************
    Definition: getHourlyPaid

   This function accepts a union as argument. It asks the
    user for the hours worked and the hourly pay rate. This
    information is stored in the appropriate member of the
    EmployeeType union.
   ********************************************************** */

void getHourlyPaid(EmployeeType &hourlyEmp)
{
    double grossPay = 0.0;

    cout << "\n\n\tHourly Paid Employee\n\n"
              "\tEnter number of hours worked:" << setw(7) << right << " ";
    cin >> hourlyEmp.worker.hoursWorked;

    while (hourlyEmp.worker.hoursWorked <= 0 || hourlyEmp.worker.hoursWorked > 80)
    {
        cout << "\tEnter number of hours worked:" << setw(7) << right << " ";
        cin >> hourlyEmp.worker.hoursWorked;
    }

    cout << "\tEnter the hourly pay rate:" << setw(5) << right << "$"
          << setw(2) << right << " ";
    cin >> hourlyEmp.worker.hourlyRate;

    while (hourlyEmp.worker.hourlyRate <= 0)
    {
        cout << "\tEnter the hourly pay rate:" << setw(5) << right << "$"
              << setw(2) << right << " ";
        cin >> hourlyEmp.worker.hourlyRate;
    }
}

/* **********************************************************
   Definition: calcHourlyGP

    This function accepts a union as argument. It calculates
    the weekly gross pay for an hourly employed worker. The
    weekly gross pay is calculated and displayed.
   ********************************************************** */

void calcHourlyGP(EmployeeType hourlyEmp)
{
    double grossPay = 0.0;

    grossPay = (hourlyEmp.worker.hoursWorked * hourlyEmp.worker.hourlyRate);

    cout << fixed << showpoint << setprecision(2);
    cout << "\tWeekly Gross Pay:" << setw(15) << right << "$ " << grossPay << "\n";
}

/* **********************************************************
   Definition: getSalaried

    This function accepts a union as argument. The user is
    asked to enter the annual salary for a staff member. This
    information is stored in the appropriate member of the
    EmployeeType union.
   ********************************************************** */

void getSalaried(EmployeeType &salariedEmp)
{
    cout << "\n\n\tSalaried Employee\n\n"
          << "\tEnter the employee's salary:" << setw(6) << right << "$ ";
    cin >> salariedEmp.staffMember.salary;

    while (salariedEmp.staffMember.salary <= 0)
    {
        cout << "\tEnter the employee's annual salary:" << setw(6) << right << "$ ";
        cin >> salariedEmp.staffMember.salary;
    }
}

/* **********************************************************
   Definition: calcSalariedGP

    This function accepts a union as argument. The weekly
    gross pay for a salaried worker is calculated. Depending
    on the annual wage, a bonus is calculated and added to the
    salary. The weekly gross pay is displayed.
   ********************************************************** */

void calcSalariedGP(EmployeeType salariedEmp)
{
    double grossPay = 0.0;

    salariedEmp.staffMember.salary >= 15000.0 && salariedEmp.staffMember.salary <= 19999.0 ?
    salariedEmp.staffMember.bonus = salariedEmp.staffMember.salary * .15 :
    salariedEmp.staffMember.salary >= 20000.0 && salariedEmp.staffMember.salary <= 29999.0 ?
    salariedEmp.staffMember.bonus = salariedEmp.staffMember.salary * .20 :
    salariedEmp.staffMember.salary >= 30000.0 && salariedEmp.staffMember.salary <= 49999.0 ?
    salariedEmp.staffMember.bonus = salariedEmp.staffMember.salary * .30 :
    salariedEmp.staffMember.bonus = salariedEmp.staffMember.salary * .50;

    grossPay = (salariedEmp.staffMember.bonus + salariedEmp.staffMember.salary) / 52;

    cout << fixed << showpoint << setprecision(2);
    cout << "\tWeekly Gross Pay:" << setw(17) << right << "$ " << grossPay << "\n";
}

Example Output:





No comments:

Post a Comment