25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogInSystem.cpp 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Written by : BinHong Lee
  3. * Last edited : 7/7/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 users' personal 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. //Reading from file to build the database
  37. ifstream fin("database.txt");
  38. while (!fin.eof())
  39. {
  40. //Reading in line-by-line into the placeholding variables
  41. fin >> username >> password >> email >> phoneNo;
  42. //Create the Person and push it into the stack
  43. Person temp(username, password, email, phoneNo);
  44. users.push(temp);
  45. }
  46. //Pop off the additional data added into the stack due to the empty line
  47. users.pop();
  48. getOption:
  49. //Display user input options
  50. cout << "Please choose one of the following options :" << endl;
  51. cout << "Log In - 1" << endl;
  52. cout << "Registration - 2" << endl;
  53. cout << "Exit - 0" << endl;
  54. //Get user input
  55. int choice;
  56. cin >> choice;
  57. //Switch according to user input
  58. switch (choice)
  59. {
  60. case 1:
  61. login();
  62. break;
  63. case 2:
  64. registration();
  65. break;
  66. case 0:
  67. break;
  68. default:
  69. //Display error message
  70. cout << "Invalid input. Please try again." << endl;
  71. goto getOption;
  72. }
  73. //Saving data into file
  74. ofstream fout("database.txt");
  75. //Check if the stack is empty
  76. while (!users.empty())
  77. {
  78. //Move the data into the files each Person per line
  79. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  80. //Pop the Person out of stack after the data is moved
  81. users.pop();
  82. }
  83. //End the program
  84. return 0;
  85. }
  86. void getUser()
  87. {
  88. //Duplicate the current stack to be checked through
  89. stack<Person> temp = users;
  90. //Loop while the temporary stack is not empty
  91. while (!temp.empty())
  92. {
  93. //Check if the username match the query
  94. //If so, set it as the currentUser
  95. if (users.top().getName() == username) currentUser = users.top();
  96. //Pop the checked user
  97. users.pop();
  98. }
  99. //Throw invalid_argument error to be caught if the person is not found
  100. throw std::invalid_argument("");
  101. }
  102. void login()
  103. {
  104. //If the user already has 3 fail attempt to login
  105. if (wrongPass >= 3)
  106. {
  107. //Print error message and exit
  108. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  109. return;
  110. }
  111. try {
  112. //Ask for username
  113. cout << "Username:";
  114. cin >> username;
  115. //Ask for password
  116. cout << "Password:";
  117. cin >> password;
  118. //Get the user
  119. getUser();
  120. } catch (invalid_argument inae) {
  121. //Print error message
  122. cout << "Invalid username or password. Please try again.";
  123. //Increase wrongPass count that indicate the amount of times of invalid
  124. //credentials input by the user
  125. wrongPass++;
  126. login();
  127. }
  128. //Check if the password is correct
  129. if (!currentUser.checkPassword(password))
  130. {
  131. //Print error message
  132. cout << "Invalid username or password. Please try again.";
  133. //Increase wrongPass count that indicate the amount of times of invalid
  134. //credentials input by the user
  135. wrongPass++;
  136. login();
  137. }
  138. //User is successfully logged in
  139. loggedIn();
  140. }
  141. void registration()
  142. {
  143. //Initialize boolean
  144. bool available;
  145. do
  146. {
  147. try
  148. {
  149. //Get username
  150. cout << "Username: ";
  151. cin >> username;
  152. //Check if username is already in use
  153. //If not, it will throw an invalid_argument error to be caught
  154. getUser();
  155. //Print error message
  156. cout << "Username unavailable. Please try again." << endl;
  157. //Set available as false and continue the loop
  158. available = false;
  159. //Catch the invalid_argument error thrown by getUser()
  160. } catch (invalid_argument inae)
  161. {
  162. //Set available to true and quit the loop
  163. available = true;
  164. }
  165. } while (available == false);
  166. //Print success message
  167. cout << "Username is available." << endl;
  168. do
  169. {
  170. //Get password
  171. cout << "Password : ";
  172. cin >> password;
  173. //Confirm password
  174. cout << "Confirm password :";
  175. cin >> password2;
  176. //Print error message if both password is not the same
  177. if (password != password2)
  178. cout << "Password unmatched. Please try again.";
  179. //Loop until both password input is the same
  180. } while (password != password2);
  181. //Get email
  182. cout << "Email : ";
  183. cin >> email;
  184. //Get phone number
  185. cout << "Phone No. : ";
  186. cin >> phoneNo;
  187. //Create and push the new 'Person' into stack
  188. Person newUser(username, password, email, phoneNo);
  189. users.push(newUser);
  190. //Print success message
  191. cout << "Account is successfully registered." << endl;
  192. //Automatically login user
  193. currentUser = newUser;
  194. loggedIn();
  195. }
  196. void loggedIn()
  197. {
  198. //Declare space to save user's option
  199. int choice;
  200. //Print options
  201. cout << "Please choose one of the following option :" << endl;
  202. cout << "View credentials - 1" << endl;
  203. cout << "Edit credentials - 2" << endl;
  204. cout << "Exit - 0" << endl;
  205. //Get user selection
  206. cin >> choice;
  207. switch (choice)
  208. {
  209. case 1:
  210. //Print out user credentials
  211. cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
  212. //Nested call back into the menu
  213. loggedIn();
  214. break;
  215. case 2:
  216. editCredentials();
  217. //Nested call back into the menu
  218. loggedIn();
  219. break;
  220. case 0:
  221. break;
  222. default:
  223. cout << "Invalid option. Please try again." << endl;
  224. cout << endl;
  225. loggedIn();
  226. }
  227. }
  228. void editCredentials()
  229. {
  230. int choice;
  231. cout << "Which of the following to edit?" << endl;
  232. cout << "Username - 1" << endl;
  233. cout << "Password - 2" << endl;
  234. cout << "Email - 3" << endl;
  235. cout << "Phone No. - 4" << endl;
  236. cout << "Exit - 0" << endl;
  237. cin >> choice;
  238. switch (choice)
  239. {
  240. case 1:
  241. chgUsername();
  242. break;
  243. case 2:
  244. chgPassword();
  245. break;
  246. case 3:
  247. chgEmail();
  248. break;
  249. case 4:
  250. chgPhoneNo();
  251. break;
  252. }
  253. }
  254. void update(Person newUser)
  255. {
  256. stack<Person> newUsers;
  257. while (!users.empty())
  258. {
  259. if (users.top().getName() == currentUser.getName())
  260. {
  261. cout << "replaced" << endl;
  262. newUsers.push(newUser);
  263. }
  264. else
  265. {
  266. newUsers.push(users.top());
  267. }
  268. users.pop();
  269. }
  270. users = newUsers;
  271. currentUser = newUser;
  272. }
  273. void chgUsername()
  274. {
  275. bool available;
  276. Person newUsers;
  277. do
  278. {
  279. available = true;
  280. cout << available << endl;
  281. cout << "Username : ";
  282. cin >> username;
  283. stack<Person> temp = users;
  284. while (!temp.empty())
  285. {
  286. if (temp.top().getName() == username)
  287. {
  288. cout << "Username unavailable. Please try again." << endl;
  289. cout << temp.top().getName() << " " << temp.top().getPhoneNo() << endl;
  290. available = false;
  291. break;
  292. }
  293. temp.pop();
  294. }
  295. } while (available == false);
  296. cout << "Username is available." << endl;
  297. Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
  298. update(newPerson);
  299. cout << "Username is updated." << endl;
  300. }
  301. void chgPassword()
  302. {
  303. cout << "Please input the current password : ";
  304. cin >> password;
  305. while (!currentUser.checkPassword(password))
  306. {
  307. cout << "Wrong password. Please try again." << endl;
  308. wrongPass++;
  309. cout << "Please input the current password : ";
  310. cin >> password;
  311. }
  312. do {
  313. cout << "Please input the new password : ";
  314. cin >> password;
  315. cout << "Please confirm your new password : ";
  316. cin >> password2;
  317. } while(password2 != password);
  318. currentUser.setPassword(password);
  319. update(currentUser);
  320. }
  321. void chgEmail()
  322. {
  323. cout << "Please input the new email : ";
  324. cin >> email;
  325. currentUser.setEmail(email);
  326. update(currentUser);
  327. }
  328. void chgPhoneNo()
  329. {
  330. cout << "Please input the new phone no. : ";
  331. cin >> phoneNo;
  332. currentUser.setPhoneNo(phoneNo);
  333. update(currentUser);
  334. }
  335. /*
  336. void quit()
  337. {
  338. ofstream fout("database.txt");
  339. while (!users.empty())
  340. {
  341. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  342. users.pop();
  343. }
  344. exit (EXIT_SUCCESS);
  345. }
  346. */