Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <cctype> // for isdigit
  7. #include "User.h"
  8. #include "HashFriend.h"
  9. #include "HashInterest.h"
  10. #include "Graph.h"
  11. #include <algorithm>
  12. //#include "Graph.h"
  13. //#include "HashInterest.h"
  14. //#include "HashSomethingIDK.h"
  15.  
  16. using namespace std;
  17.  
  18. int logIn();
  19. int mainMenu();
  20. void viewFriends(int id);
  21. void searchNewFriends(HashFriend& hf, HashInterest& hi, int id);
  22. void removeFriends(int id);
  23. void readFile(HashFriend& hf, HashInterest& hi);
  24. void friendRecs();
  25. void searchByFriend(HashFriend& hf, int id);
  26. void searchByInterest(HashInterest& hi, int id);
  27. string createAccount();
  28. void viewProfile(int id);
  29. void friendRecs(int id, HashFriend& hf);
  30. void outputFile();
  31.  
  32. vector<User> userID;
  33.  
  34. void readFile(HashFriend& hf, HashInterest& hi)
  35. {
  36. string firstName, lastName, userName, passWord, city, state, friendName, interest;
  37. string filename;
  38. unsigned id = 1;
  39.  
  40. User empty;
  41. userID.push_back(empty); // index 0 is a empty user
  42.  
  43. filename = "data.txt";
  44. ifstream inputfile;
  45. inputfile.open(filename);
  46.  
  47. assert(inputfile);
  48.  
  49. while(inputfile)
  50. {
  51. inputfile >> firstName;
  52. inputfile >> lastName;
  53. inputfile >> userName;
  54. inputfile >> passWord;
  55. inputfile.get(); // go to next line
  56. getline(inputfile, city, ',');
  57. getline(inputfile, state);
  58.  
  59. User user(firstName, lastName, userName, passWord, city, state, id);
  60.  
  61. getline(inputfile, friendName);
  62. istringstream tempF(friendName);
  63. while(tempF)
  64. {
  65. getline(tempF, friendName, ',');
  66. if(tempF.peek() == ' ')
  67. tempF.get();
  68. user.setALL(friendName);
  69. }
  70.  
  71. getline(inputfile, interest);
  72. istringstream tempI(interest);
  73. while(tempI)
  74. {
  75. getline(tempI, interest, ',');
  76. if(tempI.peek() == ' ')
  77. tempI.get();
  78. user.setInterests(interest);
  79. }
  80.  
  81. while(inputfile.peek() == '\n')
  82. inputfile.get(); // go to next line
  83. while(inputfile.peek() == ' ')
  84. inputfile.get(); // go to next line
  85.  
  86. userID.push_back(user); // insert to the id vector
  87. hf.insert(user);// insert to hashFriend
  88. hi.insert(user);// insert to hashInterest
  89. id ++;
  90. }
  91. }
  92.  
  93. int main()
  94. {
  95. HashFriend hf;
  96. HashInterest hi;
  97. string name;
  98. int choice;
  99. int id = 0;
  100. readFile(hf,hi);
  101.  
  102.  
  103. // Log in validation
  104. while (id <= 0)
  105. id = logIn();
  106.  
  107. name = "<" + userID[id].getFirstname() + " " + userID[id].getLastname() + ">";
  108. cout << "\n\nWelcome, " << name << "!\n";
  109.  
  110.  
  111.  
  112. do{
  113. choice = mainMenu();
  114.  
  115. switch(choice)
  116. {
  117. case 1: // view friends
  118. viewFriends(id);
  119. break;
  120.  
  121. case 2: // search for new friends
  122. searchNewFriends(hf, hi, id);
  123. break;
  124.  
  125. case 3: // friends rec
  126. friendRecs(id, hf);
  127. break;
  128.  
  129.  
  130. case 4: //quit
  131. {
  132. outputFile();
  133.  
  134. cout << " You are signed out"
  135. << "\n\t- GOODBYE! -" << endl;
  136. }
  137. }
  138.  
  139. }while(choice != 4);
  140.  
  141. return 0;
  142. }
  143.  
  144.  
  145. /// Returns bool for whether or not username & password combo are matched
  146. int logIn()
  147. {
  148. string un, pw, fn, ln, city, state;
  149. int choice = 0;
  150. int id;
  151. bool option = false;
  152.  
  153. while (!option)
  154. {
  155. while (choice != 1 && choice != 2 && choice != 3)
  156. {
  157. cout << "\t- WELCOME TO SPONGEBOOK -" << endl;
  158. cout << "1. Log in " << endl;
  159. cout << "2. Create an account " << endl;
  160. cout << "3. Quit " << endl;
  161. cout << "\nEnter choice: ";
  162. cin >> choice;
  163. cout << endl;
  164. }
  165.  
  166. if (choice == 1)
  167. {
  168. int count = 0;
  169.  
  170. while (true)
  171. {
  172. string userInput;
  173.  
  174. cout << "\t- LOG IN -" << endl
  175. << "Username: ";
  176. cin >> un;
  177. cout << "Password: ";
  178. cin >> pw;
  179.  
  180. for (unsigned i = 1; i <= userID.size(); i++)
  181. {
  182. if (un == userID[i].getUsername() && pw == userID[i].getPassword())
  183. {
  184. cout << "\n\t*** Login successful ***\n" << endl;
  185. id = i;
  186. option = true;
  187. return id;
  188. }
  189. }
  190.  
  191. cout << "\n\t*** Incorrect username or password. Please try again **" << endl;
  192. count ++;
  193.  
  194. if (count > 3)
  195. {
  196. while (userInput != "YES" && userInput != "NO")
  197. {
  198. cout << "\nMultiple failed attempts. Would you like to go back to the main menu? ";
  199. cin >> userInput;
  200. transform(userInput.begin(), userInput.end(), userInput.begin(),::toupper);
  201. }
  202.  
  203. if (userInput == "YES")
  204. {
  205. cout << endl;
  206. choice = 0;
  207. option = true;
  208. return false;
  209. }
  210. else if (userInput == "NO")
  211. {
  212. choice = 1;
  213. count = 0;
  214. }
  215. }
  216. }
  217. }
  218.  
  219. else if (choice == 2)
  220. {
  221. string temp = createAccount();
  222. choice = atoi(temp.c_str());
  223. }
  224. else if (choice == 3)
  225. {
  226. cout << "\t- GOODBYE! -" << endl;
  227. exit(0);
  228. }
  229. }
  230. return id;
  231.  
  232. }
  233.  
  234. string createAccount()
  235. {
  236. string un, pw, fn, ln, city, state, choice;
  237. int id;
  238. cout << "\t- CREATE AN ACCOUNT -" << endl
  239. << "First name: ";
  240. cin >> fn;
  241.  
  242. cout << "Last name: ";
  243. cin >> ln;
  244.  
  245. cout << "Username: ";
  246. cin >> un;
  247.  
  248. cout << "Password: ";
  249. cin >> pw;
  250.  
  251. cout << "City: ";
  252. cin.ignore();
  253. getline(cin, city);
  254.  
  255. cout << "State: ";
  256. cin >> ws;
  257. cin >> state;
  258.  
  259. id = userID.size() + 1;
  260.  
  261. User newUser(fn, ln, un, pw, city, state, id);
  262. userID.push_back(newUser);
  263.  
  264. bool more = true;
  265. string interests;
  266.  
  267. while (more)
  268. {
  269. cout << "Enter interest or 'stop' to finish: ";
  270. cin >> interests;
  271.  
  272. transform(interests.begin(), interests.end(), interests.begin(),::toupper);
  273.  
  274. if (interests == "STOP")
  275. more = false;
  276. else
  277. {
  278. transform(interests.begin(), interests.end(), interests.begin(),::tolower);
  279. newUser.setInterests(interests);
  280. }
  281. }
  282.  
  283. cout << "\n\t*** Account successfully created. You may now log in ***\n" << endl;
  284.  
  285. return choice = "0";
  286. }
  287.  
  288. /// Main Menu :)
  289. int mainMenu()
  290. {
  291. string input = "0";
  292. do{
  293. cout << "\n\t - MENU -" << endl
  294. << "1. View Friends" << endl
  295. << "2. Search for New Friends" << endl
  296. << "3. Friends Recommendations" << endl
  297. << "4. Quit\n\n"
  298. << "Enter choice: ";
  299. cin >> input;
  300.  
  301. if(!isdigit(input[0]))
  302. {
  303. cout << "Please enter numbers only.\n";
  304. input = "0";
  305. }
  306. else if(!(input == "1" || input == "2" || input == "3" || input == "4"))
  307. {
  308. cout << "Invalid input.\n";
  309. input = "0";
  310. }
  311.  
  312. }while(input == "0");
  313.  
  314. cout << endl;
  315. int choice = atoi(input.c_str());
  316.  
  317. return choice;
  318. }
  319.  
  320.  
  321. /// Menu option to view friends
  322. void viewFriends(int id)
  323. {
  324. string input = "0";
  325.  
  326. cout << " - View Friends -" << endl;
  327.  
  328. cout << "1. View all friends\n"
  329. << "2. View a friend's profile\n"
  330. << "3. Remove a friend\n\n"
  331. << "Enter choice or 'm' for menu: ";
  332. cin >> input;
  333.  
  334. do{ if(input[0] == 'm')
  335. return;
  336. else if(!isdigit(input[0]))
  337. {
  338. cout << "Please enter numbers or 'm' only.\n\n";
  339. input = "0";
  340. }
  341. else if(!(input == "1" || input == "2" || input == "3"))
  342. {
  343. cout << "Invalid input.\n\n";
  344. input = "0";
  345. }
  346. else if (input == "2")
  347. {
  348. viewProfile(id);
  349. }
  350. else if (input == "3")
  351. {
  352. removeFriends(id);
  353. }
  354. else // input == 1 2 or 3
  355. {
  356. cout << "Your friends printing below:";
  357. cout << "\n-----------------------------------------\n";
  358. userID[id].printFriends();
  359. cout << "-----------------------------------------\n";
  360. cout << "1. View a friend's profile\n"
  361. << "2. Remove a friend\n\n"
  362. << "Enter choice or 'm' for menu: ";
  363. cin >> input;
  364.  
  365. if (input == "1")
  366. {
  367. viewProfile(id);
  368. }
  369. else if (input == "2")
  370. {
  371. removeFriends(id);
  372. }
  373. else
  374. {
  375. cout << "Invalid input. Please try again.\n" << endl;
  376. }
  377.  
  378. }
  379.  
  380. }while(input == "0");
  381. cout << endl;
  382. }
  383.  
  384. void viewProfile(int id)
  385. {
  386. string name, fn, ln, input;
  387. bool results;
  388. bool option = true;
  389.  
  390. User u = userID[id];
  391.  
  392. while (option)
  393. {
  394. bool find = true;
  395. while (find)
  396. {
  397. cout << "\nEnter the name of the friend you'd like to view: " << endl;
  398.  
  399. cout << " First Name: ";
  400. cin >> fn;
  401. cout << " Last Name: ";
  402. cin >> ln;
  403.  
  404. name = fn + " " + ln;
  405. results = u.searchFriend(name);
  406.  
  407. if (results == 0)
  408. {
  409. cout << "Friend not found. Please try again. \n";
  410. }
  411. else
  412. {
  413. for(unsigned i = 1; i <= userID.size(); i++)
  414. {
  415. if (fn == userID[i].getFirstname() && ln == userID[i].getLastname())
  416. {
  417. User u = userID[i];
  418. List<string> interest = u.getInterest();
  419. cout << "\n-----------------------------------------";
  420. cout << "\nProfile of ";
  421. cout << u.getFirstname() << " " << u.getLastname() << endl << endl;
  422. cout << "Location: ";
  423. cout << u.getCity() << endl << endl;
  424. cout << "Friends: " << endl;
  425. u.printFriends();
  426. cout << "Interests: " ;
  427. interest.printList(cout);
  428. cout << "-----------------------------------------\n";
  429. }
  430. }
  431. find = false;
  432. }
  433. }
  434.  
  435. bool more = true;
  436.  
  437. while (more)
  438. {
  439. cout << "\nWould you like to view more friends? ";;
  440. cin >> input;
  441. transform(input.begin(), input.end(), input.begin(), ::toupper);
  442.  
  443. if (input == "NO")
  444. {
  445. cout << "Back to the main menu. " << endl;
  446. //mainMenu();
  447. more = false;
  448. option = false;
  449. }
  450. else if (input == "YES")
  451. {
  452. more = false;
  453. }
  454. else
  455. {
  456. cout << "Please enter only yes or no " << endl;
  457. }
  458. }
  459. }
  460. }
  461.  
  462. /// Menu option to search for new friends
  463. void searchNewFriends(HashFriend& hf, HashInterest& hi, int id)
  464. {
  465. string input = "0";
  466. cout << " - Search for New Friends -" << endl;
  467. do{
  468. cout << "1. Search by Name\n"
  469. << "2. Search by Interest\n\n"
  470. << "Enter choice or 'm' for menu: ";
  471. cin >> input;
  472.  
  473. if(input[0] == 'm')
  474. return;
  475. else if(!isdigit(input[0]))
  476. {
  477. cout << "Please enter numbers or 'm' only.\n\n";
  478. input = "0";
  479. }
  480. else if(!(input == "1" || input == "2"))
  481. {
  482. cout << "Invalid input.\n\n";
  483. input = "0";
  484. }
  485. else if(input == "1") // input == 1 or 2
  486. {
  487. searchByFriend(hf, id);
  488. }
  489. else
  490. {
  491. searchByInterest(hi, id);
  492. }
  493.  
  494. }while(input == "0");
  495. cout << endl;
  496. }
  497.  
  498.  
  499.  
  500. void removeFriends(int id)
  501. {
  502. string name, input;
  503. bool results;
  504. bool option = true;
  505.  
  506.  
  507. while (option)
  508. {
  509. bool find = true;
  510. while (find)
  511. {
  512. cout << "Enter the name of the friend you'd like to remove: " << endl;
  513. cin.ignore();
  514. getline(cin, name);
  515. results = userID[id].getBST().search(name);
  516.  
  517. if (results == 0)
  518. {
  519. cout << "Friend not found. Please try again. \n" << endl;
  520. }
  521. else
  522. {
  523. userID[id].removeFriend(name);
  524. cout << endl << "Friend successfully removed." << endl;
  525. cout << "Here is your updated list: ";
  526. cout << "\n-----------------------------------------\n";
  527. userID[id].printFriends();
  528. cout << "-----------------------------------------\n";
  529. find = false;
  530. }
  531. }
  532. bool more = true;
  533.  
  534. while (more)
  535. {
  536. cout << "Would you like to remove more friends? ";;
  537. cin >> input;
  538. transform(input.begin(), input.end(), input.begin(), ::toupper);
  539.  
  540. if (input == "NO")
  541. {
  542. cout << "Back to the main menu. " << endl;
  543. mainMenu();
  544. more = false;
  545. option = false;
  546. }
  547. else if (input == "YES")
  548. {
  549. more = false;
  550. }
  551. else
  552. {
  553. cout << "Please enter only yes or no " << endl;
  554. }
  555. }
  556. }
  557. }
  558.  
  559. void friendRecs(int id, HashFriend& hf)
  560. {
  561. Graph g(userID.size());
  562.  
  563. cout << "--------------------------------\n";
  564. cout << "\n - Friend Recommendations -\n" << endl;
  565.  
  566. for (unsigned i = 1; i < userID.size(); i++)
  567. {
  568.  
  569. List<string> friendList = userID[i].getFriend();
  570. friendList.startIterator();
  571.  
  572. while (!friendList.offEnd())
  573. {
  574. string name = friendList.getIterator(); //Returns a variable
  575. for (unsigned x = 1; x < userID.size(); x++)
  576. {
  577. string friendName = userID[x].getFirstname() + " " + userID[x].getLastname();
  578. if (friendName == name)
  579. {
  580. g.addEdge(i, x);
  581. }
  582. }
  583. friendList.advanceIterator();
  584. }
  585. }
  586.  
  587. g.BFS(id, userID);
  588. g.rankingAlgorithm(id, userID);
  589.  
  590. cout << "--------------------------------\n";
  591. searchByFriend(hf, id);
  592. return;
  593. }
  594.  
  595. //search friend by name, this will find the correct id in terms of firstname and lastname
  596. void searchByFriend(HashFriend& hf, int id)
  597. {
  598. string firstN, lastN;
  599. cout << "Enter the first name or m to menu:";
  600. cin >> firstN;
  601. if(firstN == "m" || firstN == "M")
  602. return;
  603.  
  604. cout << "Enter the last name or m to menu:";
  605.  
  606. if(lastN == "m" || lastN == "M")
  607. return;
  608. cin >> lastN;
  609.  
  610. while(userID[id].getFirstname() + userID[id].getLastname() == firstN + lastN) // can't search the same name to self
  611. {
  612. cout << "Sorry, you can not search for yourself!\n";
  613. return searchByFriend(hf, id);
  614. }
  615. for(unsigned i = 0; i < firstN.length(); i++) // first name can't have any integer
  616. {
  617. if(isdigit(firstN[i]))
  618. {
  619. cout <<"User's name can not contain any integer\n";
  620. return searchByFriend(hf, id);
  621. }
  622. }
  623. for(unsigned i = 0; i < lastN.length(); i++)// last name can't have any integer
  624. {
  625. if(isdigit(lastN[i]))
  626. {
  627. cout <<"User's name can not contain any integer\n";
  628. return searchByFriend(hf, id);
  629. }
  630. }
  631.  
  632. int index = hf.search(firstN + lastN); //get the index of the bucket
  633.  
  634. if(index == -1) // can't find a user
  635. {
  636. cout << "\nUser does not exist!\n\n";
  637. return searchByFriend(hf, id);
  638. }
  639. else // found a user
  640. {
  641. if(userID[id].getFriend().linearSearch(firstN + " " + lastN) != -1) // verify whether the user is your friend already
  642. {
  643. cout << "\n" <<firstN << " "<< lastN << " is your friend already!\n";
  644. return searchByFriend(hf, id);
  645. }
  646.  
  647. List<int> temp = hf.getBucket(index);// get the list of the bucket
  648.  
  649. temp.startIterator();
  650. for(int i = 0; i < temp.getLength(); i++) // each bucket might have collision
  651. {
  652. int searchid = temp.getIterator();
  653. if(userID[searchid].getFirstname() + userID[searchid].getLastname() == firstN + lastN)
  654. {
  655.  
  656. cout << "\n" << userID[searchid].getFirstname() << " " << userID[searchid].getLastname() << " is found!\n";
  657. cout << "\nDo you want to add the user to your friend list, Yes or No?\n";
  658. string choice;
  659. cin >> choice;
  660. if(choice == "YES" || choice == "yes")
  661. {
  662. cout << "\nThe user, " << firstN << " " << lastN << ", has been added to your list.\n";
  663. userID[id].setALL(firstN + " " + lastN);
  664. userID[searchid].setALL(userID[id].getFirstname() + " " + userID[id].getLastname());
  665. return;
  666. }
  667. else if(choice == "NO" || choice == "no")
  668. {
  669. return;
  670. }
  671. else
  672. {
  673. cout <<"Invalid input!\n";
  674. return;
  675. }
  676. }
  677. else
  678. {
  679. if(i == temp.getLength() - 1) // make sure go through all the collison
  680. {
  681. cout << "\nUser does not exist!\n\n";
  682. return searchByFriend(hf, id);
  683. }
  684. }
  685. temp.advanceIterator();
  686. }
  687.  
  688. }
  689. }
  690.  
  691. //searching user by interest
  692. void searchByInterest(HashInterest& hi, int id)
  693. {
  694.  
  695. string interest;
  696. cout << "\nEnter the name of the interest or me to menu:";
  697. if(interest == "m" || interest == "M")
  698. return;
  699.  
  700. cin.ignore();
  701. getline(cin, interest);
  702. cout << endl;
  703.  
  704. int index = hi.search(interest); //get the index of the bucket
  705.  
  706. if(index == -1) // can't find a user with that interest
  707. {
  708. cout << "\nNo user has this interest!\n\n";
  709. return;
  710. }
  711. else // found a user
  712. {
  713. List<int> temp = hi.getBucket(index);// get the list of the bucket
  714. vector<int> listInterest;
  715. int listIndex = 1;
  716. temp.startIterator();
  717.  
  718. for(int i = 0; i < temp.getLength(); i++)
  719. {
  720. int searchid = temp.getIterator();
  721. if(searchid != id) // not including user himself
  722. {
  723. if(userID[searchid].getInterest().linearSearch(interest) != -1) //minimize collisions by making sure only show the user has that interest
  724. {
  725. cout << listIndex << ". " <<userID[searchid].getFirstname() << " " << userID[searchid].getLastname() << " has the interest!\n";
  726. listInterest.push_back(searchid);
  727. listIndex++;
  728. }
  729.  
  730. }
  731. temp.advanceIterator();
  732. }
  733.  
  734. cout << "\nDo you want to add the user to your friend list, Yes or No?\n";
  735. string choice;
  736. cin >> choice;
  737. if(choice == "YES" || choice == "yes")
  738. {
  739. cout << "Enter the number of the user you want to add:";
  740. unsigned index;
  741. cin >> index;
  742.  
  743. if(index < 1 || index > listInterest.size())
  744. {
  745. cout << "\nnumber is out of range!\n";
  746. return;
  747. }
  748.  
  749. string tempName = userID[listInterest[index - 1]].getFirstname() + " " + userID[listInterest[index - 1]].getLastname();
  750. if(!userID[id].getBST().search(tempName)) // make sure is not friend already
  751. {
  752. userID[id].setALL(tempName);
  753. userID[listInterest[index - 1]].setALL(userID[id].getFirstname() + " " + userID[id].getLastname());
  754.  
  755. cout << "\nThe user, " << tempName << ", has been added in your friend list.\n";
  756. }
  757. else
  758. {
  759. cout << "\nThe user, " << tempName << ", is your friend already!\n";
  760. }
  761.  
  762. }
  763. else if(choice == "NO" || choice == "no")
  764. {
  765. return;
  766. }
  767. else
  768. {
  769. cout <<"Invalid input!\n";
  770. return;
  771. }
  772. }
  773. }
  774.  
  775.  
  776. void outputFile()
  777. {
  778.  
  779. ofstream outfile;
  780. outfile.open("userinfo.txt");
  781.  
  782. for(unsigned i = 1; i < userID.size(); i++)
  783. {
  784. outfile << userID[i] << endl;
  785. }
  786.  
  787. outfile.close();
  788.  
  789. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement