카테고리 없음

Exercise 13.1: Class

07514673 2021. 12. 20. 14:50

Exercise 13.1: Class

Here is the problem specification:

Design a class called EmployeeInfo that has the following member variables:

  • name
  • hourly rate
  • number of hours worked

The class has only a default constructor that assigns empty strings ("") to the name and 0 to the hourly rate and number of hours worked.

Write appropriate mutator functions that store values in these member variables and accessor functions that return the values in these member variables: getName, getHourlyRate, getHoursWorked, setName, setName, setHoursWorked, and calculatePay. You calculate the weekly pay by multiplying the hourly rate by the number of hours worked (to make it simple, there's no overtime rate).

Demonstrate the class by writing a program that creates one instance of it. Ask the user to enter the employee name, hourly rate and hours worked. Store them in the object. Display the data and the weekly pay. Exit the program when the user senter "stop."

Input Validation: Do not accept negative alues for the hourly rate and hours worked.

Sample Run

Welcome to my payroll program
Enter the employee name (stop to exit): John Doe
Please enter hourly rate: -2
Invalid hourly rate. Please enter positive numbers only:-1
Invalid hourly rate. Please enter positive numbers only:20
Please enter the number of hours worked: -5
Invalid amount of hours worked. Please enter positive numbers only:-6
Invalid amount of hours worked. Please enter positive numbers only:40
Employee Name: John Doe
Weekly pay amount : 800.00

Enter the employee name (stop to exit): Henry Smith
Please enter hourly rate: 10
Please enter the number of hours worked: 20
Employee Name: Henry Smith
Weekly pay amount : 200.00

Enter the employee name (stop to exit): stop

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class EmployeeInfo {
public:
    string getName();
    int getHourlyRate();
    int getHoursWorked();
    int calculatePay();
    void setName(string newName);
    void setHourlyRate(int hourlyPayRate);
    void setHoursWorked(int workedHours);
private:
    string name;
    int hourlyRate;
    int hoursWorked;
};
string EmployeeInfo::getName() {
    return name;
}
int EmployeeInfo::getHourlyRate() {
    return hourlyRate;
}
int EmployeeInfo::getHoursWorked() {
    return hoursWorked;
}
int EmployeeInfo::calculatePay() {
    return hourlyRate * hoursWorked;
}
void EmployeeInfo::setName(string newName) {
    name = newName;
}
void EmployeeInfo::setHourlyRate(int hourlyPayRate) {
    hourlyRate = hourlyPayRate;
}
void EmployeeInfo::setHoursWorked(int workedHours) {
    hoursWorked = workedHours;
}
int getNum(string statement, string type) {
    int num;
    cout << statement;
    cin >> num;
    
    while (num < 1) {
        cout << "Invalid " << type << ". Please enter positive numbers only: ";
        cin >> num;
    }
    return num;
}
int main()
{
    EmployeeInfo employee;
    string employeeName;
    int num;
    istringstream inSS;
    cout << "Welcome to my payroll program" << endl;
    cout << "Enter the employee name (stop to exit): ";
    getline(cin, employeeName);
    inSS.clear();
    while (employeeName.compare("stop") != 0) {
    employee.setName(employeeName);
    employee.setHourlyRate(getNum("Please enter hourly rate: ", "hourly rate"));
    employee.setHoursWorked(getNum("Please enter the number of hours worked: ", 
"amount of hours worked"));
    cout << "Employee Name: " << employee.getName() << endl;
    cout << "Weekly pay amount: " << fixed << setprecision(2) << (float) 
employee.calculatePay() << endl << endl;
    
    cout << "Enter the employee name (stop to exit): ";
    getline(cin, employeeName); // take out whitespace
    getline(cin, employeeName);
    inSS.clear();
    }
    return 0;
}