S3_Cpp/Aufg8/SalesStatMain.cpp

231 lines
7.5 KiB
C++
Raw Normal View History

2024-11-22 13:52:35 +00:00
#include <fstream>
#include <iostream>
#include <map>
2024-11-22 13:52:35 +00:00
#include <string>
#include <vector>
#include "MenuManager.h"
#include "ProductSale.h"
2024-11-23 08:39:58 +00:00
#include "../Aufg1/forLoop.h"
2024-11-22 13:52:35 +00:00
std::string readFile(std::string &fileName, std::vector<ProductSale*> &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;
}
2024-11-23 06:51:26 +00:00
void printAllSales(std::vector<ProductSale*> &allSales) {
for (int i = 0; i < allSales.size(); ++i) {
std::cout << allSales[i]->toString() << std::endl;
std::cout << "==============" << std::endl;
}
}
2024-11-22 13:52:35 +00:00
void deleteAllPointers(std::vector<ProductSale*> &allSales) {
for (auto it = allSales.begin(); it != allSales.end(); it++) {
delete (*it);
}
}
void getTotalProfitFor(std::string metric, std::vector<ProductSale*> &allSales) {
2024-11-23 08:39:58 +00:00
std::map<std::string, long long> TotalPerMetric;
2024-11-23 08:39:58 +00:00
if (metric == "Region") {
for(auto sale : allSales) {
TotalPerMetric[sale->region] += sale->totalProfit;
}
} else if (metric == "Country") {
for(auto sale : allSales) {
TotalPerMetric[sale->country] += sale->totalProfit;
}
} else if (metric=="Item_Type") {
for(auto sale : allSales) {
TotalPerMetric[sale->itemType] += sale->totalProfit;
}
}else if (metric=="Sales_Channel") {
for(auto sale : allSales) {
TotalPerMetric[sale->salesChannel] += sale->totalProfit;
}
}else if (metric == "Order_Priority") {
for(auto sale : allSales) {
TotalPerMetric[sale->orderPriority] += sale->totalProfit;
}
}else if (metric == "Order_Date") {
for(auto sale : allSales) {
TotalPerMetric[sale->orderDate] += sale->totalProfit;
}
}else if (metric == "Order_ID") {
for(auto sale : allSales) {
TotalPerMetric[sale->orderId] += sale->totalProfit;
}
}else if (metric == "Ship_Date") {
for(auto sale : allSales) {
TotalPerMetric[sale->shipDate] += sale->totalProfit;
}
}else if (metric == "Units_Sold") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitsSold);
TotalPerMetric[key] += sale->totalProfit;
}
}else if (metric == "Unit_Price") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitPrice);
TotalPerMetric[key] += sale->totalProfit;
}
}else if (metric=="Unit_Cost") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->unitCost);
TotalPerMetric[key] += sale->totalProfit;
}
}else if (metric == "Total_Revenue") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalRevenue);
TotalPerMetric[key] += sale->totalProfit;
}
}else if (metric == "Total_Cost") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalCost);
TotalPerMetric[key] += sale->totalProfit;
}
}else if (metric == "Total_Profit") {
for(auto sale : allSales) {
std::string key = std::to_string(sale->totalProfit);
TotalPerMetric[key] += sale->totalProfit;
}
}else{
std::cout << "Unknown metric :" << metric << std::endl;
return;
}
for (auto entry : TotalPerMetric) {
std::cout << entry.first << " : " << entry.second << std::endl;
}
2024-11-23 08:39:58 +00:00
std::cout << std::endl;
}
2024-11-23 07:43:25 +00:00
void getSaleCount(std::string itemType, std::string country, std::vector<ProductSale*> &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<double>(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;
}
2024-11-22 13:52:35 +00:00
void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
2024-11-23 07:12:49 +00:00
// Filter out wrong countries
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
continue;
}
2024-11-23 07:12:49 +00:00
TypeCounter[singleSale->itemType]++;
2024-11-22 13:52:35 +00:00
}
2024-11-22 13:52:35 +00:00
// 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;
2024-11-22 13:52:35 +00:00
}
2024-11-23 06:27:03 +00:00
void getOnlineVsOfflineIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
2024-11-23 07:12:49 +00:00
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
2024-11-23 06:27:03 +00:00
continue;
}
2024-11-23 07:12:49 +00:00
// Increment the counter for the sales channel directly
TypeCounter[singleSale->salesChannel]++;
2024-11-23 06:27:03 +00:00
}
int OnlineCounter = TypeCounter["Online"];
int OfflineCounter = TypeCounter["Offline"];
2024-11-23 07:12:49 +00:00
double ratio = (OfflineCounter == 0) ? 100.0 : static_cast<double>(OnlineCounter) / OfflineCounter * 50.0;
2024-11-23 06:27:03 +00:00
std::cout << "Online : " << OnlineCounter << std::endl << "Offline : " << OfflineCounter << std::endl;
std::cout << "Ratio : " << ratio << "% Online" << std::endl << std::endl;
}
2024-11-22 13:52:35 +00:00
void Aufg8Main() {
std::vector<ProductSale*> allSales;
std::string fileName = "../Aufg8/IO-Files/sales_records_small.csv";
2024-11-23 06:51:26 +00:00
fileName = "../Aufg8/IO-Files/DANGER-1500000SalesRecords.csv";
2024-11-23 08:49:35 +00:00
link : //https://github.com/HopfTorsten/dhbw-cpp-journey/blob/master/day5/DANGER-1500000SalesRecords.csv.zip
2024-11-22 13:52:35 +00:00
readFile(fileName, allSales);
2024-11-23 06:51:26 +00:00
// printAllSales(allSales);
2024-11-22 13:52:35 +00:00
2024-11-22 14:19:33 +00:00
std::string selectedRegion;
std::string selectedType;
int currentMenu = 0;
2024-11-22 13:52:35 +00:00
2024-11-22 14:19:33 +00:00
std::string* PselectedRegion = &selectedRegion;
std::string* PselectedType = &selectedType;
int* PcurrentMenu = &currentMenu;
2024-11-22 13:52:35 +00:00
2024-11-23 06:27:03 +00:00
MenuManager menu{PcurrentMenu, PselectedType, PselectedRegion};
2024-11-22 13:52:35 +00:00
menu.MainInteraction();
2024-11-22 14:19:33 +00:00
while (currentMenu) {
2024-11-23 08:49:35 +00:00
switch (currentMenu) {
2024-11-22 14:19:33 +00:00
case 0:
2024-11-23 06:51:26 +00:00
return; // To quit the programm
2024-11-22 14:19:33 +00:00
case 1:
getTotalProfitFor(selectedType, allSales);
2024-11-23 06:51:26 +00:00
break;
2024-11-22 14:19:33 +00:00
case 2:
2024-11-23 07:43:25 +00:00
getSaleCount(selectedType, selectedRegion, allSales);
2024-11-23 06:51:26 +00:00
break;
2024-11-22 14:19:33 +00:00
case 3:
getMostPopularTypeIn(selectedRegion, allSales);
2024-11-23 06:51:26 +00:00
break;
2024-11-22 14:19:33 +00:00
case 4:
2024-11-23 06:51:26 +00:00
getOnlineVsOfflineIn(selectedRegion, allSales);
break;
2024-11-22 14:19:33 +00:00
default:
std::cout << "You should not be able to reach this!";
}
menu.MainInteraction();
2024-11-22 13:52:35 +00:00
}
}