Guest User

drive

a guest
Apr 7th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. // File: a5simpledriver.cpp
  2. // Author: Geoffrey Tien
  3. // Date: March 26, 2016
  4. // Description: Simple test driver and UI for CMPT 225 assignment 5
  5.  
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10.  
  11. #include "slinkedlist.h"
  12. #include "hashtable.h"
  13.  
  14. using namespace std;
  15.  
  16. // forward function declarations
  17. void PrintMenu(bool loginstatus, int ulevel);
  18. void LLTest();
  19. void HTTest();
  20.  
  21. // program entry point
  22.  
  23. int main()
  24. {
  25. //cout <<"Badman fawud badman pullup" << endl;
  26. //getchar();
  27.  
  28. cout << "Entering linked list test..." << endl;
  29. LLTest();
  30. cout << "Entering hash table test..." << endl;
  31. HTTest();
  32. cout << endl;
  33.  
  34. int choice = 0;
  35. string inputchoice;
  36. string inputname = ""; // currently logged in user
  37. string inputnewname = ""; // for adding or removing user
  38. string inputoldpassword1 = "";
  39. string inputoldpassword2 = "";
  40. string inputnewpassword = "";
  41. bool passwordmismatch = true;
  42. string inputlevel = "";
  43. int ilevel = 1;
  44.  
  45. bool loggedin = false;
  46. int level = REGULAR_;
  47.  
  48. HashTable ht;
  49. // insert a default admin account
  50. ht.Insert(UserAccount("admin", ADMIN_));
  51.  
  52. while (choice != 7)
  53. {
  54. PrintMenu(loggedin, level);
  55. // get the menu choice from standard input
  56. getline(cin, inputchoice);
  57. choice = atoi(inputchoice.c_str());
  58.  
  59. switch (choice)
  60. {
  61. case 1:
  62. // log in, log out
  63. if (!loggedin)
  64. {
  65. cout << "Enter username: ";
  66. getline(cin, inputname);
  67. cout << "Enter password: ";
  68. getline(cin, inputoldpassword1);
  69. if (!ht.Search(UserAccount(inputname, 0)))
  70. {
  71. cout << "Invalid username.\n" << endl;
  72. }
  73. else
  74. {
  75. UserAccount* ua = ht.Retrieve(UserAccount(inputname, 0)); // will not return NULL
  76. if (inputoldpassword1 != ua->GetPassword())
  77. {
  78. cout << "Invalid password.\n" << endl;
  79. }
  80. else
  81. {
  82. loggedin = true;
  83. level = ua->GetUserLevel();
  84. }
  85. }
  86. }
  87. else
  88. {
  89. cout << "Logged out.\n" << endl;
  90. loggedin = false;
  91. level = REGULAR_;
  92. // clear local variables for next login
  93. inputname = "";
  94. inputnewname = "";
  95. inputoldpassword1 = "";
  96. inputoldpassword2 = "";
  97. inputnewpassword = "";
  98. passwordmismatch = true;
  99. }
  100. break;
  101. case 2:
  102. // change password
  103. if (loggedin)
  104. {
  105. passwordmismatch = true;
  106. while (passwordmismatch && inputoldpassword1 != "quit")
  107. {
  108. cout << "Enter old password or type quit to exit: ";
  109. getline(cin, inputoldpassword1);
  110. if (inputoldpassword1 != "quit")
  111. {
  112. cout << "Enter old password again: ";
  113. getline(cin, inputoldpassword2);
  114. passwordmismatch = (inputoldpassword1 != inputoldpassword2);
  115. }
  116. else
  117. {
  118. passwordmismatch = false;
  119. }
  120. }
  121. if (inputoldpassword1 != "quit")
  122. {
  123. cout << "Enter new password: ";
  124. getline(cin, inputnewpassword);
  125. if (ht.Retrieve(UserAccount(inputname, 0))->SetPassword(inputoldpassword1, inputnewpassword))
  126. cout << "Password updated.\n" << endl;
  127. else
  128. cout << "Error updating password.\n" << endl;
  129. }
  130. }
  131. break;
  132. case 3:
  133. // admin-only, add new user
  134. if (loggedin && level == ADMIN_)
  135. {
  136. cout << "Enter new username (lowercase only): ";
  137. getline(cin, inputnewname);
  138. cout << "Enter access level (0 = ADMIN, 1 = REGULAR): ";
  139. getline(cin, inputlevel);
  140. ilevel = atoi(inputlevel.c_str());
  141. if (ht.Insert(UserAccount(inputnewname, ilevel)))
  142. cout << "New user " << inputnewname << " added.\n" << endl;
  143. else
  144. cout << "Error adding user.\n" << endl;
  145. }
  146. break;
  147. case 4:
  148. // admin-only, reset user password
  149. if (loggedin && level == ADMIN_)
  150. {
  151. cout << "Enter username for password reset: ";
  152. getline(cin, inputnewname);
  153. if (!ht.Search(UserAccount(inputnewname, 0)))
  154. {
  155. cout << "Invalid username.\n" << endl;
  156. }
  157. else
  158. {
  159. UserAccount* uap = ht.Retrieve(UserAccount(inputnewname, 0));
  160. uap->SetPassword(uap->GetPassword(), "password");
  161. cout << "Password for user " << uap->GetUsername() << " reset to default.\n" << endl;
  162. }
  163. }
  164. break;
  165. case 5:
  166. // admin-only, edit user level
  167. if (loggedin && level == ADMIN_)
  168. {
  169. cout << "Enter username for access level edit: ";
  170. getline(cin, inputnewname);
  171. if (inputnewname == "admin")
  172. {
  173. cout << "Cannot edit access level of admin.\n" << endl;
  174. }
  175. else if (!ht.Search(UserAccount(inputnewname, 0)))
  176. {
  177. cout << "Invalid username.\n" << endl;
  178. }
  179. else
  180. {
  181. cout << "Enter new access level (0 = ADMIN, 1 = REGULAR): ";
  182. getline(cin, inputlevel);
  183. ilevel = atoi(inputlevel.c_str());
  184. UserAccount* uap = ht.Retrieve(UserAccount(inputnewname, 0));
  185. if (uap->SetUserLevel(ilevel))
  186. cout << "Access level for user " << uap->GetUsername() << " successfully changed.\n" << endl;
  187. else
  188. cout << "Error setting access level for user " << uap->GetUsername() << ".\n" << endl;
  189. }
  190. }
  191. break;
  192. case 6:
  193. // admin-only, remove user
  194. if (loggedin && level == ADMIN_)
  195. {
  196. cout << "Enter username to remove: ";
  197. getline(cin, inputnewname);
  198. if (inputnewname == "admin")
  199. {
  200. cout << "Cannot remove admin.\n" << endl;
  201. }
  202. else if (ht.Remove(UserAccount(inputnewname, 0)))
  203. {
  204. cout << "User " << inputnewname << " removed.\n" << endl;
  205. }
  206. else
  207. {
  208. cout << "Error removing user " << inputnewname << ".\n" << endl;
  209. }
  210. }
  211. break;
  212. case 7:
  213. // do nothing, causes while loop to exit
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219.  
  220. return 0;
  221.  
  222. }
  223.  
  224. void PrintMenu(bool loginstatus, int ulevel)
  225. {
  226. if (!loginstatus)
  227. {
  228. cout << "****************************************************\n"
  229. << "* Please select an option: *\n"
  230. << "* 1. Login 7. Quit *\n"
  231. << "****************************************************\n" << endl;
  232. cout << "Enter your choice: ";
  233. }
  234. else
  235. {
  236. if (ulevel == ADMIN_)
  237. {
  238. cout << "****************************************************\n"
  239. << "* Please select an option: *\n"
  240. << "* 1. Logout 6. Remove a user *\n"
  241. << "* 2. Change password *\n"
  242. << "* 3. Add a new user *\n"
  243. << "* 4. Reset user password *\n"
  244. << "* 5. Edit user level 7. Quit *\n"
  245. << "****************************************************\n" << endl;
  246. cout << "Enter your choice: ";
  247. }
  248. else
  249. {
  250. cout << "****************************************************\n"
  251. << "* Please select an option: *\n"
  252. << "* 1. Logout 7. Quit *\n"
  253. << "* 2. Change password *\n"
  254. << "****************************************************\n" << endl;
  255. cout << "Enter your choice: ";
  256. }
  257. }
  258. }
  259.  
  260. void LLTest()
  261. {
  262.  
  263. SLinkedList<int> lla;
  264. if ( lla.IsEmpty()== true){
  265. cout << "empty \n";}
  266.  
  267.  
  268. lla.InsertBack(3);
  269. lla.InsertFront(2);
  270. lla.InsertBack(4);
  271. lla.InsertFront(1);
  272. int* ret = lla.Retrieve(1);
  273.  
  274. cout << " - "<< *ret << " - a di number oh";
  275.  
  276.  
  277. vector<int> v1 = lla.Dump();
  278.  
  279. int sz = v1.size();
  280. cout << "{ ";
  281. for (int i=0;i<sz;i++)
  282. {
  283. cout << v1[i] ;
  284. cout << " ";
  285. }
  286. cout << " }";
  287.  
  288. lla.IsEmpty();
  289. lla.Retrieve(1);
  290. int g= lla.Size();
  291. cout<< " SIZE OF A: " << g << endl;
  292.  
  293.  
  294. SLinkedList<int> llb(lla);
  295. vector<int> v2 = llb.Dump();
  296.  
  297. int sz2 = v2.size();
  298. cout << "{ ";
  299. for (int i=0;i<sz2;i++)
  300. {
  301. cout << v2[i] ;
  302. cout << " ";
  303. }
  304. cout << " }";
  305. int a= llb.Size();
  306. cout<< " SIZE OF B: " << a << endl;
  307.  
  308. SLinkedList<int> llc = lla;
  309. vector<int> v3 = llc.Dump();
  310.  
  311. int sz3 = v3.size();
  312. cout << "{ ";
  313. for (int i=0;i<sz3;i++)
  314. {
  315. cout << v3[i] ;
  316. cout << " ";
  317. }
  318. cout << " }";
  319. int b= llc.Size();
  320. cout<< " SIZE OF C: " << b << endl;
  321. }
  322.  
  323. void HTTest()
  324. {
  325. HashTable ht1;
  326. int sz;
  327.  
  328. ht1.Insert(UserAccount("admin", ADMIN_));
  329.  
  330.  
  331. ht1.Remove(UserAccount("bob", REGULAR_));
  332.  
  333. sz = ht1.Size();
  334. cout<< "Size 2: "<< sz << "\n" ;
  335.  
  336.  
  337. //ht1.Retrieve(UserAccount("bob", REGULAR_));
  338.  
  339.  
  340.  
  341. if ( ht1.Search(UserAccount("admin", ADMIN_))){
  342. cout << "FOUND\n";
  343. }
  344.  
  345.  
  346. HashTable ht2(10);
  347. ht1.MaxSize();
  348. ht1.ListAt(0);
  349. ht1.LoadFactor();
  350.  
  351. HashTable ht3 = ht1;
  352.  
  353.  
  354. }
Add Comment
Please, Sign In to add comment