Advertisement
Guest User

ManageActivities.cpp

a guest
Nov 25th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include "Event.h"
  8. #include "Activity.h"
  9. #include "EventList.h"
  10. using namespace std;
  11.  
  12. void openingPrompt();
  13. vector<string> parseInput(string inputToParse);
  14. string toLower(string wordToLower);
  15. void search(EventList & container);
  16. void cancel();
  17. void readFileToEventList(char* fileName, EventList &myEventList);
  18.  
  19. enum
  20. {
  21. kIdIndex,
  22. kMonthIndex,
  23. kDayIndex,
  24. kYearIndex,
  25. kDescriptionStartIndex
  26. };
  27.  
  28. void openingPrompt()
  29. {
  30. cout << endl;
  31. cout << "List of commands\n";
  32. cout << " 1. Display all events/activities\n" <<
  33. " 2. Search for events/activities\n" <<
  34. " 3. Cancel/delete an event/activity\n" <<
  35. " 4. Exit\n\n";
  36. cout << "Please enter the number of the desired function: ";
  37. }
  38.  
  39. vector<string> parseInput(string inputToParse)
  40. {
  41. string token;
  42. stringstream rdr(inputToParse);
  43.  
  44. vector<string> tokenList;
  45. while (rdr >> token)
  46. tokenList.push_back(token);
  47. return tokenList;
  48. }
  49.  
  50. string toLower(string wordToLower)
  51. {
  52.  
  53. std::transform(wordToLower.begin(), wordToLower.end(), wordToLower.begin(), ::tolower);
  54. return wordToLower;
  55. }
  56.  
  57. void print(EventList & container)
  58. {
  59. container.print();
  60. }
  61.  
  62. void search(EventList & container)
  63. {
  64.  
  65. string toSearch;
  66.  
  67. cout << "Please enter a word to search: ";
  68. cin.ignore();
  69. std::getline(std::cin, toSearch);
  70.  
  71. vector<Event *> elemList = container.contains(toSearch);
  72.  
  73. if (elemList.size() == 0)
  74. cout << " No match is found. " << endl;
  75.  
  76. else
  77. {
  78. for (int count = 0; count < elemList.size(); count++)
  79. {
  80. cout << elemList[count]->display() << endl;
  81. }
  82. }
  83. }
  84.  
  85. void cancel(EventList & container)
  86. {
  87. string toCancel;
  88. cout << "Please enter the ID of the event you wish to cancel: ";
  89. cin.ignore();
  90. getline(cin, toCancel);
  91.  
  92. container.cancel(toCancel);
  93. }
  94.  
  95. void readFileToEventList(char* fileName, EventList &myEventList)
  96. {
  97. string line;
  98. ifstream myfile(fileName);
  99. if (myfile.is_open())
  100. {
  101. while (getline(myfile, line))
  102. {
  103. vector<string> lineList;
  104. string description = "";
  105. string location = "";
  106. Event* newActivity = NULL;
  107. Event* newEvent = NULL;
  108. int found = 0;
  109. int dividerLocation;
  110.  
  111. lineList = parseInput(line);
  112. int count = lineList.size();
  113.  
  114. for (int i = kDescriptionStartIndex; i < count; i++)
  115. {
  116. if (i != kDescriptionStartIndex && i != count)
  117. {
  118. description += " ";
  119. description += lineList[i];
  120. }
  121. else if (i == kDescriptionStartIndex && i != count)
  122. description += lineList[i];
  123. }
  124.  
  125. dividerLocation = description.find("|");
  126. if (dividerLocation > 0)
  127. {
  128. string parsedDescription = description.substr(0, dividerLocation - 1);
  129. string location = description.substr(dividerLocation + 2, description.length() - 1);
  130. newActivity = (Event *) new Activity(lineList[kIdIndex], lineList[kMonthIndex], lineList[kDayIndex], lineList[kYearIndex], parsedDescription, location);
  131. myEventList.add(newActivity);
  132. }
  133. else
  134. {
  135.  
  136. newEvent = (Event *) new Event(lineList[kIdIndex], lineList[kMonthIndex], lineList[kDayIndex], lineList[kYearIndex], description);
  137. myEventList.add(newEvent);
  138. }
  139.  
  140. }
  141. myfile.close();
  142. }
  143. }
  144.  
  145. int main(int argc, char * argv[])
  146. {
  147. EventList myEventList;
  148. int userInput;
  149. string loweredUserInput;
  150. string specifier;
  151. vector<string> tokenList;
  152.  
  153. if (argc == 2)
  154. {
  155. cout << "\nWelcome to the Activity Manager!\n";
  156.  
  157. readFileToEventList(argv[1], myEventList);
  158. do
  159. {
  160. openingPrompt();
  161. cin >> userInput;
  162. cout << endl;
  163.  
  164. if (userInput == 1)
  165. print(myEventList);
  166.  
  167. else if (userInput == 2)
  168. {
  169. search(myEventList);
  170. }
  171. else if (userInput == 3)
  172. {
  173. cancel(myEventList);
  174. }
  175. else if (userInput != 4)
  176. cout << "Invalid command, please try again.\n";
  177. } while (userInput != 4);
  178. cout << "Thank you for using my program!";
  179. }
  180. else cout << "Unable to open file";
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement