Advertisement
Guest User

Untitled

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