Advertisement
Guest User

Main that contains HashFriend, HashInterest implementations

a guest
Mar 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.45 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.  
  12.  
  13. using namespace std;
  14.  
  15. int logIn();
  16. int mainMenu();
  17. void viewFriends(int id);
  18. void searchNewFriends(HashFriend& hf, HashInterest& hi, int id);
  19. void removeFriends(int id);
  20. void friendRecs();
  21. void searchByFriend(HashFriend& hf, int id);
  22. void searchByInterest(HashInterest& hi, int id);
  23. void viewProfile(int id);
  24. string createAccount();
  25.  
  26. vector<User> userID;
  27. HashFriend hf;
  28. HashInterest hi;
  29.  
  30. void readFile()
  31. {
  32. string firstName, lastName, userName, passWord, city, state, friendName, interest;
  33. string filename;
  34. unsigned id = 1;
  35.  
  36. User empty;
  37. userID.push_back(empty); // index 0 is a empty user
  38.  
  39. filename = "/Users/ChrisGentibano/Desktop/data.txt";
  40. ifstream inputfile;
  41. inputfile.open(filename);
  42.  
  43. assert(inputfile);
  44.  
  45. while(inputfile)
  46. {
  47. inputfile >> firstName;
  48. inputfile >> lastName;
  49. inputfile >> userName;
  50. inputfile >> passWord;
  51. inputfile.get(); // go to next line
  52. getline(inputfile, city, ',');
  53. inputfile.get();
  54. getline(inputfile, state);
  55.  
  56. User user(firstName, lastName, userName, passWord, city, state, id);
  57.  
  58. getline(inputfile, friendName);
  59. istringstream tempF(friendName);
  60. while(tempF)
  61. {
  62. getline(tempF, friendName, ',');
  63. if(tempF.peek() == ' ')
  64. tempF.get();
  65. user.setFriends(friendName);
  66. }
  67.  
  68. getline(inputfile, interest);
  69. istringstream tempI(interest);
  70. while(tempI)
  71. {
  72. getline(tempI, interest, ',');
  73. if(tempI.peek() == ' ')
  74. tempI.get();
  75. user.setInterests(interest);
  76. }
  77.  
  78. while(inputfile.peek() == '\n')
  79. inputfile.get(); // go to next line
  80. while(inputfile.peek() == ' ')
  81. inputfile.get(); // go to next line
  82.  
  83. userID.push_back(user); // insert to the id vector
  84. hf.insert(user);// insert to hashFriend
  85. hi.insert(user);// insert to hashInterest
  86. id ++;
  87. }
  88. inputfile.close();
  89. }
  90.  
  91. int main()
  92. {
  93.  
  94. string name;
  95. int choice;
  96. int id = 0;
  97. readFile();
  98. cout << userID[1];
  99.  
  100. // Log in validation
  101. while (id <= 0)
  102. id = logIn();
  103.  
  104.  
  105. name = userID[id].getFirstname();
  106. cout << "\nWelcome, " << name << "!\n";
  107.  
  108. do{
  109. choice = mainMenu();
  110.  
  111. switch(choice)
  112. {
  113. case 1: // view friends
  114. viewFriends(id);
  115. break;
  116.  
  117. case 2: // search for new friends
  118. searchNewFriends(hf, hi, id);
  119. break;
  120.  
  121. case 3: // friends rec
  122. friendRecs();
  123. break;
  124.  
  125. case 4: //quit
  126. {
  127. /*
  128. ofstream outfile;
  129. outfile.open("userinfo.txt");
  130. //print to outfile
  131. outfile.close();
  132. */
  133.  
  134.  
  135.  
  136. cout << " You are signed out"
  137. << "\n\t- GOODBYE! -" << endl;
  138. }
  139. }
  140.  
  141. }while(choice != 4);
  142.  
  143. return 0;
  144. }
  145.  
  146.  
  147. /// Returns bool for whether or not username & password combo are matched
  148. int logIn()
  149. {
  150. string un, pw, fn, ln, city, state;
  151. string choice = "0";
  152. int id;
  153. bool option = false;
  154.  
  155. while (!option)
  156. {
  157. while (choice != "1" && choice != "2" && choice != "3")
  158. {
  159. cout << " - WELCOME TO SPONGEBOOK -" << endl;
  160. cout << "1. Log in " << endl;
  161. cout << "2. Create an account " << endl;
  162. cout << "3. Quit " << endl;
  163. cout << "\nEnter choice: ";
  164. cin >> choice;
  165. cout << endl;
  166. }
  167.  
  168. if (choice == "1")
  169. {
  170. int count = 0;
  171.  
  172. while (true)
  173. {
  174. string userInput;
  175.  
  176. cout << "\t- LOG IN -" << endl
  177. << "Username: ";
  178. cin >> un;
  179. cout << "Password: ";
  180. cin >> pw;
  181. //un = "mynamesnotrick";
  182. //pw = "krabbypatty101";
  183. for (int i = 1; i <= userID.size(); i++)
  184. {
  185. if (un == userID[i].getUsername() && pw == userID[i].getPassword())
  186. {
  187. cout << "\n *** Login successful ***" << endl;
  188. id = i;
  189. option = true;
  190. return id;
  191. }
  192. }
  193.  
  194. cout << "\n*** Incorrect username or password. Please try again **\n\n";
  195. count ++;
  196.  
  197. if (count > 3)
  198. {
  199. while (userInput != "YES" && userInput != "NO")
  200. {
  201. cout << "Multiple failed attempts. Would you like to go back to the main menu? ";
  202. cin >> userInput;
  203. transform(userInput.begin(), userInput.end(), userInput.begin(),::toupper);
  204. }
  205.  
  206. if (userInput == "YES")
  207. {
  208. cout << endl;
  209. choice = "0";
  210. option = true;
  211. return false;
  212. }
  213. else if (userInput == "NO")
  214. {
  215. choice = "1";
  216. count = 0;
  217. }
  218. }
  219. }
  220. }
  221. else if (choice == "2")
  222. {
  223. choice = createAccount();
  224. }
  225. else if (choice == "3")
  226. {
  227. cout << "\t- GOODBYE! -" << endl;
  228. exit(0);
  229. }
  230. }
  231. return id;
  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. /// Main Menu :)
  288. int mainMenu()
  289. {
  290. string input = "0";
  291. do{
  292. cout << "\n\t - MENU -" << endl
  293. << "1. View Friends" << endl
  294. << "2. Search for New Friends" << endl
  295. << "3. Friends Recommendations" << endl
  296. << "4. Quit\n\n"
  297. << "Enter choice: ";
  298. cin >> input;
  299.  
  300. if(!isdigit(input[0]))
  301. {
  302. cout << "Please enter numbers only.\n";
  303. input = "0";
  304. }
  305. else if(!(input == "1" || input == "2" || input == "3" || input == "4"))
  306. {
  307. cout << "Invalid input.\n";
  308. input = "0";
  309. }
  310.  
  311. }while(input == "0");
  312.  
  313. cout << endl;
  314. int choice = atoi(input.c_str());
  315.  
  316. return choice;
  317. }
  318.  
  319.  
  320. /// Menu option to view friends
  321. void viewFriends(int id)
  322. {
  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]; // temporarily assigned to spongebob
  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. //cin.ignore();
  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(int 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. /// Menu option to search for new friends
  462. void searchNewFriends(HashFriend& hf, HashInterest& hi, int id)
  463. {
  464. string input = "0";
  465. cout << " - Search for New Friends -" << endl;
  466. do{
  467. cout << "1. Search by Name\n"
  468. << "2. Search by Interest\n\n"
  469. << "Enter choice or 'm' for menu: ";
  470. cin >> input;
  471.  
  472. if(input[0] == 'm')
  473. return;
  474. else if(!isdigit(input[0]))
  475. {
  476. cout << "Please enter numbers or 'm' only.\n\n";
  477. input = "0";
  478. }
  479. else if(!(input == "1" || input == "2"))
  480. {
  481. cout << "Invalid input.\n\n";
  482. input = "0";
  483. }
  484. else if(input == "1") // input == 1 or 2
  485. {
  486. searchByFriend(hf, id);
  487. }
  488. else
  489. {
  490. searchByInterest(hi, id);
  491. }
  492.  
  493. }while(input == "0");
  494. cout << endl;
  495. }
  496.  
  497.  
  498. /// Menu option to get friend recommendations
  499. void friendRecs()
  500. {
  501. cout << " - Friend Recommendations -" << endl
  502. << "People you may know:\n\n"
  503. // print all the ppl using graph (?)
  504. << "Enter choice or 'm' for menu: ";
  505. cout << endl;
  506. }
  507.  
  508. void removeFriends(int id)
  509. {
  510. string name, input;
  511. bool results;
  512. bool option = true;
  513.  
  514. //User u = userID[id]; // temporarily assigned to spongebob
  515.  
  516. while (option)
  517. {
  518. bool find = true;
  519. while (find)
  520. {
  521. cout << "Enter the name of the friend you'd like to remove: " << endl;
  522. cin.ignore();
  523. getline(cin, name);
  524. results = userID[id].searchFriend(name);
  525.  
  526. if (results == 0)
  527. {
  528. cout << "Friend not found. Please try again. \n" << endl;
  529. }
  530. else
  531. {
  532. userID[id].removeFriend(name);
  533. cout << endl << "Friend successfully removed." << endl;
  534. cout << "Here is your updated list: ";
  535. cout << "\n-----------------------------------------\n";
  536. userID[id].printFriends();
  537. cout << "-----------------------------------------\n";
  538. find = false;
  539. }
  540. }
  541.  
  542. bool more = true;
  543.  
  544. while (more)
  545. {
  546. cout << "Would you like to remove more friends? ";;
  547. cin >> input;
  548. transform(input.begin(), input.end(), input.begin(), ::toupper);
  549.  
  550. if (input == "NO")
  551. {
  552. cout << "Back to the main menu. " << endl;
  553. //mainMenu();
  554. more = false;
  555. option = false;
  556. }
  557. else if (input == "YES")
  558. {
  559. more = false;
  560. }
  561. else
  562. {
  563. cout << "Please enter only yes or no " << endl;
  564. }
  565. }
  566. }
  567. }
  568.  
  569. //search friend by name, this will find the correct id in terms of firstname and lastname
  570. void searchByFriend(HashFriend& hf, int id)
  571. {
  572. string firstN, lastN;
  573. cout << "Enter the first name:";
  574. cin >> firstN;
  575. cout << "Enter the last name:";
  576. cin >> lastN;
  577.  
  578. while(userID[id].getFirstname() + userID[id].getLastname() == firstN + lastN) // can't search the same name to self
  579. {
  580. cout << "Sorry, you can not search for yourself!\n";
  581. searchByFriend(hf, id);
  582. }
  583. for(unsigned i = 0; i < firstN.length(); i++) // first name can't have any integer
  584. {
  585. if(isdigit(firstN[i]))
  586. {
  587. cout <<"User's name can not contain any integer\n";
  588. searchByFriend(hf, id);
  589. }
  590. }
  591. for(unsigned i = 0; i < lastN.length(); i++)// last name can't have any integer
  592. {
  593. if(isdigit(lastN[i]))
  594. {
  595. cout <<"User's name can not contain any integer\n";
  596. searchByFriend(hf, id);
  597. }
  598. }
  599.  
  600. int index = hf.search(firstN + lastN); //get the index of the bucket
  601.  
  602. if(index == -1) // can't find a user
  603. {
  604. cout << "\nUser does not exist!\n\n";
  605. searchByFriend(hf, id);
  606. }
  607. else // found a user
  608. {
  609. List<int> temp = hf.getBucket(index);// get the list of the bucket
  610.  
  611. if(temp.getLength() == 1) // if no collision in terms of the name
  612. {
  613. int tempid = temp.getFirst();
  614. cout << "\n" <<userID[tempid].getFirstname() << " " << userID[tempid].getLastname() << " is found!\n\n";
  615. }
  616. else// collision happens
  617. {
  618. temp.startIterator();
  619. for(int i = 0; i < temp.getLength(); i++)
  620. {
  621. int searchid = temp.getIterator();
  622. if(userID[searchid].getFirstname() + userID[searchid].getLastname() == firstN + lastN)
  623. {
  624. cout << "\n" << userID[searchid].getFirstname() << " " << userID[searchid].getLastname() << " is found!\n";
  625. cout << "\nDo you want to add the user to your friend list, Yes or No?\n";
  626. string choice;
  627. cin >> choice;
  628. if(choice == "YES" || choice == "yes")
  629. {
  630. cout << "The user ," << firstN << " " << lastN << ", has been added to your list.\n";
  631. userID[id].setFriendList(firstN + " " + lastN);
  632. userID[id].setFriends(firstN + " " + lastN);
  633. }
  634. else if(choice == "NO" || choice == "no")
  635. {
  636. viewFriends(id);
  637. }
  638. else
  639. {
  640. cout <<"Invalid input!\n";
  641. }
  642. }
  643. temp.advanceIterator();
  644. }
  645. }
  646.  
  647. }
  648. }
  649.  
  650. //searching user by interest
  651. void searchByInterest(HashInterest& hi, int id)
  652. {
  653.  
  654. string interest;
  655. cout << "\nEnter the name of the interest:";
  656. cin.ignore();
  657. getline(cin, interest);
  658. cout << endl;
  659.  
  660. int index = hi.search(interest); //get the index of the bucket
  661.  
  662. if(index == -1) // can't find a user with that interest
  663. {
  664. cout << "\nno user has this interest!\n\n";
  665. searchByInterest(hi, id);
  666. }
  667. else // found a user
  668. {
  669. List<int> temp = hi.getBucket(index);// get the list of the bucket
  670. vector<int> listInterest;
  671. int listIndex = 1;
  672. temp.startIterator();
  673. for(int i = 0; i < temp.getLength(); i++)
  674. {
  675. int searchid = temp.getIterator();
  676. if(searchid != id) // not including user himself
  677. {
  678. if(userID[searchid].getInterest().linearSearch(interest) != -1) //minimize collisions by making sure only show the user has that interest
  679. {
  680. cout << listIndex << ". " <<userID[searchid].getFirstname() << " " << userID[searchid].getLastname() << " has the interest!\n";
  681. listInterest.push_back(searchid);
  682. listIndex++;
  683. }
  684. }
  685. temp.advanceIterator();
  686. }
  687.  
  688. cout << "\nDo you want to add the user to your friend list, Yes or No?\n";
  689. string choice;
  690. cin >> choice;
  691. if(choice == "YES" || choice == "yes")
  692. {
  693. cout << "Enter the number of the user you want to add:";
  694. int index;
  695. cin >> index;
  696.  
  697. string tempName = userID[listInterest[index - 1]].getFirstname() + " " + userID[listInterest[index - 1]].getLastname();
  698. if(!userID[id].getBST().search(tempName))
  699. {
  700. userID[id].setFriendList(tempName);
  701. userID[id].setFriends(tempName);
  702. cout << "\nThe user, " << tempName << ", has been added in your friend list.\n";
  703. }
  704. else
  705. {
  706. cout << "\nThe user, " << tempName << ", is your friend already!\n";
  707. }
  708.  
  709. }
  710. else if(choice == "NO" || choice == "no")
  711. {
  712. viewFriends(id);
  713. }
  714. else
  715. {
  716. cout <<"Invalid input!\n";
  717. }
  718. }
  719. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement