Solution to Employees Breaking Down

Programming Assignment

Employees Breaking Down

Solution

(output is below program)

Program:

//ctrlbreak.cpp

//Employee Breakdown

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string>

using namespace std;

///////////////////////////// report class definition ////////////////////////

class Report {

public:

Report(string);

void printPersonalInfo();

void printReportHeading();

void printPageHeading();

void printColumnHeadings();

void printDetailLine(string, string, string, string, string);

void printPageEnd();

void printDeptFooter();

void printReportSummary();

void newPage();

void PrintaLine(int, char);

void SkipLines (int);

void indent (int);

string getCurrentDept(); //access function

void updateEmployeeTotals(); //modifier functions

void setCurrentDept(string);

private:

int linesize;

int numLines;

int linesMax;

int pageNum;

int totalEmployees;

int totalByDept;

string reportPeriod;

string currentDept;

string deptName[10];

int deptTotal[10];

short d;

ofstream outReport;

}; // end Report class definition

////////////////////////////// report class implementation ////////////////////

Report::Report (string p){

reportPeriod = p;

linesize = 65;

linesMax = 20;

numLines = 0;

pageNum = 0;

totalEmployees = 0;

totalByDept = 0;

d = -1;

outReport.open("f:\\cplus\\reports\\DeptBreakdown.doc", ios::out);

outReport << setiosflags (ios::left);

printReportHeading();

}

void Report::printPersonalInfo(){

outReport << "Prof. Linda W. Friedman\n"

<< "Baruch College Zicklin School of Business\n"

<< "CIS 4100 / CIS 9410\n";

}

void Report::printReportHeading(){

//separate header page for "Employee Listing Report"

printPersonalInfo();

SkipLines (8);

PrintaLine (linesize, ':');

SkipLines(8);

indent(15);

outReport << "Klingon Enterprises Employee Roster\n";

SkipLines(1);

indent(26);

outReport << "By Department\n";

SkipLines(8);

PrintaLine (linesize, ':');

SkipLines (7);

outReport << "This report contains a listing of\n"

<< "Klingon Enterprises employees by department.\n\n"

<< "Report period: " << reportPeriod << endl;

SkipLines(2);

outReport << "The last page of this report contains"

<< "summary information by department.\n";

}

void Report::printPageHeading(){

outReport << endl;

PrintaLine (linesize, ':');

outReport << "Klingon Enterprises Employee Roster";

indent (20);

outReport << "page " << pageNum << endl;

PrintaLine (linesize, ':');

SkipLines (5);

outReport << "Department: " << currentDept << endl;

SkipLines(1);

PrintaLine (linesize, '-');

printColumnHeadings();

PrintaLine (linesize, '-');

outReport << endl;

}

void Report::printColumnHeadings(){

outReport << setw (25) << "Employee ID" << setw (17) << "Name" << setw (14)

<< "Job Title" << setw(1) << "Location" << endl;

}

void Report::printDetailLine(string id, string fname, string lname, string job,

string loc){

outReport << setw (14) << id << fname << lname << job

<< setw(1) << " " << loc << endl;

totalByDept++;

numLines++;

if (numLines >= linesMax) { //new page?

printPageEnd();

newPage();

printPageHeading();

outReport << "... continued from previous page...\n";

} // end if new page

}

void Report::printPageEnd(){

SkipLines (1);

PrintaLine (linesize, '-');

SkipLines(1);

outReport << currentDept << " Department employee listing continues on the next page.\n";

}

void Report::printDeptFooter(){

SkipLines (3);

PrintaLine (linesize, ':');

outReport << "Number of employees in " << currentDept << " Department: "

<< totalByDept << endl;

PrintaLine (linesize, ':');

}

void Report::printReportSummary(){

outReport << endl;

PrintaLine (linesize, ':');

outReport << "Klingon Enterprises Employee Roster";

indent (20);

outReport << "page " << pageNum << endl;

PrintaLine (linesize, ':');

SkipLines (2);

indent(25);

outReport << "Report Summary" << endl;

indent(25);

outReport << "--------------" << endl;

SkipLines(10);

indent(20);

outReport << setw(20) << "Department" << "Total" << endl;

indent(20);

outReport << setw(20) << "----------" << "-----" << endl;

for (int i=0; i<=d; i++){

indent(20);

outReport << setiosflags(ios::left) << setw(15) << deptName[i]

<< setiosflags(ios::right) << setw(9) << deptTotal[i] << endl;

}

SkipLines(6);

outReport << setiosflags(ios::left)

<< "Total # employees in all Departments: " << totalEmployees;

SkipLines(10);

PrintaLine (linesize, ':');

outReport << "Report period:";

indent (35);

outReport << reportPeriod << endl;

PrintaLine (linesize, ':');

}

void Report::newPage(){

outReport << '\f';

pageNum++;

numLines = 0;

}

void Report::PrintaLine(int n, char c){

for (int i=1; i<=n; i++)

outReport << c;

outReport << endl;

}

void Report::SkipLines (int n){

for (int i=1; i<=n; i++)

outReport << endl;

}

void Report::indent (int n){

for (int i=1; i<=n; i++)

outReport << ' ';

}

string Report::getCurrentDept(){

return currentDept;

}

void Report::updateEmployeeTotals(){

deptTotal[d] = totalByDept;

totalEmployees += totalByDept;

totalByDept = 0;

}

void Report::setCurrentDept(string dept){

currentDept = dept;

d++;

deptName[d] = dept;

}

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

int main(){

string period;

string id, lname, fname, job, loc, dept;

cout << "Period? ";

cin >> period;

Report report (period); // create a report for this period

ifstream infile ("f:\\cplus\\datafiles\\depts.dat"); //open file for input

if (!infile) cerr << "Error: could not open input file\n"; //testing input file

else{ //read first record and start first department listing

infile >> id >> lname >> fname >> job >> loc >> dept;

report.setCurrentDept(dept);

report.newPage();

report.printPageHeading();

report.printDetailLine (id, fname, lname, job, loc);

while (infile >> id >> lname >> fname >> job >> loc >> dept){

if (dept != report.getCurrentDept()){ //new department?

report.printDeptFooter();

report.updateEmployeeTotals();

report.setCurrentDept(dept);

report.newPage();

report.printPageHeading();

} //end if new department

report.printDetailLine (id, fname, lname, job, loc);

} //end while

report.printDeptFooter();

report.updateEmployeeTotals();

report.newPage();

report.printReportSummary();

} //end if(!infile)

cout << "\n\nEmployee listing by department report is in the file \n"

<< "f:\\cplus\\reports\\DeptBreakdown.txt"

<< "\n\nClose console window? "; char c; cin >> c;

return 0;

}

Output: