選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

394 行
7.4 KiB

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