Write a program that calculates the take-home pay for an employee. The two types of employees are salaried and hourly. Allow the user to input the employee first and last name, id, and type. If an employee is salaried, allow the user to input the salary amount. If an employee is hourly, allow the user to input the hourly rate and the number of hours clocked for the week. For hourly employees, overtime is paid for hours over 40 at a rate of 1.5 of the base rate. For all employees’ take home pay, federal tax of 18% is deducted. A retirement contribution of 10% and a Social Security tax rate of 6% should also be deducted. Use appropriate constants. Design an object-oriented solution. Create a second class to test your design.
#include <iostream>
#include <string>
const float FEDERAL_TAX = 0.18;
const float RETIREMENT_CONTRIBUTION = 0.1;
const float SOCIAL_SECURITY_TAX = 0.06;
const float STANDART_HOURS = 40;
const float HOURLY_BOOST = 1.5;
class Employee {
protected:
std::string first_name;
std::string last_name;
std::string id;
float take_home_pay;
public:
Employee(std::string _first_name, std::string _last_name, std::string _id)
: first_name(_first_name), last_name(_last_name), id(_id) {}
std::string getFirstName() {
return first_name;
}
std::string getLastName() {
return last_name;
}
std::string getID() {
return id;
}
float getTakeHomePay() {
return take_home_pay;
}
~Employee() {}
};
class SalariedEmployee : public Employee {
private:
float salary;
public:
SalariedEmployee(std::string first_name, std::string last_name, std::string id, float _salary)
: Employee(first_name, last_name, id), salary(_salary) {
take_home_pay = salary * (1 - FEDERAL_TAX - RETIREMENT_CONTRIBUTION - SOCIAL_SECURITY_TAX);
}
~SalariedEmployee() {}
};
class HourlyEmployee : public Employee {
private:
float hourly_rate;
float hours;
public:
HourlyEmployee(std::string first_name, std::string last_name, std::string id, float _hourly_rate, float _hours)
: Employee(first_name, last_name, id), hourly_rate(_hourly_rate), hours(_hours) {
if (hours > 40) {
take_home_pay = 40 * hourly_rate + (hours - 40) * HOURLY_BOOST * hourly_rate;
}
else {
take_home_pay = hours * hourly_rate;
}
take_home_pay *= (1 - FEDERAL_TAX - RETIREMENT_CONTRIBUTION - SOCIAL_SECURITY_TAX);
}
~HourlyEmployee() {}
};
class Test {
public:
Test() {
std::string first_name, last_name, id, type;
std::cout << "Enter first name: ";
std::cin >> first_name;
std::cout << "Enter last name: ";
std::cin >> last_name;
std::cout << "Enter id: ";
std::cin >> id;
std::cout << "Enter payment type (salaried / hourly): ";
std::cin >> type;
if (type == "salaried") {
float salary;
std::cout << "Enter salary: ";
std::cin >> salary;
SalariedEmployee employee(first_name, last_name, id, salary);
printEmployeeData(
employee.getFirstName(),
employee.getLastName(),
employee.getID(),
employee.getTakeHomePay()
);
}
else {
float hourly_rate;
float hours;
std::cout << "Enter hourly rate: ";
std::cin >> hourly_rate;
std::cout << "Enter number of hours: ";
std::cin >> hours;
HourlyEmployee employee(first_name, last_name, id, hourly_rate, hours);
printEmployeeData(
employee.getFirstName(),
employee.getLastName(),
employee.getID(),
employee.getTakeHomePay()
);
}
}
void printEmployeeData(std::string first_name, std::string last_name, std::string id, float take_home_pay) {
std::cout << "\nEmployee data:\n--------------\n";
std::cout << "Full name: " << first_name << ' ' << last_name << '\n';
std::cout << "ID: " << id << '\n';
std::cout << "Take-home pay: " << take_home_pay << '\n';
}
~Test() {}
};
int main() {
Test();
return 0;
}
Comments
Leave a comment