
Patrick B. answered 12/03/20
Math and computer tutor/teacher
Again, I will work on this tonight.
We'll need the following class:
Typedef
Class Employee
{
protected:
String EmployeeFirstName;
String EmployeeLastName;
double Wage;
//YOU need to specify the employee details
public:
Constructor
Copy constructor
Getters and setters
SerializeToCSV // comma separated values
Parse // converts c.s.v buffer to object
} * TEmployee;
#define EMPLOYEE_SIZE (sizeof(Employee))
Also, function to read the file and populate array of employees
Function to write the employee records to file...
These two functions will have a single parameter, that is the following database structure
Typedef
struct _TEmployeeDB
{
TEmployee employees; //array
Int numEmployee; // how many in the array
} *TEmployeeDB;
I would recommend the first line of the file contains the # of employee records...
Then the array in C s.v format, one row per record
/***********************************************************************************************************/
using namespace std;
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define EMPLOYEE_FILE ("e:\\employee.dat")
#define MAX_NUM_EMPLOYEES (1000)
#define EMPLOYEE_NAME_SIZE (55)
typedef
class Employee
{
protected:
long employeeID;
char employeeFirstName[EMPLOYEE_NAME_SIZE];
char employeeLastName [EMPLOYEE_NAME_SIZE];
double wage;
float numHoursWorked;
public:
Employee() { }
Employee(long empId)
{
this->employeeID = empId;
memset( this->employeeFirstName,0,EMPLOYEE_NAME_SIZE);
memset( this->employeeLastName,0,EMPLOYEE_NAME_SIZE);
wage=0;
numHoursWorked=0;
}
long GetEmployeeID() { return(employeeID); }
void GetEmployeeFirstName( char * nameBuff) { strncpy(nameBuff,this->employeeFirstName,EMPLOYEE_NAME_SIZE); }
void GetEmployeeLastName( char * nameBuff) { strncpy(nameBuff,this->employeeLastName,EMPLOYEE_NAME_SIZE); }
double GetEmployeeWage() { return(wage); }
float GetEmployeeHoursWorked() { return(numHoursWorked); }
void SetEmployeeFirstName( char * nameBuff) { strncpy(this->employeeFirstName,nameBuff,EMPLOYEE_NAME_SIZE); }
void SetEmployeeLastName( char * nameBuff) { strncpy(this->employeeLastName,nameBuff,EMPLOYEE_NAME_SIZE); }
void SetNumHoursWorked( int n) { this->numHoursWorked = n; }
void SetEmployeeWage( double wage) { this->wage = wage; }
double CalculateSalary()
{
double salaryReturn=0;
double OT_Salary=0;
if (numHoursWorked>40)
{
OT_Salary = (numHoursWorked-40) * wage * 1.5;
salaryReturn = 40*wage + OT_Salary;
}
else
{
OT_Salary = 0;
salaryReturn = numHoursWorked*wage;
}
return(salaryReturn);
}
Employee( Employee & employee)
{
this->employeeID = employee.GetEmployeeID();
employee.GetEmployeeFirstName(this->employeeFirstName);
employee.GetEmployeeLastName(this->employeeLastName);
this->wage = employee.GetEmployeeWage();
this->numHoursWorked = employee.GetEmployeeHoursWorked();
}
void SerializeToCSV( char * csvBuff)
{
sprintf(csvBuff,"%ld,%s,%s,%6.2f,%6.2f",employeeID,employeeFirstName,employeeLastName,wage,numHoursWorked);
}
Employee* Parse(char * csvBuff)
{
char tokenBuff[55];
strcpy(tokenBuff,strtok(csvBuff,","));
long employeeID = atol(tokenBuff);
char firstNameBuff[EMPLOYEE_NAME_SIZE];
char lastNameBuff[EMPLOYEE_NAME_SIZE];
strcpy(firstNameBuff,strtok(NULL,","));
strcpy(lastNameBuff,strtok(NULL,",")) ;
strcpy(tokenBuff,strtok(NULL,","));
double wage = (double) atof(tokenBuff);
strcpy(tokenBuff,strtok(NULL,","));
float hoursWorked = atof(tokenBuff);
Employee * employeeReturn = new Employee(employeeID);
employeeReturn->SetEmployeeFirstName(firstNameBuff);
employeeReturn->SetEmployeeLastName(lastNameBuff);
employeeReturn->SetEmployeeWage(wage);
employeeReturn->SetNumHoursWorked(hoursWorked);
return(employeeReturn);
}
} *TEmployee;
#define EMPLOYEE_SIZE (sizeof(Employee))
typedef struct _TEmployeeDatabase
{
Employee employees[MAX_NUM_EMPLOYEES];
int numEmployees;
} *TEmployeeDatabase;
int EmployeeRead(TEmployeeDatabase employeeDatabase)
{
int iReturn=0;
char inbuff[255];
FILE * fptr = fopen(EMPLOYEE_FILE,"r");
if (fptr!=NULL)
{
fgets(inbuff,255,fptr);
int N=employeeDatabase->numEmployees = atoi(inbuff);
cout << N << endl;
if (N>MAX_NUM_EMPLOYEES) { N = MAX_NUM_EMPLOYEES; }
Employee empDummyWorker(0);
for (int iLoop=0; iLoop<N; iLoop++)
{
fgets(inbuff,255,fptr);
TEmployee curEmployeeRec = empDummyWorker.Parse(inbuff);
memcpy(&employeeDatabase->employees[iLoop],curEmployeeRec,EMPLOYEE_SIZE);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
}
int EmployeeWrite(TEmployeeDatabase employeeDatabase)
{
FILE * fptr = fopen(EMPLOYEE_FILE,"w");
int iReturn=0;
char outbuff[255];
if (fptr != NULL)
{
sprintf(outbuff,"%d",employeeDatabase->numEmployees);
fprintf(fptr,"%s \n",outbuff);
for (int iLoop=0; iLoop<employeeDatabase->numEmployees; iLoop++)
{
memset(outbuff,0,255);
employeeDatabase->employees[iLoop].SerializeToCSV(outbuff);
fprintf(fptr,"%s\n",outbuff);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
int main()
{
struct _TEmployeeDatabase employeeDatabase;
int iReturn = EmployeeRead(&employeeDatabase);
if (iReturn ==0)
{
for (int iLoop=0; iLoop<employeeDatabase.numEmployees; iLoop++)
{
char csvBuff[255];
employeeDatabase.employees[iLoop].SerializeToCSV(csvBuff);
cout << csvBuff << endl;
cout << " salary = " << employeeDatabase.employees[iLoop].CalculateSalary() << endl;
}
}
else
{
cout << "error reading employee database" << endl;
}
}
/****************************************************/
//******* employee.dat ********************************8/
5
1001,Patrick,Baldwin,25.36,40
1002,Tausif,K,12.34,40
1003,Franny,Roy,10.00,20
1004,Cindy,Barnes,160,25
1005,Amy,Shannon,17,46

Patrick B.
source code uploaded to RESOURCES section12/04/20