Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include "BST.h"
  7. #include <algorithm>
  8. #include "User.h"
  9. #include "HashFriend.h"
  10. #include "HashInterest.h"
  11. //#include "Graph.h"
  12. //#include "HashInterest.h"
  13. //#include "HashSomethingIDK.h"
  14.  
  15. using namespace std;
  16.  
  17. int logIn();
  18. int mainMenu();
  19. void viewFriends(int id);
  20. void searchNewFriends();
  21. void removeFriends(int id);
  22. void friendRecs();
  23. void viewProfile(int id);
  24.  
  25. vector<User> userID;
  26.  
  27. void readFile()
  28. {
  29. HashFriend hf;
  30. HashInterest hi;
  31. string firstName, lastName, userName, passWord, city, state, friendName, interest;
  32. string filename;
  33. unsigned id = 1;
  34.  
  35. User empty;
  36. userID.push_back(empty); // index 0 is a empty user
  37.  
  38. filename = "data.txt";
  39. ifstream inputfile;
  40. inputfile.open(filename);
  41.  
  42. assert(inputfile);
  43.  
  44. while(inputfile)
  45. {
  46. inputfile >> firstName;
  47. inputfile >> lastName;
  48. inputfile >> userName;
  49. inputfile >> passWord;
  50. inputfile.get(); // go to next line
  51. getline(inputfile, city, ',');
  52. inputfile.get();
  53. getline(inputfile, state);
  54.  
  55. User user(firstName, lastName, userName, passWord, city, state, id);
  56.  
  57. getline(inputfile, friendName);
  58. istringstream tempF(friendName);
  59. while(tempF)
  60. {
  61. getline(tempF, friendName, ',');
  62. if(tempF.peek() == ' ')
  63. tempF.get();
  64. user.setFriends(friendName);
  65. }
  66.  
  67. getline(inputfile, interest);
  68. istringstream tempI(interest);
  69. while(tempI)
  70. {
  71. getline(tempI, interest, ',');
  72. if(tempI.peek() == ' ')
  73. tempI.get();
  74. user.setInterests(interest);
  75. }
  76.  
  77. while(inputfile.peek() == '\n')
  78. inputfile.get(); // go to next line
  79. while(inputfile.peek() == ' ')
  80. inputfile.get(); // go to next line
  81.  
  82. userID.push_back(user); // insert to the id vector
  83. hf.insert(user);// insert to hashFriend
  84. hi.insert(user);// insert to hashInterest
  85. id ++;
  86. }
  87. inputfile.close();
  88. }
  89.  
  90. int main()
  91. {
  92. string name;
  93. int choice;
  94. int id = 0;
  95. readFile();
  96.  
  97. // Log in validation
  98. while (id <= 0)
  99. id = logIn();
  100.  
  101.  
  102. name = userID[id].getFirstname();
  103. cout << "\nWelcome, " << name << "!\n";
  104.  
  105.  
  106.  
  107. do{
  108. choice = mainMenu();
  109.  
  110. int temp = 1; //DELETE LATER, USED JUST FOR TESTING
  111.  
  112. switch(choice)
  113. {
  114. case 1: // view friends
  115. viewFriends(temp);
  116. break;
  117.  
  118. case 2: // search for new friends
  119. searchNewFriends();
  120. break;
  121.  
  122. case 3: // friends rec
  123. friendRecs();
  124. break;
  125.  
  126.  
  127. case 4: //quit
  128. {
  129. /*
  130. ofstream outfile;
  131. outfile.open("userinfo.txt");
  132. //print to outfile
  133. outfile.close();
  134. */
  135.  
  136.  
  137.  
  138. cout << " You are signed out"
  139. << "\n\t- GOODBYE! -" << endl;
  140. }
  141. }
  142.  
  143. }while(choice != 4);
  144.  
  145. return 0;
  146. }
  147.  
  148.  
  149. /// Returns bool for whether or not username & password combo are matched
  150. int logIn()
  151. {
  152. string un, pw, fn, ln, city, state;
  153. string choice = "0";
  154. int id;
  155. bool option = false;
  156.  
  157. while (!option)
  158. {
  159. while (choice != "1" && choice != "2" && choice != "3")
  160. {
  161. cout << " - WELCOME TO SPONGEBOOK -" << endl;
  162. cout << "1. Log in " << endl;
  163. cout << "2. Create an account " << endl;
  164. cout << "3. Quit " << endl;
  165. cout << "\nEnter choice: ";
  166. cin >> choice;
  167. cout << endl;
  168. }
  169.  
  170. if (choice == "1")
  171. {
  172. int count = 0;
  173.  
  174. while (true)
  175. {
  176. string userInput;
  177.  
  178. cout << "\t- LOG IN -" << endl
  179. << "Username: ";
  180. cin >> un;
  181. cout << "Password: ";
  182. cin >> pw;
  183. //un = "mynamesnotrick";
  184. //pw = "krabbypatty101";
  185. for (int i = 1; i <= userID.size(); i++)
  186. {
  187. if (un == userID[i].getUsername() && pw == userID[i].getPassword())
  188. {
  189. cout << "\n *** Login successful ***" << endl;
  190. id = i;
  191. option = true;
  192. return id;
  193. }
  194. }
  195.  
  196. cout << "\n*** Incorrect username or password. Please try again **\n\n";
  197. count ++;
  198.  
  199. if (count > 3)
  200. {
  201. while (userInput != "YES" && userInput != "NO")
  202. {
  203. cout << "Multiple failed attempts. Would you like to go back to the main menu? ";
  204. cin >> userInput;
  205. transform(userInput.begin(), userInput.end(), userInput.begin(),::toupper);
  206. }
  207.  
  208. if (userInput == "YES")
  209. {
  210. cout << endl;
  211. choice = "0";
  212. option = true;
  213. return false;
  214. }
  215. else if (userInput == "NO")
  216. {
  217. choice = "1";
  218. count = 0;
  219. }
  220. }
  221. }
  222. }
  223. else if (choice == "2")
  224. {
  225. cout << "\t- CREATE AN ACCOUNT -" << endl
  226. << "First name: ";
  227. cin >> fn;
  228.  
  229. cout << "Last name: ";
  230. cin >> ln;
  231.  
  232. cout << "Username: ";
  233. cin >> un;
  234.  
  235. cout << "Password: ";
  236. cin >> pw;
  237.  
  238. cout << "City: ";
  239. cin.ignore();
  240. getline(cin, city);
  241.  
  242. cout << "State: ";
  243. //cin >> ws; //COMMENT OUT IF USING WINDOWS
  244. cin >> state;
  245.  
  246. id = userID.size() + 1;
  247.  
  248. User newUser(fn, ln, un, pw, city, state, id);
  249. userID.push_back(newUser);
  250.  
  251. cout << "\n\t*** Account successfully created. You may now log in ***\n" << endl;
  252. choice = "0";
  253. }
  254. else if (choice == "3")
  255. {
  256. cout << "\t- GOODBYE! -" << endl;
  257. exit(0);
  258. }
  259. }
  260. return id;
  261. }
  262.  
  263.  
  264. /// Main Menu :)
  265. int mainMenu()
  266. {
  267. string input = "0";
  268. do{
  269. cout << "\n\t - MENU -" << endl
  270. << "1. View Friends" << endl
  271. << "2. Search for New Friends" << endl
  272. << "3. Friends Recommendations" << endl
  273. << "4. Quit\n\n"
  274. << "Enter choice: ";
  275. cin >> input;
  276.  
  277. if(!isdigit(input[0]))
  278. {
  279. cout << "Please enter numbers only.\n";
  280. input = "0";
  281. }
  282. else if(!(input == "1" || input == "2" || input == "3" || input == "4"))
  283. {
  284. cout << "Invalid input.\n";
  285. input = "0";
  286. }
  287.  
  288. }while(input == "0");
  289.  
  290. cout << endl;
  291. int choice = atoi(input.c_str());
  292.  
  293. return choice;
  294. }
  295.  
  296.  
  297. /// Menu option to view friends
  298. void viewFriends(int id)
  299. {
  300. //id = 1; //temporarily assigned to spongebob
  301.  
  302. string input = "0";
  303. User u = userID[id];
  304.  
  305.  
  306. cout << " - View Friends -" << endl;
  307.  
  308. cout << "1. View all friends\n"
  309. << "2. View a friend's profile\n"
  310. << "3. Remove a friend\n\n"
  311. << "Enter choice or 'm' for menu: ";
  312. cin >> input;
  313.  
  314. do{ if(input[0] == 'm')
  315. return;
  316. else if(!isdigit(input[0]))
  317. {
  318. cout << "Please enter numbers or 'm' only.\n\n";
  319. input = "0";
  320. }
  321. else if(!(input == "1" || input == "2" || input == "3"))
  322. {
  323. cout << "Invalid input.\n\n";
  324. input = "0";
  325. }
  326. else if (input == "2")
  327. {
  328. viewProfile(id);
  329. }
  330. else if (input == "3")
  331. {
  332. removeFriends(id);
  333. }
  334. else // input == 1 2 or 3
  335. {
  336. cout << "\n";
  337. u.printFriends();
  338. cout << "\n1. View a friend's profile\n"
  339. << "2. Remove a friend\n\n"
  340. << "Enter choice or 'm' for menu: ";
  341. cin >> input;
  342.  
  343. if (input == "1")
  344. {
  345. viewProfile(id);
  346. }
  347. else if (input == "2")
  348. {
  349. removeFriends(id);
  350. }
  351. else
  352. {
  353. cout << "Invalid input. Please try again.\n" << endl;
  354. }
  355.  
  356. }
  357.  
  358. }while(input == "0");
  359. cout << endl;
  360. }
  361.  
  362. void viewProfile(int id)
  363. {
  364. string name, fn, ln, input;
  365. bool results;
  366. bool option = true;
  367.  
  368. User u = userID[id]; // temporarily assigned to spongebob
  369.  
  370. while (option)
  371. {
  372. bool find = true;
  373. while (find)
  374. {
  375. cout << "\nEnter the name of the friend you'd like to view: " << endl;
  376. //cin.ignore();
  377. cout << " First Name: ";
  378. cin >> fn;
  379. cout << " Last Name: ";
  380. cin >> ln;
  381.  
  382. name = fn + " " + ln;
  383. results = u.searchFriend(name);
  384.  
  385. if (results == 0)
  386. {
  387. cout << "Friend not found. Please try again. \n" << endl;
  388. }
  389. else
  390. {
  391. for(int i = 1; i <= userID.size(); i++)
  392. {
  393. if (fn == userID[i].getFirstname() && ln == userID[i].getLastname())
  394. {
  395. cout << "\n-----------------------------------------"
  396. << "\nProfile of "
  397. << userID[i]
  398. << "-----------------------------------------\n";
  399.  
  400. }
  401. }
  402. find = false;
  403. }
  404. }
  405.  
  406. bool more = true;
  407.  
  408. while (more)
  409. {
  410. cout << "\nWould you like to view more friends? ";;
  411. cin >> input;
  412. transform(input.begin(), input.end(), input.begin(), ::toupper);
  413.  
  414. if (input == "NO")
  415. {
  416. cout << "Back to the main menu. " << endl;
  417. //mainMenu();
  418. more = false;
  419. option = false;
  420. }
  421. else if (input == "YES")
  422. {
  423. more = false;
  424. }
  425. else
  426. {
  427. cout << "Please enter only yes or no " << endl;
  428. }
  429. }
  430. }
  431. }
  432. /// Menu option to search for new friends
  433. void searchNewFriends()
  434. {
  435. string input = "0";
  436. cout << " - Search for New Friends -" << endl;
  437. do{
  438. cout << "1. Search by Name\n"
  439. << "2. Search by Interest\n\n"
  440. << "Enter choice or 'm' for menu: ";
  441. cin >> input;
  442.  
  443. if(input[0] == 'm')
  444. return;
  445. else if(!isdigit(input[0]))
  446. {
  447. cout << "Please enter numbers or 'm' only.\n\n";
  448. input = "0";
  449. }
  450. else if(!(input == "1" || input == "2"))
  451. {
  452. cout << "Invalid input.\n\n";
  453. input = "0";
  454. }
  455. else // input == 1 or 2
  456. {
  457. //int choice = atoi(input.c_str());
  458.  
  459. }
  460.  
  461. }while(input == "0");
  462. cout << endl;
  463. }
  464.  
  465.  
  466. /// Menu option to get friend recommendations
  467. void friendRecs()
  468. {
  469. cout << " - Friend Recommendations -" << endl
  470. << "People you may know:\n\n"
  471. // print all the ppl using graph (?)
  472. << "Enter choice or 'm' for menu: ";
  473. cout << endl;
  474. }
  475.  
  476. void removeFriends(int id)
  477. {
  478. string name, input;
  479. bool results;
  480. bool option = true;
  481.  
  482. User u = userID[id]; // temporarily assigned to spongebob
  483.  
  484. while (option)
  485. {
  486. bool find = true;
  487. while (find)
  488. {
  489. cout << "Enter the name of the friend you'd like to remove: " << endl;
  490. cin.ignore();
  491. getline(cin, name);
  492. results = u.searchFriend(name);
  493.  
  494. if (results == 0)
  495. {
  496. cout << "Friend not found. Please try again. \n" << endl;
  497. }
  498. else
  499. {
  500. u.removeFriend(name);
  501. u.printFriends();
  502. find = false;
  503. }
  504. }
  505.  
  506. bool more = true;
  507.  
  508. while (more)
  509. {
  510. cout << "Would you like to remove more friends? ";;
  511. cin >> input;
  512. transform(input.begin(), input.end(), input.begin(), ::toupper);
  513.  
  514. if (input == "NO")
  515. {
  516. cout << "Back to the main menu. " << endl;
  517. //mainMenu();
  518. more = false;
  519. option = false;
  520. }
  521. else if (input == "YES")
  522. {
  523. more = false;
  524. }
  525. else
  526. {
  527. cout << "Please enter only yes or no " << endl;
  528. }
  529. }
  530. }
  531. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement