From 9321ec8d71ba5976d706d55ac3429d1e2f2b60ee Mon Sep 17 00:00:00 2001 From: Matti Date: Sat, 23 Nov 2024 08:12:49 +0100 Subject: [PATCH] Improve Map access --- Aufg8/SalesStatMain.cpp | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/Aufg8/SalesStatMain.cpp b/Aufg8/SalesStatMain.cpp index 9f71fa7..5722174 100644 --- a/Aufg8/SalesStatMain.cpp +++ b/Aufg8/SalesStatMain.cpp @@ -45,26 +45,15 @@ void deleteAllPointers(std::vector &allSales) { void getMostPopularTypeIn(std::string country, std::vector &allSales) { std::mapTypeCounter; - 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 &allSal void getOnlineVsOfflineIn(std::string country, std::vector &allSales) { std::mapTypeCounter; - 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(OnlineCounter) / OfflineCounter * 100.0; + double ratio = (OfflineCounter == 0) ? 100.0 : static_cast(OnlineCounter) / OfflineCounter * 50.0; std::cout << "Online : " << OnlineCounter << std::endl << "Offline : " << OfflineCounter << std::endl; std::cout << "Ratio : " << ratio << "% Online" << std::endl << std::endl;