Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <cstdlib>
- #include <vector>
- #include "BST.h"
- #include <algorithm>
- #include "User.h"
- #include "HashFriend.h"
- #include "HashInterest.h"
- //#include "Graph.h"
- //#include "HashInterest.h"
- //#include "HashSomethingIDK.h"
- using namespace std;
- int logIn();
- int mainMenu();
- void viewFriends(int id);
- void searchNewFriends();
- void removeFriends(int id);
- void friendRecs();
- void viewProfile(int id);
- string createAccount();
- vector<User> userID;
- HashFriend hf;
- HashInterest hi;
- void readFile()
- {
- string firstName, lastName, userName, passWord, city, state, friendName, interest;
- string filename;
- unsigned id = 1;
- User empty;
- userID.push_back(empty); // index 0 is a empty user
- filename = "/Users/ChrisGentibano/Desktop/data.txt";
- ifstream inputfile;
- inputfile.open(filename);
- assert(inputfile);
- while(inputfile)
- {
- inputfile >> firstName;
- inputfile >> lastName;
- inputfile >> userName;
- inputfile >> passWord;
- inputfile.get(); // go to next line
- getline(inputfile, city, ',');
- inputfile.get();
- getline(inputfile, state);
- User user(firstName, lastName, userName, passWord, city, state, id);
- getline(inputfile, friendName);
- istringstream tempF(friendName);
- while(tempF)
- {
- getline(tempF, friendName, ',');
- if(tempF.peek() == ' ')
- tempF.get();
- user.setFriends(friendName);
- }
- getline(inputfile, interest);
- istringstream tempI(interest);
- while(tempI)
- {
- getline(tempI, interest, ',');
- if(tempI.peek() == ' ')
- tempI.get();
- user.setInterests(interest);
- }
- while(inputfile.peek() == '\n')
- inputfile.get(); // go to next line
- while(inputfile.peek() == ' ')
- inputfile.get(); // go to next line
- userID.push_back(user); // insert to the id vector
- hf.insert(user);// insert to hashFriend
- hi.insert(user);// insert to hashInterest
- id ++;
- }
- inputfile.close();
- }
- int main()
- {
- string name;
- int choice;
- int id = 0;
- readFile();
- // Log in validation
- while (id <= 0)
- id = logIn();
- name = userID[id].getFirstname();
- cout << "\nWelcome, " << name << "!\n";
- do{
- choice = mainMenu();
- int temp = 1; //DELETE LATER, USED JUST FOR TESTING
- switch(choice)
- {
- case 1: // view friends
- viewFriends(temp);
- break;
- case 2: // search for new friends
- searchNewFriends();
- break;
- case 3: // friends rec
- friendRecs();
- break;
- case 4: //quit
- {
- /*
- ofstream outfile;
- outfile.open("userinfo.txt");
- //print to outfile
- outfile.close();
- */
- cout << " You are signed out"
- << "\n\t- GOODBYE! -" << endl;
- }
- }
- }while(choice != 4);
- return 0;
- }
- /// Returns bool for whether or not username & password combo are matched
- int logIn()
- {
- string un, pw, fn, ln, city, state;
- string choice = "0";
- int id;
- bool option = false;
- while (!option)
- {
- while (choice != "1" && choice != "2" && choice != "3")
- {
- cout << " - WELCOME TO SPONGEBOOK -" << endl;
- cout << "1. Log in " << endl;
- cout << "2. Create an account " << endl;
- cout << "3. Quit " << endl;
- cout << "\nEnter choice: ";
- cin >> choice;
- cout << endl;
- }
- if (choice == "1")
- {
- int count = 0;
- while (true)
- {
- string userInput;
- cout << "\t- LOG IN -" << endl
- << "Username: ";
- cin >> un;
- cout << "Password: ";
- cin >> pw;
- //un = "mynamesnotrick";
- //pw = "krabbypatty101";
- for (int i = 1; i <= userID.size(); i++)
- {
- if (un == userID[i].getUsername() && pw == userID[i].getPassword())
- {
- cout << "\n *** Login successful ***" << endl;
- id = i;
- option = true;
- return id;
- }
- }
- cout << "\n*** Incorrect username or password. Please try again **\n\n";
- count ++;
- if (count > 3)
- {
- while (userInput != "YES" && userInput != "NO")
- {
- cout << "Multiple failed attempts. Would you like to go back to the main menu? ";
- cin >> userInput;
- transform(userInput.begin(), userInput.end(), userInput.begin(),::toupper);
- }
- if (userInput == "YES")
- {
- cout << endl;
- choice = "0";
- option = true;
- return false;
- }
- else if (userInput == "NO")
- {
- choice = "1";
- count = 0;
- }
- }
- }
- }
- else if (choice == "2")
- {
- choice = createAccount();
- }
- else if (choice == "3")
- {
- cout << "\t- GOODBYE! -" << endl;
- exit(0);
- }
- }
- return id;
- }
- string createAccount()
- {
- string un, pw, fn, ln, city, state, choice;
- int id;
- cout << "\t- CREATE AN ACCOUNT -" << endl
- << "First name: ";
- cin >> fn;
- cout << "Last name: ";
- cin >> ln;
- cout << "Username: ";
- cin >> un;
- cout << "Password: ";
- cin >> pw;
- cout << "City: ";
- cin.ignore();
- getline(cin, city);
- cout << "State: ";
- cin >> ws;
- cin >> state;
- id = userID.size() + 1;
- User newUser(fn, ln, un, pw, city, state, id);
- userID.push_back(newUser);
- bool more = true;
- string interests;
- while (more)
- {
- cout << "Enter interest or 'stop' to finish: ";
- cin >> interests;
- transform(interests.begin(), interests.end(), interests.begin(),::toupper);
- if (interests == "STOP")
- more = false;
- else
- {
- transform(interests.begin(), interests.end(), interests.begin(),::tolower);
- newUser.setInterests(interests);
- }
- }
- cout << "\n\t*** Account successfully created. You may now log in ***\n" << endl;
- return choice = "0";
- }
- /// Main Menu :)
- int mainMenu()
- {
- string input = "0";
- do{
- cout << "\n\t - MENU -" << endl
- << "1. View Friends" << endl
- << "2. Search for New Friends" << endl
- << "3. Friends Recommendations" << endl
- << "4. Quit\n\n"
- << "Enter choice: ";
- cin >> input;
- if(!isdigit(input[0]))
- {
- cout << "Please enter numbers only.\n";
- input = "0";
- }
- else if(!(input == "1" || input == "2" || input == "3" || input == "4"))
- {
- cout << "Invalid input.\n";
- input = "0";
- }
- }while(input == "0");
- cout << endl;
- int choice = atoi(input.c_str());
- return choice;
- }
- /// Menu option to view friends
- void viewFriends(int id)
- {
- //id = 1; //temporarily assigned to spongebob
- string input = "0";
- User u = userID[id];
- cout << " - View Friends -" << endl;
- cout << "1. View all friends\n"
- << "2. View a friend's profile\n"
- << "3. Remove a friend\n\n"
- << "Enter choice or 'm' for menu: ";
- cin >> input;
- do{ if(input[0] == 'm')
- return;
- else if(!isdigit(input[0]))
- {
- cout << "Please enter numbers or 'm' only.\n\n";
- input = "0";
- }
- else if(!(input == "1" || input == "2" || input == "3"))
- {
- cout << "Invalid input.\n\n";
- input = "0";
- }
- else if (input == "2")
- {
- viewProfile(id);
- }
- else if (input == "3")
- {
- removeFriends(id);
- }
- else // input == 1 2 or 3
- {
- cout << "\n";
- u.printFriends();
- cout << "\n1. View a friend's profile\n"
- << "2. Remove a friend\n\n"
- << "Enter choice or 'm' for menu: ";
- cin >> input;
- if (input == "1")
- {
- viewProfile(id);
- }
- else if (input == "2")
- {
- removeFriends(id);
- }
- else
- {
- cout << "Invalid input. Please try again.\n" << endl;
- }
- }
- }while(input == "0");
- cout << endl;
- }
- void viewProfile(int id)
- {
- string name, fn, ln, input;
- bool results;
- bool option = true;
- User u = userID[id]; // temporarily assigned to spongebob
- while (option)
- {
- bool find = true;
- while (find)
- {
- cout << "\nEnter the name of the friend you'd like to view: " << endl;
- //cin.ignore();
- cout << " First Name: ";
- cin >> fn;
- cout << " Last Name: ";
- cin >> ln;
- name = fn + " " + ln;
- results = u.searchFriend(name);
- if (results == 0)
- {
- cout << "Friend not found. Please try again. \n" << endl;
- }
- else
- {
- for(int i = 1; i <= userID.size(); i++)
- {
- if (fn == userID[i].getFirstname() && ln == userID[i].getLastname())
- {
- cout << "\n-----------------------------------------"
- << "\nProfile of "
- << userID[i]
- << "-----------------------------------------\n";
- }
- }
- find = false;
- }
- }
- bool more = true;
- while (more)
- {
- cout << "\nWould you like to view more friends? ";;
- cin >> input;
- transform(input.begin(), input.end(), input.begin(), ::toupper);
- if (input == "NO")
- {
- cout << "Back to the main menu. " << endl;
- //mainMenu();
- more = false;
- option = false;
- }
- else if (input == "YES")
- {
- more = false;
- }
- else
- {
- cout << "Please enter only yes or no " << endl;
- }
- }
- }
- }
- /// Menu option to search for new friends
- void searchNewFriends()
- {
- string input = "0";
- cout << " - Search for New Friends -" << endl;
- do{
- cout << "1. Search by Name\n"
- << "2. Search by Interest\n\n"
- << "Enter choice or 'm' for menu: ";
- cin >> input;
- if(input[0] == 'm')
- return;
- else if(!isdigit(input[0]))
- {
- cout << "Please enter numbers or 'm' only.\n\n";
- input = "0";
- }
- else if(!(input == "1" || input == "2"))
- {
- cout << "Invalid input.\n\n";
- input = "0";
- }
- else // input == 1 or 2
- {
- //int choice = atoi(input.c_str());
- }
- }while(input == "0");
- cout << endl;
- }
- /// Menu option to get friend recommendations
- void friendRecs()
- {
- cout << " - Friend Recommendations -" << endl
- << "People you may know:\n\n"
- // print all the ppl using graph (?)
- << "Enter choice or 'm' for menu: ";
- cout << endl;
- }
- void removeFriends(int id)
- {
- string name, input;
- bool results;
- bool option = true;
- User u = userID[id]; // temporarily assigned to spongebob
- while (option)
- {
- bool find = true;
- while (find)
- {
- cout << "Enter the name of the friend you'd like to remove: " << endl;
- cin.ignore();
- getline(cin, name);
- results = u.searchFriend(name);
- if (results == 0)
- {
- cout << "Friend not found. Please try again. \n" << endl;
- }
- else
- {
- u.removeFriend(name);
- u.printFriends();
- find = false;
- }
- }
- bool more = true;
- while (more)
- {
- cout << "Would you like to remove more friends? ";;
- cin >> input;
- transform(input.begin(), input.end(), input.begin(), ::toupper);
- if (input == "NO")
- {
- cout << "Back to the main menu. " << endl;
- //mainMenu();
- more = false;
- option = false;
- }
- else if (input == "YES")
- {
- more = false;
- }
- else
- {
- cout << "Please enter only yes or no " << endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement