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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <iostream>
#include <iomanip>
#include <sstream>
#include <map>
#include <regex>
using namespace std;
struct Employee
{
Employee() :
pay_rate(0.0f),
hours_worked(0.0f)
{
}
Employee(string id, string name, float pay_rate) :
id(id),
name(name),
pay_rate(pay_rate),
hours_worked(0.0f)
{
}
Employee(string id, string name, float pay_rate, float hours_worked) :
id(id),
name(name),
pay_rate(pay_rate),
hours_worked(hours_worked)
{
}
string id;
string name;
float pay_rate;
float hours_worked;
};
typedef Employee Employee;
class Company
{
public:
Company()
{
// Initial seeding
workers_ =
{
{"101", {"101", "Carla", 14.00f, 40.0f}},
{"102", {"102", "Steve", 12.00f, 35.5f}},
{"103", {"103", "Bob", 15.00f, 12.5f}},
{"S13925813", {"S13925813", "Rachel", 16.50f, 40.0f}},
};
}
const map<string, Employee>& Workers() const
{
return workers_;
}
// Adds an employee to the company
void Add(string id, string name, float pay_rate)
{
if (workers_.count(id) != 0)
cout << "\nError: id \""<<id<<"\" already exists in the company!\n";
else
{
workers_[id] = Employee(id,name,pay_rate);
cout << "\nSuccessfully added id "<<id<<" to the company.\n";
}
}
void Show()
{
cout << setw(15) << "ID" <<
setw(15) << "Name" <<
setw(15) << "Rate" <<
setw(15) << "Hours" <<
setw(15) << "Total" << endl;
for (auto& wpair : workers_)
{
auto& e = wpair.second;
cout << setw(15) << e.id <<
setw(15) << e.name <<
setw(12) << MoneyString(e.pay_rate) << "/hr" <<
setw(12) << e.hours_worked << "hrs" <<
setw(15) << MoneyString(e.hours_worked * e.pay_rate) << endl;
}
}
private:
string MoneyString(float amount)
{
ostringstream oss;
oss << "$" << fixed << setprecision(2) << amount;
return oss.str();
}
map<string, Employee> workers_;
};
void ShowMainMenu()
{
cout << "Welcome to Employee Management." << endl <<
"[1] View employees" << endl <<
"[2] Add employees" << endl <<
"[3] Change hours worked" << endl <<
"[q] Quit" << endl << endl;
}
void ShowAddPrompt()
{
cout << "Adding an employee to the Company." << endl <<
"[q] Go Back" << endl <<
"Please add an employee using the following comma-separated format:"<< endl <<
" id,Employee Name,Pay Rate" << endl << endl <<
"Example: " << endl <<
"12345,John Doe,12.50" << endl << endl;
}
int main ()
{
regex employee_r(R"(([A-Za-z\d]+),([A-Za-z\s]+),([0-9]*\.?[0-9]+))");
Company company;
ShowMainMenu();
string main_input;
while (true)
{
cout << "Choice? ";
cin >> main_input;
if (main_input == "1")
{
company.Show();
}
else if (main_input == "2")
{
ShowAddPrompt();
string add_input;
for (;;)
{
cout << "\n? ";
getline(cin, add_input);
if (add_input == "") // Weird empty string bug?
continue;
if (add_input == "q")
break;
smatch employee_matches;
if (regex_search(add_input, employee_matches, employee_r))
{
string id = employee_matches[1];
string name = employee_matches[2];
float pay_rate = stof(employee_matches[3]);
company.Add(id,name,pay_rate);
}
else
{
cout << "\nInvalid input \""<<add_input<<"\". Follow the add employee format\n";
}
}
ShowMainMenu();
}
else if (main_input == "3")
{
cout << "Choice 3 not implemented yet!\n";
}
else if (main_input == "q")
{
break;
}
else
{
cout << "\nInvalid option.\n";
}
}
return 0;
}
|