Calculating Salaries based various rate and base with a class - C++ Operator

C++ examples for Operator:Arithmetic Operator

Description

Calculating Salaries based various rate and base with a class

Demo Code

                                                    
#include <iostream>
                                                    
class Employee {//from   w w w  . j  a  v a2  s.c  om
 private:
    const double COMMISSION_BASE = 250.0f;
    const double COMMISSION_RATE = 0.057f;
    const double HOURLY_OVERTIME = 1.5f;
    const double PIECE_RATE = 10.0f;
                                                    
    double managerRate = 0.0f;
    double hourlyRate = 0.0f;
    double weeklySales = 0.0f;
    double hoursWorked = 0.0f;
                                                    
    int employeeCode = 0;
    int piecesProduced = 0;
                                                    
 public:
    Employee();
                                                    
    // SETTERS
    void setManagerRate(double);
    void setHourlyRate(double);
    void setWeeklySales(double);
    void setHoursWorked(double);
    void setEmployeeCode(int);
    void setPiecesProduced(int);
                                                    
    // GETTERS
    double getManagerRate() { return managerRate; }
    double getHourlyRate() { return hourlyRate; }
    double getWeeklySales() { return weeklySales; }
    double getHoursWorked() { return hoursWorked; }
    int getEmployeeCode() { return employeeCode; }
    int getPiecesProduced() { return piecesProduced; }
                                                    
    void inputSalaries();
                                                    
    double calculateSalary();
                                                    
    void reset();
};
Employee::Employee() {}
void Employee::setManagerRate(double rate) {
    if (rate > 0.0f) {
        managerRate = rate;
    } else {
        std::cout << "Manager rate must be > 0.0" << std::endl;
    }
}
void Employee::setHourlyRate(double rate) {
    if (rate > 0.0f) {
        hourlyRate = rate;
    } else {
        std::cout << "Hourly rate must be > 0.0" << std::endl;
    }
}
void Employee::setWeeklySales(double sales) {
    weeklySales = sales;
                                                    
    if (sales == 0.0f)
        std::cout << "\nPoor thing. You sold nothing all week :(\n"
                  << std::endl;
}
void Employee::setHoursWorked(double hw) {
    if (hw >= 0.0f) {
        hoursWorked = hw;
    } else {
        std::cout << "Hours worked must be >= 0.0" << std::endl;
    }
}
void Employee::setEmployeeCode(int emp) {
    if (emp >= 1 && emp <= 4) {
        employeeCode = emp;
    } else {
        std::cout << "Invalid employee code (1-4)" << std::endl;
    }
}
void Employee::setPiecesProduced(int pieces) {
    piecesProduced = pieces;
                                                    
    if (pieces == 0.0f)
        std::cout << "\nPoor little fella made nothing this week :(\n"
                  << std::endl;
}
void Employee::inputSalaries() {
    int iValue = 0;
                                                    
    std::cout << "Salary Calculator" << std::endl;
                                                    
    while (iValue != -1) {
        std::cout << "Enter an employee number (1-4). -1 to quit: ";
        std::cin >> iValue;
                                                    
        if (iValue != -1) {
            setEmployeeCode(iValue);
        }
                                                    
        if (iValue >= 1 && iValue <= 4) {
            calculateSalary();
            reset();
        }
    }
}
double Employee::calculateSalary() {
    double dValue = 0.0f;
                                                    
    switch (getEmployeeCode()) {
        // MANAGER
        case 1:
            std::cout << "\nEnter manager weekly salary: ";
            std::cin >> dValue;
                                                    
            setManagerRate(dValue);
                                                    
            printf("Manager Salary: $%.2f\n\n", getManagerRate());
                                                    
            break;
        // HOURLY PAY
        case 2:
            std::cout << "\nEnter hourly rate: ";
            std::cin >> dValue;
                                                    
            if (dValue > 0.0f) {
                setHourlyRate(dValue);
                                                    
                std::cout << "Enter hours worked: ";
                std::cin >> dValue;
                                                    
                setHoursWorked(dValue);
                                                    
                dValue = getHourlyRate();
                                                    
                dValue *=
                    (getHoursWorked() <= 40.0f) ? getHoursWorked() : 40.0f;
                                                    
                if (getHoursWorked() > 40.0f) {
                    dValue += ((getHoursWorked() - 40.0f) *
                               (getHourlyRate() * HOURLY_OVERTIME));
                }
                                                    
                printf("Hourly Worker Salary: $%.2f\n\n", dValue);
            }
            break;
        // COMMISSION
        case 3:
            std::cout << "\nEnter gross weekly sales: ";
            std::cin >> dValue;
                                                    
            setWeeklySales(dValue);
                                                    
            dValue = COMMISSION_BASE + (getWeeklySales() * COMMISSION_RATE);
                                                    
            printf("Commission Worker Salary: $%.2f\n\n", dValue);
            break;
        // PIECE WORK
        case 4:
            std::cout << "\nEnter pieces produced: ";
            std::cin >> dValue;
                                                    
            setPiecesProduced(dValue);
                                                    
            printf("Piece Worker Salary: $%.2f\n\n",
                   getPiecesProduced() * PIECE_RATE);
            break;
        default:
            std::cout << "Incorrect employee code entered" << std::endl;
            break;
    }
    return dValue;
}
void Employee::reset() {
    managerRate = 0.0f;
    hourlyRate = 0.0f;
    weeklySales = 0.0f;
    hoursWorked = 0.0f;
                                                    
    employeeCode = 0;
    piecesProduced = 0;
}
int main(int argc, const char *argv[]) {
    Employee sc;
                                                    
    sc.inputSalaries();
                                                    
    return 0;
}

Result


Related Tutorials