Change to giant if-else Chain

This commit is contained in:
Matti 2024-11-23 09:39:58 +01:00
parent a0f05d9130
commit 00b4e196da

View File

@ -6,6 +6,7 @@
#include "MenuManager.h" #include "MenuManager.h"
#include "ProductSale.h" #include "ProductSale.h"
#include "../Aufg1/forLoop.h"
std::string readFile(std::string &fileName, std::vector<ProductSale*> &allSales) { std::string readFile(std::string &fileName, std::vector<ProductSale*> &allSales) {
std::string content; std::string content;
@ -43,100 +44,71 @@ void deleteAllPointers(std::vector<ProductSale*> &allSales) {
} }
void getTotalProfitFor(std::string metric, std::vector<ProductSale*> &allSales) { void getTotalProfitFor(std::string metric, std::vector<ProductSale*> &allSales) {
std::map<std::string, long> TotalPerMetric; std::map<std::string, long long> TotalPerMetric;
switch (metric) { if (metric == "Region") {
case "Region" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->region] += sale->totalProfit; TotalPerMetric[sale->region] += sale->totalProfit;
} }
break; } else if (metric == "Country") {
case "Country":
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->country] += sale->totalProfit; TotalPerMetric[sale->country] += sale->totalProfit;
} }
break; } else if (metric=="Item_Type") {
case "Item_Type":
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->itemType] += sale->totalProfit; TotalPerMetric[sale->itemType] += sale->totalProfit;
} }
break; }else if (metric=="Sales_Channel") {
case "Sales_Channel" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->salesChannel] += sale->totalProfit; TotalPerMetric[sale->salesChannel] += sale->totalProfit;
} }
break; }else if (metric == "Order_Priority") {
case "Order_Priority" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->orderPriority] += sale->totalProfit; TotalPerMetric[sale->orderPriority] += sale->totalProfit;
} }
break; }else if (metric == "Order_Date") {
case "Order_Date" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->orderDate] += sale->totalProfit; TotalPerMetric[sale->orderDate] += sale->totalProfit;
} }
break; }else if (metric == "Order_ID") {
case "Order_ID" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->orderId] += sale->totalProfit; TotalPerMetric[sale->orderId] += sale->totalProfit;
} }
break; }else if (metric == "Ship_Date") {
case "Ship_Date" :
for(auto sale : allSales) { for(auto sale : allSales) {
TotalPerMetric[sale->shipDate] += sale->totalProfit; TotalPerMetric[sale->shipDate] += sale->totalProfit;
} }
break; }else if (metric == "Units_Sold") {
case "Units_Sold" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->unitsSold); std::string key = std::to_string(sale->unitsSold);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else if (metric == "Unit_Price") {
case "Unit_Price" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->unitPrice); std::string key = std::to_string(sale->unitPrice);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else if (metric=="Unit_Cost") {
case "Unit_Cost" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->unitCost); std::string key = std::to_string(sale->unitCost);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else if (metric == "Total_Revenue") {
case "Total_Revenue" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->totalRevenue); std::string key = std::to_string(sale->totalRevenue);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else if (metric == "Total_Cost") {
case "Total_Cost" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->totalCost); std::string key = std::to_string(sale->totalCost);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else if (metric == "Total_Profit") {
case "Total_Profit" :
for(auto sale : allSales) { for(auto sale : allSales) {
std::string key = std::to_string(sale->totalProfit); std::string key = std::to_string(sale->totalProfit);
TotalPerMetric[key] += sale->totalProfit; TotalPerMetric[key] += sale->totalProfit;
} }
break; }else{
default:
std::cout << "Unknown metric :" << metric << std::endl; std::cout << "Unknown metric :" << metric << std::endl;
return; return;
} }
@ -144,6 +116,7 @@ void getTotalProfitFor(std::string metric, std::vector<ProductSale*> &allSales)
for (auto entry : TotalPerMetric) { for (auto entry : TotalPerMetric) {
std::cout << entry.first << " : " << entry.second << std::endl; std::cout << entry.first << " : " << entry.second << std::endl;
} }
std::cout << std::endl;
} }
void getSaleCount(std::string itemType, std::string country, std::vector<ProductSale*> &allSales) { void getSaleCount(std::string itemType, std::string country, std::vector<ProductSale*> &allSales) {