How to Develop & Test Pay Calculator in C++?

pay calculator assignment

Task:

The following working Pay Calculator project and using the business rules and review the logical program structure as well as the file structure.  You will need to understand the structure of the program before you are able to debug the program.

You will then create a test plan and execute the test against the provided program.  For any tests that fail (there are errors in the program), debug the program and determine, but do not correct, the source of the error and describe in the comments field what the problem is.

Note:  if you have a working Pay Calculator program you can use your own version to perform the tests.

Testing

Download the following working Pay Calculator project and using the business rules and review the logical program structure as well as the file structure.  You will need to understand the structure of the program before you are able to debug the program.

Note:  if you have a working Pay Calculator program you can use your own version to perform the tests.

  1. The employee name cannot be empty and must be provided.
  2. An employee may not work at all during the pay period, but cannot work more than 60 hours during the pay period.
  3. The lowest pay rate is $8.50 per hour and the highest pay rate is $75 per hour.
  4. The net pay will be calculated from the gross pay by deducting taxes at the following rates:
Gross Pay Range Tax Rate
0 < Gross Pay < 1500 .15
1500 <= Gross Pay < 3000 .19
3000 <= Gross Pay < 5000 .21
5000 <= Gross Pay < 7000 .23
7000 <= Gross Pay .27

 

  1. Processing a pay will halt and an error message displayed if any of the inputs are not valid.
  2. Upon successful input of all values and calculations the program will display the following:
    1. Name
    2. Hours Worked
    3. Hourly Pay Rage
    4. Gross Pay
    5. Tax Rate
    6. Net pay

Update the Test cases using the following table: (add rows as necessary)

# Purpose Input Values Expected Results Actual Results Pass/Fail Comments
1.
2.
3.
4.

 

Create sufficient test cases to test the following:

  1. Employee Name is not empty
  2. Employee work hours lower bounds (both inclusive and exclusive)
  3. Employee work hours normal values
  4. Employee work hours upper bounds (both inclusive and exclusive)
  5. Pay rate lower bounds (both inclusive and exclusive)
  6. Pay rate normal values
  7. Pay rate upper bounds (both inclusive and exclusive)
  8. Validate that for the following the correct tax rate is selected and the correct net pay is calculated: (note you will need to generate work hours and pay rate to give you the given gross pay).
    1. Gross Pay = 1400
    2. Gross Pay = 1500
    3. Gross Pay = 2999
    4. Gross Pay = 3000
    5. Gross Pay = 4999
    6. Gross Pay = 5000
    7. Gross Pay = 6999
    8. Gross Pay = 7000
    9. Gross Pay = 10000

Execute each test and document that expected results match the actual results.  Mark the test as pass/fail.

For any test that fails, debug the program and determine, but do not correct, the source of the error.

Program Structure

logical programming structure

 

Solution:

Pay_Main.cpp

#include “common.h”

int main()
{
    displayDirections();
    string directions = string(“\nDo you want to Continue processing or select antoher option?”);
    do
    {
        switch (menu_select())
        {
        case 1:
            processPay();
            break;
        case 2:
            processFile();
            break;
        case 3:
            terminateApplication(APP_NAME);
            break;
        default:
            soundError();
            cout << “Invalid input please choose a valid menu selection”;
        }
    } while (keepGoing(directions));

    terminateApplication(APP_NAME);
    return 0;
}

PayCalculator.cpp

#include “common.h”
#include “payCalculator.h”

void processPay()
{
    bool isValid = false;
    string name;
    float hoursWorked = 0;
    float payRate = 0;
    float grossPay = 0;
    float taxRate = 0;
    float netPay = 0;

    isValid = getPayInformation(name, hoursWorked, payRate);
    
    grossPay = calculateGrossPay(hoursWorked, payRate);
    calculateNetpay(grossPay, taxRate, netPay);
    displayPayInformation(name,
                        hoursWorked,
                        payRate,
                        grossPay,
                        taxRate,
                        netPay);

}

bool getPayInformation(string& name, float& hours, float& rate)
{
    bool isValid = false;
        string message = “Employee first and last name”;
    isValid = getString(message, name);

    if (isValid)
    {
        isValid = getNumber(“Hours worked”, MIN_HOURS, MAX_HOURS, hours);
    }
    if (isValid)
    {
        isValid = getNumber(“Pay rate”, MIN_RATE, MAX_RATE, rate);
    }

    return isValid;
}

float calculateGrossPay(float hours, float rate)
{
    return hours * hours;
}

void calculateNetpay(float grossPay, float & taxRate, float & netPay)
{
    if (grossPay >= RANGE0 && grossPay <= RANGE1)
    {
        taxRate = RATE1;
    }
    else if (grossPay < RANGE2)
    {
        taxRate = RATE2;
    }
    else if (grossPay < RANGE3)
    {
        taxRate = RATE3;
    }
    else if (grossPay < RANGE4)
    {
        taxRate = RATE4;
    }
    else
    {
        taxRate = RATE4;
    }
    netPay = grossPay * (1 – taxRate);
}

PayInput.cpp

#include “common.h”

enum Menu_Select
{
    CalculatePay = 1,
    File = 2,
    Terminate = 3
};

