Question

J Inc. has a file with employee details. You are asked to write a C++ program...

J Inc. has a file with employee details. You are asked to write a C++ program to read the file and display the results with appropriate formatting. Read from the file a person's name, SSN, and hourly wage, the number of hours worked in a week, and his or her status and store it in appropriate vector of obiects. For the output, you must display the person's name, SSN, wage, hours worked, straight time pay, overtime pay, employee status and net pay. Pay calculations and output display should be carried out using functions. For full-time employees, $10 is deducted from wages for union fees. A person is full-time if the status is F. Time and a half is paid for any time worked over 40 hours in a week. Make sure the output is formatted into rows and columns and includes the appropriate titles and column headings. Additional requirements: 1) Define a class Employee with appropriate member variables and the following member functions: a. Two constructors: /I constructor that takes five parameters Employee(string theName, string theSsn, double theHourlywage, double theHoursWorked, char theStatus); // default constructor Employee(); b. Appropriate accessors and mutators. Make all accessors and mutators inline functions c. calculatePay0 and displayEmployee0 should also be member functions of class Employee. Eor all the member functions.use const modifier whereverappropriate. 2) Define a vector of Employee objects in main0. Handle the file reading properly, and use constructor to set values for each Employee object.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::left;

#include <fstream>

using std::ifstream;

#include <iomanip>

using std::setw;

#include <cstring>

const char FILE_NAME[] = {"input.txt"};
const int MAX_BUFFER_SIZE = 100;

struct Employee
{
    char * FirstName;
    char * LastName;
    char * SSN;
    double Wage;
    int HoursWorked;
    char Status;
    double StraightPay;
    double OvertimePay;
    double NetPay;
};

void ReadFile(Employee **& MyEmployees, int & NumberOfEmployees);
void AddEmployee(Employee **& MyEmployees, int & NumberOfEmployees,
                 ifstream & File);
void PrintEmployees(Employee **& MyEmployees, int & NumberOfEmployees);
void PurgeEmployees(Employee **& MyEmployees, int & NumberOfEmployees);


int main()
{


    Employee ** MyEmployees = nullptr;
    int NumberOfEmployees = 0;

    ReadFile(MyEmployees, NumberOfEmployees);

    PrintEmployees(MyEmployees, NumberOfEmployees);

    PurgeEmployees(MyEmployees, NumberOfEmployees);

    return(0);
}

void ReadFile(Employee **& MyEmployees, int & NumberOfEmployees)
{
    ifstream File;
    File.open(FILE_NAME);
    if(File.is_open())
    {
        while(NumberOfEmployees != 10)
        {
            AddEmployee(MyEmployees, NumberOfEmployees, File);
        }
        File.close();
    }
    else
    {
        cout << "Error! The file was unable to open!" << endl;
    }
}


void AddEmployee(Employee **& MyEmployees, int & NumberOfEmployees,
                 ifstream & File)
{
    char BufferFirstName[MAX_BUFFER_SIZE] = {'\0'};
    char BufferLastName[MAX_BUFFER_SIZE] = {'\0'};
    char BufferSSN[MAX_BUFFER_SIZE] = {'\0'};

    Employee * TempEmployee = new Employee;

    File >> BufferFirstName;
    TempEmployee->FirstName = new char[strlen(BufferFirstName) + 1];
    strcpy(TempEmployee->FirstName, BufferFirstName);

    File >> BufferLastName;
    TempEmployee->LastName = new char[strlen(BufferLastName) + 1];
    strcpy(TempEmployee->LastName, BufferLastName);

    File >> BufferSSN;
    TempEmployee->SSN = new char[strlen(BufferSSN) + 1];
    strcpy(TempEmployee->SSN, BufferSSN);

    File >> TempEmployee->Wage;
    File >> TempEmployee->HoursWorked;
    File >> TempEmployee->Status;

    if(TempEmployee->Status == 'F') //NOTE(SighPhy): check if full
        // time or part time
    {
        if(TempEmployee->HoursWorked > 40) //NOTE(SighPhy): works over 40 hours
        {
            TempEmployee->StraightPay = TempEmployee->Wage * 40;
            TempEmployee->OvertimePay = (TempEmployee->Wage * 1.5) *
                                        (TempEmployee->HoursWorked - 40);
            TempEmployee->NetPay = TempEmployee->StraightPay +
                                   TempEmployee->OvertimePay - 5.0;
        }
        else //NOTE(SighPhy): works under 40 hours
        {
            TempEmployee->StraightPay = TempEmployee->Wage *
                                        TempEmployee->HoursWorked;
            TempEmployee->OvertimePay = 0;
            TempEmployee->NetPay = TempEmployee->StraightPay - 5.0;
        }
    }
    else //NOTE(SighPhy): Part time
    {
        if(TempEmployee->HoursWorked > 40) //NOTE(SighPhy): works over 40 hours
        {
            TempEmployee->StraightPay = TempEmployee->Wage * 40;
            TempEmployee->OvertimePay = (TempEmployee->Wage * 1.5) *
                                        (TempEmployee->HoursWorked - 40);
            TempEmployee->NetPay = TempEmployee->StraightPay +
                                   TempEmployee->OvertimePay;
        }
        else //NOTE(SighPhy): works under 40 hours
        {
            TempEmployee->StraightPay = TempEmployee->Wage *
                                        TempEmployee->HoursWorked;
            TempEmployee->OvertimePay = 0;
            TempEmployee->NetPay = TempEmployee->StraightPay;
        }
    }

    Employee ** Temp = new Employee * [NumberOfEmployees + 1];

    for(int i = 0; i < NumberOfEmployees; i++)
    {
        Temp[i] = MyEmployees[i];
    }

    Temp[NumberOfEmployees] = TempEmployee;
    delete [] MyEmployees;
    MyEmployees = Temp;
    NumberOfEmployees++;
}

