您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LogInSystem.cpp 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Written by : BinHong Lee
  3. * Last edited : 5/12/2016
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <stack>
  8. #include <fstream>
  9. #include "Person.cpp"
  10. using namespace std;
  11. //Declaration of functions
  12. void getUser(string);
  13. void login();
  14. void adminLogin();
  15. void loggedIn();
  16. void registration();
  17. void editCredentials();
  18. void update(Person);
  19. void chgUsername();
  20. void chgPassword();
  21. void chgEmail();
  22. void chgPhoneNo();
  23. void quit();
  24. //Stacks to store the data
  25. stack<Person> users;
  26. //Declaration of place holding variables
  27. Person currentUser;
  28. string username;
  29. string password;
  30. string password2;
  31. string email;
  32. string phoneNo;
  33. int wrongPass = 0;
  34. int main()
  35. {
  36. ifstream fin("database.txt");
  37. int choice;
  38. while (!fin.eof())
  39. {
  40. fin >> username >> password >> email >> phoneNo;
  41. Person temp(username, password, email, phoneNo);
  42. users.push(temp);
  43. }
  44. users.pop();
  45. cout << "Please choose one of the following options :" << endl;
  46. cout << "Log In - 1" << endl;
  47. cout << "Registration - 2" << endl;
  48. cout << "Exit - 0" << endl;
  49. cin >> choice;
  50. switch (choice)
  51. {
  52. case 1:
  53. login();
  54. break;
  55. case 2:
  56. registration();
  57. break;
  58. case 0:
  59. break;
  60. }
  61. ofstream fout("database.txt");
  62. while (!users.empty())
  63. {
  64. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  65. users.pop();
  66. }
  67. return 0;
  68. }
  69. void getUser(string query)
  70. {
  71. stack<Person> temp = users;
  72. while (!temp.empty())
  73. {
  74. Person x = users.top();
  75. if (x.getName() == query) currentUser = x;
  76. users.pop();
  77. cout << "User found";
  78. }
  79. throw std::invalid_argument("");
  80. }
  81. void login()
  82. {
  83. if (wrongPass >= 3)
  84. {
  85. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  86. quit();
  87. }
  88. try {
  89. cout << "Username:";
  90. cin >> username;
  91. cout << "Password:";
  92. cin >> password;
  93. getUser(username);
  94. } catch (invalid_argument inae) {
  95. cout << "Invalid username or password. Please try again.";
  96. wrongPass++;
  97. login();
  98. }
  99. if (!currentUser.checkPassword(password))
  100. {
  101. cout << "Invalid username or password. Please try again.";
  102. wrongPass++;
  103. login();
  104. }
  105. loggedIn();
  106. }
  107. void registration()
  108. {
  109. bool available = true;
  110. do
  111. {
  112. cout << "Username : ";
  113. cin >> username;
  114. stack<Person> temp = users;
  115. while(!temp.empty())
  116. {
  117. Person currentPerson = temp.top();
  118. if (currentUser.getName() == username)
  119. {
  120. cout << "Username unavailable. Please try again." << endl;
  121. available = false;
  122. break;
  123. }
  124. temp.pop();
  125. }
  126. } while (available == false);
  127. cout << "Username is available." << endl;
  128. do
  129. {
  130. cout << "Password : ";
  131. cin >> password;
  132. cout << "Confirm password :";
  133. cin >> password2;
  134. if (password != password2)
  135. cout << "Password unmatched. Please try again.";
  136. } while (password != password2);
  137. cout << "Email : ";
  138. cin >> email;
  139. cout << "Phone No. : ";
  140. cin >> phoneNo;
  141. Person newUser(username, password, email, phoneNo);
  142. users.push(newUser);
  143. cout << "Account is successfully registered." << endl;
  144. currentUser = newUser;
  145. loggedIn();
  146. }
  147. void loggedIn()
  148. {
  149. int choice;
  150. cout << "Please choose one of the following option :" << endl;
  151. cout << "View credentials - 1" << endl;
  152. cout << "Edit credentials - 2" << endl;
  153. cout << "Exit - 0" << endl;
  154. cin >> choice;
  155. switch (choice)
  156. {
  157. case 1:
  158. cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
  159. loggedIn();
  160. break;
  161. case 2:
  162. editCredentials();
  163. loggedIn();
  164. break;
  165. case 0:
  166. quit();
  167. }
  168. }
  169. void editCredentials()
  170. {
  171. int choice;
  172. cout << "Which of the following to edit?" << endl;
  173. cout << "Username - 1" << endl;
  174. cout << "Password - 2" << endl;
  175. cout << "Email - 3" << endl;
  176. cout << "Phone No. - 4" << endl;
  177. cout << "Exit - 0" << endl;
  178. cin >> choice;
  179. switch (choice)
  180. {
  181. case 1:
  182. chgUsername();
  183. break;
  184. case 2:
  185. chgPassword();
  186. break;
  187. case 3:
  188. chgEmail();
  189. break;
  190. case 4:
  191. chgPhoneNo();
  192. break;
  193. }
  194. }
  195. void update(Person newUser)
  196. {
  197. stack<Person> newUsers;
  198. while (!users.empty())
  199. {
  200. if (users.top().getName() == currentUser.getName())
  201. {
  202. cout << "replaced" << endl;
  203. newUsers.push(newUser);
  204. }
  205. else
  206. {
  207. newUsers.push(users.top());
  208. }
  209. users.pop();
  210. }
  211. users = newUsers;
  212. currentUser = newUser;
  213. }
  214. void chgUsername()
  215. {
  216. bool available;
  217. Person newUsers;
  218. do
  219. {
  220. available = true;
  221. cout << available << endl;
  222. cout << "Username : ";
  223. cin >> username;
  224. stack<Person> temp = users;
  225. while (!temp.empty())
  226. {
  227. if (temp.top().getName() == username)
  228. {
  229. cout << "Username unavailable. Please try again." << endl;
  230. cout << temp.top().getName() << " " << temp.top().getPhoneNo() << endl;
  231. available = false;
  232. break;
  233. }
  234. temp.pop();
  235. }
  236. } while (available == false);
  237. cout << "Username is available." << endl;
  238. Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
  239. update(newPerson);
  240. cout << "Username is updated." << endl;
  241. }
  242. void chgPassword()
  243. {
  244. cout << "Please input the current password : ";
  245. cin >> password;
  246. while (!currentUser.checkPassword(password))
  247. {
  248. cout << "Wrong password. Please try again." << endl;
  249. wrongPass++;
  250. cout << "Please input the current password : ";
  251. cin >> password;
  252. }
  253. do {
  254. cout << "Please input the new password : ";
  255. cin >> password;
  256. cout << "Please confirm your new password : ";
  257. cin >> password2;
  258. } while(password2 != password);
  259. currentUser.setPassword(password);
  260. update(currentUser);
  261. }
  262. void chgEmail()
  263. {
  264. cout << "Please input the new email : ";
  265. cin >> email;
  266. currentUser.setEmail(email);
  267. update(currentUser);
  268. }
  269. void chgPhoneNo()
  270. {
  271. cout << "Please input the new phone no. : ";
  272. cin >> phoneNo;
  273. currentUser.setPhoneNo(phoneNo);
  274. update(currentUser);
  275. }
  276. void quit()
  277. {
  278. ofstream fout("database.txt");
  279. while (!users.empty())
  280. {
  281. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  282. users.pop();
  283. }
  284. exit (EXIT_SUCCESS);
  285. }