Skip to content

Instantly share code, notes, and snippets.

@Red0214
Last active September 6, 2024 19:19
Show Gist options
  • Save Red0214/bfb97482f74879e51dcb22abed2ba8be to your computer and use it in GitHub Desktop.
Save Red0214/bfb97482f74879e51dcb22abed2ba8be to your computer and use it in GitHub Desktop.
#include <iostream>
#include <limits>
#include <cstdlib>
#include <string>
#include <array>
// Estructura de todos las las categorias
struct CustomerType
{
std::string classification{};
int totalCustomers{};
};
// Limpia la pantalla haciendo una llamada al sistema
void cleanScreen()
{
system("cls");
}
// Pausa la pantalla dando tiempo al usuario para leer
void pauseScreen()
{
std::cout << "Press Enter to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
}
// Imprime una sola categoria de clientes
void printCustomer(const CustomerType& customer)
{
std::cout << "Category: " << customer.classification;
std::cout << "\nTotal Customer: " << customer.totalCustomers << "\n\n";
}
// Calcula la categoria de clientes con menos clientes
void fewerCustomers(const std::array<CustomerType, 5>& arr)
{
CustomerType min{ arr[0] };
for (std::size_t i = 0; i < arr.size(); i++)
{
if (min.totalCustomers > arr[i].totalCustomers)
{
min.classification = arr[i].classification;
min.totalCustomers = arr[i].totalCustomers;
}
}
std::cout << "The category with the fewest customers is:\n\n";
printCustomer(min);
}
// Calcula la categoria de clientes con mas clientes
void moreCustomers(const std::array<CustomerType, 5>& arr)
{
CustomerType max{ arr[0] };
for (std::size_t i = 0; i < arr.size(); i++)
{
if (max.totalCustomers < arr[i].totalCustomers)
{
max.classification = arr[i].classification;
max.totalCustomers = arr[i].totalCustomers;
}
}
std::cout << "The category with the fewest customers is:\n\n";
printCustomer(max);
}
// Permite ingresar clientes en una sola categoria valida
void storeCustomers(std::array<CustomerType, 5>& arr, char x)
{
std::size_t found{};
for (std::size_t i = 0; i < arr.size(); i++)
{
found = { arr[i].classification.rfind(x) };
if (found != std::string::npos)
{
std::cout << "Insert the number of customers: ";
int input{};
std::cin >> input;
arr[i].totalCustomers += input;
std::cout << "\nCustomers inserted succesfully!\n\n";
break;
}
}
if (found == std::string::npos)
{
std::cout << "Insert a valid value!\n\n";
}
}
int main()
{
std::array<CustomerType, 5> customers
{ {
{"Type A", 0},
{"Type B", 0},
{"Type C", 0},
{"Type D", 0},
{"Type E", 0}
} };
char option{};
do {
cleanScreen();
std::cout << "--- MENU ---\n\n";
std::cout << "1. Insert customers to a category.\n";
std::cout << "2. Show all category.\n";
std::cout << "3. Category with fewest customers.\n";
std::cout << "4. Category with more customers.\n";
std::cout << "5. Exit.\n";
std::cout << "\nOption: ";
std::cin >> option;
cleanScreen();
switch (option)
{
case '1':
{
std::cout << "Availables categories:\n";
std::cout << "Type A.\nType B.\nType C.\n";
std::cout << "Type D.\nType E.\n\n";
std::cout << "Insert a category: ";
char category{};
std::cin >> category;
storeCustomers(customers, category);
pauseScreen();
option = { 'x' };
break;
}
case '2':
for (CustomerType customer : customers)
{
printCustomer(customer);
}
pauseScreen();
option = { 'x' };
break;
case '3':
fewerCustomers(customers);
pauseScreen();
option = { 'x' };
break;
case '4':
moreCustomers(customers);
pauseScreen();
option = { 'x' };
break;
case '5':
std::cout << "See you later!\n";
break;
default:
std::cout << "Inset a valid option!\n\n";
pauseScreen();
option = { 'x' };
break;
}
} while (option == 'x');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment