**Note - Any updates to the description will be found on the ZyBook description of this project.
Pete’s Pets has four types of employees.
Managers, who receive a fixed weekly salary
Clerks, who receives a fixed hourly wage for up to the first 40 hours they work and "time-and-a-half," i.e., 1.5 times their hourly wage, for overtime hours worked
Vets, who receive 800plus3.75 for each vaccine they administer
Stylists, who receive a fixed amount of money per appointment.
Create a program that calculates the weekly pay for employees of "Pete's Pets", the total payroll and print's out a Weekly Pay Report.
You do not know the number of employees in the file in advance.
Each type of employee has its own pay code:
Managers have paycode 1
Clerks have code 2
Vets have code 3
Stylists have code 4
Input will come from a sequential file that has a line for each employee.
The first item in the line will always be the paycode.
The second and third item will be the first and last name.
The fourth item will be the employee ID.
For a paycode of 1, the salary will be the next item. If the pay code is 2, hourly salary is the next item followed by hours worked. If the pay code is 3, the next item will be the number of vaccines administered. If the pay code is 4, the next item will be the payrate per appointment followed by the number of appointments. All items in a line of the file are separated by a space.
You have been provided with the following classes:
Employee.h (simple class, no need for .cpp file). Implement the < and > operators. Use the employee number to determine > or <.
main.cpp (driver class)
test.cpp (this is used for the autograding)
testing.cpp (this is the one to run on the gl server and get a valgrind report on)
Requirements:
Implement 4 derived classes that inherit from Employee (these are your Data Element classes). An empty .h and .cpp file have been created for each of these.
(a) Manager -
(b) Clerk
(c) Stylist
(d) Vet
They will contain variables appropriate for the type of employee. They inherit from Employee and define the virtual method(s). There should also be getters for private members unique to each class, and any other methods that are needed for your design. The parameterized constructor should call the constructor of the Employee class with member initialization syntax. Provide a copy constructor. The toString method will override the Employee toString method and use the Employee::toString method within the implementation. Provide a destructor. Use const to represent any constants.
Assistant
Create a Data Manager class called Employees. You have been provided with the .h file and an empty .cpp file. It will contain a vector that holds pointers to Employee objects that will accommodate pointers to Manager, Clerk, Stylist and Vet objects. Add any other variables needed for your design including numManagers, numClerks, numStylist and numVets, which will be incremented when you add an Employee of this type. Employees will contain the following functions:
(a) void addEmployee(string). The string passed in will be one line from the input file. Use the paycode to determine what is to be extracted from the string. Create the correct type of object and add it to the vector.
(b) double calculateWeeklyPay(). Returns the sum of weekly pay for all objects in the vector.
(c) string printReport(). Returns a string with the Weekly Pay Report. The employees are sorted by employee number in the report. (The necessary methods and overloaded operators needed for the sort are provided for you.)The Employee Number and First and Last Names are printed in a column of width 25. The Weekly Pay is printed in a column of width 10. Total Payroll will be printed as well as the number of each type of employee. Look at the example output to see the details of the Weekly Pay Report. All pay must be printed with 2 decimal places.
(d) Rule of 3 - Copy constructor, Destructor, operator= . You will need the clone() for the copy constructor and =.
(e) Default constructor
Any additional functions you may need for your design must be private utility functions. **** Make sure any .h files you need for the derived classes are put in the Employees.h, not the Employees.cpp
main.cpp will read from the file and print out the report.
Given the following input file:
4 Paul Johnson 44444 8.75 75
1 Eric Hall 11111 1200.00
3 Katie Bell 33333 55
2 Patrick Brown 22222 12.50 50
The following report will be printed:
Weekly Pay Report for Pete's Pets
Employee Weekly Pay
11111 Eric Hall 1200.00
22222 Patrick Brown 687.50
33333 Katie Bell 1006.25
44444 Paul Johnson 656.25
Total payroll: 3550.00
Managers: 1
Clerks: 1
Vets: 1
Stylist: 1
Given the following file:
4 Paul Johnson 44444 8.75 75
2 Erica Smith 12345 8.75 35
1 Eric Hall 11111 1200.00
4 Paula Potter 34567 12.25 43
3 Katie Bell 33333 55
2 Patrick Brown 22222 12.50 50
3 Kelly Myers 23456 22
3 Karen Miller 45678 34
The following report will be printed:
Weekly Pay Report for Pete's Pets
Employee Weekly Pay
11111 Eric Hall 1200.00
12345 Erica Smith 306.25
22222 Patrick Brown 687.50
23456 Kelly Myers 882.50
33333 Katie Bell 1006.25
34567 Paula Potter 526.75
44444 Paul Johnson 656.25
45678 Karen Miller 927.50
Total payroll: 6193.00
Managers: 1
Clerks: 2
Vets: 3
Stylist: 2
You have been provided with two data files - employees.txt and employeesA.txt You have been provided with two testing files - tests.cpp is for the autograding and testing.cpp is the one to test your program on the gl server and run a valgrind report. A makefile is provided. "make driver" will run main.cpp. "make testing" will run all the tests and give you a score. "make testMemory" is for the gl server to find memory leaks. "make test" will run the tests that are used for the autograding.
and currently stuck trying to get my project to pass this test
int testPassedPrintReportEmployee()
{
int returnResult = 1;
string expectedString = "";
string student = "";
Employees employees;
code
ifstream in;
string line;
string fileName = "employees.txt";
string result;
char result1[80];
int empNum;
double total;
in.open(fileName);
if (!in)
{
student = "Could not open file: " + fileName;
expectedString = "Opened file: " + fileName + "\n";
printResultsStr(returnResult, expectedString, student);
return 1;
}
// read a line and add to employees
while (!in.eof())
{
getline(in, line);
employees.addEmployee(line);
}
// report
istringstream inp(employees.printReport());
inp.getline(result1, 80, '\n'); //"Weekly Pay Report for Pete's Pets"
inp.getline(result1, 80, '\n'); // empty line
inp.getline(result1, 80, '\n'); //"Employee Weekly Pay"
inp >> empNum;
if (empNum != 11111)
{
expectedString = "First employee number should be 11111\n";
student = empNum;
}
else
{
inp.getline(result1, 80, '\n'); // get the rest of the line
inp >> empNum;
if (empNum != 22222)
{
expectedString = "Second employee number should be 22222\n";
student = empNum;
}
else
{
inp.getline(result1, 80, '\n'); // get the rest of the line
inp >> empNum;
if (empNum != 33333)
{
expectedString = "Third employee number should be 33333\n";
student = empNum;
}
else
{
inp.getline(result1, 80, '\n'); // get the rest of the line
inp >> empNum;
if (empNum != 44444)
{
expectedString = "Fourth employee number should be 44444\n";
student = empNum;
}
else
{
inp.getline(result1, 80, '\n'); // get the rest of the line
inp.getline(result1, 80, '\n'); // empty line
inp >> result; //"Total"
inp >> result; //"payroll"
inp >> total;
if (((total - 3550.00) > .01))
{
expectedString = "total: 3500.00\n";
student = "total: " + to_string(total);
}
else
{
inp >> result; //"Managers"
inp >> total;
if (total != 1)
{
expectedString = "Managers: 1\n";
student = "Managers: " + to_string(total);
}
else
{
inp >> result; //"Clerks"
inp >> total;
if (total != 1)
{
expectedString = "Clerks: 1\n";
student = "Clerks: " + to_string(total);
}
else
{
inp >> result; //"Vets"
inp >> total;
if (total != 1)
{
expectedString = "Vets: 1\n";
student = "Vets: " + to_string(total);
}
else
{
inp >> result; //"Sylist"
inp >> total;
if (total != 1)
{
expectedString = "Stylist: 1\n";
student = "Stylist: " + to_string(total);
}
else returnResult =0;
}
}
}
}
}
}
}
}
printResultsStr(returnResult, expectedString, student);
return returnResult;
}
it's currently returing this
./test testPassedPrintReportEmployee
Test Name: testPassedPrintReportEmployee
FAILED this test
Expected result:
First employee number should be 11111
Your result:
╬
It sounds like you're encountering an issue where your printReport function isn't producing the expected output, specifically failing to correctly identify the first employee number as 11111. The unexpected character (╬) suggests that the parsing or sorting of employee data might not be functioning as intended. Let's walk through some potential causes and solutions to help you debug and resolve this issue.
1. Reading the Input File Correctly
Issue: Improper File Reading Loop
Your test function uses the following loop to read the input file:
cpp
while (!in.eof())
{
getline(in, line);
employees.addEmployee(line);
}
Problem: Using while (!in.eof()) can lead to reading an extra empty line at the end of the file, which might cause addEmployee to process an empty string and create an invalid Employee object.
Solution: Use Proper Loop Conditions
Change your loop to ensure that only valid lines are read and processed:
cpp
while (getline(in, line))
{
if (!line.empty()) // Optional: Skip empty lines
{
employees.addEmployee(line);
}
}
Benefits:
Prevents processing an empty line.
Ensures that addEmployee is only called with valid data.
2. Implementing Comparison Operators Correctly
Issue: Incorrect < and > Operator Overloads
The Employees class relies on the comparison operators (< and >) to sort employees by their employee number (empNum). If these operators aren't correctly implemented in the Employee base class, sorting will fail, leading to incorrect report ordering.
Solution: Correctly Implement Comparison Operators in Employee.h
Ensure that your Employee class has the comparison operators implemented based on empNum. Here's how you can define them:
Ensure that empNum is correctly initialized in all derived classes.
3. Ensuring Proper Sorting in printReport
Issue: Employees Not Being Sorted Before Reporting
If the printReport function doesn't sort the employees by empNum, the report might display employees in the order they were added, which may not match the expected sorted order.
Solution: Sort Employees Before Generating the Report
In your Employees class, before generating the report, sort the vector of employee pointers. Here's how you can implement it:
Sorting: Uses std::sort with a lambda that dereferences the Employee pointers and compares them using the overloaded < operator.
Formatting: Ensures that employee details and payroll figures are formatted correctly.
Totals: Calculates and appends total payroll and counts of each employee type.
4. Validating addEmployee Implementation
Issue: Incorrect Parsing Based on Paycode
If addEmployee doesn't correctly parse the input line based on the paycode, employees might not be created with the correct details, leading to incorrect empNum or other attributes.
Solution: Carefully Parse Each Line Based on Paycode
Ensure that addEmployee correctly interprets each line and creates the appropriate derived class object. Here's an example implementation:
cpp
// Employees.cpp
#include "Employees.h"
#include "Manager.h"
#include "Clerk.h"
#include "Vet.h"
#include "Stylist.h"
#include <sstream>
void Employees::addEmployee(const std::string& line) {
std::istringstream iss(line);
int paycode;
std::string firstName, lastName;
int empNum;
iss >> paycode >> firstName >> lastName >> empNum;
switch (paycode) {
case 1: { // Manager
double salary;
iss >> salary;
employees.push_back(new Manager(empNum, firstName, lastName, salary));
numManagers++;
break;
}
case 2: { // Clerk
double hourlyWage;
double hoursWorked;
iss >> hourlyWage >> hoursWorked;
employees.push_back(new Clerk(empNum, firstName, lastName, hourlyWage, hoursWorked));
numClerks++;
break;
}
case 3: { // Vet
int vaccinesAdministered;
iss >> vaccinesAdministered;
employees.push_back(new Vet(empNum, firstName, lastName, vaccinesAdministered));
numVets++;
break;
}
case 4: { // Stylist
double payPerAppointment;
int numAppointments;
iss >> payPerAppointment >> numAppointments;
employees.push_back(new Stylist(empNum, firstName, lastName, payPerAppointment, numAppointments));
numStylist++;
break;
}
default:
// Handle unknown paycode if necessary
break;
}
}
Key Points:
Parsing: Correctly parses each line based on the paycode.
Object Creation: Instantiates the correct derived class with appropriate parameters.
Counters: Increments the relevant employee type counters (numManagers, numClerks, etc.).
5. Ensuring toString Methods Are Correct
Issue: Incorrect toString Implementations
If the toString method in each derived class doesn't correctly represent the employee's information, the printReport function may produce malformed output.
Solution: Implement toString in Each Derived Class Appropriately
Ensure similar implementations for other derived classes, appending their specific payment details.
6. Additional Debugging Steps
a. Verify Employee Numbers
Ensure that all employees have unique and correctly parsed empNum values. Any duplication or misparsing can lead to sorting issues.
b. Check for Memory Leaks and Proper Destruction
Since you're using dynamic memory allocation (new), ensure that your Employees class properly deletes all Employee objects in its destructor to prevent memory leaks.
Ensure that your employees.txt and employeesA.txt files strictly follow the expected format without any additional spaces or unexpected characters.
d. Add Debug Statements
Incorporate debug std::cout statements within addEmployee, printReport, and other critical functions to monitor the flow of data and identify where things might be going wrong.
Note: Implement similar classes for Clerk, Vet, and Stylist with their specific payment calculations.
Employees.h
cpp
#ifndef EMPLOYEES_H
#define EMPLOYEES_H
#include "Employee.h"
#include <vector>
#include <string>
class Employees {
private:
std::vector<Employee*> employees;
int numManagers;
int numClerks;
int numVets;
int numStylist;
public:
Employees() : numManagers(0), numClerks(0), numVets(0), numStylist(0) {}
void addEmployee(const std::string& line);
double calculateWeeklyPay() const;
std::string printReport() const;
// Rule of Three
Employees(const Employees& other);
Employees& operator=(const Employees& other);
~Employees();
// Additional private utility functions as needed
};
#endif
Employees.cpp
cpp
#include "Employees.h"
// Include derived classes headers
#include "Manager.h"
#include "Clerk.h"
#include "Vet.h"
#include "Stylist.h"
#include <sstream>
#include <algorithm>
#include <iomanip>
void Employees::addEmployee(const std::string& line) {
// Implementation as discussed earlier
}
double Employees::calculateWeeklyPay() const {
double total = 0.0;
for (const auto& emp : employees) {
total += emp->calculatePay();
}
return total;
}
std::string Employees::printReport() const {
// Implementation as discussed earlier
}
// Implement Rule of Three
Employees::Employees(const Employees& other) : numManagers(other.numManagers),
numClerks(other.numClerks), numVets(other.numVets), numStylist(other.numStylist) {
for (const auto& emp : other.employees) {
employees.push_back(emp->clone()); // Assuming a clone() method is implemented
}
}
Employees& Employees::operator=(const Employees& other) {
if (this != &other) {
// Clean up existing employees
for (auto emp : employees) {
delete emp;
}
employees.clear();
// Copy from other
numManagers = other.numManagers;
numClerks = other.numClerks;
numVets = other.numVets;
numStylist = other.numStylist;
for (const auto& emp : other.employees) {
employees.push_back(emp->clone()); // Assuming a clone() method is implemented
}
}
return *this;
}
Employees::~Employees() {
for (auto emp : employees) {
delete emp;
}
}
Important: Ensure that each derived class implements a clone method if you plan to use it in the copy constructor and assignment operator.
8. Final Checklist
Before rerunning your tests, go through the following checklist:
File Reading:
Use while (getline(in, line)) to read lines.
Skip empty lines if necessary.
Employee Creation:
Correctly parse each line based on paycode.
Instantiate the correct derived class with appropriate parameters.
Comparison Operators:
Ensure < and > are based solely on empNum.
Verify that empNum is correctly initialized for all employees.
Sorting:
Sort the employees by empNum before generating the report.
Report Generation:
Ensure printReport formats the report exactly as expected.
Validate that all monetary values display two decimal places.
Memory Management:
Implement the Rule of Three in the Employees class.
Ensure all dynamically allocated memory is properly deleted.
Derived Classes:
Verify that each derived class correctly calculates weekly pay.
Ensure toString methods return the expected string formats.
Testing:
Add debug statements to monitor the flow and data.
Compare your printReport output manually with expected results.
By systematically verifying each component of your implementation, you can identify where discrepancies are occurring and adjust your code accordingly. If after these steps the issue persists, consider sharing specific parts of your implementation (such as the addEmployee, printReport, and comparison operator overloads) for a more detailed review.