void PrintEmployees(Employee **& MyEmployees, int & NumberOfEmployees)
{
    cout << "FirstName LastName SSN        "
         << "Wage Hours Straight Overtime P/F NetPay" << endl
         << "                                          Pay      Pay" << endl;

    for(int i = 0; i < NumberOfEmployees; i++)
    {
        cout.fill(' ');
        cout << left << setw(10) << MyEmployees[i]->FirstName;
        cout << left << setw(10) << MyEmployees[i]->LastName;
        cout << left << setw(12) << MyEmployees[i]->SSN;
        cout << left << setw(5) << MyEmployees[i]->Wage;
        cout << left << setw(5) << MyEmployees[i]->HoursWorked;
        cout << left << setw(9) << MyEmployees[i]->StraightPay;
        cout << left << setw(9) << MyEmployees[i]->OvertimePay;
        cout << left << setw(4) << MyEmployees[i]->Status;
        cout << left << setw(4) << MyEmployees[i]->NetPay << endl;
    }
}

void PurgeEmployees(Employee **& MyEmployees, int & NumberOfEmployees)
{
    for(int i = 0; i < NumberOfEmployees; i++)
    {
        delete [] MyEmployees[i]->FirstName;
        MyEmployees[i]->FirstName = nullptr;

        delete [] MyEmployees[i]->LastName;
        MyEmployees[i]->LastName = nullptr;

        delete [] MyEmployees[i]->SSN;
        MyEmployees[i]->SSN = nullptr;

        delete [] MyEmployees[i];
        MyEmployees[i] = nullptr;
    }

    delete MyEmployees;
    MyEmployees = nullptr;
}

E EmployeeSalaryCalculation - [CUserstSwapninCLionProjectslEmployeeSalaryCalculation ..ain.cpp - CLion 2017.2.3 Eile Edit Vie

Add a comment
Know the answer?
Add Answer to:
J Inc. has a file with employee details. You are asked to write a C++ program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to...

    C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to the project. Copy and paste the code is listed below: Design a class named Employee. The class should keep the following information in member variables: Employee name Employee number Hire date // Specification file for the Employee class #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { private:        // Declare the Employee name string variable here. // Declare the Employee...

  • C++ Lab 9B Inheritance Class Production Worker Create a project C2010Lab9b; add a source file Lab...

    C++ Lab 9B Inheritance Class Production Worker Create a project C2010Lab9b; add a source file Lab9b.cpp to the project. Copy and paste the code is listed below: Next, write a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have member variables to hold the following information: Shift (an integer) Hourly pay rate (a double) // Specification file for the ProductionWorker Class #ifndef PRODUCTION_WORKER_H #define PRODUCTION_WORKER_H #include "Employee.h" #include <string> using namespace std; class ProductionWorker...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • in c++ Define and implement the class Employee with the following requirements: private data members string...

    in c++ Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • I would like help and there are three different classes in the coding.. 14.11 Prog11 Inheritance(PayrollOvertime)...

    I would like help and there are three different classes in the coding.. 14.11 Prog11 Inheritance(PayrollOvertime) Create three files to submit: • Payroll class.java -Base class definition • PayrollOvertime.jave - Derived class definition • ClientClass.java - contains main() method Implement the two user define classes with the following specifications: Payroll class (the base class) (4 pts) • 5 data fields(protected) o String name - Initialized in default constructor to "John Doe" o int ID - Initialized in default constructor to...

  • Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will...

    Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will be called TimeOff. This class makes use of another class called NumDays. Then you will create a driver (main) that will handle the personnel records for a company and generate a report. TimeOff class will keep track of an employee’s sick leave, vacation, and unpaid time off. It will have appropriate constructors and member functions for storing and retrieving data in any of the...

  • Design a PayRoll class that has data members for an employee’s first and last name, hourly...

    Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions: displayPayroll function that displays all data members...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT