Advertisement
Guest User

wtf2

a guest
Mar 9th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX_CART 100
  6.  
  7. typedef char string15[16];
  8. typedef char string20[21];
  9. typedef char string50[51];
  10. typedef char string8[9];
  11.  
  12. typedef struct {
  13. string20 first,middle,last;
  14. }nameType;
  15.  
  16. typedef struct{
  17. nameType name;
  18. string50 address;
  19. }userInfoType; //for user information
  20.  
  21. typedef struct{
  22. string8 code;
  23. int qty;
  24. }prodBoughtType; //for the cart
  25.  
  26. typedef prodBoughtType arrBought[MAX_CART];
  27. struct userTag{
  28.  
  29. string15 username,
  30. password;
  31. userInfoType info;
  32. char type;
  33. float creditlimit,
  34. outstanding;
  35. arrBought cart;
  36. int nItems;
  37. struct userTag *pNext;
  38. };
  39. typedef struct userTag userType;
  40. typedef userType * ptrUser;
  41.  
  42.  
  43.  
  44. void signUp (userType **pUser)
  45. {
  46. char cDump, choice, cTempType = '\0';
  47. char arrInputCode[9];
  48. char arrCompareCode[9] = {"DLSU2017"};
  49.  
  50.  
  51.  
  52. do{
  53. printf("Enter username: ");
  54. scanf("%s%c", (*pUser)->username,&cDump); //placing the information directly within the container. Username is inside pUser
  55. }while (strlen((*pUser)->username)<3 || strlen((*pUser)->username)>15);
  56.  
  57. do{
  58. printf("Enter password: ");
  59. scanf("%s%c", (*pUser)->password,&cDump); //placing the information directly within the container. Username is inside pUser
  60. if (strlen((*pUser)->password)<6){
  61.  
  62. printf("Password too short, please try again. \n");
  63. printf("Enter password: ");
  64. scanf("%s%c", (*pUser)->password,&cDump);
  65. }
  66. if (strlen((*pUser)->password)>15){
  67.  
  68. printf("Password too long, please try again. \n");
  69. printf("Enter password: ");
  70. scanf("%s%c", (*pUser)->password,&cDump);
  71. }
  72.  
  73. if (ValidPass(pUser)==1)
  74. printf("Password is okay");
  75. else{
  76. printf("Error, password must contain non-letter character.\n");
  77. printf("Enter password: ");
  78. scanf("%s%c", (*pUser)->password,&cDump);
  79. }
  80.  
  81.  
  82. }while (strlen((*pUser)->password)<6 || strlen((*pUser)->password)>15);
  83.  
  84.  
  85.  
  86.  
  87.  
  88. printf("Create [S]hopper or [A]dministrator account? \n");
  89. printf("Input: ");
  90. scanf("%c%c", &cTempType, &cDump);
  91.  
  92. if(cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
  93. printf("Invalid input, please try again: \n");
  94. while (cTempType != 's' && cTempType != 'S' && cTempType != 'a' && cTempType != 'A'){
  95. printf("Input: ");
  96. scanf("%c%c",&cTempType, &cDump);
  97. }
  98. }
  99.  
  100.  
  101. if (cTempType == 's' || cTempType == 'S'){
  102.  
  103. (*pUser)->type = 'S';
  104. printf("Account Type: Shopper \n");
  105.  
  106. }
  107.  
  108. else if (cTempType == 'a' || cTempType == 'A'){
  109.  
  110. printf("Input authorization code: ");
  111. while (strcmp(arrInputCode,arrCompareCode)!=0){
  112. scanf("%s%c", arrInputCode,&cDump);
  113.  
  114. if (strcmp(arrInputCode,arrCompareCode)==0){
  115.  
  116. (*pUser)->type = 'A';
  117. //AdminChoice();
  118. printf("Account Type: Admin \n");
  119.  
  120. }
  121. else {
  122.  
  123. printf("Invalid authorization code, try again. \n");
  124. printf("Input authorization code: ");
  125. scanf("%s%c", arrInputCode,&cDump);
  126. }
  127. }
  128. }
  129.  
  130.  
  131. }
  132.  
  133. void getUserInfo (userInfoType *pInfo)
  134. {
  135.  
  136. printf("Enter name: \n");
  137. printf("First Name: ");
  138. fgets((*pInfo).name.first,21,stdin);
  139. printf("Middle Name: ");
  140. fgets((*pInfo).name.middle,21,stdin);
  141. printf("Last Name: ");
  142. fgets((*pInfo).name.last,51,stdin);
  143.  
  144. printf("%s,%s,%s \n", (*pInfo).name.last,(*pInfo).name.first,(*pInfo).name.middle);
  145.  
  146.  
  147.  
  148.  
  149. printf("Enter address: ");
  150. fgets((*pInfo).address,51,stdin);
  151. (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  152. (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  153. (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  154. (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  155.  
  156. }
  157.  
  158.  
  159. int ValidPass (userType *pUser)
  160. {
  161. int i, valid=0;
  162. /*while (i<strlen(pUser->password)){
  163.  
  164. if ((pUser->password[i]) < 'a' || (pUser->password[i]) > 'z')
  165. if ((pUser->password[i]) < 'A' || (pUser->password)[i] > 'Z')
  166. valid = 1;
  167. i++;
  168. }
  169. */
  170.  
  171. for(i=0;i<(strlen(pUser->password));i++){
  172.  
  173. if (pUser->password[i] < 'a' || pUser->password[i] >'z')
  174. if (pUser->password[i] < 'A' || pUser->password[i] >'Z')
  175. valid=1;
  176. }
  177. return valid;
  178.  
  179. }
  180.  
  181.  
  182.  
  183. ptrUser search (ptrUser pFirst, string15 username)
  184. {
  185. ptrUser pRun;
  186.  
  187. pRun = pFirst;
  188.  
  189. while (pRun != NULL && strcmp(username, pRun->username)!=0)
  190. pRun = pRun->pNext;
  191.  
  192. return pRun;
  193. }
  194.  
  195. void displayAll (userType *pUsers)
  196. {
  197. while (pUsers!=NULL){ //stop traversing when there is no more new address
  198.  
  199. printf("%s\n", pUsers->username);
  200. pUsers = pUsers->pNext; //node traversal
  201.  
  202. }
  203.  
  204.  
  205. }
  206.  
  207. void signIn (userType *pUsers)
  208. {
  209. int loggedin=0;
  210. char arrTempUser[16], arrTempPW[16], arrInputCode[9], arrUserCode[16];
  211. char authcode[9]={"DLSU2017"};
  212. char cDump;
  213. ptrUser pLogin, pPassword;
  214.  
  215. while (loggedin == 0){
  216.  
  217.  
  218. printf("Enter username: ");
  219. scanf("%s", arrTempUser);fflush(stdin);
  220.  
  221. printf("Enter password: ");
  222. scanf("%s", arrTempPW);fflush(stdin);
  223.  
  224.  
  225. pLogin = search(pUsers, arrTempUser);
  226. pPassword = search(pUsers, pUsers->password);
  227.  
  228. if (pLogin == NULL)
  229. printf("Not found \n");
  230.  
  231.  
  232. if (strcmp(pLogin->username, arrTempUser)==0){
  233.  
  234. if (strcmp(pPassword->password, arrTempPW)==0){
  235. printf("Username is fine \n");
  236. printf("Password is fine \n");
  237. loggedin = 1;
  238. }
  239. else
  240. printf("Something is wrong with password \n");
  241.  
  242. }
  243. else printf("Something is wrong with username \n");
  244. printf("%s", arrTempUser);
  245. printf("%d", strcmp(pLogin->username, arrTempUser));
  246.  
  247. if (pUsers->type == 'A'){
  248.  
  249. printf("Input authorization code: ");
  250. scanf("%s%c", arrInputCode, &cDump);
  251.  
  252. while (strcmp(arrInputCode,authcode)!=0){
  253. printf("Incorrect code, please try again: ");
  254. scanf("%s%c", arrInputCode, &cDump);
  255. }
  256.  
  257. loggedin = 1;
  258. printf("Admin menu");
  259. }
  260.  
  261. if (pUsers->type == 'S'){
  262.  
  263. printf("Shopper Menu");
  264. loggedin = 1;
  265. }
  266.  
  267. }
  268.  
  269.  
  270.  
  271. }
  272.  
  273.  
  274. void freeAll (ptrUser *pFirst)
  275. {
  276. userType *pDel;
  277.  
  278. while(*pFirst != NULL){
  279. pDel = *pFirst;
  280. *pFirst = (*pFirst)->pNext;
  281. free(pDel);
  282. }
  283.  
  284. }
  285.  
  286. void menu (userType *pUsers, userType *pNew, userType *pLast)
  287. {
  288. int opt,
  289. choice=0;
  290. char cDump;
  291.  
  292. printf("MAIN MENU \n");
  293. printf("[1] Sign Up \n");
  294. printf("[2] Log In \n");
  295. printf("Choice: ");
  296. scanf("%d%c", &choice,&cDump);
  297.  
  298. do {
  299.  
  300.  
  301.  
  302. pNew = malloc(sizeof(userType));
  303. signUp(&pNew);
  304.  
  305.  
  306. (*pNew).pNext = NULL;
  307.  
  308. if (pUsers == NULL) //if initially empty
  309. pUsers = pNew; //pNew is the first node
  310. else //connect nodes to the list
  311. pLast->pNext = pNew;
  312.  
  313. pLast = pNew;
  314. getUserInfo (&pUsers->info);
  315. printf("Press [1] to create a new user or [0] to return to main menu: ");
  316.  
  317. scanf("%d%c", &opt,&cDump);
  318.  
  319. switch (choice){
  320. case 1: menu (pUsers, pNew, pLast);
  321. break;
  322.  
  323. case 2: signIn(pUsers);
  324. break;
  325. default:freeAll(&pUsers);
  326. exit (0);
  327. }
  328.  
  329. }while (opt == 1);
  330.  
  331.  
  332.  
  333.  
  334. freeAll(&pUsers);
  335.  
  336. }
  337.  
  338. void ShopperMenu (userType *pUsers, userType *pNew, userType *pLast)
  339. {
  340. int choice = 0;
  341.  
  342. printf("[1] Modify User Info \n");
  343. printf("[2] Browse All Products \n");
  344. printf("[3] Browse Products by Category \n");
  345. printf("[4] Browse Products on Sale \n");
  346. printf("[5] Add to Cart \n");
  347. printf("[6] View Cart \n");
  348. printf("[7] Check Out \n");
  349. printf("[8] Edit Cart Items \n");
  350. printf("[0] Return to Shopper Menu");
  351. scanf("%d", choice);
  352.  
  353. switch (choice){
  354. case 1: //view locked
  355. break;
  356. case 2: //unlock specific
  357. break;
  358. case 3: //unlock all
  359. break;
  360. case 4: //outstanding
  361. break;
  362. case 5: //outstanding
  363. break;
  364. case 6: //outstanding
  365. break;
  366. case 7: //outstanding
  367. break;
  368. case 8: //outstanding
  369. break;
  370. default: menu (pUsers, pNew, pLast);
  371. break;
  372. }
  373.  
  374.  
  375. }
  376.  
  377. int main ()
  378. {
  379.  
  380. userType *pUsers = NULL,
  381. *pNew = NULL,
  382. *pLast = NULL;
  383. ptrUser pRun, pTrail;
  384. menu (pUsers, pNew, pLast);
  385.  
  386.  
  387.  
  388. //displayAll(pUsers);
  389.  
  390.  
  391. return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement