Improve Map access

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

View File

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