You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
6.6 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 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(string query)
  87. {
  88. stack<Person> temp = users;
  89. while (!temp.empty())
  90. {
  91. Person x = users.top();
  92. if (x.getName() == query) currentUser = x;
  93. users.pop();
  94. cout << "User found";
  95. }
  96. throw std::invalid_argument("");
  97. }
  98. void login()
  99. {
  100. if (wrongPass >= 3)
  101. {
  102. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  103. quit();
  104. }
  105. try {
  106. cout << "Username:";
  107. cin >> username;
  108. cout << "Password:";
  109. cin >> password;
  110. getUser(username);
  111. } catch (invalid_argument inae) {
  112. cout << "Invalid username or password. Please try again.";
  113. wrongPass++;
  114. login();
  115. }
  116. if (!currentUser.checkPassword(password))
  117. {
  118. cout << "Invalid username or password. Please try again.";
  119. wrongPass++;
  120. login();
  121. }
  122. loggedIn();
  123. }
  124. void registration()
  125. {
  126. bool available = true;
  127. do
  128. {
  129. cout << "Username : ";
  130. cin >> username;
  131. stack<Person> temp = users;
  132. while(!temp.empty())
  133. {
  134. Person currentPerson = temp.top();
  135. if (currentUser.getName() == username)
  136. {
  137. cout << "Username unavailable. Please try again." << endl;
  138. available = false;
  139. break;
  140. }
  141. temp.pop();
  142. }
  143. } while (available == false);
  144. cout << "Username is available." << endl;
  145. do
  146. {
  147. cout << "Password : ";
  148. cin >> password;
  149. cout << "Confirm password :";
  150. cin >> password2;
  151. if (password != password2)
  152. cout << "Password unmatched. Please try again.";
  153. } while (password != password2);
  154. cout << "Email : ";
  155. cin >> email;
  156. cout << "Phone No. : ";
  157. cin >> phoneNo;
  158. Person newUser(username, password, email, phoneNo);
  159. users.push(newUser);
  160. cout << "Account is successfully registered." << endl;
  161. currentUser = newUser;
  162. loggedIn();
  163. }
  164. void loggedIn()
  165. {
  166. int choice;
  167. cout << "Please choose one of the following option :" << endl;
  168. cout << "View credentials - 1" << endl;
  169. cout << "Edit credentials - 2" << endl;
  170. cout << "Exit - 0" << endl;
  171. cin >> choice;
  172. switch (choice)
  173. {
  174. case 1:
  175. cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
  176. loggedIn();
  177. break;
  178. case 2:
  179. editCredentials();
  180. loggedIn();
  181. break;
  182. case 0:
  183. quit();
  184. }
  185. }
  186. void editCredentials()
  187. {
  188. int choice;
  189. cout << "Which of the following to edit?" << endl;
  190. cout << "Username - 1" << endl;
  191. cout << "Password - 2" << endl;
  192. cout << "Email - 3" << endl;
  193. cout << "Phone No. - 4" << endl;
  194. cout << "Exit - 0" << endl;
  195. cin >> choice;
  196. switch (choice)
  197. {
  198. case 1:
  199. chgUsername();
  200. break;
  201. case 2:
  202. chgPassword();
  203. break;
  204. case 3:
  205. chgEmail();
  206. break;
  207. case 4:
  208. chgPhoneNo();
  209. break;
  210. }
  211. }
  212. void update(Person newUser)
  213. {
  214. stack<Person> newUsers;
  215. while (!users.empty())
  216. {
  217. if (users.top().getName() == currentUser.getName())
  218. {
  219. cout << "replaced" << endl;
  220. newUsers.push(newUser);
  221. }
  222. else
  223. {
  224. newUsers.push(users.top());
  225. }
  226. users.pop();
  227. }
  228. users = newUsers;
  229. currentUser = newUser;
  230. }
  231. void chgUsername()
  232. {
  233. bool available;
  234. Person newUsers;
  235. do
  236. {
  237. available = true;
  238. cout << available << endl;
  239. cout << "Username : ";
  240. cin >> username;
  241. stack<Person> temp = users;
  242. while (!temp.empty())
  243. {
  244. if (temp.top().getName() == username)
  245. {
  246. cout << "Username unavailable. Please try again." << endl;
  247. cout << temp.top().getName() << " " << temp.top().getPhoneNo() << endl;
  248. available = false;
  249. break;
  250. }
  251. temp.pop();
  252. }
  253. } while (available == false);
  254. cout << "Username is available." << endl;
  255. Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
  256. update(newPerson);
  257. cout << "Username is updated." << endl;
  258. }
  259. void chgPassword()
  260. {
  261. cout << "Please input the current password : ";
  262. cin >> password;
  263. while (!currentUser.checkPassword(password))
  264. {
  265. cout << "Wrong password. Please try again." << endl;
  266. wrongPass++;
  267. cout << "Please input the current password : ";
  268. cin >> password;
  269. }
  270. do {
  271. cout << "Please input the new password : ";
  272. cin >> password;
  273. cout << "Please confirm your new password : ";
  274. cin >> password2;
  275. } while(password2 != password);
  276. currentUser.setPassword(password);
  277. update(currentUser);
  278. }
  279. void chgEmail()
  280. {
  281. cout << "Please input the new email : ";
  282. cin >> email;
  283. currentUser.setEmail(email);
  284. update(currentUser);
  285. }
  286. void chgPhoneNo()
  287. {
  288. cout << "Please input the new phone no. : ";
  289. cin >> phoneNo;
  290. currentUser.setPhoneNo(phoneNo);
  291. update(currentUser);
  292. }
  293. void quit()
  294. {
  295. ofstream fout("database.txt");
  296. while (!users.empty())
  297. {
  298. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  299. users.pop();
  300. }
  301. exit (EXIT_SUCCESS);
  302. }