Program#9 c++ Your job is to update your payroll program for Armadillo Automotiv
ID: 3730536 • Letter: P
Question
Program#9
c++
Your job is to update your payroll program for Armadillo Automotive Group to use a C++ class. Each employee class object should hold the master file information for one employee. You can assume that the company has exactly 6 employees. Use an array of employee objects to hold the master file information for the company employees.
Do not put any pay information, including hours worked, in an Employee object. You might want to create a paycheck struct or object to hold pay information for one employee (this could include the hours worked).
DO NOT DO ANY INPUT OR OUTPUT IN ANY CLASS MEMBER FUNCTION.
The employee information and hours worked will come from input files instead of from the keyboard.
Employee class
Create a class to represent the master file information for one employee. Start with the following partial Employee class. You will need to add a "get" function for each class data member to return its value.
class Employee
{
private:
int id; // employee ID
string name; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
Employee( int initId=0, string initName="",
double initHourlyPay=0.0,
int initNumDeps=0, int initType=0 ); // Constructor
bool set(int newId, string newName, double newHourlyPay,
int newNumDeps, int newType);
};
Employee::Employee( int initId, string initName,
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName, initHourlyPay,
initNumDeps, initType );
if ( !status )
{
id = 0;
name = "";
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, string newName, double newHourlyPay,
int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
*Note that the constructor and set functions do validation on the data that is to be stored in the Employee object. They are similar to the validation in the Rectangle class from the textbook in Section 7.11 Focus on Software Engineering: Separating class Specification, Implementation and Client Code. For a more detailed discussion of validation for class objects, and the Employee class validation, see Employee Data Validation.
*You should be able to copy this class into your editor by highlighting the code, making a copy of it (ctrl-c in Windows), and then pasting the code into your editor window.
*Do not make any changes to the data members of the class. Do not add any new data members to the class. Do not make any changes to the constructor and set functions.
*To complete the class, add a "get" function for each of the private data members (that is, 5 functions). Each get function should return the value of a data member.
Program input
The program input consists of two files - a master file and a transaction file. Your code must work for the 2 input files provided. You may also want to test your program with other input data.
Master file
The master file has one line of input per employee containing:
*employee ID number (integer value)
*name (20 characters) - see Hint 6 below on how to input the name
*pay rate per hour (floating-point value)
*number of dependents (integer value)
*type of employee (0 for union, 1 for management)
This file is ordered by ID number and contains information for 6 emplyees. You can assume that there is exactly one space between the employee ID number and the name. You can also assume that the name occupies 20 columns in the file. Important: See the Requirements/Hints section at the bottom of this page for more information on the input files.
Transaction file (weekly timesheet information)
The transaction file has one line for each employee containing:
-number of hours worked for the week (floating-point value)
This file is also ordered by employee ID number and contains information for the 6 employees. Note: You can assume that the master file and the transaction file have the same number of records, and that the first hours worked is for the first employee, etc. You can also assume that the employee IDs in the master file are exactly the same as the employee IDs in the transaction file. Important: See the Requirements/Hints section at the bottom of this page for more information on the input files.
Calculations
*Gross Pay - Union members are paid 1.5 times their normal pay rate for any hours worked over 40. Management employees are paid their normal pay rate for all hours worked (they are not paid extra for hours over 40).
*Tax - All employees pay a flat 15% income tax.
*Insurance - The company pays for insurance for the employee. Employees are required to buy insurance for their dependents at a price of $20 per dependent.
*Net Pay is Gross Pay minus Tax minus Insurance.
Payroll Processing
Notice that when you store employee master information in an Employee object, the set() function does data validation. If any of the employee master information is invalid, the set() function stores default values in the Employee object. In particular, the ID of the employee is set to zero.
When processing the payroll:
*If the employee master information for the employee is invalid (if the ID is 0), print an appropriate error message on the screen and do not pay the employee. The employee should not appear in the Payroll Report.
*If the hours worked for an employee is invalid (less than 0.0), print an appropriate error message on the screen. The employee should not be paid and should not appear in the Payroll Report.
*When all employees have been processed, print on the screen the total number of transactions that were processed correctly during the run.
Payroll Report
This report should be printed to a file. It should not be printed on the screen. The payroll report should be printed in a tabular (row and column) format with each column clearly labeled. Do not use tabs to align your columns - you need to use the setw() manipulator. Print one line for each transaction that contains:
employee ID number
name
gross pay
tax
insurance
net pay
The final line of the payroll report should print the total gross pay for all employees, and the total net pay for all employees.
Requirements/Hints:
1.Global variables are variables that are declared outside any function. Do not use global variables in your programs. Declare all your variables inside functions.
2.A sample Master file and Transaction file can downloaded here:
master9.txt
trans9.txt
Your program must work correctly for these files.
Maybe the best way to copy a file to your computer is to right-click on the link, then choose "Save As" or "Save Link As" from the popup menu. Optionally you may be able to open the text file in your browser and select "Save As" or "Save Page As" from your browser menu.
If you create your own test files in a text editor, be sure to press the enter key after the last line of input and before you save your text. If you choose to copy the text from my sample file and paste it into a text editor, be sure to press the enter key after the last line of input and before you same your text.
3.Use the C++ string class to hold the employee name.
4.You should use an Employee class object to hold the master file information for one employee.
5.The Payroll Report should be written to a file.
6.Notes on reading C++ string objects:
The getline function is first introduced in Chapter 3 and then covered more thoroughly in the Files chapter. The C++ code:
string name;
getline( cin, name );
will read all characters up to the end of the line (to the first newline character in the input stream). You can specify a character other than the newline character to stop the input. For example:
getline( cin, name, '#' );
will read all characters until the '#' is found in the input. The Transaction file uses the '#' to mark the end of the names so that they are all 20 characters long.
Note that you can use getline with input file streams by replacing cin with the input file object.
Explanation / Answer
#include <iostream> // I/O Declarations
#include <iomanip> // I/O Manipulators
#include <fstream> // Filestream objects
#include <string> // stream objects
using namespace std;
struct EmployeePay
{
int employeeId; // employee ID
float hoursWorked; // employee hours worked
float grossPay; // employee gross pay
float tax; // employee tax
int insurance; // employee insurance cost
float netPay; // employee net pay
};
class Employee
{
private:
int id; // employee ID
string name; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
Employee( int initId=0, string initName="",
double initHourlyPay=0.0,
int initNumDeps=0, int initType=0 ); // Constructor
bool set(int newId, string newName, double newHourlyPay,
int newNumDeps, int newType);
int getId() // get employee id
{ return id; }
string getName() // get employee name
{ return name; }
double getHourlyPay() // get employee hourly pay
{ return hourlyPay; }
int getNumDep() // get number of dependants on insurance
{ return numDeps; }
int getType() // get type of employee. 1 for management 0 for union
{ return type; }
};
Employee::Employee( int initId, string initName,
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName, initHourlyPay,
initNumDeps, initType );
if ( !status )
{
id = 0;
name = "";
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, string newName, double newHourlyPay,
int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
void calculateGrossPay(EmployeePay [], Employee[]); // function prototypes
void calculateNetPay(EmployeePay [], Employee[]);
void displayPayRoll(EmployeePay [], Employee[]);
int main()
{
int tempId; // store the id read in by the program
string tempName; // store the name read in by the program
double tempHourlyPay; // store the hourly pay read in by the program
int tempNumDeps; // store the number of dependants read in by the program
int tempType; // store the type of employee read in by the program
int sucCount = 0; // count the number of employees successfully processed
ifstream inputFile; // file objects for input
ifstream inputFile2;
Employee employees[6]; // array of employee objects
EmployeePay payData[6]; // array of pay structures
inputFile.open("master9.txt"); // open a file
if (inputFile)
{
for(int count = 0; count < 6; count++) // read in employee data from the file
{
inputFile >> tempId;
getline( inputFile, tempName, '#' );
inputFile >> tempHourlyPay;
inputFile >> tempNumDeps;
inputFile >> tempType;
employees[count].set(tempId, tempName, tempHourlyPay, // store the data read in into the objects
tempNumDeps, tempType);
if(tempId <= 0) // validation to ensure employee id is positive
{
cout << "Error. Employee " << tempName << endl;
cout << "has employee id less than or equal to 0. this employee will be skipped on the payroll. " << endl;
}
}
inputFile.close(); // close the file
}
else
cout << "Error opening the file master9.txt ";
inputFile2.open("trans9.txt"); // open another file object for reading
if (inputFile2)
{
for(int count = 0; count < 6; count++) // read in employee data again, this time for payroll
{
inputFile2 >> payData[count].employeeId;
inputFile2 >> payData[count].hoursWorked;
if(payData[count].hoursWorked <= 0) // validation to ensure hours worked is positive
{
cout << "Error. Employee " << employees[count].getName() << endl;
cout << "Has negative or 0 hours worked. This employee will be skipped on the payroll. " << endl;
}
else
sucCount++; // count the number of employees successfully processed
}
inputFile2.close(); // close the file
cout << sucCount << " employees successfully processed. " << endl;
}
else
cout << "Error opening the file trans9.txt " << endl;
calculateGrossPay(payData, employees); // Calculate employee gross pay
calculateNetPay(payData, employees); // calculate employee net pay
displayPayRoll(payData, employees); // display employee payroll
system("pause");
return 0;
}
// function: calculate gross pay
// purpose: to calculate the gross play of every valid employee processed
void calculateGrossPay(EmployeePay payInfo[], Employee numEmps[])
{
float bonusHours = 0.0; // bonus hours for union employees who worked overtime
float bonusPay = 0.0; // bonus pay for union employees who worked overtime
// calculate gross pay for the employees
for(int count = 0; count < 6; count++)
{
if(numEmps[count].getId() > 0) // if the employee's id is valid
{
if(numEmps[count].getType() == 0 && payInfo[count].hoursWorked > 40) // if the employee worked more than 40 hours and is union
{
bonusHours = payInfo[count].hoursWorked - 40;
bonusPay = bonusHours * (numEmps[count].getHourlyPay() * 1.5);
payInfo[count].grossPay = numEmps[count].getHourlyPay() * 40 + bonusPay; // calculate gross pay + bonus
}
else
payInfo[count].grossPay = payInfo[count].hoursWorked * numEmps[count].getHourlyPay(); // calculate regular gross pay
}
}
}
// function: calculate net pay
// purpose: to calculate the net pay of each employee
void calculateNetPay(EmployeePay payInfo[], Employee numEmps[])
{
// calculate tax
for(int count = 0; count < 6; count++)
{
if(numEmps[count].getId() > 0 && payInfo[count].grossPay > 0) // if the employee's id and pay are valid
payInfo[count].tax = payInfo[count].grossPay * .15; // calculate tax
}
// calculate insurance
for(int count = 0; count < 6; count++)
{
if(numEmps[count].getId() > 0 && payInfo[count].grossPay > 0) // if the employee's id and pay are valid
payInfo[count].insurance = numEmps[count].getNumDep() * 20; // calculate insurance
}
// calculate net pay
for(int count = 0; count < 6; count++)
{
if(numEmps[count].getId() > 0 && payInfo[count].grossPay > 0) // if the employees id and pay are valid
payInfo[count].netPay = payInfo[count].grossPay - (payInfo[count].tax + payInfo[count].insurance); // calculate net pay
}
}
// function: Display Payroll
// Purpose: This function writes the employee payroll to a file
void displayPayRoll(EmployeePay payInfo[], Employee numEmps[])
{
float totalGross = 0.0; // Total employee gross pay
float totalNet = 0.0; // total employee net pay
ofstream outputFile;
outputFile << fixed << showpoint << setprecision(2);
outputFile.open("Payroll.txt");
outputFile << "ID" << setw(10) << "Name" << setw(20) << "Gross Pay" << setw(5) << "Tax" << setw(6) << "Insur" << setw(9) << "Net Pay" << endl;
for(int count = 0; count < 6; count++)
{
if(numEmps[count].getId() > 0 && payInfo[count].grossPay > 0) // output employee payroll data to the file
{
outputFile << left << setw(3) << numEmps[count].getId() << left << setw(20) << numEmps[count].getName()
<< setw(8) << payInfo[count].grossPay << setw(8) << payInfo[count].tax << setw(6) << payInfo[count].insurance
<< setw(8) << payInfo[count].netPay << endl;
totalGross += payInfo[count].grossPay;
totalNet += payInfo[count].netPay;
}
}
outputFile << " Total Employee gross pay is:" << right << setw(8) << totalGross << endl; // output total gross pay to file
outputFile << "Total Employee net pay is" << setw(3) <<":" << right << setw(8) << totalNet << endl; // output total net pay to file
outputFile.close(); // close the file
}
trans.txt
5 40.0
15 42.0
16 40.0
17 41.5
18 -40.0
22 20.0
master9.txt
5 Christine Kim # 30.00 3 1
15 Ray Allrich # 10.25 0 0
16 Adrian Bailey # 12.50 0 0
17 Juan Gonzales # 30.00 1 1
18 J. P. Morgan # 8.95 0 0
22 Cindy Burke # 15.00 1 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.