eate a Payroll class for the project. The class should contain the following: (1
ID: 3770074 • Letter: E
Question
eate a Payroll class for the project. The class should contain the following: (10 pts) o 5 private data members. Each data member should correspond to the first 5 fields in the output file description above o A constructor that will create an instance of the class and initialize each of the 5 data members of the object o Get member functions for each of the data members o Set member functions if you choose to use these to update the data members of the class o A static data member that will track the total number of Payroll records created o Appropriate static member functions that will access and increment the static data member o A method to calculate the total pay amount for the employee. This should be calculated by multiplying the hours worked by the pay rate. 3) Your program will prompt the user for Payroll information. o Provide a descriptive introduction to the program to inform the user of the purpose of the program. (2 pts) o Provide the user the option to quit the program before entering any payroll data (2 pts) o You will ask the user how many Payroll records they will enter into the program. You will collect Payroll data until one of the following: (5 pts) i. the user chooses to stop entering data into the program. ii. the number of payroll records the user specified to enter has been reached You will need to use a loop structure to accomplish this. o The user should be prompted in your program to enter the Payroll information. You should design the program so that the user will be prompted to enter each data item on a separate line on the screen (4 pts) o Provide a clear option for the user at the completion of the payroll data entry. They should be prompted to either add another payroll record or quit the program. (2 pts) o Notify the user when they have reached their number of payroll records – no more payroll records need to be accepted once they reach the limit they specified at the beginning of the program (3 pts) o Validate that the Employee Number is exactly 6 characters long (3 pts) 4) The program should create a dynamic array which will store Payroll objects. The array should be dynamic so that the user of the program can enter the number of Payroll records as specified in (3) above. (5 pts) 5) The Payroll information entered by the user for each employee should be stored in a separate Payroll object. Each object should be included as an element of the dynamic array that was created in (4) above. (4 pts) 6) The static data member of the class will keep track of the number of Payroll records added by the user. (4 pts) o As the user completes entering payroll for an employee, the static data member of the class should be incremented by 1. o You need to use the static data member to track the number of Payroll records input by the user, as there is no guarantee that the user will enter the number of records they specify at the beginning of the program. o Be sure to use a static member function to access and update the static data member 7) When the user has completed entering Payroll records, the payroll information will be written to the output text file. The program should read through the Payroll object elements of the dynamic array and write a record to the output file for each object in the array. The trailer record - which includes the number of payroll records in the text file - should be written at the end of the text file. (6 pts)
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string trim(string& str)
{
size_t first = str.find_first_not_of(' ');
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last-first+1));
}
class payroll{
private :
string empno,fname, lname;
double rate, hours, pay;
public:
static int count;
payroll(string en,string fn, string ln, double rt, double hr){
empno = en;
fname = fn;
lname = ln;
rate = rt;
hours = hr;
coutticker();
}
double calc_pay(){
pay = rate* hours;
return pay;
}
double get_pay(){
return pay;
}
public: static void coutticker(){
count++;
}
void set_empno(string en){
empno = en;
}
void set_fname(string fn){
fname = fn;
}
void set_lname(string ln){
lname = ln;
}
void set_rate(double rt){
rate = rt;
}
void set_hours(double hr){
hours = hr;
}
string get_empno(){
return empno;
}
string get_fname(){
return fname;
}
string get_lname(){
return lname;
}
double get_rate(){
return rate;
}
double get_hours(){
return hours;
}
~payroll(){
count--;
}
};
int payroll::count = 0;
int main(){
char enter, leave ;
int no_records;
string en, fn, ln;
double rt, hrs, pay;
ofstream outputFile;
payroll **emp_records;
outputFile.open("outputfile.txt");
cout << " Payroll information."<< endl;
cout << "******** Program for maintain you employee payroll data *********"<< endl;
cout << "Would you like to enter a employee records?"<< endl;
cin >> enter;
if ((enter=='y')||(enter =='Y'))
{
cout<< "How many Payroll records will you enter "<<endl;
cin>> no_records;
while((no_records<1))
{
cout << "Please enter your valid number" << endl;
cin>>no_records;
}
emp_records = new payroll*[no_records];
for(int i = 0; (leave!='n')&&(leave!= 'N')&&(i < no_records); i++) {
cout << "Please enter your 6 digit employee number " << endl;
cin.ignore();//To skip remaining ' ' character
getline(cin, en);
trim(en);
while( en.size()<6 )
{
cout << "Please enter your valid 6 digit number" << endl;
getline(cin, en);
trim(en);
}
cout<< "Please Enter your first name "<<endl;
//cin.ignore();//To skip remaining ' ' character
getline(cin, fn);
cout<< "fn check :"<<fn<<endl;
trim(fn);
cout << "Please enter your last name "<<endl;
//cin.ignore();//To skip remaining ' ' character
getline(cin, ln);
trim(ln);
cout << "Please enter the PayRate: " << endl;
cin >> rt;
while (rt< 0)
{cout <<" Please enter a valid value ie. greater than 0"<< endl;
cin >> rt;}
cout << "Please enter the hours worked during the week: " << endl;
cin >> hrs;
while (rt< 0)
{cout <<" Please enter a valid value ie. greater than 0"<< endl;
cin >> hrs;}
cout << "Would you like to enter another Employee Record?"<< endl;
cin >> leave;
emp_records[i] = new payroll(en,fn,ln,rt,hrs);
if(i == no_records){
cout<<"You have reached no. of payroll records limt – no more payroll records needed as specified at the beginning"<<endl;
break;
}
}
for(int j= 0;j< payroll::count;j++){
outputFile <<emp_records[j]->get_empno()<< ", " ;
outputFile <<emp_records[j]->get_fname()<< " ";
outputFile <<emp_records[j]->get_lname() << ", ";
outputFile<< fixed << setprecision(2)<<"Payrate - $" << emp_records[j]->get_rate()<< ", " ;
outputFile<< fixed << setprecision(2)<<"Hours Worked-"<<emp_records[j]->get_hours()<< ", ";
outputFile << ". "<<endl ;
}
outputFile << payroll::count<<endl ;
cout << "The file has been saved." << endl;
}
outputFile.close();
system ("pause");
//delete emp_records[];
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.