From 8af59118c2a8ef5125de52fe94f12f3f0290822a Mon Sep 17 00:00:00 2001 From: Matti Date: Fri, 22 Nov 2024 15:41:12 +0100 Subject: [PATCH] Implement getting most popular type in a country --- Aufg8/ProductSale.h | 4 ++-- Aufg8/SalesStatMain.cpp | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Aufg8/ProductSale.h b/Aufg8/ProductSale.h index 4d30011..8cc5467 100644 --- a/Aufg8/ProductSale.h +++ b/Aufg8/ProductSale.h @@ -8,7 +8,7 @@ class ProductSale { -private: +public: std::string region; std::string country; std::string itemType; @@ -23,7 +23,7 @@ private: double totalRevenue; double totalCost; double totalProfit; -public: + ProductSale(std::string line); std::string toString(); }; diff --git a/Aufg8/SalesStatMain.cpp b/Aufg8/SalesStatMain.cpp index 42cfe09..f221e6a 100644 --- a/Aufg8/SalesStatMain.cpp +++ b/Aufg8/SalesStatMain.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -36,10 +37,41 @@ void deleteAllPointers(std::vector &allSales) { void getMostPopularTypeIn(std::string country, std::vector &allSales) { // TODO Add Map DataType - for (auto singelSale = allSales.begin(); singelSale != allSales.end(); singelSale++) { + std::mapTypeCounter; + + for (ProductSale* singelSale : allSales) { // map.add(singleSale) + if (singelSale->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; + } } + + // return map.getMostCommon + std::string mostPopularType; + int counter = 0; + for (auto element : TypeCounter) { + if (element.second > counter) { + counter = element.second; + mostPopularType = element.first; + } + } + + std::cout << "Most popular Type : " << mostPopularType << std::endl + << "Occurences : " << counter << std::endl << std::endl; + } void Aufg8Main() { @@ -64,7 +96,6 @@ void Aufg8Main() { MenuManager menu{PcurrentMenu, PselectedRegion, PselectedType}; menu.MainInteraction(); while (currentMenu) { - std::cout << "Deciding if youre allowed to quit" << (currentMenu) << std::endl; switch (currentMenu) { // TODO Finish these case 0: return;