Improve Map access

This commit is contained in:
Matti 2024-11-23 08:12:49 +01:00
parent dbdd4ade8e
commit 9321ec8d71

View File

@ -45,26 +45,15 @@ void deleteAllPointers(std::vector<ProductSale*> &allSales) {
void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
for (ProductSale* singelSale : allSales) {
// map.add(singleSale)
if (singelSale->country != country) {
// Filter out wrong countries
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->itemType) {
element.second++;
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->itemType] = 1;
}
TypeCounter[singleSale->itemType]++;
}
// return map.getMostCommon
std::string mostPopularType;
int counter = 0;
@ -83,29 +72,18 @@ void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSal
void getOnlineVsOfflineIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
for (ProductSale* singelSale : allSales) {
// map.add(singleSale)
if (singelSale->country != country) {
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->salesChannel) {
element.second++;
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->salesChannel] = 1;
}
// Increment the counter for the sales channel directly
TypeCounter[singleSale->salesChannel]++;
}
int OnlineCounter = TypeCounter["Online"];
int OfflineCounter = TypeCounter["Offline"];
double ratio = (OfflineCounter == 0) ? 100.0 : static_cast<double>(OnlineCounter) / OfflineCounter * 100.0;
double ratio = (OfflineCounter == 0) ? 100.0 : static_cast<double>(OnlineCounter) / OfflineCounter * 50.0;
std::cout << "Online : " << OnlineCounter << std::endl << "Offline : " << OfflineCounter << std::endl;
std::cout << "Ratio : " << ratio << "% Online" << std::endl << std::endl;