Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdio.h>
- #include <iomanip>
- #include <sstream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- struct Data {
- string number;
- string theme;
- string name;
- string minifigs;
- string pieces;
- string price;
- Data(string number_, string theme_, string name_, string mini_, string pieces_, string price_) {
- number_ = number;
- theme_ = theme;
- name_ = name;
- mini_ = minifigs;
- pieces_ = pieces;
- price_ = price;
- }
- };
- ifstream legoFolder;
- vector<Data> fileData;
- int option;
- string readDataFileChoice(int option) {
- switch (option) {
- case 1:
- return "lego1.csv";
- break;
- case 2:
- return "lego2.csv";
- break;
- case 3:
- return "lego3.csv";
- break;
- default:
- cout << "Invalid Response" << endl;
- }
- return "";
- }
- string chosenFile = readDataFileChoice(option);
- void readFileData(int option, string chosenFile, vector<Data>& fileData) {
- if (option < 4) {
- legoFolder.open(chosenFile);
- string lineFromFile;
- istringstream stream(lineFromFile);
- string number;
- string theme;
- string name;
- string minifigs;
- string pieces;
- string price;
- int numOfSets = 0;
- while (getline(legoFolder, lineFromFile)) {
- numOfSets++;
- getline(stream, number, ',');
- getline(stream, theme, ',');
- getline(stream, name, ',');
- getline(stream, minifigs, ',');
- getline(stream, pieces, ',');
- getline(stream, price);
- Data data(number, theme, name, minifigs, pieces, price);
- fileData.push_back(data);
- }
- cout << "Total number of sets: " << numOfSets << endl;
- }
- }
- void mostExp(vector<Data>& fileData) {
- vector<int> mostExpPrice;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- cout << i << ": " << fileData[i].price << endl;
- double convertToD = stod(fileData[i].price);
- mostExpPrice.push_back(convertToD);
- }
- double max = mostExpPrice[0];
- for (unsigned int i = 0; i < mostExpPrice.size(); i++) {
- if (max < mostExpPrice[i]) {
- max = mostExpPrice[i];
- }
- }
- for (unsigned int i = 0; i < mostExpPrice.size(); i++) {
- if (max == mostExpPrice[i]){
- cout << "The most expensive set is:" << endl;
- cout << "Name: " << fileData[i].name << endl;
- cout << "Number: " << fileData[i].number << endl;
- cout << "Theme: " << fileData[i].theme << endl;
- cout << "Price: $" << fileData[i].price << endl;
- cout << "Minifigures: " << fileData[i].minifigs << endl;
- cout << "Piece count: " << fileData[i].pieces << endl;
- }
- }
- }
- void pieceCount(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All Files") {
- vector<int> largestPieceSet;
- for (unsigned int i = 1; i < fileData.size(); i++) {
- int convertToInt = stoi(fileData[i].pieces);
- largestPieceSet.push_back(convertToInt);
- }
- int highest = largestPieceSet[1];
- for (unsigned int i = 1; i < largestPieceSet.size(); i++) {
- if (highest < largestPieceSet[i]) {
- highest = largestPieceSet[i];
- }
- }
- for (unsigned int i = 1; i < largestPieceSet.size(); i++) {
- if (highest == largestPieceSet[i]){
- cout << "The set with the highest parts count:" << endl;
- cout << "Name: " << fileData[i].name << endl;
- cout << "Number: " << fileData[i].number << endl;
- cout << "Theme: " << fileData[i].theme << endl;
- cout << "Price: $" << fileData[i].price << endl;
- cout << "Minifigures: " << fileData[i].minifigs << endl;
- cout << "Piece count: " << fileData[i].pieces << endl;
- }
- }
- }
- }
- void searchName(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All files") {
- vector<string> matchingList;
- cout << "Enter a name to find:" << endl;
- string nameChoice;
- cin >> nameChoice;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- if (fileData[i].name.find(nameChoice)) {
- matchingList.push_back(fileData[i].number);
- matchingList.push_back(fileData[i].name);
- matchingList.push_back(fileData[i].price);
- }
- }
- for (unsigned int i = 0; i < matchingList.size(); i++) {
- cout << matchingList[i] << " ";
- if(i%3 == 0) {
- cout << endl;
- }
- }
- if (matchingList.empty()) {
- cout << "No sets found matching that search term" << endl;
- }
- }
- }
- void searchTheme(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All Files") {
- vector<string> matchingList;
- cout << "Enter a theme to find:" << endl;
- string themeChoice;
- cin >> themeChoice;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- if (fileData[i].theme.find(themeChoice)) {
- matchingList.push_back(fileData[i].number);
- matchingList.push_back(fileData[i].name);
- matchingList.push_back(fileData[i].price);
- }
- }
- for (unsigned int i = 0; i < matchingList.size(); i++) {
- cout << matchingList[i] << " ";
- if (i%3 == 0) {
- cout << endl;
- }
- if (matchingList.empty()) {
- cout << "No sets found matching that search term" << endl;
- }
- }
- }
- }
- void avgCount(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All Files") {
- double sum = 0;
- vector<int> countedPieces;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int oneSet = stoi(fileData[i].pieces);
- countedPieces.push_back(oneSet);
- }
- for (int c:countedPieces) {
- sum = sum + c;
- }
- double average = (sum/countedPieces.size());
- cout << "Average part count for " << countedPieces.size() << " sets: " << average << endl;
- }
- }
- void avgPrice(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All Files") {
- double sum = 0;
- vector<int> countedPrices;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int oneSet = stoi(fileData[i].price);
- countedPrices.push_back(oneSet);
- }
- for (int c:countedPrices) {
- sum = sum + c;
- }
- double average = (sum/countedPrices.size());
- cout << "Average price information for " << countedPrices.size() << " sets: $" << average << endl;
- int min = countedPrices[0];
- for (unsigned int i = 0; i < countedPrices.size(); i++) {
- if (min > countedPrices[i]) {
- min = countedPrices[i];
- }
- }
- for (unsigned int i = 0; i < countedPrices.size(); i++) {
- if (min == countedPrices[i]) {
- cout << "Set with the minimum price:" << endl;
- cout << "Name: " << fileData[i].name << endl;
- cout << "Number: " << fileData[i].number << endl;
- cout << "Theme: " << fileData[i].theme << endl;
- cout << "Price: $" << fileData[i].price << endl;
- cout << "Minifigures: " << fileData[i].minifigs << endl;
- cout << "Piece count: " << fileData[i].pieces << endl;
- }
- }
- int max = countedPrices[0];
- for (unsigned int i = 0; i < countedPrices.size(); i++) {
- if (max < countedPrices[i]) {
- max = countedPrices[i];
- }
- }
- for (unsigned int i = 0; i < countedPrices.size(); i++) {
- if (max == countedPrices[i]) {
- cout << "Set with the maximum price:" << endl;
- cout << "Name: " << fileData[i].name << endl;
- cout << "Number: " << fileData[i].number << endl;
- cout << "Theme: " << fileData[i].theme << endl;
- cout << "Price: $" << fileData[i].price << endl;
- cout << "Minifigures: " << fileData[i].minifigs << endl;
- cout << "Piece Count: " << fileData[i].pieces << endl;
- }
- }
- }
- }
- void printMinifigData(string chosenFile, vector<Data>& fileData) {
- if (chosenFile != "All Files") {
- double sum = 0;
- vector<int> countedMinifigs;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int oneSet = stoi(fileData[i].minifigs);
- countedMinifigs.push_back(oneSet);
- }
- for (int c:countedMinifigs) {
- sum = sum + c;
- }
- double average = (sum/countedMinifigs.size());
- cout << "Average number of minifigurines: " << average << endl;
- int max = countedMinifigs[0];
- for (unsigned int i = 0; i < countedMinifigs.size(); i++) {
- if (max < countedMinifigs[i]) {
- max = countedMinifigs[i];
- }
- }
- for (unsigned int i = 0; i < countedMinifigs.size(); i++) {
- if (max == countedMinifigs[i]) {
- cout << "Set with the most minifigurines:" << endl;
- cout << "Name: " << fileData[i].name << endl;
- cout << "Number: " << fileData[i].number << endl;
- cout << "Theme: " << fileData[i].theme << endl;
- cout << "Price: $" << fileData[i].price << endl;
- cout << "Minifigures: " << fileData[i].minifigs << endl;
- cout << "Piece Count: " << fileData[i].pieces << endl;
- }
- }
- }
- }
- void ifYouBought(vector<Data>& fileData) {
- int sum = 0;
- vector<int> countedPrices;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int oneSet = stoi(fileData[i].price);
- countedPrices.push_back(oneSet);
- }
- for (int c:countedPrices) {
- sum = sum + c;
- }
- cout << "It would cost: $" << sum << endl;
- int pieceSum = 0;
- vector<int> countedPieces;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int setPieces = stoi(fileData[i].pieces);
- countedPieces.push_back(setPieces);
- }
- for (int p:countedPieces) {
- pieceSum = pieceSum + p;
- }
- cout << "You would have " << pieceSum << " pieces in your collection" << endl;
- int minifigSum = 0;
- vector<int> countedMinifigs;
- for (unsigned int i = 0; i < fileData.size(); i++) {
- int setMinifig = stoi(fileData[i].minifigs);
- countedMinifigs.push_back(setMinifig);
- }
- for (int m:countedMinifigs) {
- minifigSum = minifigSum + m;
- }
- cout << "You would have an army of " << minifigSum << " mini_figurines!" << endl;
- }
- void taskChoice(int choice) {
- if (choice == 1) {
- mostExp(fileData);
- }
- else if (choice == 2) {
- pieceCount(chosenFile, fileData);
- }
- else if (choice == 3) {
- searchName(chosenFile, fileData);
- }
- else if (choice == 4) {
- searchTheme(chosenFile, fileData);
- }
- else if (choice == 5) {
- avgCount(chosenFile, fileData);
- }
- else if (choice == 6) {
- avgPrice(chosenFile, fileData);
- }
- else if (choice == 7) {
- printMinifigData(chosenFile, fileData);
- }
- else if (choice == 8) {
- ifYouBought(fileData);
- }
- else {
- exit(0);
- }
- }
- int main()
- {
- cout << std::fixed << setprecision(2);
- cout << "Which file(s) to open?\n";
- cout << "1. lego1.csv" << endl;
- cout << "2. lego2.csv" << endl;
- cout << "3. lego3.csv" << endl;
- cout << "4. All 3 files" << endl;
- cin >> option;
- if (option < 4) {
- readFileData(option, readDataFileChoice(option), fileData);
- }
- cout << "1. Most Expensive set" << endl;
- cout << "2. Largest piece count" << endl;
- cout << "3. Search for set name containing..." << endl;
- cout << "4. Search themes..." << endl;
- cout << "5. Part count information" << endl;
- cout << "6. Price information" << endl;
- cout << "7. Minifigure information" << endl;
- cout << "8. If you bought one of everything..." << endl;
- cout << "0. Exit" << endl;
- int choice;
- cin >> choice;
- cin.get(); // Clear newline character for any later input
- taskChoice(choice);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement