CS 135 Fall 2017 Assignment #2 Point value: 125 points Date due: submit on bobby
ID: 3598561 • Letter: C
Question
CS 135 Fall 2017 Assignment #2 Point value: 125 points Date due: submit on bobby.cs.unlv.edu to course instructor by 11:59pm Monday, Oct. 16 New Skills Practiced (Learning Goals) . Problem solving and debugging. - Use of Linux redirection to read a file. . Generation of formatted output. . Appropriate use of selection and repetition structures. A company has 5 categories of employees and pays each as follows Salaried employees-pay code S or s-Salaried employees receive a fixed weekly salary. Hourly employees-pay code H or h-Hourly employees receive a fixed hourly wage for up to the first 40 hours worked. They receive time and a half (1.5 times their hourly wage) for any overtime hours (hours>40) . . Commissioned employees pay code Cor c- Commissioned employees are paid $400 a week plus 20% of their total weekly sales. . Temporary employees pay code T ort Temporary employees are paid a fixed hourly wage (they do not get paid overtime if they work more than 40 hours) plus 15% of their total sales . Pieceworkers pay code P or p- Pieceworkers are paid a fixed amount for each item they produce. Data File A file contains the data needed to generate the weekly payroll summary. It is made up of several employee records. Each record begins with the employee's identification number (int) and pay code (char), see description of employee categories above. Then, depending on what category an employee is in, the appropriate data will follow. Each data value will be separated by 1 or more blanks. If the employee is salaried, the amount of his/her weekly salary (double) will be provided. Example: 1005 s 500.00 If the employee is hourly, the number of hours worked (double) followed by the hourly pay rate (double) will be provided Example: 2017 h 41.0 8.75 If the employee is on commission, his/her sales (double) made for the 5 days of the work week will be provided. Example: 1896 C 100.0 125.0 0.0 157.75 40.0 If the employee is temporary, his/her hours worked (double) followed by hourly pay rate (double) followed by the total amount of sales (double) will be provided Example: 2531 T 13.5 7.5 250.0 . . . . Page 1/4Explanation / Answer
#include<iostream>
#include <stdlib.h>
#include<fstream>
#include<iomanip>
using namespace std;
// Class Employee definition
class Employee
{
// Data member to store data
string empID;
char category;
double salaryEarned;
double totalSalary;
int numberOfRecords;
public:
// Prototype of member functions
void readEmployee(Employee[]);
void displayEmployee(Employee[]);
};// End of class
// Read data from file
void Employee::readEmployee(Employee e[])
{
double wage;
double time;
int c = 0;
// fstream object created
fstream f;
//Open file in read mode
f.open("EmployeeSalary.txt");
// Initializes total salary to zero
e[0].totalSalary = 0;
// Reads data till end of the file
while(!f.eof())
{
// Reads employee if and category
f>>e[c].empID;
f>>e[c].category;
// Checks if the category is either 'S' or 's'
if(e[c].category == 'S' || e[c].category == 's')
f>>salaryEarned;
// Checks if the category is either 'H' or 'h'
else if(e[c].category == 'H' || e[c].category == 'h')
{
f>>time;
f>>wage;
// Checks if time is greater than 40
if(time > 40)
// Calculate salary for first 40 hours
// then subtracts first 40 hours and multiply it with 1.5 times of wage
e[c].salaryEarned = (40 * wage) + (time - 40) * (1.5 * wage);
// Otherwise time is not greater than 40
else
// Calculates salary by multiplying time with wage
e[c].salaryEarned = (time * wage);
}// End of else if
// Checks if the category is either 'C' or 'c'
else if(e[c].category == 'C' || e[c].category == 'c')
{
double sales[5];
double total = 0;
// Loops 5 times to read sales
for(int x = 0; x < 5; x++)
{
f>>sales[x];
// Calculates total sales
total += sales[x];
}// End of for loop
// Calculates salary by adding 400 to 20% of total sales
e[c].salaryEarned = (total * 0.2) + 400;
}// End of else if
// Checks if the category is either 'T' or 't'
else if(e[c].category == 'T' || e[c].category == 't')
{
double sales;
wage = 0;
f>>time;
f>>wage;
f>>sales;
// Calculates salary
e[c].salaryEarned = (time * wage) + (sales * .15);
}// End of else if
// Checks if the category is either 'P' or 'p'
else if(e[c].category == 'P' || e[c].category == 'p')
{
wage = 0;
f>>wage;
double total = 0;
// Loops till time is zero
do
{
f>>time;
// Checks if the time is zero come out of the loop
if(time == 0)
break;
// Otherwise calculate total time
else
total += time * wage;
}while(1);
// Calculates salary
e[c].salaryEarned = total;
}// End of else if
// Calculates total salary of all employees
e[0].totalSalary = e[0].totalSalary + e[c].salaryEarned;
// Record counter is increased by one
c++;
}// End of while loop
// Assigns c value to number of records
numberOfRecords = c;
//Close file
f.close();
}//End of function
// Function to display employee information
void Employee::displayEmployee(Employee e[])
{
// Displays heading
// std::left to left align
// set() to set the space
cout<<std::left<<setw(8)<<"ID#"<<setw(15)<<"Category"<<setw(7)<<"Earnings"<<endl;
// Loops till number of records available
for(int c = 0; c < numberOfRecords; c++)
{
// Displays employee information
// std::fixed to fix number of decimal places
// std::setprecision(2) to display up to 2 decimal places
cout<<std::left<<setw(8)<<e[c].empID<<setw(15)<<e[c].category<<setw(7)<<std::fixed << std::setprecision(2)<<std::right<<e[c].salaryEarned<<endl;
}// End of for loop
// Displays total earning
cout<<std::left<<setw(23)<<"Total Earned "<<std::right<<e[0].totalSalary<<endl;
// Displays number of records
cout<<numberOfRecords<<" employees processed";
// Calculates average and displays it
cout<<" Average pay for employee: "<<(totalSalary/numberOfRecords);
}// End of function
// Main function definition
int main()
{
// Creates an array of Employee class objects
Employee emp[10];
// Calls the function read data from file and store it in data member of the class and calculate salary and total salary
emp[0].readEmployee(emp);
// Calls the function to display employee information
emp[0].displayEmployee(emp);
}// End of main function
Sample Run:
ID# Category Earnings
1005 S 500.00
20 h 332.00
333 T 65.00
2109 H 300.00
4438 P 125.00
189 C 500.00
3149 P 112.50
Total Earned 1934.50
7 employees processed
Average pay for employee: 276.36