1 //employee.h #include <fstream.h> Sheet 3 #include <iostream.h> #include <string.h> #include <stdlib.h> const float PAYRATE_MIN = 0.0; const float PAYRATE_MAX = 50.0; const float HOURS_MIN = 0.0; const float HOURS_MAX = 60.0; class Employee public: //constructors Employee() Employee(char * eid); Employee(char * eid, char * name, float rate, float hours); ~Employee() //member methods char * GetId() ; char * GetLastName() ; float GetPayRate() const ; float GetHours() const; void SetHours(float hours); void SetPayRate(float rate); float CalcGrossPay(); void Validation(ifstream & in); //operator overloading friend ifstream & operator>> (ifstream & in, Employee & e ); friend ofstream & operator<< (ofstream & out, Employee & e); private: //private members char id[30] ; char lastname[30]; float payrate ; float hoursworked; ; //employee.cpp #include "employee.h" //implementation of constructor that initialize the private members Employee::Employee(char * eid, char * name, float rate, float hours) strcpy(id,eid); strcpy(lastname,name); payrate = rate; hoursworked = hours; Employee::Employee(char * eid)
2 strcpy(id,eid); strcpy(lastname,""); payrate = 0; hoursworked = 0; //methods that return the values of the student //get employee id char * Employee::GetId() return id; //get employee last name char * Employee::GetLastName() return lastname; //get employee pay rate float Employee::GetPayRate() const return payrate; //get employee worked hours float Employee::GetHours() const return hoursworked ; //two methods to set pay rate and hours void Employee::SetPayRate(float rate) if(rate >= PAYRATE_MIN && rate <= PAYRATE_MAX) payrate = rate; payrate = 0; void Employee::SetHours(float hours) if(hours >= HOURS_MIN && hours <= HOURS_MAX) hoursworked = hours; hoursworked = 0; float Employee::CalcGrossPay() return payrate * hoursworked ; void Employee::Validation(ifstream & in) int count; int j; int test = 0; //read number of records in>>count; ofstream outvalid("outputvalidation.txt");
3 char id[30]; char name[30]; char pay[30]; char hours[30]; while(hours[j]!='\0') if((int)hours[j] >=65) // A for(int i=0 ; i<count ; i++) test=0; //read each time one record in>>id>>name>>pay>>hours; //case one if invalid id while(id[j]!= '\0') if((int)id[j] >=65) // A outvalid<<"invalid Employee Id: "; //case two if invalid hours outvalid<<"invalid Hours Worked: "; //case three if invalid payrate while(pay[j]!='\0') if((int)pay[j] >=65) // A outvalid<<"invalid Pay Rate: ";
4 //case four if mising name while(name[j]!='\0') if((int)(name[j]) <65) outvalid<<"missing Last Name: "; //check if the rate or hours are not in the appropirate value if(rate >= PAYRATE_MIN && rate <= PAYRATE_MAX) e.payrate = rate; e.payrate = 0; if(hours >= HOURS_MIN && hours <= HOURS_MAX) return in; e.hoursworked = hours; e.hoursworked = 0; // overloading operator << to write the employee information into file ofstream & operator<< (ofstream & out, Employee & e) out<<"id= "<<e.id<<" "; out<<"last Name: "<<e.lastname<<" "; //operators overloading // overloading operator >> to read the employee information from file ifstream & operator>> (ifstream & in, Employee & e ) float rate, hours; in>>e.id>>e.lastname>>rate>>hours; "; out<<"pay Rate= "<<e.payrate<<" out<<"hours Worked= "<<e.hoursworked<<endl; //main.cpp return out; #include "employee.h"
5 void main() in>>employee2; Employee employee1; out<<employee2; Employee employee2("1000"); Employee employee3("1000", "Jonson", 30.5, 55.0); int employeesnumber; //Return GrossRate and PayRate formatted cout<<"1-"; cout<<"pay Rate for Jonsone: "; cout<<employee3.getpayrate(); cout<<"\n"; cout<<"2-write Records in output file done"; cout<<"\n\n\n"; //Validation Part and Write it in validation.txt employee2.validation(invalid); cout<<"3-validation Done"<<"\n\n\n"; cout<<"grossrate for Jonsone: "; cout<<employee3.calcgrosspay(); cout<<"\n\n\n"; //Read Records from Input File and Write them in Output File Organized //define input and output streams to read and write ifstream in("input.txt",ios::in); ofstream out("output.txt",ios::out); ifstream invalid("inputvalidation.txt",ios::in); //read number of employees in>>employeesnumber; //for loop to read each record and file i++) //write it in well formated in output for(int i=0 ; i<employeesnumber ;