int menu_select()
{
    int selection;
    displaySeparator();
    cout << “Please select one of the following options:” << endl;
    cout << “\t” << numToString(CalculatePay) << “. \t” << menu_item(CalculatePay) << endl;
    cout << “\t” << numToString(File) << “. \t” << menu_item(File) << endl;
    cout << “\t” << numToString(Terminate) << “. \t” << menu_item(Terminate) << endl;
    cout << “Selection: “;
    cin >> selection;

    return selection;
}

string menu_item(int item)
{
    string value = “”;
    switch (item)
    {
            case CalculatePay:
        value = “Calculate Employee Pay”;
        break;
            case File:
        value = “Process File”;
        break;
            case Terminate:
        value = “Exit application”;
        break;
    default:
        value = “Unknown selection”;
        break;
    }

    return value;
}

PayOutput.cpp

#include “common.h”
void displayDirections()
{
displaySeparator();
setOutputFormat(2);

cout << “Please enter the following: ” << endl;
cout << “\tEmployee name” << endl;
cout << “\tHours worked between during the pay period” << endl;
cout << “\tHourly pay rate” << endl;

displaySeparator();
//clearBuffer();

}

void displayPayInformation(string name, float hours, float rate, float grossPay, float taxRate, float netPay)
{
displaySeparator();
setOutputFormat(2);
cout << “The pay information is: ” << endl;
cout << “\tName: ” << name << endl;
cout << “\tHours Worked: ” << hours << ” hours” << endl;
cout << “\tHourly Pay Rate: $” << rate << endl;
cout << “\tGross Pay $” << grossPay << endl;
cout << “\ttaxRate$” << taxRate << endl;
cout << “\tNet pay: $” << netPay << endl;
}

PresentationHelpers.cpp

#include “common.h”
using namespace std;

void displayApplicationInformation(string appName, string directions)
{
    string message = “”;
    displaySeparator();
    message = “Welcome to ” + appName + “\n”
        + directions + “\n”;
    cout << message;
    displaySeparator();
}
void displayErrorMessage(string source, string errMsg, string directions)
{
    string message;
    displaySeparator();
    message = “Error Source: ” + source + “\n”
        += errMsg + “\n”
        += directions;
    cout << message;
    soundError();
    displaySeparator();
}
void displaySeparator()
{
    string separator = “__________________________________________________”;
    cout << endl << separator << endl;
}
void clearBuffer()
{
    cin.ignore();
    cin.clear();
    fflush(stdin);
}
void setOutputFormat(int precision)
{
    cout.setf(ios::fixed);
    if (precision > 0)
    {
        cout.setf(ios::showpoint);
    }
    else
    {
        std::noshowpoint;
    }
    cout.precision(precision);
}
void soundError()
{
    cout << “\a”;
}
void terminateApplication(string appName)
{
    char ch;
    displaySeparator();
    cout << “Quitting ” << appName << endl;
    cout << “Enter any key to exit: “;
    cin >> ch;
    exit(0);
}
bool getNumber(string field, int lowerBound, int upperBound, int& value)
{
    bool isValid = true;
    int count = 0;
    int input = 0;
    string message;
    do
    {
        cout << “Enter the ” << field << ” value between ” << lowerBound << ” and ” << upperBound << “: “;
        cin >> value;
        count++;
        if (value >= lowerBound && value <= upperBound)
        {
            isValid = true;
        }
        else
        {
            isValid = false;
            message = “You entered ” + numToString(value) + field
                + “\nThe ” + field + ” value needs to be between “
                + numToString(lowerBound)
                + ” and “
                + numToString(upperBound);
            displayErrorMessage(field + “: getNumber()_Int”, “Invalid input”, message);
        }

    } while (isValid == false && count <= MAX_COUNT);
    clearBuffer();

    return isValid;
}
bool getNumber(string field, float lowerBound, float upperBound, float & value)
{
    bool isValid = true;
    int count = 0;
    float input = 0;
    string message;
    do
    {
        cout << “Enter the ” << field << ” value between ” << lowerBound << ” and ” << upperBound << “: “;
        cin >> value;
        count++;
        if (value >= lowerBound && value <= upperBound)
        {
            isValid = true;
        }
        else
        {
            isValid = false;
            message = “You entered ” + numToString(value) + ” for ” + field
                + “\nThe ” + field + ” value needs to be between “
                + numToString(lowerBound)
                + ” and “
                + numToString(upperBound);

            displayErrorMessage(field + “: getNumber()_Float”, “Invalid input”, message);
        }

    } while (isValid == false && count <= MAX_COUNT);
    clearBuffer();

    return isValid;
}
bool getString(string field, string& value)
{
    bool isValid = false;
    int count = 0;
    string message;
    clearBuffer();
    do
    {
        count++;
        cout << “Enter the ” << field << “: “;
        getline(cin, value);
        if (value.empty())
        {
            isValid = false;
            message = “You entered an empty value for ” + field;

            displayErrorMessage(field + “: getString()”, “Invalid input”, message);
        }
        else
        {
            isValid = true;
        }

    } while (isValid == false && count <= MAX_COUNT);

    return isValid;
}
string numToString(double value)
{
    ostringstream convert;
    convert << value;
    return convert.str();
}
bool keepGoing(string directions)
{
    bool keep = true;
    char ch;

    cout << directions << ”  Enter N to stop, any other key to continue: “;
    cin >> ch;
    if (ch == ‘n’ || ch == ‘N’)
    {
        keep = false;
    }
    return keep;

}

Output:

logical programming structure

LEAVE A REPLY

Please enter your comment!
Please enter your name here