Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include "AccountManager.h"
  2. #include "FileManager.h"
  3. #include "InputValidator.h"
  4. #include "Chat.h"
  5.  
  6. AccountManager::AccountManager()
  7. {
  8. CurrentUser = nullptr;
  9. }
  10.  
  11. AccountManager::~AccountManager()
  12. {
  13. delete CurrentUser;
  14. Users.clear();
  15. }
  16.  
  17. void AccountManager::loadUsers()
  18. {
  19. }
  20.  
  21. bool AccountManager::loginUser()
  22. {
  23. string name, password;
  24.  
  25. cout << "\tuser name: ";
  26. cin >> name;
  27. cout << "\tpassword: ";
  28. cin >> password;
  29.  
  30. for each (Account* acc in Users)
  31. {
  32. if (acc->getUserName() == name) {
  33. if (acc->isPasswordValid(password)) {
  34. CurrentUser = acc;
  35. return true;
  36. }
  37. }
  38. }
  39.  
  40. cout << "\n\tWrong username or password!" << endl;
  41. return false;
  42. }
  43.  
  44. bool AccountManager::registerUser()
  45. {
  46. string userName, password, email, realName, city, address, birthDate;
  47. InputValidator iv;
  48.  
  49. do {
  50. cout << "\tEnter Username (required): ";
  51. getline(cin, userName);
  52. } while (!iv.isUserNameValid(userName));
  53.  
  54. do {
  55. cout << "\tEnter Password (required): ";
  56. getline(cin, password);
  57. } while (!iv.isPasswordValid(password));
  58.  
  59. do {
  60. cout << "\tEnter Email (required): ";
  61. getline(cin, email);
  62. } while (!iv.isEmailValid(email));
  63.  
  64. do {
  65. cout << "\tEnter your real name (optional): ";
  66. getline(cin, realName);
  67. } while (!iv.isRealNameValid(realName));
  68.  
  69. do {
  70. cout << "\tEnter your birth date (optional): ";
  71. getline(cin, birthDate);
  72. } while (!iv.isBirthdateValid(birthDate));
  73.  
  74. do {
  75. cout << "\tEnter the city you are living in (optional): ";
  76. getline(cin, city);
  77. } while (!iv.isAddressValid(city));
  78.  
  79. do {
  80. cout << "\tEnter your address (optional): ";
  81. getline(cin, address);
  82. } while (!iv.isAddressValid(address));
  83.  
  84. for each (Account* acc in Users)
  85. {
  86. if (userName == acc->getUserName()) {
  87. cout << "There is already user with that name!" << endl;
  88. return false;
  89. }
  90. }
  91.  
  92. FileManager fm;
  93.  
  94. Account* newAccount = new Account(userName, password, email, realName, birthDate, city, address);
  95. fm.saveUser(newAccount);
  96. Users.push_back(newAccount);
  97. CurrentUser = newAccount;
  98.  
  99. return true;
  100. }
  101.  
  102. void AccountManager::previewUser()
  103. {
  104. cout << "\n\t***********Profile of " << CurrentUser->getUserName() << " ***********" << endl;
  105. }
  106.  
  107. void AccountManager::previewChats()
  108. {
  109. vector<Chat*> userChats = CurrentUser->getChats();
  110. cout << "\tUser chats:" << endl;
  111. for (int i = userChats.size() - 1; i >= 0; i--)
  112. {
  113. cout << "\t--- " << userChats[i]->getChatWith()->getUserName() << endl;
  114. }
  115. }
  116.  
  117. bool AccountManager::writeMessage()
  118. {
  119. cout << "\tEnter the name of the user you want to chat with" << endl;;
  120. string userName;
  121. getline(cin, userName);
  122.  
  123. if (userName == CurrentUser->getUserName()) {
  124. cout << "/tYou can't chat your self!\n";
  125. return false;
  126. }
  127.  
  128. Account* chatWith = nullptr;
  129.  
  130. for each (Account* acc in Users)
  131. {
  132. if (userName == acc->getUserName()) {
  133. chatWith = acc;
  134. break;
  135. }
  136. }
  137.  
  138. if (chatWith == nullptr) {
  139. cout << "\tthere is no user with that name!" << endl;
  140. return false;
  141. }
  142. cout << "\tEnter message to " << chatWith->getUserName() << endl;
  143.  
  144. string content;
  145. getline(cin, content);
  146.  
  147. for each (Chat* chatOutGoing in CurrentUser->getChats())
  148. {
  149. if (chatOutGoing->getChatWith()->getUserName() == chatWith->getUserName()) {
  150. chatOutGoing->addContent(content);
  151.  
  152. for each (Chat* chatInComming in chatWith->getChats())
  153. {
  154. if (chatInComming->getChatWith()->getUserName() == CurrentUser->getUserName()) {
  155. chatInComming->addContent(content);
  156. }
  157. }
  158. cout << "\n\tMessage sent\n\n" << endl;
  159. return true;
  160. }
  161. }
  162.  
  163. Chat* newChatOutGoing = new Chat(chatWith);
  164. Chat* newChatInComming = new Chat(CurrentUser);
  165. newChatOutGoing->addContent(content);
  166. newChatInComming->addContent(content);
  167. CurrentUser->addChat(newChatOutGoing);
  168. chatWith->addChat(newChatInComming);
  169.  
  170. cout << "\n\tMessage sent\n\n" << endl;
  171. return true;
  172. }
  173.  
  174. bool AccountManager::readMessage()
  175. {
  176. cout << "\tEnter the name of the chat you want to read" << endl;;
  177. string userName;
  178. cin >> userName;
  179.  
  180. for each (Chat* chat in CurrentUser->getChats())
  181. {
  182. if (chat->getChatWith()->getUserName() == userName) {
  183. cout << "\n" << chat->getContent() << "\n";
  184. return true;
  185. }
  186. }
  187.  
  188. cout << "\tYou have no chats with that user" << endl;
  189.  
  190. return true;
  191. }
  192.  
  193. void AccountManager::previewFriends()
  194. {
  195. vector<Account*> friends = CurrentUser->getFriends();
  196. cout << "\tUser friends:" << endl;
  197. for each (Account* acc in friends)
  198. {
  199. cout << "\t--- " << acc->getUserName() << endl;
  200.  
  201. }
  202. }
  203.  
  204. void AccountManager::searchUser()
  205. {
  206. cout << "\tEnter name to search for\n";
  207. string userName;
  208. bool found = false;
  209. getline(cin, userName);
  210.  
  211. for each (Account* acc in Users)
  212. {
  213. int a = acc->getUserName().find_first_of(userName);
  214. if (a >= 0) {
  215. cout << "\t---" << acc->getUserName() << endl;
  216. found = true;
  217. }
  218. }
  219.  
  220. if (!found) {
  221. cout << "\tNo users with that name!" << endl;
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement