Advertisement
Guest User

Main.cpp Updated 3/22

a guest
Mar 22nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.19 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. 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.     string name;
  94.     int choice;
  95.     int id = 0;
  96.     readFile();
  97.  
  98.     // Log in validation
  99.     while (id <= 0)
  100.             id = logIn();
  101.  
  102.  
  103.     name = userID[id].getFirstname();
  104.     cout << "\nWelcome, " << name << "!\n";
  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.             choice = createAccount();
  226.         }
  227.         else if (choice == "3")
  228.         {
  229.             cout << "\t- GOODBYE! -" << endl;
  230.             exit(0);
  231.         }
  232.     }
  233.     return id;
  234. }
  235.  
  236. string createAccount()
  237. {
  238.     string un, pw, fn, ln, city, state, choice;
  239.     int id;
  240.     cout << "\t- CREATE AN ACCOUNT -" << endl
  241.          << "First name: ";
  242.     cin >> fn;
  243.  
  244.     cout << "Last name: ";
  245.     cin >> ln;
  246.  
  247.     cout << "Username: ";
  248.     cin >> un;
  249.  
  250.     cout << "Password: ";
  251.     cin >> pw;
  252.  
  253.     cout << "City: ";
  254.     cin.ignore();
  255.     getline(cin, city);
  256.  
  257.     cout << "State: ";
  258.     cin >> ws;
  259.     cin >> state;
  260.  
  261.     id = userID.size() + 1;
  262.  
  263.     User newUser(fn, ln, un, pw, city, state, id);
  264.     userID.push_back(newUser);
  265.  
  266.     bool more = true;
  267.     string interests;
  268.  
  269.     while (more)
  270.     {
  271.         cout << "Enter interest or 'stop' to finish: ";
  272.         cin >> interests;
  273.  
  274.         transform(interests.begin(), interests.end(), interests.begin(),::toupper);
  275.  
  276.         if (interests == "STOP")
  277.             more = false;
  278.         else
  279.         {
  280.             transform(interests.begin(), interests.end(), interests.begin(),::tolower);
  281.             newUser.setInterests(interests);
  282.         }
  283.     }
  284.  
  285.     cout << "\n\t*** Account successfully created. You may now log in ***\n" << endl;
  286.  
  287.     return choice = "0";
  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.     //id = 1; //temporarily assigned to spongebob
  326.  
  327.     string input = "0";
  328.     User u = userID[id];
  329.  
  330.  
  331.     cout << "     - View Friends -" << endl;
  332.  
  333.         cout << "1. View all friends\n"
  334.              << "2. View a friend's profile\n"
  335.              << "3. Remove a friend\n\n"
  336.              << "Enter choice or 'm' for menu: ";
  337.         cin >> input;
  338.  
  339.      do{   if(input[0] == 'm')
  340.                 return;
  341.         else if(!isdigit(input[0]))
  342.         {
  343.             cout << "Please enter numbers or 'm' only.\n\n";
  344.             input = "0";
  345.         }
  346.         else if(!(input == "1" || input == "2" || input == "3"))
  347.         {
  348.             cout << "Invalid input.\n\n";
  349.             input = "0";
  350.         }
  351.         else if (input == "2")
  352.         {
  353.             viewProfile(id);
  354.         }
  355.         else if (input == "3")
  356.         {
  357.             removeFriends(id);
  358.         }
  359.         else        // input == 1 2 or 3
  360.         {
  361.             cout << "\n";
  362.             u.printFriends();
  363.             cout << "\n1. View a friend's profile\n"
  364.                  << "2. Remove a friend\n\n"
  365.                  << "Enter choice or 'm' for menu: ";
  366.             cin >> input;
  367.  
  368.             if (input == "1")
  369.             {
  370.                 viewProfile(id);
  371.             }
  372.             else if (input == "2")
  373.             {
  374.                 removeFriends(id);
  375.             }
  376.             else
  377.             {
  378.                 cout << "Invalid input. Please try again.\n" << endl;
  379.             }
  380.  
  381.         }
  382.  
  383.     }while(input == "0");
  384.     cout << endl;
  385. }
  386.  
  387. void viewProfile(int id)
  388. {
  389.     string name, fn, ln, input;
  390.     bool results;
  391.     bool option = true;
  392.  
  393.     User u = userID[id]; // temporarily assigned to spongebob
  394.  
  395.     while (option)
  396.         {
  397.             bool find = true;
  398.             while (find)
  399.             {
  400.                 cout << "\nEnter the name of the friend you'd like to view: " << endl;
  401.                 //cin.ignore();
  402.                 cout << "   First Name: ";
  403.                 cin >> fn;
  404.                 cout << "   Last Name: ";
  405.                 cin >> ln;
  406.  
  407.                 name = fn + " " + ln;
  408.                 results = u.searchFriend(name);
  409.  
  410.                 if (results == 0)
  411.                 {
  412.                     cout << "Friend not found. Please try again. \n" << endl;
  413.                 }
  414.                 else
  415.                 {
  416.                     for(int i = 1; i <= userID.size(); i++)
  417.                     {
  418.                         if (fn == userID[i].getFirstname() && ln == userID[i].getLastname())
  419.                         {
  420.                             cout << "\n-----------------------------------------"
  421.                                  << "\nProfile of "
  422.                                  << userID[i]
  423.                                  << "-----------------------------------------\n";
  424.  
  425.                         }
  426.                     }
  427.                     find = false;
  428.                 }
  429.             }
  430.  
  431.             bool more = true;
  432.  
  433.             while (more)
  434.             {
  435.                 cout << "\nWould you like to view more friends? ";;
  436.                 cin >> input;
  437.                 transform(input.begin(), input.end(), input.begin(), ::toupper);
  438.  
  439.                 if (input == "NO")
  440.                 {
  441.                     cout << "Back to the main menu. " << endl;
  442.                     //mainMenu();
  443.                     more = false;
  444.                     option = false;
  445.                 }
  446.                 else if (input == "YES")
  447.                 {
  448.                     more = false;
  449.                 }
  450.                 else
  451.                 {
  452.                     cout << "Please enter only yes or no " << endl;
  453.                 }
  454.             }
  455.         }
  456. }
  457. /// Menu option to search for new friends
  458. void searchNewFriends()
  459. {
  460.     string input = "0";
  461.     cout << " - Search for New Friends -" << endl;
  462.     do{
  463.         cout << "1. Search by Name\n"
  464.              << "2. Search by Interest\n\n"
  465.              << "Enter choice or 'm' for menu: ";
  466.         cin >> input;
  467.  
  468.         if(input[0] == 'm')
  469.                 return;
  470.         else if(!isdigit(input[0]))
  471.         {
  472.             cout << "Please enter numbers or 'm' only.\n\n";
  473.             input = "0";
  474.         }
  475.         else if(!(input == "1" || input == "2"))
  476.         {
  477.             cout << "Invalid input.\n\n";
  478.             input = "0";
  479.         }
  480.         else        // input == 1 or 2
  481.         {
  482.             //int choice = atoi(input.c_str());
  483.  
  484.         }
  485.  
  486.     }while(input == "0");
  487.     cout << endl;
  488. }
  489.  
  490.  
  491. /// Menu option to get friend recommendations
  492. void friendRecs()
  493. {
  494.     cout << " - Friend Recommendations -" << endl
  495.          << "People you may know:\n\n"
  496.          // print all the ppl using graph (?)
  497.          << "Enter choice or 'm' for menu: ";
  498.     cout << endl;
  499. }
  500.  
  501. void removeFriends(int id)
  502. {
  503.     string name, input;
  504.     bool results;
  505.     bool option = true;
  506.  
  507.     User u = userID[id]; // temporarily assigned to spongebob
  508.  
  509.     while (option)
  510.     {
  511.         bool find = true;
  512.         while (find)
  513.         {
  514.             cout << "Enter the name of the friend you'd like to remove: " << endl;
  515.             cin.ignore();
  516.             getline(cin, name);
  517.             results = u.searchFriend(name);
  518.  
  519.             if (results == 0)
  520.             {
  521.                 cout << "Friend not found. Please try again. \n" << endl;
  522.             }
  523.             else
  524.             {
  525.                 u.removeFriend(name);
  526.                 u.printFriends();
  527.                 find = false;
  528.             }
  529.         }
  530.  
  531.         bool more = true;
  532.  
  533.         while (more)
  534.         {
  535.             cout << "Would you like to remove more friends? ";;
  536.             cin >> input;
  537.             transform(input.begin(), input.end(), input.begin(), ::toupper);
  538.  
  539.             if (input == "NO")
  540.             {
  541.                 cout << "Back to the main menu. " << endl;
  542.                 //mainMenu();
  543.                 more = false;
  544.                 option = false;
  545.             }
  546.             else if (input == "YES")
  547.             {
  548.                 more = false;
  549.             }
  550.             else
  551.             {
  552.                 cout << "Please enter only yes or no " << endl;
  553.             }
  554.         }
  555.     }
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement