Author Topic: C++ Program for Employee Payroll System - Hourly, Salaried, Commission Employee  (Read 19798 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Q:A company pays its employees weekly. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base-salary-plus-commission employees receive a base salary plus a percentage of their sales.

For the current pay period, the company has decided to reward basesalary-plus-commission employees by adding 10 percent to their base salaries. The company wants to implement a C++ program that performs its payroll calculations polymorphically. 

1) You should draw the UML class diagram which shows the inheritance hierarchy for our polymorphic employee payroll application. 

2) You should use abstract class Employee to represent the general concept of an employee. The classes that derive directly from  Employee are SalariedEmployee,  CommissionEmployee and  HourlyEmployee. Class BasePlusCommissionEmployee  derived from  CommissionEmployee
represents the last employee type.

3) Abstract base class Employee declares the "interface" to the hierarchy that is, the set of member functions that a program can invoke on all Employee 4objects. Each employee, regardless of the way his or her earnings are calculated, has a first name, a last name and a social security number, so
private data members  firstName,  lastName and socialSecurityNumber appear in abstract base class Employee.

4)You should include private data member birthDate in class Employee. Use class  Date from lecture notes to represent an employee's birthday.

Assume that payroll is processed once per month. Create a  vector of Employee references to store the various employee objects. In a loop, calculate the payroll for each  Employee (polymorphically), and add a $100.00 bonus to the person's payroll amount if the current month is the month in which the Employee's birthday occurs.

Code: (C++)
//Source Code Listing


#include <string.h>
#include <stdio.h>

// Make the value 0 for SIMULATE_INPUT so that you can enter employee name and other details
#define SIMULATE_INPUT  1

// Employee Type 1 : No input data required to processs payroll
#define FIXED_WEEKLY_SALARY               5000  


// Employee Type 2 : Number of hours worked is a mandatory field
#define SALARY_PER_HOUR                   100   
#define OVERTIME_SALARY_PER_HOUR          150   


// Employee Type 3 : Net Sales Amount is the required Field
#define COMMISSION_PERCENTAGE             15    

// Employee Type 4 : Net Sales Amount is the required Field
#define BASE_SALARY_COMMISSION_EMPLOYEE   2000  
#define BASE_PLUS_COMMISSION_PERCENTAGE   10    

// Birthday Bonus for Employee
#define BIRTHDAY_BONUS 100


enum EmployeeType
{
   etSalaried = 1, etHourly, etCommission, etBasePlusCommission
};

typedef struct _PayrollMonth
{
   int numberOfWorkingDays;
   int month;
   int year;
} PayrollMonth;

class Employee 
{
protected:
   EmployeeType et;
   int payroll_month;
   int payroll_year;
   double wages;
   char firstname[128];
   char lastname[128];
   char ssn[12];
   int birthmonth;
public:
   virtual void CalculateWages(PayrollMonth info) = 0;   
   Employee(EmployeeType type);
   void InputBaseData(int simulate);
   void PrintWageHeader();
   void PrintWageInformation();
};

Employee::Employee(EmployeeType type) : et(type) 
{ 
}

void Employee::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] = '\0';
      lastname[4] = '\0';
      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 Employee::PrintWageHeader()
{
   printf("\n%-16s %-16s %-2s/%-4s     %-10s", "FirstName", "LastName", "MM", "YYYY",  "Wages");
}

void Employee::PrintWageInformation()
{
   printf("\n%-16s %-16s %02d/%04d     %.2lf", firstname, lastname, payroll_month, payroll_year, wages);
}

class SalariedEmployee : public Employee
{
public:
   SalariedEmployee();
   virtual void CalculateWages(PayrollMonth info);
};

SalariedEmployee::SalariedEmployee() : Employee(etSalaried) 
{ 
}

void SalariedEmployee::CalculateWages(PayrollMonth info)
{
   double salary_per_day = FIXED_WEEKLY_SALARY / 5;
   Employee::wages = info.numberOfWorkingDays * salary_per_day;
   Employee::payroll_month = info.month;
   Employee::payroll_year = info.year;

   if(Employee::payroll_month == Employee::birthmonth)
      Employee::wages += BIRTHDAY_BONUS;
}

class HourlyEmployee : public Employee
{
public:
   HourlyEmployee();
   virtual void CalculateWages(PayrollMonth info);
};

HourlyEmployee::HourlyEmployee() : Employee(etHourly) 
{ 
}

void HourlyEmployee::CalculateWages(PayrollMonth 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)
   {
      Employee::wages = numHoursWorked * SALARY_PER_HOUR;
   }
   else
   {
      Employee::wages = numhoursPerMonth * SALARY_PER_HOUR;
      Employee::wages += excessHours * OVERTIME_SALARY_PER_HOUR;
   }
   Employee::payroll_month = info.month;
   Employee::payroll_year = info.year;

   if(Employee::payroll_month == Employee::birthmonth)
      Employee::wages += BIRTHDAY_BONUS;
}

class CommissionEmployee : public Employee
{
public:
   CommissionEmployee();
   virtual void CalculateWages(PayrollMonth info);
};

CommissionEmployee::CommissionEmployee() : Employee(etCommission) 
{ 
}

void CommissionEmployee::CalculateWages(PayrollMonth info)
{
   double netSalesAmount;
   printf("Net Sales Amount by Employee \"%s %s\": ", firstname, lastname);
   scanf("%lf", &netSalesAmount);
   Employee::wages = netSalesAmount * COMMISSION_PERCENTAGE / 100;
   Employee::payroll_month = info.month;
   Employee::payroll_year = info.year;

   if(Employee::payroll_month == Employee::birthmonth)
      Employee::wages += BIRTHDAY_BONUS;
}

class BasePlusCommissionEmployee : public Employee
{
public:
   BasePlusCommissionEmployee();
   virtual void CalculateWages(PayrollMonth info);
};

BasePlusCommissionEmployee::BasePlusCommissionEmployee() : Employee(etBasePlusCommission) 
{ 
}

void BasePlusCommissionEmployee::CalculateWages(PayrollMonth info)
{
   double netSalesAmount;
   printf("Net Sales Amount by Employee \"%s %s\": ", firstname, lastname);
   scanf("%lf", &netSalesAmount);
   Employee::wages = BASE_SALARY_COMMISSION_EMPLOYEE;
   Employee::wages += netSalesAmount * BASE_PLUS_COMMISSION_PERCENTAGE / 100;
   Employee::payroll_month = info.month;
   Employee::payroll_year = info.year;

   if(Employee::payroll_month == Employee::birthmonth)
      Employee::wages += BIRTHDAY_BONUS;
}

void main()
{
   int n = 0;
   printf("\nEnter the number of Employees: ");
   scanf("%d", &n);
   Employee **employeeList = new Employee* [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("\nEmployee 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;
         }
      }

      enum EmployeeType type = (enum EmployeeType) itype;
      switch(type)
      {
      case etSalaried:
         employeeList[i] = new SalariedEmployee();
         break;
      case etHourly:
         employeeList[i] = new HourlyEmployee();
         break;
      case etCommission:
         employeeList[i] = new CommissionEmployee();
         break;
      case etBasePlusCommission:
         employeeList[i] = new BasePlusCommissionEmployee();
         break;
      }
      employeeList[i]->InputBaseData(SIMULATE_INPUT);
   }

   printf("\nPreparing Payroll Information...\n");

   PayrollMonth pm;
   printf("Enter Payroll Month: ");
   scanf("%d", &pm.month);
   printf("Enter Payroll 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++)
   {
      employeeList[i]->CalculateWages(pm); // Polymorphism 
   }  
   
   employeeList[0]->PrintWageHeader();
   for(int i = 0; i < n; i++)
   {
      employeeList[i]->PrintWageInformation();
   }  
   printf("\n\n");
}




The output is given below:

Enter the number of Employees: 11

Preparing Payroll Information...
Enter Payroll Month: 11
Enter Payroll Year: 2011
Enter Number of Working days on 11/2011: 23
Number of hours worked by Employee "BBBB HHHH" : 45
Net Sales Amount by Employee "CCCC IIII": 3000
Net Sales Amount by Employee "DDDD JJJJ": 3400
Number of hours worked by Employee "FFFF LLLL" : 30
Net Sales Amount by Employee "GGGG MMMM": 4000
Net Sales Amount by Employee "HHHH NNNN": 4400
Number of hours worked by Employee "JJJJ PPPP" : 50
Net Sales Amount by Employee "KKKK QQQQ": 5000

FirstName        LastName         MM/YYYY     Wages
AAAA             GGGG             11/2011     23000.00
BBBB             HHHH             11/2011     4500.00
CCCC             IIII             11/2011     450.00
DDDD             JJJJ             11/2011     2340.00
EEEE             KKKK             11/2011     23000.00
FFFF             LLLL             11/2011     3000.00
GGGG             MMMM             11/2011     600.00
HHHH             NNNN             11/2011     2440.00
IIII             OOOO             11/2011     23000.00
JJJJ             PPPP             11/2011     5000.00
KKKK             QQQQ             11/2011     850.00

Press any key to continue . . .

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1