From 616970a7f6257ad13ad5a66dbfd4eafcb9640422 Mon Sep 17 00:00:00 2001 From: didyouexpectthat Date: Wed, 19 Apr 2023 22:19:59 -0700 Subject: [PATCH] Add files via upload --- Items.cpp | 229 +++++++++++++++++++++++++++++++++++++++ Items.h | 52 +++++++++ Project3.aps | Bin 0 -> 2816 bytes Project3.rc | 100 +++++++++++++++++ Project3.vcxproj | 155 ++++++++++++++++++++++++++ Project3.vcxproj.filters | 38 +++++++ Project3.vcxproj.user | 4 + inputfile.txt | 104 ++++++++++++++++++ main.cpp | 82 ++++++++++++++ resource.h | 14 +++ 10 files changed, 778 insertions(+) create mode 100644 Items.cpp create mode 100644 Items.h create mode 100644 Project3.aps create mode 100644 Project3.rc create mode 100644 Project3.vcxproj create mode 100644 Project3.vcxproj.filters create mode 100644 Project3.vcxproj.user create mode 100644 inputfile.txt create mode 100644 main.cpp create mode 100644 resource.h diff --git a/Items.cpp b/Items.cpp new file mode 100644 index 0000000..3107b86 --- /dev/null +++ b/Items.cpp @@ -0,0 +1,229 @@ +/* Cody Cook + 2023/04/13 + CS-210 Project 3 +*/ +#include "Items.h" + +// constructors + +// default constructor; parses default file name +Items::Items() +{ + if (!ParseInputFile()) + { + // try getting a new file + RequestNewFile(); + } +} + +// constructor with a custom input option +Items::Items(string p_inputFile = defaultItemFileName) +{ + SetInputFile(p_inputFile); + if (!ParseInputFile()) + { + // try geting a new file + RequestNewFile(); + } +} + +// destructors + +// run a set of functions when the class closes +Items::~Items() +{ + if (r_input.is_open()) + { + r_input.close(); + } +} + +// getters + +// prints and returns the inventory for a named item +int Items::GetInventory(string p_item) +{ + // search the string in the map + auto it = r_mapping.find(p_item); + + // if there were results + if (it != r_mapping.end()) + { + string times; // allow for more natural responses with the word "time" + + // if there is only one item, it is only there "one time" + if (it->second == 1) + { + times = " time."; + } + else + { + // if it is 0 or 2 or more, then it is "times" + times = " times."; + } + + // output the quantity purchased + cout << c_red << it->first << c_normal + << " were purchased "; + cout << c_red << it->second << c_normal; + cout << times << endl; + return it->second; + } + else + { + // because there wasn't quantity, explain it wasn't purchased. + cerr << c_red << p_item << c_normal + << " were not purchased." << endl; + return 0; + } +} + +// return the string for the inputFile +string Items::GetInputFile() +{ + return r_inputFile; +} + +char Items::GetStar() +{ + return r_star; +} + +// setters + +// update the filename of the input file +void Items::SetInputFile(string p_inputFile) +{ + r_inputFile = p_inputFile; +} + +// change the star used in the histogram +void Items::SetStar(char p_star) +{ + r_star = p_star; +} + +// methods + +// ParseInputFile reads the input file and stores the data in a map; it also runs a backup of the data +bool Items::ParseInputFile() +{ + // try opening the file + r_input.open(r_inputFile); + + // if the file could not be opened + if (!r_input.is_open()) + { + // error out + cout << "Error opening file " + << c_red << r_inputFile << c_normal << endl; + return false; + } + clog << endl + << "Parsing " + << c_red << r_inputFile << c_normal + << "... "; + + string line; + // while we go through each line of the file + while (getline(r_input, line)) + { + // Increment the frequency of the current line into the map + r_mapping[line]++; + } + // close the file + r_input.close(); + + // run a backup of this data and return true + SaveInventory(); + return true; +} + +// output the inventory based on the config +void Items::PrintInventory(int p_type) +{ + // find the length of the longest item to help align the output + size_t longest = 0; + for (const auto &entry : r_mapping) + { + if (entry.first.length() > longest) + { + longest = entry.first.length(); + } + } + + // output the inventory + for (const auto &entry : r_mapping) + { + // output the item name with the longest item name as the width + cout << setw(longest) << entry.first << " "; + + // see the quantity as a number + if (p_type == 1) + { + cout << c_red << entry.second << c_normal << endl; + } + // see the quantity as a histogram + else if (p_type == 2) + { + cout << c_red << string(entry.second, r_star) << c_normal << endl; + } + } +} + +// SaveInventory creates a backup of the data in the map +bool Items::SaveInventory() +{ + // open file for backup + ofstream file(r_database); + + // if the file isn't open + if (!file.is_open()) + { + // then there was a opening the file; so warn the user and exit the function + cerr << "Error opening " << c_red << r_database << c_normal << " for backup. Please check your permissions."; + return false; + } + + // iterate through the mappings and save them to the file + for (const auto &entry : r_mapping) + { + file << entry.first << " " << entry.second << endl; + } + + // save and close the file + file.close(); + + // output a successful save and return true + cout << "Backup file " + << c_red << r_database << c_normal + << " updated." << endl; + cout << endl; + return true; +} + +void Items::RequestNewFile() +{ + string newFile; + bool moveOn = false; + while (!moveOn) + { + cout << endl; + cout << "Type " << c_red << "exit" << c_normal << " to quit, or enter the filename to import: "; + cout << c_red; + cin >> newFile; + cout << c_normal; + + if (newFile == "exit") + { + exit(0); + } + + SetInputFile(newFile); + + if (ParseInputFile()) + { + moveOn = true; + } + } +} diff --git a/Items.h b/Items.h new file mode 100644 index 0000000..e86dadf --- /dev/null +++ b/Items.h @@ -0,0 +1,52 @@ +/* Cody Cook + 2023/04/13 + CS-210 Project 3 +*/ +#include // use cout and cin +#include // use strings +#include // use maps +#include // use files +#include // use setw + +using namespace std; // use the standard namespace + +#pragma once // only include this file onces + +// set some constants +constexpr auto defaultItemFileName = "inputfile.txt"; // the default filename to import +constexpr auto defaultDatabaseFilename = "frequency.dat"; // the default filename to export +constexpr auto c_red = "\033[31m"; // set the terminal red color +constexpr auto c_normal = "\033[0m"; // set the terminal normal color + +class Items +{ +public: + // constructors + Items(); // default constructor + Items(string p_inputFile); // constructor with a custom import file + + //destructor + ~Items(); // destructor + + // getters + string GetInputFile(); // return the string for the inputFile + int GetInventory(string p_item); // return the quantity of a specific item + char GetStar(); // get the configured star + + //setters + void SetInputFile(string p_inputFile); // update the filename of the input file + void SetStar(char p_star); // set the star char + + //methods + void RequestNewFile(); // ask user to set new filename + bool ParseInputFile(); // parse the configured input file + void PrintInventory(int p_type); // output the inventory in the map + bool SaveInventory(); // save the inventory to a file + +private: + string r_inputFile = defaultItemFileName; // the default file to import + ifstream r_input; // the input file + map r_mapping; // the map of items and their quantities + string r_database = defaultDatabaseFilename; // the default filename to export + char r_star = '*'; // default star for the histogram +}; diff --git a/Project3.aps b/Project3.aps new file mode 100644 index 0000000000000000000000000000000000000000..72407a4b06cf17e3ea7c8722a255f47f484c7c7b GIT binary patch literal 2816 zcmb7G&2HO95dPSxK%F#bP!vHg1(=^ha*#^dZrb`%$RuqOkx7A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+ Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {afa661b1-ec92-446b-8827-0e94c067e063} + Project3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Project3.vcxproj.filters b/Project3.vcxproj.filters new file mode 100644 index 0000000..8facd36 --- /dev/null +++ b/Project3.vcxproj.filters @@ -0,0 +1,38 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/Project3.vcxproj.user b/Project3.vcxproj.user new file mode 100644 index 0000000..0f14913 --- /dev/null +++ b/Project3.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/inputfile.txt b/inputfile.txt new file mode 100644 index 0000000..2fa499b --- /dev/null +++ b/inputfile.txt @@ -0,0 +1,104 @@ +Spinach +Radishes +Broccoli +Peas +Cranberries +Broccoli +Potatoes +Cucumbers +Radishes +Cranberries +Peaches +Zucchini +Potatoes +Cranberries +Cantaloupe +Beets +Cauliflower +Cranberries +Peas +Zucchini +Peas +Onions +Potatoes +Cauliflower +Spinach +Radishes +Onions +Zucchini +Cranberries +Peaches +Yams +Zucchini +Apples +Cucumbers +Broccoli +Cranberries +Beets +Peas +Cauliflower +Potatoes +Cauliflower +Celery +Cranberries +Limes +Cranberries +Broccoli +Spinach +Broccoli +Garlic +Cauliflower +Pumpkins +Celery +Peas +Potatoes +Yams +Zucchini +Cranberries +Cantaloupe +Zucchini +Pumpkins +Cauliflower +Yams +Pears +Peaches +Apples +Zucchini +Cranberries +Zucchini +Garlic +Broccoli +Garlic +Onions +Spinach +Cucumbers +Cucumbers +Garlic +Spinach +Peaches +Cucumbers +Broccoli +Zucchini +Peas +Celery +Cucumbers +Celery +Yams +Garlic +Cucumbers +Peas +Beets +Yams +Peas +Apples +Peaches +Garlic +Celery +Garlic +Cucumbers +Garlic +Apples +Celery +Zucchini +Cucumbers +Onions \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..318992e --- /dev/null +++ b/main.cpp @@ -0,0 +1,82 @@ +/* Cody Cook + 2023/04/13 + CS-210 Project 3 +*/ +#include // use cout and cin +#include // for use in menuItems + +#include "Items.h" + +using namespace std; + +void menu() +{ + unsigned int input = 0; + Items Items; + string search; + + // create a vector of the menu items + vector menuItems = { + "Check the purchase frequency of an item.", // input 1 + "Display the purchase frequency of all items.", // input 2 + "Display the purchase frequency of all items in a histogram.", // input 3 + "Exit." // input 4 + }; + + // loop until the user enters the exit input + while (input != 4) + { + cout << "Corner Grocer: Inventory Menu\n\n"; + for (unsigned int i = 0; i < menuItems.size(); i++) + { + cout << c_red << i + 1 << c_normal << ". " << menuItems[i] << endl; + } + cout << endl + << "Enter your choice: " << c_red; + cin >> input; + cout << c_normal; + + // if the user enters a non-integer + if (cin.fail()) + { + cin.clear(); // clear the error flag + cin.ignore(1000, '\n'); // ignore the rest of the input + input = 0; // set the input to 0 + } + cout << endl; + switch (input) + { + case 1: + cout << "Enter an item you wish to check the purchase frequency: "; + cout << c_red; + cin >> search; + cout << c_normal; + Items.GetInventory(search); + break; + case 2: + Items.PrintInventory(1); + break; + case 3: + Items.PrintInventory(2); + break; + case 4: + cout << "Goodbye!" << endl; + break; + default: + cerr << "Invalid input. Please try again." << endl; + break; + } + cout << endl; + system("pause"); + cout << endl; + } +} + +int main() +{ + // call the menu function + menu(); + + // return successful + return 0; +} diff --git a/resource.h b/resource.h new file mode 100644 index 0000000..868ad17 --- /dev/null +++ b/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by Project3.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif