0% found this document useful (0 votes)
33 views8 pages

Case 7

Uploaded by

paulfellows91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views8 pages

Case 7

Uploaded by

paulfellows91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

include <iostream>

#include <iomanip>

#include <vector>

#include <algorithm>

using namespace std;

class Employee {

protected:

string employeeid;

string employeename;

char maritalstatus;

int hoursworked;

double grosspay, overtimepay, taxamount, netpay;

int overtime;

public:

Employee(string id, string name, char status, int hours)

: employeeid(id), employeename(name), maritalstatus(status),


hoursworked(hours),

grosspay(0), overtimepay(0), taxamount(0), netpay(0), overtime(0) {}

virtual void calculateGrossPay() = 0;

void calculateTax() {

double taxrate;

if (grosspay >= 500)

taxrate = 0.30;
else if (grosspay > 200)

taxrate = 0.20;

else

taxrate = 0.10;

if (maritalstatus == 'S' || maritalstatus == 's')

taxrate += 0.05;

taxamount = grosspay * taxrate;

void calculateNetPay() {

netpay = grosspay - taxamount;

virtual void display() const {

cout << setprecision(2) << fixed;

cout << setw(8) << employeename << setw(8) << employeeid <<
setw(6)

<< hoursworked << setw(6) << overtime << setw(10) << grosspay

<< setw(10) << overtimepay << setw(10) << taxamount <<


setw(10)

<< netpay << endl;

double getNetPay() const {

return netpay;
}

virtual ~Employee() = default;

};

class HourlyEmployee : public Employee {

double hourlyrate;

public:

HourlyEmployee(string id, string name, char status, int hours, double rate)

: Employee(id, name, status, hours), hourlyrate(rate) {}

void calculateGrossPay() override {

if (hoursworked > 40) {

overtime = hoursworked - 40;

grosspay = (40 * hourlyrate) + (overtime * hourlyrate * 1.5);

overtimepay = overtime * hourlyrate * 1.5;

} else {

overtime = 0;

grosspay = hoursworked * hourlyrate;

overtimepay = 0;

};

class SalaryEmployee : public Employee {

double yearlysalary;
public:

SalaryEmployee(string id, string name, char status, int hours, double


salary)

: Employee(id, name, status, hours), yearlysalary(salary) {}

void calculateGrossPay() override {

grosspay = yearlysalary / 52;

if (hoursworked > 40) {

overtime = hoursworked - 40;

double hourlyrate = grosspay / 40;

overtimepay = overtime * hourlyrate * 1.5;

grosspay += overtimepay;

} else {

overtime = 0;

overtimepay = 0;

};

class Payroll {

vector<Employee*> employees;

public:

void addEmployee(Employee* emp) {

employees.push_back(emp);

}
void calculatePayroll() {

for (auto emp : employees) {

emp->calculateGrossPay();

emp->calculateTax();

emp->calculateNetPay();

void printReport() {

cout << "\n--------------------- PAYROLL REPORT ---------------------" << endl;

cout << "NAME ID HW OT GROSS OT-PAY TAX NETPAY"


<< endl;

cout << "-----------------------------------------------------------" << endl;

for (auto emp : employees) {

emp->display();

void findMinAndMaxNetPay() {

auto minmax = minmax_element(employees.begin(), employees.end(),

[](Employee* a, Employee* b) { return a->getNetPay() < b-


>getNetPay(); });

cout << "\nMinimum Net Pay: " << setprecision(2) << fixed <<
(*minmax.first)->getNetPay() << endl;

cout << "Maximum Net Pay: " << setprecision(2) << fixed <<
(*minmax.second)->getNetPay() << endl;
}

void hybridSortByNetPay() {

int n = employees.size();

// Selection sort for the first half

for (int i = 0; i < n / 2; ++i) {

int minIndex = i;

for (int j = i + 1; j < n; ++j) {

if (employees[j]->getNetPay() < employees[minIndex]-


>getNetPay()) {

minIndex = j;

// Swap the smallest element found with the current element

swap(employees[i], employees[minIndex]);

// Exchange sort (Bubble Sort) for the second half

for (int i = n / 2; i < n - 1; ++i) {

for (int j = i + 1; j < n; ++j) {

if (employees[i]->getNetPay() > employees[j]->getNetPay()) {

swap(employees[i], employees[j]);

}
~Payroll() {

for (auto emp : employees) {

delete emp;

};

int main() {

Payroll payroll;

// Adding some employees

payroll.addEmployee(new HourlyEmployee("4569", "Renee", 'S', 45,


20.0));

payroll.addEmployee(new HourlyEmployee("7845", "Bob", 'M', 40, 18.5));

payroll.addEmployee(new HourlyEmployee("1004", "Paul", 'S', 38, 22.0));

payroll.addEmployee(new SalaryEmployee("8004", "Regan", 'S', 50,


52000));

payroll.addEmployee(new SalaryEmployee("6005", "Rohan", 'M', 42,


70000));

// Calculate payroll for all employees

payroll.calculatePayroll();

// Print the initial payroll report

payroll.printReport();

// Find the employee with the smallest and largest net pay
payroll.findMinAndMaxNetPay();

// Sort employees by net pay using a combination of selection and


exchange sort

payroll.hybridSortByNetPay();

// Print the sorted payroll report

cout << "\n------------------ SORTED PAYROLL REPORT ------------------" <<


endl;

payroll.printReport();

return 0;

You might also like