Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include "Event.h"
- #include "Activity.h"
- #include "EventList.h"
- using namespace std;
- void openingPrompt();
- vector<string> parseInput(string inputToParse);
- string toLower(string wordToLower);
- void search(EventList & container);
- void cancel();
- void readFileToEventList(char* fileName, EventList &myEventList);
- enum
- {
- kIdIndex,
- kMonthIndex,
- kDayIndex,
- kYearIndex,
- kDescriptionStartIndex
- };
- void openingPrompt()
- {
- cout << endl;
- cout << "List of commands\n";
- cout << " 1. Display all events/activities\n" <<
- " 2. Search for events/activities\n" <<
- " 3. Cancel/delete an event/activity\n" <<
- " 4. Exit\n\n";
- cout << "Please enter the number of the desired function: ";
- }
- vector<string> parseInput(string inputToParse)
- {
- string token;
- stringstream rdr(inputToParse);
- vector<string> tokenList;
- while (rdr >> token)
- tokenList.push_back(token);
- return tokenList;
- }
- string toLower(string wordToLower)
- {
- std::transform(wordToLower.begin(), wordToLower.end(), wordToLower.begin(), ::tolower);
- return wordToLower;
- }
- void print(EventList & container)
- {
- container.print();
- }
- void search(EventList & container)
- {
- string toSearch;
- cout << "Please enter a word to search: ";
- cin.ignore();
- std::getline(std::cin, toSearch);
- vector<Event *> elemList = container.contains(toSearch);
- if (elemList.size() == 0)
- cout << " No match is found. " << endl;
- else
- {
- for (int count = 0; count < elemList.size(); count++)
- {
- cout << elemList[count]->display() << endl;
- }
- }
- }
- void cancel(EventList & container)
- {
- string toCancel;
- cout << "Please enter the ID of the event you wish to cancel: ";
- cin.ignore();
- getline(cin, toCancel);
- container.cancel(toCancel);
- }
- void readFileToEventList(char* fileName, EventList &myEventList)
- {
- string line;
- ifstream myfile(fileName);
- if (myfile.is_open())
- {
- while (getline(myfile, line))
- {
- vector<string> lineList;
- string description = "";
- string location = "";
- Event* newActivity = NULL;
- Event* newEvent = NULL;
- int found = 0;
- int dividerLocation;
- lineList = parseInput(line);
- int count = lineList.size();
- for (int i = kDescriptionStartIndex; i < count; i++)
- {
- if (i != kDescriptionStartIndex && i != count)
- {
- description += " ";
- description += lineList[i];
- }
- else if (i == kDescriptionStartIndex && i != count)
- description += lineList[i];
- }
- dividerLocation = description.find("|");
- if (dividerLocation > 0)
- {
- string parsedDescription = description.substr(0, dividerLocation - 1);
- string location = description.substr(dividerLocation + 2, description.length() - 1);
- newActivity = (Event *) new Activity(lineList[kIdIndex], lineList[kMonthIndex], lineList[kDayIndex], lineList[kYearIndex], parsedDescription, location);
- myEventList.add(newActivity);
- }
- else
- {
- newEvent = (Event *) new Event(lineList[kIdIndex], lineList[kMonthIndex], lineList[kDayIndex], lineList[kYearIndex], description);
- myEventList.add(newEvent);
- }
- }
- myfile.close();
- }
- }
- int main(int argc, char * argv[])
- {
- EventList myEventList;
- int userInput;
- string loweredUserInput;
- string specifier;
- vector<string> tokenList;
- if (argc == 2)
- {
- cout << "\nWelcome to the Activity Manager!\n";
- readFileToEventList(argv[1], myEventList);
- do
- {
- openingPrompt();
- cin >> userInput;
- cout << endl;
- if (userInput == 1)
- print(myEventList);
- else if (userInput == 2)
- {
- search(myEventList);
- }
- else if (userInput == 3)
- {
- cancel(myEventList);
- }
- else if (userInput != 4)
- cout << "Invalid command, please try again.\n";
- } while (userInput != 4);
- cout << "Thank you for using my program!";
- }
- else cout << "Unable to open file";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement