#include #include #include #include #include #include "MenuManager.h" #include "ProductSale.h" std::string readFile(std::string &fileName, std::vector &allSales) { std::string content; std::ifstream infile; infile.open(fileName); if (!infile.is_open()) { std::cout << "File does not exist" << std::endl; return "FAILED_TO_READ_FILE"; } std::string line; std::getline(infile, line); // Skip first Line while (std::getline(infile, line)) { content += line; ProductSale *temp = new ProductSale(line); // TODO Use Smart Pointers allSales.push_back(temp); } return content; } void printAllSales(std::vector &allSales) { for (int i = 0; i < allSales.size(); ++i) { std::cout << allSales[i]->toString() << std::endl; std::cout << "==============" << std::endl; } } void deleteAllPointers(std::vector &allSales) { for (auto it = allSales.begin(); it != allSales.end(); it++) { delete (*it); } } void getTotalProfitFor(std::string metric, std::vector &allSales) { std::map 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 &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; // Filter out wrong countries for (ProductSale* singleSale : allSales) { if (singleSale->country != country) { continue; } TypeCounter[singleSale->itemType]++; } // 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 getOnlineVsOfflineIn(std::string country, std::vector &allSales) { std::mapTypeCounter; for (ProductSale* singleSale : allSales) { if (singleSale->country != country) { continue; } // 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 * 50.0; std::cout << "Online : " << OnlineCounter << std::endl << "Offline : " << OfflineCounter << std::endl; std::cout << "Ratio : " << ratio << "% Online" << std::endl << std::endl; } void Aufg8Main() { std::vector allSales; std::string fileName = "../Aufg8/IO-Files/sales_records_small.csv"; fileName = "../Aufg8/IO-Files/DANGER-1500000SalesRecords.csv"; readFile(fileName, allSales); // printAllSales(allSales); std::string selectedRegion; std::string selectedType; int currentMenu = 0; std::string* PselectedRegion = &selectedRegion; std::string* PselectedType = &selectedType; int* PcurrentMenu = ¤tMenu; MenuManager menu{PcurrentMenu, PselectedType, PselectedRegion}; menu.MainInteraction(); while (currentMenu) { switch (currentMenu) { // TODO Finish these case 0: return; // To quit the programm case 1: getTotalProfitFor(selectedType, allSales); break; case 2: getSaleCount(selectedType, selectedRegion, allSales); break; case 3: getMostPopularTypeIn(selectedRegion, allSales); break; case 4: getOnlineVsOfflineIn(selectedRegion, allSales); break; default: std::cout << "You should not be able to reach this!"; } menu.MainInteraction(); } }