Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LogInSystem.cpp 8.2 KiB

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