Use Switch Statement to handle String Cases (Doesnt Work)

This commit is contained in:
Matti 2024-11-23 09:32:27 +01:00
parent bff9cf5935
commit a0f05d9130

View File

@ -42,6 +42,110 @@ void deleteAllPointers(std::vector<ProductSale*> &allSales) {
}
}
void getTotalProfitFor(std::string metric, std::vector<ProductSale*> &allSales) {
std::map<std::string, long> TotalPerMetric;
switch (metric) {
case "Region" :
for(auto sale : allSales) {
TotalPerMetric[sale->region] += sale->totalProfit;
}
break;
case "Country":
for(auto sale : allSales) {
TotalPerMetric[sale->country] += sale->totalProfit;
}
break;
case "Item_Type":
for(auto sale : allSales) {
TotalPerMetric[sale->itemType] += sale->totalProfit;
}
break;
case "Sales_Channel" :
for(auto sale : allSales) {
TotalPerMetric[sale->salesChannel] += sale->totalProfit;
}
break;
case "Order_Priority" :
for(auto sale : allSales) {
TotalPerMetric[sale->orderPriority] += sale->totalProfit;
}
break;
case "Order_Date" :
for(auto sale : allSales) {
TotalPerMetric[sale->orderDate] += sale->totalProfit;
}
break;
case "Order_ID" :
for(auto sale : allSales) {
TotalPerMetric[sale->orderId] += sale->totalProfit;
}
break;
case "Ship_Date" :
for(auto sale : allSales) {
TotalPerMetric[sale->shipDate] += sale->totalProfit;
}
break;
case "Units_Sold" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitsSold);
TotalPerMetric[key] += sale->totalProfit;
}
break;
case "Unit_Price" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitPrice);
TotalPerMetric[key] += sale->totalProfit;
}
break;
case "Unit_Cost" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitCost);
TotalPerMetric[key] += sale->totalProfit;
}
break;
case "Total_Revenue" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalRevenue);
TotalPerMetric[key] += sale->totalProfit;
}
break;
case "Total_Cost" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalCost);
TotalPerMetric[key] += sale->totalProfit;
}
break;
case "Total_Profit" :
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalProfit);
TotalPerMetric[key] += sale->totalProfit;
}
break;
default:
std::cout << "Unknown metric :" << metric << std::endl;
return;
}
for (auto entry : TotalPerMetric) {
std::cout << entry.first << " : " << entry.second << std::endl;
}
}
void getSaleCount(std::string itemType, std::string country, std::vector<ProductSale*> &allSales) {
long totalSales;
long salesOfTypeX;
@ -134,7 +238,7 @@ void Aufg8Main() {
case 0:
return; // To quit the programm
case 1:
// getTotalProfitFor(*selectedType);
getTotalProfitFor(selectedType, allSales);
break;
case 2:
getSaleCount(selectedType, selectedRegion, allSales);