1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
class Employee
{
public:
Employee(const std::string& name, int age, float wage, bool isSalaried) :
mName(name), mAge(age), mWage(wage), mIsSalaried(isSalaried)
{}
std::string GetName() const
{
return mName;
}
int GetAge() const
{
return mAge;
}
float GetWage() const
{
return mWage;
}
bool GetIsSalaried() const
{
return mIsSalaried;
}
float GetTotalHoursWorked() const
{
return mTotalHoursWorked;
}
void GiveRaise(float stepAmount)
{
if (stepAmount > 0.0f)
{
mWage += stepAmount;
}
else
{
mWage += mWageStep;
}
}
void SetTotalHoursWorked(float totalHoursWorked)
{
mTotalHoursWorked += totalHoursWorked;
}
private:
std::string mName{};
int mAge{};
float mWage{};
float mWageStep{ 0.35f };
bool mIsSalaried{};
float mTotalHoursWorked{ 0.0f };
};
void DisplayEmployeeInformation(std::vector<Employee>& employees);
void SortByName(std::vector<Employee>& employees);
void GiveRaise(std::vector<Employee>& employee, const std::string& employeeToGiveRaise);
int main()
{
Employee robert("Robert", 29, 15.25f, false);
Employee sarah("Sarah", 25, 13.65f, false);
Employee mike("Mike", 43, 3215.0f, true);
Employee angela("Angela", 31, 19.80f, false);
std::vector<Employee> employees = { robert, sarah, mike, angela };
std::cout << std::fixed << std::setprecision(2);
std::cout << "Before Sort\n\n";
DisplayEmployeeInformation(employees);
GiveRaise(employees, angela.GetName());
SortByName(employees);
std::cout << "\n\nAfter Sort\n\n";
DisplayEmployeeInformation(employees);
}
void DisplayEmployeeInformation(std::vector<Employee>& employees)
{
for (const auto& employee : employees)
{
std::cout << employee.GetName() << '\n';
std::cout << " -Wage: $" << employee.GetWage() << '\n';
std::cout << " -Age: " << employee.GetAge() << '\n';
std::cout << std::boolalpha << " -Is Salaried: " << employee.GetIsSalaried() << '\n';
}
}
void SortByName(std::vector<Employee>& employees)
{
auto sortName = [](Employee& nameA, Employee& nameB)
{
return nameA.GetName() < nameB.GetName();
};
std::sort(employees.begin(), employees.end(), sortName);
}
void GiveRaise(std::vector<Employee>& employee, const std::string& employeeToGiveRaise)
{
for (auto& i : employee)
{
if (i.GetName() == employeeToGiveRaise)
{
i.GiveRaise(0);
}
}
}
| |