Skip to content

Instantly share code, notes, and snippets.

@Red0214
Created August 6, 2024 05:27
Show Gist options
  • Save Red0214/72eb5d533d1b29cc3206c07b0773ffe4 to your computer and use it in GitHub Desktop.
Save Red0214/72eb5d533d1b29cc3206c07b0773ffe4 to your computer and use it in GitHub Desktop.
// Incluimos las librerias necesarias
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <cstdlib>
#include <cstdint>
// ----------------------------------
// ESTRUCTURAS
struct Employee
{
std::string name{};
std::uint16_t totalRequests{};
float salary{};
float bonus{};
};
// ----------------------------------
// FUNCIONES
void clearScreen()
{
system("cls");
}
void pauseScreen()
{
std::cout << "Press Enter to continue...";
std::cin.ignore();
std::cin.get();
}
float calculateBonus(std::uint16_t requests)
{
if(requests >= 2000 && requests <= 2500)
{
return 1500.0f;
}
else if(requests >= 2501 && requests <= 3000)
{
return 2000.0f;
}
else if(requests > 3000)
{
return 3000.0f;
}
else{
return 0.0f;
}
}
Employee readEmployee()
{
Employee emp;
std::cout << "Insert name: ";
std::cin >> std::ws;
std::getline(std::cin, emp.name);
std::cout << "Insert total of requests: ";
std::cin >> emp.totalRequests;
std::cout << "Insert salary: ";
std::cin >> emp.salary;
emp.bonus = calculateBonus(emp.totalRequests);
return emp;
}
void printEmployee(const Employee& employee)
{
std::cout << "Name: " << employee.name;
std::cout << "\nTotal request: " << employee.totalRequests;
std::cout << "\nSalary: " << employee.salary;
std::cout << "\nBonus: " << employee.bonus << "\n\n";
}
void printEmployees(const std::vector<Employee> &arr)
{
clearScreen();
if(arr.empty())
{
std::cout << "There aren't employees yet!\n";
}
else
{
for(int i = 1; Employee emp : arr)
{
std::cout << "EMPLOYEE NO. " << i << ": \n";
printEmployee(emp);
i++;
}
}
pauseScreen();
}
void storeEmployee(std::vector<Employee> &arr)
{
char band = 'n';
do
{
clearScreen();
std::cout << "--- INSERT AN EMPLOYEE ---\n\n";
Employee emp = readEmployee();
arr.push_back(emp);
std::cout << "\nDo you want to insert a new employee?(s/n): ";
std::cin >> band;
} while (band == 's' || band == 'S');
}
std::array<Employee, 3> chooseBestThree(const std::vector<Employee>& arr)
{
std::array<size_t, 3> bestThree{};
std::uint8_t i{0};
while(i < 3)
{
std::uint16_t higherRequests = 0;
for(size_t j = 0; j < arr.size(); j++)
{
if(i == 0)
{
if(higherRequests < arr[j].totalRequests)
{
higherRequests = arr[j].totalRequests;
bestThree[i] = j;
}
}
else if(i == 1)
{
if(j == bestThree[0])
{
continue;
}
else
{
if(higherRequests < arr[j].totalRequests)
{
higherRequests = arr[j].totalRequests;
bestThree[i] = j;
}
}
}
else
{
if(j == bestThree[0] || j == bestThree[1])
{
continue;
}
else
{
if(higherRequests < arr[j].totalRequests)
{
higherRequests = arr[j].totalRequests;
bestThree[i] = j;
}
}
}
}
i++;
}
return
{
arr[bestThree[0]],
arr[bestThree[1]],
arr[bestThree[2]]
};
}
// ----------------------------------
// FUNCION PRINCIPAL
int main()
{
std::vector<Employee> employees;
char bandSwitch{'y'};
do
{
switch (bandSwitch)
{
case 'y':
clearScreen();
std::cout << "--- MAIN MENU ---\n\n";
std::cout << "1. Insert an employee.\n";
std::cout << "2. See employees. \n";
std::cout << "3. Choose best 3.\n";
std::cout << "4. Worst of best 3.\n";
std::cout << "0. Exit.\n\n";
std::cout << "Option: ";
std::cin >> bandSwitch;
break;
case '1':
storeEmployee(employees);
bandSwitch = 'y';
break;
case '2':
printEmployees(employees);
bandSwitch = 'y';
break;
case '3':
{
std::array<Employee, 3> bestThree = chooseBestThree(employees);
for(Employee emp : bestThree)
{
printEmployee(emp);
}
pauseScreen();
bandSwitch = 'y';
break;
}
case '4':
if(employees.empty())
{
clearScreen();
std::cout << "There aren't employees yet!\n\n";
pauseScreen();
clearScreen();
}
else if(employees.size() < 2)
{
clearScreen();
std::cout << "There aren't enough employees!\n\n";
pauseScreen();
clearScreen();
}
else{
printEmployee(chooseBestThree(employees)[2]);
}
bandSwitch = 'y';
break;
case '0':
clearScreen();
std::cout << "See you later!\n";
pauseScreen();
clearScreen();
bandSwitch = 'j';
break;
default:
std::cout << "Invalid input, try again.\n\n";
pauseScreen();
clearScreen();
bandSwitch = 'y';
break;
}
} while (bandSwitch != 'j');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment