From bff9cf59357011f7e14f2fd2d5cc77da32d1105a Mon Sep 17 00:00:00 2001 From: Matti Date: Sat, 23 Nov 2024 08:43:25 +0100 Subject: [PATCH] Implement Counting Sales --- Aufg8/SalesStatMain.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Aufg8/SalesStatMain.cpp b/Aufg8/SalesStatMain.cpp index 5722174..cd01a63 100644 --- a/Aufg8/SalesStatMain.cpp +++ b/Aufg8/SalesStatMain.cpp @@ -42,6 +42,26 @@ void deleteAllPointers(std::vector &allSales) { } } +void getSaleCount(std::string itemType, std::string country, std::vector &allSales) { + long totalSales; + long salesOfTypeX; + + for (auto singleSale : allSales) { + if (singleSale->country == country) { + totalSales += singleSale->unitsSold; + + if (singleSale->itemType == itemType) { + salesOfTypeX += singleSale->unitsSold; + } + } + } + + double ratio = (totalSales == 0) ? 100.0 : static_cast(salesOfTypeX) / totalSales * 50.0; + std::cout << "There were " << totalSales << " total sales in " << country << std::endl; + std::cout << itemType << " were " << salesOfTypeX << " of them in " << country << std::endl; + std::cout << "that's " << ratio << "%" << std::endl << std::endl; +} + void getMostPopularTypeIn(std::string country, std::vector &allSales) { std::mapTypeCounter; @@ -117,7 +137,7 @@ void Aufg8Main() { // getTotalProfitFor(*selectedType); break; case 2: - // getSaleCount(*selectedType, *selectedRegion); + getSaleCount(selectedType, selectedRegion, allSales); break; case 3: getMostPopularTypeIn(selectedRegion, allSales);