Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

390 righe
7.2 KiB

  1. /*
  2. * Written by : Bin Hong Lee
  3. * Last edited : Dec 18, 2016
  4. *
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. #include <algorithm>
  11. #include "Person.hpp"
  12. using namespace std;
  13. //Declaration of functions
  14. Person getUser(string);
  15. Person getUser(int);
  16. void login();
  17. void loggedIn(Person);
  18. Person registration();
  19. Person editCredentials(Person);
  20. void update(Person);
  21. string chgUsername();
  22. string chgPassword();
  23. string chgEmail();
  24. string chgPhoneNo();
  25. vector<Person> users;
  26. static int wrongPass = 0;
  27. static int globalId;
  28. int main()
  29. {
  30. ifstream fin("database.txt");
  31. while (!fin.eof())
  32. {
  33. string username;
  34. string password;
  35. string email;
  36. string phoneNo;
  37. int id;
  38. fin >> username >> password >> email >> phoneNo >> id;
  39. Person newPerson(username, password, email, phoneNo, id);
  40. users.push_back(newPerson);
  41. }
  42. users.pop_back();
  43. globalId = users.back().getId() + 1;
  44. int userOption = -1;
  45. while (userOption != 0)
  46. {
  47. cout << "Please choose one of the following options :" << endl;
  48. cout << "Log In - 1" << endl;
  49. cout << "Registration - 2" << endl;
  50. cout << "Exit - 0" << endl;
  51. cin >> userOption;
  52. switch (userOption)
  53. {
  54. case 1:
  55. while (wrongPass < 3)
  56. {
  57. login();
  58. }
  59. break;
  60. case 2: loggedIn(registration()); break;
  61. default:
  62. cout << "Invalid input. Please try again." << endl;
  63. }
  64. }
  65. ofstream fout("database.txt");
  66. while (!users.empty())
  67. {
  68. fout << users.back().getName() << " " << users.back().getPassword() << " " << users.back().getEmail() << " " << users.back().getPhoneNo() << " " << users.back().getId() << endl;
  69. users.pop_back();
  70. }
  71. return 0;
  72. }
  73. Person getUser(string name)
  74. {
  75. for (int i = 0; i < users.size(); i++)
  76. {
  77. if (users.at(i).getName() == name)
  78. {
  79. return users.at(i);
  80. }
  81. }
  82. throw invalid_argument("");
  83. }
  84. Person getUser(int toSearchId)
  85. {
  86. for (int i = 0; i < users.size(); i++)
  87. {
  88. if (users.at(i).getId() == toSearchId)
  89. {
  90. return users.at(i);
  91. }
  92. }
  93. throw invalid_argument("");
  94. }
  95. void login()
  96. {
  97. Person currentUser;
  98. string username;
  99. string password;
  100. //If the user already has 3 fail attempt to login
  101. if (wrongPass > 2)
  102. {
  103. //Print error message and exit
  104. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  105. return;
  106. }
  107. try
  108. {
  109. //Ask for username
  110. cout << "Username:";
  111. cin >> username;
  112. //Ask for password
  113. cout << "Password:";
  114. cin >> password;
  115. //Get the user
  116. currentUser = getUser(username);
  117. }
  118. catch (invalid_argument ag)
  119. {
  120. cout << "Invalid username or password. Please try again.";
  121. wrongPass++;
  122. return;
  123. }
  124. if (!currentUser.checkPassword(password))
  125. {
  126. cout << "Invalid username or password. Please try again.";
  127. wrongPass++;
  128. return;
  129. }
  130. loggedIn(currentUser);
  131. }
  132. Person registration()
  133. {
  134. bool available;
  135. string username;
  136. string password;
  137. string password2;
  138. string email;
  139. string phoneNo;
  140. do
  141. {
  142. try
  143. {
  144. cout << "Username: ";
  145. cin >> username;
  146. getUser(username);
  147. cout << "Username taken. Please try again." << endl;
  148. available = false;
  149. }
  150. catch (invalid_argument ag)
  151. {
  152. available = true;
  153. }
  154. } while (!available);
  155. cout << "Username is available." << endl;
  156. do
  157. {
  158. //Get password
  159. cout << "Password : ";
  160. cin >> password;
  161. //Confirm password
  162. cout << "Confirm password :";
  163. cin >> password2;
  164. //Print error message if both password is not the same
  165. if (password != password2)
  166. cout << "Password unmatched. Please try again.";
  167. //Loop until both password input is the same
  168. } while (password != password2);
  169. //Get email
  170. cout << "Email : ";
  171. cin >> email;
  172. //Get phone number
  173. cout << "Phone No. : ";
  174. cin >> phoneNo;
  175. //Create and push the new 'Person' into stack
  176. Person newUser(username, password, email, phoneNo, globalId);
  177. users.push_back(newUser);
  178. globalId++;
  179. //Print success message
  180. cout << "Account is successfully registered." << endl;
  181. return newUser;
  182. }
  183. void loggedIn(Person currentUser)
  184. {
  185. int choice;
  186. do {
  187. cout << "Please choose one of the following options: " << endl;
  188. cout << "View credentials - 1" << endl;
  189. cout << "Edit credentials - 2" << endl;
  190. cout << "Exit - 0" << endl;
  191. cin >> choice;
  192. switch(choice)
  193. {
  194. case 1:
  195. cout << "Username : " << currentUser.getName() << endl;
  196. cout << "Email : " << currentUser.getEmail() << endl;
  197. cout << "Phone No.: " << currentUser.getPhoneNo() << endl;
  198. break;
  199. case 2:
  200. editCredentials(currentUser);
  201. break;
  202. case 0:
  203. return;
  204. default:
  205. cout << "Invalid option. Please try again." << endl;
  206. cout << endl;
  207. }
  208. } while(choice != 0);
  209. }
  210. Person editCredentials(Person currentUser)
  211. {
  212. int choice;
  213. cout << "Which of the following to edit?" << endl;
  214. cout << "Username - 1" << endl;
  215. cout << "Password - 2" << endl;
  216. cout << "Email - 3" << endl;
  217. cout << "Phone No. - 4" << endl;
  218. cout << "Exit - 0" << endl;
  219. cin >> choice;
  220. switch (choice)
  221. {
  222. case 1:
  223. currentUser.setName(chgUsername());
  224. cout << "Username is updated." << endl;
  225. break;
  226. case 2:
  227. currentUser.setPassword(chgPassword());
  228. cout << "Password is updated." << endl;
  229. break;
  230. case 3:
  231. currentUser.setEmail(chgEmail());
  232. cout << "Email is updated." << endl;
  233. break;
  234. case 4:
  235. currentUser.setPhoneNo(chgPhoneNo());
  236. cout << "Phone number is updated." << endl;
  237. break;
  238. }
  239. return currentUser;
  240. }
  241. void update(Person newInfo)
  242. {
  243. int position = users.size() / 2;
  244. while (position < users.size() && position >= 0)
  245. {
  246. if (users.at(position).getId() == newInfo.getId())
  247. {
  248. Person oldInfo = users.at(position);
  249. replace(users.begin(), users.end(), oldInfo, newInfo);
  250. return;
  251. }
  252. if (users.at(position).getId() < newInfo.getId())
  253. {
  254. position++;
  255. }
  256. else
  257. {
  258. position--;
  259. }
  260. }
  261. }
  262. string chgUsername()
  263. {
  264. bool availablility;
  265. string newUsername;
  266. do
  267. {
  268. availablility = true;
  269. cout << "New Username: ";
  270. cin >> newUsername;
  271. for (int i = 0; i < users.size(); i++)
  272. {
  273. if (users.at(i).getName() == newUsername)
  274. {
  275. availablility = false;
  276. break;
  277. }
  278. }
  279. } while (!availablility);
  280. cout << "Username is available." << endl;
  281. return newUsername;
  282. }
  283. string chgPassword(Person currentUser)
  284. {
  285. string password;
  286. string newPassword0;
  287. string newPassword1;
  288. cout << "Please input the current password : ";
  289. cin >> password;
  290. while (!currentUser.checkPassword(password) && wrongPass < 3)
  291. {
  292. cout << "Wrong password. Please try again." << endl;
  293. wrongPass++;
  294. cout << "Please input the current password : ";
  295. cin >> password;
  296. }
  297. do {
  298. cout << "Please input the new password : ";
  299. cin >> newPassword0;
  300. cout << "Please confirm your new password : ";
  301. cin >> newPassword1;
  302. } while(newPassword0 != newPassword1);
  303. return newPassword0;
  304. }
  305. string chgEmail()
  306. {
  307. string newEmail;
  308. cout << "Please input the new email : ";
  309. cin >> newEmail;
  310. return newEmail;
  311. }
  312. string chgPhoneNo()
  313. {
  314. string newPhoneNo;
  315. cout << "Please input the new phone no. : ";
  316. cin >> newPhoneNo;
  317. return newPhoneNo;
  318. }