148 lines
4.8 KiB
C++
148 lines
4.8 KiB
C++
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#include <cstring>
|
||
|
#include <algorithm>
|
||
|
#include "Employee.h"
|
||
|
|
||
|
#include "SalaryEmployee.h"
|
||
|
#include "WageEmployee.h"
|
||
|
#include "simpleMenu.h"
|
||
|
|
||
|
|
||
|
|
||
|
const int SIZE = 3;
|
||
|
|
||
|
//call the toString
|
||
|
void printEmployees( Employee * p[], int size)
|
||
|
{
|
||
|
for ( int i=0; i < size; ++i )
|
||
|
std::cout << p[i]->toString() << std::endl;
|
||
|
}
|
||
|
//sorts by name
|
||
|
void sortEmployees( Employee * p[], int size)
|
||
|
{
|
||
|
// Sort the array of pointers to Employee objects
|
||
|
std::sort(p, p + size, [](Employee* a, Employee* b) {
|
||
|
// Compare the names of the employees
|
||
|
return strcmp(a->getName(), b->getName()) < 0;
|
||
|
});
|
||
|
}
|
||
|
//sorts by name
|
||
|
void sortEmployeesByPay( Employee * p[], int size)
|
||
|
{
|
||
|
std::sort(p, p + size, [](Employee* a, Employee* b) {
|
||
|
return a->computePay() < b->computePay();
|
||
|
});
|
||
|
}
|
||
|
// read from the user
|
||
|
void deleteEmployees( Employee* p[], int size)
|
||
|
{
|
||
|
std::vector<std::string> menuItems;
|
||
|
for (int i = 0; i < size; ++i) {
|
||
|
menuItems.emplace_back(p[i]->getName());
|
||
|
}
|
||
|
int selection = simpleMenu(menuItems, "Select an employee to eliminate.");
|
||
|
delete[] p[selection];
|
||
|
}
|
||
|
|
||
|
bool isValid(std::string name, std::string salaryInput, std::string wageInput) {
|
||
|
bool isValid = false;
|
||
|
if ((std::atof(salaryInput.c_str()) > 0.0) && name.length() > 0 && std::atof(wageInput.c_str()) > 0.0) {
|
||
|
isValid = true;
|
||
|
}
|
||
|
if (!isValid) {
|
||
|
printf("Invalid input! Please try again!\n");
|
||
|
}
|
||
|
return isValid;
|
||
|
}
|
||
|
|
||
|
void readEmployees( Employee* p[], int size)
|
||
|
{
|
||
|
std::vector<std::string> menuOptions = {"Wage Employee", "Salary Employee"};
|
||
|
std::string name, salaryInput, wageInput, bonusInput;
|
||
|
double salary, wage, bonus;
|
||
|
for (int i = 0; i < size; ++i) {
|
||
|
int employeeType = simpleMenu(menuOptions, "What type of employee will this be?");
|
||
|
switch (employeeType) {
|
||
|
case 0: {
|
||
|
do {
|
||
|
printf("Enter a name for Employee %i of %i:", (i + 1), size);
|
||
|
std::getline(std::cin, name);
|
||
|
printf("Enter salary for %s: ", name.c_str());
|
||
|
std::getline(std::cin, salaryInput);
|
||
|
printf("Enter wage for %s: ", name.c_str());
|
||
|
std::getline(std::cin, wageInput);
|
||
|
} while (!isValid(name, salaryInput, wageInput));
|
||
|
salary = std::atof(salaryInput.c_str());
|
||
|
wage = std::atof(wageInput.c_str());
|
||
|
char *empName = strdup(name.c_str());
|
||
|
p[i] = new WageEmployee(empName, wage, salary);
|
||
|
free(empName);
|
||
|
break;
|
||
|
}
|
||
|
case 1: {
|
||
|
do {
|
||
|
printf("Enter a name for Employee %i of %i:", (i + 1), size);
|
||
|
std::getline(std::cin, name);
|
||
|
printf("Enter salary for %s: ", name.c_str());
|
||
|
std::getline(std::cin, salaryInput);
|
||
|
printf("Enter bonus for %s: ", name.c_str());
|
||
|
std::getline(std::cin, bonusInput);
|
||
|
} while (!isValid(name, salaryInput, bonusInput));
|
||
|
salary = std::atof(salaryInput.c_str());
|
||
|
bonus = std::atof(wageInput.c_str());
|
||
|
char *empName = strdup(name.c_str());
|
||
|
p[i] = new SalaryEmployee(empName, salary, bonus);
|
||
|
free(empName);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// What is even the purpose for these methods...
|
||
|
void poly1( Employee * p )
|
||
|
{
|
||
|
std::cout << p->computePay() << std::endl;
|
||
|
std::cout << p->weeklyPay() << std::endl;
|
||
|
}
|
||
|
void poly2( Employee &e )
|
||
|
{
|
||
|
std::cout << e.computePay() << std::endl;
|
||
|
std::cout <<e.weeklyPay() << std::endl;
|
||
|
}
|
||
|
void createAndDeleteObjects()
|
||
|
{
|
||
|
SalaryEmployee se1;
|
||
|
std::string s = "john";
|
||
|
SalaryEmployee se2( s.c_str(), 80000 , 2500);
|
||
|
std::cout << "--------------------\n";
|
||
|
}
|
||
|
void createAndDeleteObjectsPointers() {
|
||
|
SalaryEmployee * pSe1 = new SalaryEmployee();
|
||
|
std::string s = "john";
|
||
|
SalaryEmployee * pSe2 = new SalaryEmployee( s.c_str(), 80000 , 2500);
|
||
|
std::cout << "--------------------\n";
|
||
|
delete pSe1;
|
||
|
delete pSe2;
|
||
|
}
|
||
|
int main(int argc, char** argv)
|
||
|
{
|
||
|
const std::string DASHES = "----------------";
|
||
|
// Create an array of employee objects
|
||
|
Employee *employeeArray[SIZE];
|
||
|
readEmployees(employeeArray, SIZE);
|
||
|
printEmployees(employeeArray, SIZE);
|
||
|
std::printf("%s Sort by name %s\n", DASHES.c_str(), DASHES.c_str());
|
||
|
sortEmployees(employeeArray, SIZE);
|
||
|
printEmployees(employeeArray, SIZE);
|
||
|
std::printf("%s Sort by pay %s\n", DASHES.c_str(), DASHES.c_str());
|
||
|
sortEmployeesByPay(employeeArray, SIZE);
|
||
|
printEmployees(employeeArray, SIZE);
|
||
|
deleteEmployees(employeeArray, SIZE);
|
||
|
// free the array
|
||
|
delete[] &employeeArray;
|
||
|
return 0;
|
||
|
}
|