Advertisement
Guest User

Main.cpp

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