Home‎ > ‎Assignments‎ > ‎Hour of Code‎ > ‎Solutions‎ > ‎

Payroll 2 | Solution

Dataset:

111111111 Guiliani Rude 40.0 14.000
555555555 Kramer Cosmo 35.0 15.75
333333333 Brady Marcia 50.0 14.00
777777777 Lane Lois 43.2 12.25
999999999 Stalin Joey 52.5 17.50

 

Program:

// payroll2.cpp
// Employee Payroll Report - The Sequel
// uses an employee class
// non-multifile version
// Prof. Linda W. Friedman
// Object-Oriented Programming 
// Baruch College Zicklin School of Business

#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
ofstream outReport;

 

///////////////////////////////// Employee class definition ///////////////////
class Employee {

public:
Employee();
int readEmployeeData();
void calcSalary();
void displayEmployeeData();
float getPayRate();
float getHours();
string getEmpID();
string getLname();
string getFname();
float getOvtHours();
float getOvtSal();
float getSalary();

private:
string empID;
string lastName, firstName;
float payRate, hours;
float regHours, ovtHours, ovtSal, salary;
ifstream infile;
};

////////////////////////////// Employee class implementation //////////////////

Employee::Employee(){
infile.open ("f:\\cplus\\datafiles\\employees.dat", ios::in);
if (!infile) cerr << "Error: could not open input file\n";
}

int Employee::readEmployeeData(){
if (infile >> empID >> lastName >> firstName >> hours >> payRate) return 1;
else return 0;
}

void Employee::calcSalary() {
if (hours > 40) {regHours = 40; ovtHours = hours - 40;}
else {regHours = hours; ovtHours = 0;}
ovtSal =
(ovtHours <= 10) ?
ovtHours*1.5*payRate :
(1.5*payRate*10 + 2*payRate*(ovtHours-10));
salary = regHours * payRate + ovtSal; 
}

void Employee::displayEmployeeData(){
//This function, when completed, will display to the console window
//information about one employee object
}

//access functions
float Employee::getPayRate() {return payRate;}
float Employee::getHours() {return hours;}
string Employee::getEmpID() {return empID;}
string Employee::getLname() {return lastName;}
string Employee::getFname() {return firstName;}
float Employee::getOvtHours() {return ovtHours;}
float Employee::getOvtSal() {return ovtSal;}
float Employee::getSalary() {return salary;}

 

void insertDetail(Employee& e, int n){

outReport << setiosflags (ios::fixed) << setprecision(2)
<< setiosflags (ios::left) << setw (7)
<< e.getFname() << setw (8) << e.getLname()
<< setiosflags(ios::right) << setw (10) << e.getPayRate()
<< setw (10)<< e.getHours() << setw (10)<< e.getOvtHours()
<< setw (10)<< e.getOvtSal() << setw (10) << e.getSalary() << endl;
}


/////////////////////////////////// functions declared////////////

void PayrollReport (string, int);
void printReportHeadings(string,int);
void printColumnHeadings();
void printaLine(int, char);
void skipLines (int);
void indent (int);
void insertDetail (Employee&);
void printTotals (float, float, float, float, int);
void printSummaryInfo (int, int);

void PayrollReport(string period, int linesize){
printReportHeadings(period, linesize);
}

 

void printReportHeadings(string period, int linesize){
printaLine (linesize, ':');
indent(15);
outReport << "School of Business Employee Roster\n";
printaLine (linesize, ':');
skipLines(1);
indent (20);
outReport << "Employee Payroll Report" << endl;
indent (23);
outReport << "Period: " << period <<endl;
skipLines(1);
printaLine (linesize, '.');
skipLines(1);
printColumnHeadings();
printaLine (linesize, '.');
}

void printColumnHeadings(){
outReport << setiosflags(ios::right)
<< setw (15) << ' ' << setw (10) << "Hourly"
<< setw(10) <<" " << setw(10) << "Overtime"
<< setw(10) << "Overtime" << setw(10) <<"Total" << endl
<< setiosflags(ios::left) << setw (15) << "Name"
<< setiosflags(ios::right)
<< setw (10) << "Rate" << setw(10) << "Hours" << setw(10) << "Hours"
<< setw(10) << "Salary" << setw(10) <<"Salary" << endl;
}

void printaLine(int n, char c){
for (int i=1; i<=n; i++)
outReport << c;
outReport << endl;
}

void skipLines (int n){
for (int i=1; i<=n; i++)
outReport << endl;
}

void indent (int n){
for (int i=1; i<=n; i++)
outReport << ' ';
}

void printTotals (float totHours, float totOvtHours, float totOvtSal, float totSalary, int linesize){
printaLine (linesize, '.');
outReport << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setw(35) << totHours
<< setw(10) << totOvtHours
<< setw(10) << totOvtSal
<< setw(10) << totSalary << endl;
}

void printSummaryInfo(int n, int linesize){
skipLines (2);
outReport << "Number of employees this period: " << n << endl;
printaLine (linesize, ':');
}

 

 

 

//////////////////////////////////// main ////////////////////////////////////

int main (){
string period;
int n;
float totHours, totOvtHours, totOvtSal, totSalary;
int linesize = 65;

outReport.open("f:\\cplus\\reports\\payroll.txt", ios::out);
cout << "\n\nWhat pay period will this report cover? (no spaces please) ";
cin >> period;
PayrollReport(period, linesize);

Employee emp;
n = 0; 
while (emp.readEmployeeData()) {
if (n == 0 ) {totHours = totOvtHours = totOvtSal = totSalary = 0;}
emp.calcSalary();
insertDetail(emp, n);
totOvtHours += emp.getOvtHours();
totHours += emp.getHours();
totOvtSal += emp.getOvtSal();
totSalary += emp.getSalary();
n++;
} //end while

printTotals(totHours, totOvtHours, totOvtSal, totSalary, linesize);
printSummaryInfo(n, linesize);

cout << "\n\nEmployee Payroll Report is in the file f:\\cplus\\reports\\payroll.txt"
<<"\n\nClose console window? "; char c; cin>>c;
return 0;
}

 

Output:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
               School of Business Employee Roster
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                    Employee Payroll Report
                       Period:  Fall1999

.................................................................

                   Hourly            Overtime  Overtime     Total
Name                 Rate     Hours     Hours    Salary    Salary
.................................................................
Rude   Guiliani     14.00     40.00      0.00      0.00    560.00
Cosmo  Kramer       15.75     35.00      0.00      0.00    551.25
Marcia Brady        14.00     50.00     10.00    210.00    770.00
Lois   Lane         12.25     43.20      3.20     58.80    548.80
Joey   Stalin       17.50     52.50     12.50    350.00   1050.00
.................................................................
                             220.70     25.70    618.80   3480.05


Number of employees this period:  5
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 

Comments