o You need to use the static data member to track the number of Payroll records
ID: 3765670 • Letter: O
Question
o You need to use the static data member to track the number of Payroll records inputby the user, as there is no guarantee that the user will enter the number of recordsthey 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 elementsof the dynamic array and write a record to the output file for each object in the array. Thetrailer record - which includes the number of payroll records in the text file - should be
written at the end of the text file.
Explanation / Answer
enum PayrollType
{
etSalaried = 1, etHourly, etCommission, etBasePlusCommission
};
typedef struct _PayMonth
{
int numberOfWorkingDays;
int month;
int year;
} PayMonth;
class Payroll
{
protected:
PayrollType et;
int pay_month;
int pay_year;
double wages;
char firstname[128];
char lastname[128];
char ssn[12];
int birthmonth;
public:
virtual void CalculateWages(PayMonth info) = 0;
Payroll(PayrollType type);
void InputBaseData(int simulate);
void PrintWageHeader();
void PrintWageInformation();
};
Payroll::Payroll(PayrollPayrollType type) : et(type)
{
}
void Payroll::InputBaseData(int simulate)
{
if(simulate == 1)
{
static int niteration = 0;
for(int i = 0; i < 4; i++)
{
firstname[i] = 'A' + niteration;
lastname[i] = 'G' + niteration;
}
firstname[4] = '';
lastname[4] = '';
niteration++;
birthmonth = niteration;
}
else
{
printf("First Name: ");
scanf("%s", firstname);
printf("Last Name: ");
scanf("%s", lastname);
printf("SSN: ");
scanf("%s", ssn);
printf("Birth Month(1-12): ");
scanf("%d", &birthmonth);
if(birthmonth < 1 || birthmonth > 12)
birthmonth = 1;
}
}
void Payroll::PrintWageHeader()
{
printf(" %-16s %-16s %-2s/%-4s %-10s", "FirstName", "LastName", "MM", "YYYY", "Wages");
}
void Payroll::PrintWageInformation()
{
printf(" %-16s %-16s %02d/%04d %.2lf", firstname, lastname, pay_month, pay_year, wages);
}
class SalariedPayroll : public Payroll
{
public:
SalariedPayroll();
virtual void CalculateWages(PayMonth info);
};
SalariedPayroll::SalariedPayroll() : Payroll(etSalaried)
{
}
void SalariedPayroll::CalculateWages(PayMonth info)
{
double salary_per_day = FIXED_WEEKLY_SALARY / 5;
Payroll::wages = info.numberOfWorkingDays * salary_per_day;
Payroll::pay_month = info.month;
Payroll::pay_year = info.year;
if(Payroll::pay_month ==Payroll::birthmonth)
Payroll::wages += BIRTHDAY_BONUS;
}
class HourlyPayroll : public Payroll
{
public:
HourlyPayroll();
virtual void CalculateWages(PayMonth info);
};
HourlyPayroll::HourlyPayroll() : Payroll(etHourly)
{
}
void HourlyPayroll::CalculateWages(Pay Month info)
{
int numHoursWorked;
printf("Number of hours worked by Employee "%s %s" : ", firstname, lastname);
scanf("%d", &numHoursWorked);
int numhoursPerMonth = info.numberOfWorkingDays * 8;
int excessHours = numHoursWorked - numhoursPerMonth;
if(excessHours < 0)
{
Payroll::wages = numHoursWorked * SALARY_PER_HOUR;
}
else
{
Payroll::wages = numhoursPerMonth * SALARY_PER_HOUR;
Payroll::wages += excessHours * OVERTIME_SALARY_PER_HOUR;
}
Payroll::pay_month = info.month;
Payroll::pay_year = info.year;
if(Payroll::pay_month == Payroll::birthmonth)
Payroll::wages += BIRTHDAY_BONUS;
}
class CommissionPayroll : public Payroll
{
public:
CommissionPayroll();
virtual void CalculateWages(PayMonth info);
};
CommissionPayroll::CommissionPayroll() : Payroll(etCommission)
{
}
void CommissionPayroll::CalculateWages(PayMonth info)
{
double netSalesAmount;
printf("Net Sales Amount by Payroll "%s %s": ", firstname, lastname);
scanf("%lf", &netSalesAmount);
Payroll::wages = netSalesAmount * COMMISSION_PERCENTAGE / 100;
Payroll::pay_month = info.month;
Payroll::pay_year = info.year;
if(Payroll::pay_month == Payroll::birthmonth)
Payroll::wages += BIRTHDAY_BONUS;
}
class BasePlusCommissionPayroll : public Payroll
{
public:
BasePlusCommissionPayroll();
virtual void CalculateWages(PayMonth info);
};
BasePlusCommissionPayroll::BasePlusCommissionPayroll() : Payroll(etBasePlusCommission)
{
}
void BasePlusCommissionPayroll::CalculateWages(PayMonth info)
{
double netSalesAmount;
printf("Net Sales Amount by Payroll "%s %s": ", firstname, lastname);
scanf("%lf", &netSalesAmount);
Payroll::wages = BASE_SALARY_COMMISSION_Payroll;
Payroll::wages += netSalesAmount * BASE_PLUS_COMMISSION_PERCENTAGE / 100;
Payroll::pay_month = info.month;
Payroll::pay_year = info.year;
if(Payroll::pay_month == Payroll::birthmonth)
Payroll::wages += BIRTHDAY_BONUS;
}
void main()
{
int n = 0;
printf(" Enter the number of Employees: ");
scanf("%d", &n);
Payroll **payrollList = new Payroll* [n];
for(int i = 0; i < n; i++)
{
int itype;
if(SIMULATE_INPUT == 1)
{
int type = i;
type = type % 4;
itype = type + 1;
}
else
{
printf(" Employee Type (1 for Salried, 2 for Hourly, 3 for Commission, 4 for Base + Commission) : ");
scanf("%d", &itype);
if(itype < 1 || itype > 4)
{
printf("Invalid Input");
i--;
continue;
}
}
enumPayrollType type = (enum PayrollType) itype;
switch(type)
{
case etSalaried:
payrollList[i] = new SalariedPayroll();
break;
case etHourly:
payrollList[i] = new HourlyPayroll();
break;
case etCommission:
payrollList[i] = new CommissionPayroll();
break;
case etBasePlusCommission:
payrollList[i] = new BasePlusCommissionPayroll();
break;
}
payrollList[i]->InputBaseData(SIMULATE_INPUT);
}
printf(" Preparing Pay roll Information... ");
PayMonth pm
printf("Enter Pay roll of Month: ");
scanf("%d", &pm.month);
printf("Enter Pay roll of Year: ");
scanf("%d", &pm.year);
printf("Enter Number of Working days on %2d/%4d: ", pm.month, pm.year);
scanf("%d", &pm.numberOfWorkingDays);
for(int i = 0; i < n; i++)
{
payrollList[i]->CalculateWages(pm);
}
payrollList[0]->PrintWageHeader();
for(int i = 0; i < n; i++)
{
payrollList[i]->PrintWageInformation();
}
printf(" ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.