Posted on

This is study resource 15 for the 24-week cohort during the summer of 2019. There is only one question for this study resource.

Click here to see all the study resources.

Question #1

 Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, hint, you need something extra. This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 

17 5.25 Daniel Katz 

18 6.75 John F. Jones 

Additionally we would like to be able to print payroll information from data in a different file. The data is the employee’s id number and a number of hours that they worked (example): 

17 40 

18 20 

18 20 

As you see we can have the same number listed twice in which case that person should be paid the sum of the numbers (John Jones did 40 hours work, but it’s listed as 20+20). 

You should start by reading in the first file and storing the data as objects in a linked list. You will need to create the linked list class and the Employee data class. You may choose to use the Linked List class we created, or you may opt to create your own doubly-linked list class. (Note: if you cannot get either working, you can use the STL list class but 20% will be deducted). The Linked list could be templated or not, it’s up to you, however templating it would allow it to be used for other projects, so it might be a good idea. 

Once you have read in the information from the first file, read in the second file. Ultimately we would like to print payroll information based on the hourly wage from the first file multiplied by the number of times an employee worked in the second file. How you do this is entirely up to you. 

The output must be in sorted (descending; so the person who gets paid most prints first) order in the form of: 

*********Payroll Information******** 

Daniel Katz, $270 

John F. Jones, $210 

*********End payroll************** 

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Employee{
public:
    Employee(int new_empId, double new_empHourlyRate, string new_empName);
    int getEmpId() const { return empId; }
    string getEmpName() const { return empName; }
    double  getEmpHourlyRate() const { return empHourlyRate; }

private:
    int empId;
    double empHourlyRate;
    string empName;

};

Employee::Employee(int new_empId, double new_empHourlyRate, string new_empName) {
    empId = new_empId;
    empHourlyRate = new_empHourlyRate;
    empName = new_empName;
}


struct Node{
    Employee *data;
    Node *next;
    Node *prev;
};


class LLinkedList{
public:
    LLinkedList() { start = nullptr; }
    void addEmployee(int id, double hourlyRate, string name);
    Node *getStart() const { return start; }

private:
    Node *start;
};

void LLinkedList::addEmployee(int id, double hourlyRate, string name) {
    Employee *e = new Employee(id, hourlyRate, name);
    Node *q = new Node;
    q->data = e;
    q->next = nullptr;
    q->prev = nullptr;
    if(start == nullptr){
        start = q;
        return;
    }else{
        Node *p = start;
        while(p->next != nullptr){
            p = p->next;
        }
        p->next = q;
        q->prev = p;
    }

}

void openInputFile(ifstream& inFile) {
    string filename;
    cout << "What filename would you like to open: ";
    cin >> filename;
    inFile.open(filename);
    while (!inFile) {
        cout << "Bad filename!" << endl;
        cout << "What filename would you like to open: ";
        cin >> filename;
        inFile.clear(); //release all flags!
        inFile.open(filename);
    }
}

int main() {

    ifstream inFile;
    openInputFile(inFile);

    int id;
    double hourlyRate;
    string name;
    LLinkedList empList;
    int counter = 0;


    while (inFile >> id){
        inFile >> hourlyRate;
        inFile.ignore(9999,' ');
        getline(inFile, name);
        empList.addEmployee(id,hourlyRate,name);
    }

    ifstream inFile2;
    openInputFile(inFile2);

    int file2Id[10];
    int file2Hours[10];

    Node *p = empList.getStart();

    cout << "****** PAYROLL INFORMATION ******" << endl;

    while(inFile2 >> file2Id[counter]){
        inFile2 >> file2Hours[counter];
        counter++;
    }


    while(p!=nullptr){
        double pay = 0.00;
        for(int i = 0; i < counter; i++){
            if(p->data->getEmpId() == file2Id[i]){
                pay += p->data->getEmpHourlyRate() * file2Hours[i];
            }
        }

        cout << p->data->getEmpName() << "\t" << "$"<< pay << endl;
        p = p->next;
    }

    cout << "****** END PAYROLL INFORMATION ******" << endl;

    return 0;
}

Photo by Fabian Grohs on Unsplash

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.