Advertisement
Guest User

haynako

a guest
Mar 8th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 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. pUser->password;
  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", &cTempType);
  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. printf("Input authorization code: ");
  110. while (strcmp(arrInputCode,arrCompareCode)!=0){
  111. scanf("%s%c", arrInputCode,&cDump);
  112.  
  113. if (strcmp(arrInputCode,arrCompareCode)==0){
  114.  
  115. (*pUser).type = 'A';
  116. //AdminChoice();
  117. printf("Account Type: Admin \n");
  118.  
  119. }
  120. else {
  121.  
  122. printf("Invalid authorization code, try again. \n");
  123. printf("Input authorization code: ");
  124. scanf("%s%c", arrInputCode,&cDump);
  125. }
  126. }
  127.  
  128.  
  129. }
  130.  
  131. void getUserInfo (userInfoType *pInfo)
  132. {
  133.  
  134. printf("Enter name: \n");
  135. printf("First Name: ");
  136. fgets((*pInfo).name.first,21,stdin);
  137. printf("Middle Name: ");
  138. fgets((*pInfo).name.middle,21,stdin);
  139. printf("Last Name: ");
  140. fgets((*pInfo).name.last,51,stdin);
  141.  
  142. printf("%s,%s,%s \n", (*pInfo).name.last,(*pInfo).name.first,(*pInfo).name.middle);
  143.  
  144.  
  145.  
  146.  
  147. printf("Enter address: ");
  148. fgets((*pInfo).address,51,stdin);
  149. (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  150. (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  151. (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  152. (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  153.  
  154. }
  155.  
  156.  
  157. int ValidPass (userType *pUser)
  158. {
  159. int i, valid=0;
  160. /*while (i<strlen(pUser->password)){
  161.  
  162. if ((pUser->password[i]) < 'a' || (pUser->password[i]) > 'z')
  163. if ((pUser->password[i]) < 'A' || (pUser->password)[i] > 'Z')
  164. valid = 1;
  165. i++;
  166. }
  167. */
  168.  
  169. for(i=0;i<(strlen(pUser->password));i++){
  170.  
  171. if (pUser->password[i] < 'a' || pUser->password[i] >'z')
  172. if (pUser->password[i] < 'A' || pUser->password[i] >'Z')
  173. valid=1;
  174. }
  175. return valid;
  176.  
  177. }
  178.  
  179. ptrUser search (ptrUser pFirst, string15 username)
  180. {
  181. ptrUser pRun;
  182.  
  183. pRun = pFirst;
  184.  
  185. while (pRun != NULL && strcmp(username, pRun->username)!=0)
  186. pRun = pRun->pNext;
  187.  
  188. return pRun;
  189. }
  190.  
  191. void displayAll (userType *pUsers)
  192. {
  193. while (pUsers!=NULL){ //stop traversing when there is no more new address
  194.  
  195. printf("%s\n", pUsers->username);
  196. pUsers = pUsers->pNext; //node traversal
  197.  
  198. }
  199.  
  200.  
  201. }
  202.  
  203. void signIn (userType *pUsers)
  204. {
  205. int loggedin=0;
  206. char arrTempUser[16], arrTempPW[16], arrInputCode[9];
  207. char authcode[9]={"DLSU2017"};
  208.  
  209. while (loggedin == 0){
  210.  
  211. printf("Enter username: \n");
  212. fgets(arrTempUser,16,stdin);
  213.  
  214. printf("Enter password: \n");
  215. fgets(arrTempPW,16,stdin);
  216.  
  217. search(pUsers, pUsers->username);
  218. if (strcmp(arrTempUser,pUsers->username)!=0)
  219. printf("Not found \n");
  220. signIn (pUsers);
  221.  
  222. if (pUsers->type == 'A'){
  223.  
  224. printf("Input authorization code: ");
  225. scanf("%s", arrInputCode);
  226. while (strcmp(arrInputCode,authcode)!=0)
  227. if (strcmp(arrInputCode,authcode)==0){
  228. printf("Admin");
  229. loggedin == 1;
  230. }
  231. else {
  232.  
  233. printf("Invalid authorization code, try again.");
  234. scanf("%s", arrInputCode);
  235. }
  236. }
  237.  
  238. if (pUsers->type == 'S')
  239. loggedin == 1;
  240. printf("Shopper");
  241.  
  242. }
  243. }
  244.  
  245. void freeAll (ptrUser *pFirst)
  246. {
  247. userType *pDel;
  248.  
  249. while(*pFirst != NULL){
  250. pDel = *pFirst;
  251. *pFirst = (*pFirst)->pNext;
  252. free(pDel);
  253. }
  254.  
  255.  
  256.  
  257. }
  258.  
  259. void menu (userType *pUsers, userType *pNew, userType *pLast)
  260. {
  261.  
  262. int opt,
  263. choice=0;
  264. char cDump;
  265.  
  266. printf("MAIN MENU \n");
  267. printf("[1] Sign Up \n");
  268. printf("[2] Log In \n");
  269. printf("Choice: ");
  270. scanf("%d%c", &choice,&cDump);
  271.  
  272. switch (choice){
  273. case 1: do {
  274.  
  275.  
  276.  
  277. pNew = malloc(sizeof(userType));
  278. signUp(pNew);
  279.  
  280.  
  281. (*pNew).pNext = NULL;
  282.  
  283. if (pUsers == NULL) //if initially empty
  284. pUsers = pNew; //pNew is the first node
  285. else //connect nodes to the list
  286. pLast->pNext = pNew;
  287.  
  288. pLast = pNew;
  289. getUserInfo (&pUsers->info);
  290. printf("Press [1] to create a new user or [0] to return to main menu: ");
  291. scanf("%d%c", &opt,&cDump);
  292.  
  293. if (opt == 0)
  294. menu (pUsers, pNew, pLast);
  295. }while(opt == 0);
  296.  
  297. break;
  298.  
  299. case 2: signIn(pUsers);
  300. break;
  301. default:freeAll(&pUsers);
  302. exit (0);
  303. }
  304.  
  305.  
  306.  
  307. }
  308.  
  309.  
  310.  
  311.  
  312. int main ()
  313. {
  314.  
  315. userType *pUsers = NULL,
  316. *pNew,
  317. *pLast;
  318.  
  319. menu (pUsers, pNew, pLast);
  320.  
  321.  
  322.  
  323. //displayAll(pUsers);
  324. freeAll(&pUsers);
  325.  
  326. return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement