S3_Cpp/Aufg8/SalesStatMain.cpp

150 lines
4.2 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"
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;
}
void deleteAllPointers(std::vector<ProductSale*> &allSales) {
for (auto it = allSales.begin(); it != allSales.end(); it++) {
delete (*it);
}
}
void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
for (ProductSale* singelSale : allSales) {
2024-11-22 13:52:35 +00:00
// map.add(singleSale)
if (singelSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->itemType) {
element.second++;
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->itemType] = 1;
}
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;
for (ProductSale* singelSale : allSales) {
// map.add(singleSale)
if (singelSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->salesChannel) {
element.second++;
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->salesChannel] = 1;
}
}
int OnlineCounter = TypeCounter["Online"];
int OfflineCounter = TypeCounter["Offline"];
double ratio = (OfflineCounter==0) ? 100.0: OnlineCounter/OfflineCounter;
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";
readFile(fileName, allSales);
for (int i = 0; i < allSales.size(); ++i) {
std::cout << allSales[i]->toString() << std::endl;
std::cout << "==============" << std::endl;
}
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) {
switch (currentMenu) { // TODO Finish these
case 0:
return;
case 1:
// getTotalProfitFor(*selectedType);
break;
case 2:
// getSaleCount(*selectedType, *selectedRegion);
break;
case 3:
getMostPopularTypeIn(selectedRegion, allSales);
2024-11-22 13:52:35 +00:00
break;
2024-11-22 14:19:33 +00:00
case 4:
2024-11-23 06:27:03 +00:00
getOnlineVsOfflineIn(selectedRegion, allSales);
2024-11-22 14:19:33 +00:00
break;
default:
std::cout << "You should not be able to reach this!";
}
menu.MainInteraction();
2024-11-22 13:52:35 +00:00
}
}