Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AccountManager.h"
- #include "FileManager.h"
- #include "InputValidator.h"
- #include "Chat.h"
- AccountManager::AccountManager()
- {
- CurrentUser = nullptr;
- }
- AccountManager::~AccountManager()
- {
- delete CurrentUser;
- Users.clear();
- }
- void AccountManager::loadUsers()
- {
- }
- bool AccountManager::loginUser()
- {
- string name, password;
- cout << "\tuser name: ";
- cin >> name;
- cout << "\tpassword: ";
- cin >> password;
- for each (Account* acc in Users)
- {
- if (acc->getUserName() == name) {
- if (acc->isPasswordValid(password)) {
- CurrentUser = acc;
- return true;
- }
- }
- }
- cout << "\n\tWrong username or password!" << endl;
- return false;
- }
- bool AccountManager::registerUser()
- {
- string userName, password, email, realName, city, address, birthDate;
- InputValidator iv;
- do {
- cout << "\tEnter Username (required): ";
- getline(cin, userName);
- } while (!iv.isUserNameValid(userName));
- do {
- cout << "\tEnter Password (required): ";
- getline(cin, password);
- } while (!iv.isPasswordValid(password));
- do {
- cout << "\tEnter Email (required): ";
- getline(cin, email);
- } while (!iv.isEmailValid(email));
- do {
- cout << "\tEnter your real name (optional): ";
- getline(cin, realName);
- } while (!iv.isRealNameValid(realName));
- do {
- cout << "\tEnter your birth date (optional): ";
- getline(cin, birthDate);
- } while (!iv.isBirthdateValid(birthDate));
- do {
- cout << "\tEnter the city you are living in (optional): ";
- getline(cin, city);
- } while (!iv.isAddressValid(city));
- do {
- cout << "\tEnter your address (optional): ";
- getline(cin, address);
- } while (!iv.isAddressValid(address));
- for each (Account* acc in Users)
- {
- if (userName == acc->getUserName()) {
- cout << "There is already user with that name!" << endl;
- return false;
- }
- }
- FileManager fm;
- Account* newAccount = new Account(userName, password, email, realName, birthDate, city, address);
- fm.saveUser(newAccount);
- Users.push_back(newAccount);
- CurrentUser = newAccount;
- return true;
- }
- void AccountManager::previewUser()
- {
- cout << "\n\t***********Profile of " << CurrentUser->getUserName() << " ***********" << endl;
- }
- void AccountManager::previewChats()
- {
- vector<Chat*> userChats = CurrentUser->getChats();
- cout << "\tUser chats:" << endl;
- for (int i = userChats.size() - 1; i >= 0; i--)
- {
- cout << "\t--- " << userChats[i]->getChatWith()->getUserName() << endl;
- }
- }
- bool AccountManager::writeMessage()
- {
- cout << "\tEnter the name of the user you want to chat with" << endl;;
- string userName;
- getline(cin, userName);
- if (userName == CurrentUser->getUserName()) {
- cout << "/tYou can't chat your self!\n";
- return false;
- }
- Account* chatWith = nullptr;
- for each (Account* acc in Users)
- {
- if (userName == acc->getUserName()) {
- chatWith = acc;
- break;
- }
- }
- if (chatWith == nullptr) {
- cout << "\tthere is no user with that name!" << endl;
- return false;
- }
- cout << "\tEnter message to " << chatWith->getUserName() << endl;
- string content;
- getline(cin, content);
- for each (Chat* chatOutGoing in CurrentUser->getChats())
- {
- if (chatOutGoing->getChatWith()->getUserName() == chatWith->getUserName()) {
- chatOutGoing->addContent(content);
- for each (Chat* chatInComming in chatWith->getChats())
- {
- if (chatInComming->getChatWith()->getUserName() == CurrentUser->getUserName()) {
- chatInComming->addContent(content);
- }
- }
- cout << "\n\tMessage sent\n\n" << endl;
- return true;
- }
- }
- Chat* newChatOutGoing = new Chat(chatWith);
- Chat* newChatInComming = new Chat(CurrentUser);
- newChatOutGoing->addContent(content);
- newChatInComming->addContent(content);
- CurrentUser->addChat(newChatOutGoing);
- chatWith->addChat(newChatInComming);
- cout << "\n\tMessage sent\n\n" << endl;
- return true;
- }
- bool AccountManager::readMessage()
- {
- cout << "\tEnter the name of the chat you want to read" << endl;;
- string userName;
- cin >> userName;
- for each (Chat* chat in CurrentUser->getChats())
- {
- if (chat->getChatWith()->getUserName() == userName) {
- cout << "\n" << chat->getContent() << "\n";
- return true;
- }
- }
- cout << "\tYou have no chats with that user" << endl;
- return true;
- }
- void AccountManager::previewFriends()
- {
- vector<Account*> friends = CurrentUser->getFriends();
- cout << "\tUser friends:" << endl;
- for each (Account* acc in friends)
- {
- cout << "\t--- " << acc->getUserName() << endl;
- }
- }
- void AccountManager::searchUser()
- {
- cout << "\tEnter name to search for\n";
- string userName;
- bool found = false;
- getline(cin, userName);
- for each (Account* acc in Users)
- {
- int a = acc->getUserName().find_first_of(userName);
- if (a >= 0) {
- cout << "\t---" << acc->getUserName() << endl;
- found = true;
- }
- }
- if (!found) {
- cout << "\tNo users with that name!" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement