25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

355 satır
6.0 KiB

  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. }