Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.61 KB | None | 0 0
  1. /**********************************************************************************************************************
  2. This is to certify that this project is my own work, based on my personal efforts in studying and applying the concepts
  3. learned. I have constructed the functions and their respective algorithms and corresponding code by myself. The program
  4. was run, tested, and debugged by my own efforts. I further certify that I have not copied in part or whole or otherwise
  5. plagiarized the work of other students and/or persons.
  6. Fritz Edron M.Calimag, 11621206
  7. **********************************************************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define MAX_CART 100
  13.  
  14. typedef char string15[16];
  15. typedef char string20[21];
  16. typedef char string50[51];
  17. typedef char string8[9];
  18. typedef struct{
  19.  
  20. string20 first,
  21. middle,
  22. last;
  23.  
  24. }nameType;
  25. typedef struct{
  26.  
  27. nameType name;
  28. string50 address;
  29.  
  30. }userInfoType;
  31. typedef struct{
  32.  
  33. string8 code;
  34. int qty;
  35.  
  36. }prodBoughtType;
  37. typedef prodBoughtType arrBought[MAX_CART];
  38. struct userTag{
  39.  
  40. string15 username,
  41. password;
  42.  
  43. userInfoType info;
  44.  
  45. char type;
  46. float creditlimit,
  47. outstanding;
  48.  
  49. arrBought cart;
  50.  
  51. int items,
  52. nlogin,
  53. nlock;
  54.  
  55. struct userTag *pNext;
  56.  
  57. };
  58.  
  59. typedef struct userTag userType;
  60.  
  61. typedef struct userTag * ptrUser;
  62.  
  63. struct stockcateg
  64. {
  65. string15 category;
  66. struct stockinfo{
  67. string15 supplier,
  68. product;
  69. string8 pCode;
  70. int Qavail,
  71. Qsold;
  72. float purchasePrice,
  73. sellingPrice,
  74. discountRate;
  75. struct stockinfo *pNextstockinfo;
  76. };
  77. struct stockcateg *pNextstockcateg;
  78. };
  79.  
  80. typedef struct stockcateg stockcategtype;
  81.  
  82. typedef struct stockcateg * ptrStockcateg;
  83.  
  84. typedef struct stockinfo stocktype;
  85.  
  86. typedef struct stockinfo * ptrStock;
  87.  
  88. void displayAll(ptrUser pUsers){
  89.  
  90. while (pUsers != NULL){
  91. printf("%s\n", pUsers -> username);
  92. pUsers = pUsers -> pNext;
  93. }
  94.  
  95. }
  96.  
  97. void freeAll(ptrUser * pFirst){
  98.  
  99. ptrUser pDel;
  100.  
  101. while (*pFirst != NULL){
  102. pDel = *pFirst;
  103. *pFirst = (*pFirst) -> pNext;
  104. free(pDel);
  105. }
  106. }
  107.  
  108. void getUserInfo(userInfoType *pInfo){
  109.  
  110. printf("Enter Name \n");
  111. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  112. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  113. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  114.  
  115. printf("%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  116.  
  117.  
  118. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  119.  
  120. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  121. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  122. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  123. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  124.  
  125. }
  126.  
  127. void signUp(userType *pUser, userType *pUsers){
  128. string8 tempcode;
  129. char cDump;
  130. char acode[9]={"DLSU2017"};
  131.  
  132. do{
  133.  
  134. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  135. while(pUsers != NULL)
  136. if(strcmp((*pUser).username, pUsers->username)==0)
  137. {
  138. pUsers = pUsers->pNext;
  139. printf("Username Taken try again\n");
  140. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  141. }
  142. }while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
  143.  
  144. do{
  145.  
  146. printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
  147.  
  148. }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
  149.  
  150. do{
  151.  
  152. printf("Type A for Admin and S for Shopper: "); scanf("%c%c", &pUser->type, &cDump);
  153. if(pUser->type == 'A' || pUser->type == 'a'){
  154. printf("Enter authorization code: "); scanf("%s%c",&tempcode,&cDump);
  155. while(strcmp(tempcode, acode)!=0)
  156. printf(" Wrong Authorization Code try again");
  157. }
  158. else if(pUser->type == 'S' || pUser->type == 's'){
  159. printf("Congratulations you are now registered as a shopper!\n");
  160. pUsers->creditlimit = 5000.00;
  161. pUsers->outstanding = 0.00;
  162. }
  163. else
  164. printf("Invalid Input please try again:");
  165.  
  166. }while (pUser->type != 'A' && pUser->type != 'a' && pUser->type != 'S' && pUser->type != 's');
  167.  
  168. getUserInfo(&pUser -> info);
  169.  
  170. }
  171.  
  172. void displayAllRecur(ptrUser pUsers){
  173.  
  174. if (pUsers != NULL){
  175. printf("%s\n", pUsers -> username);
  176. displayAllRecur(pUsers -> pNext);
  177. }
  178. }
  179.  
  180. ptrUser search(ptrUser pFirst, string15 username){
  181.  
  182. ptrUser pRun;
  183. pRun = pFirst;
  184.  
  185. while (pRun != NULL && strcmp(username, pRun -> username) != 0){
  186.  
  187. pRun = pRun -> pNext;
  188.  
  189. }
  190.  
  191. return pRun;
  192.  
  193. }
  194.  
  195. void deleteNode(ptrUser *pFirst, string15 username){
  196.  
  197. ptrUser pFind, pRun;
  198.  
  199. if (*pFirst == NULL)
  200. printf("The List is empty");
  201.  
  202. else {
  203.  
  204. pFind = search(*pFirst, username);
  205. if (pFind == NULL)
  206.  
  207. printf("%s is not in the List\n", username);
  208.  
  209. else { //found the node to be deleted
  210.  
  211. if(pFind == *pFirst){ //deleting first node
  212.  
  213. *pFirst = (*pFirst) -> pNext;
  214.  
  215. }
  216.  
  217. else{ //delete from middle or end
  218.  
  219. pRun = *pFirst;
  220. while (pRun -> pNext == pFind)
  221. pRun = pRun -> pNext;
  222.  
  223. pRun -> pNext = pFind -> pNext;
  224.  
  225. }
  226.  
  227. free(pFind);
  228. pFind = NULL;
  229.  
  230. }
  231.  
  232. }
  233.  
  234. }
  235.  
  236. int login(userType *pUsers){
  237. int ctr=0;
  238. string15 userchk, passchk;
  239.  
  240. printf("Enter Username:");scanf("%s",&userchk);
  241. printf("Enter Password:");scanf("%s",&passchk);
  242. while(pUsers != NULL){
  243. do{
  244. pUsers = pUsers->pNext;
  245. printf("Username Invalid try again\n");
  246. }while(strcmp(pUsers -> username, userchk)!=0);
  247. do{
  248. ctr++;
  249. pUsers = pUsers->pNext;
  250. if(ctr==3){
  251. printf("counter exceeded account locked... Exiting program");
  252. pUsers->nlock = 1;
  253. return 0;
  254. }
  255.  
  256. printf("Incorrect Password try again\n");
  257. printf("Enter Password:");scanf("%s",&passchk);
  258.  
  259. }while(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0);
  260.  
  261. if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
  262. {
  263. if(pUsers->type=='A' || pUsers->type == 'a')
  264. {
  265. printf("going to admin menu\n");
  266. return 2;
  267. }
  268. else if(pUsers->type=='S' || pUsers->type == 's'){
  269. printf("welcome to the shopper menu\n");
  270. return 1;
  271. }
  272. }
  273. }
  274. }
  275.  
  276. void manageaccs(userType *pUsers){
  277. int g;
  278. string15 userchk;
  279.  
  280. printf("Welcome to Manage Accounts Menu");
  281.  
  282. do{
  283. printf("Type 1 to View Locked Accounts\nType 2 to Unlock Specific Account\nType 3 To Unlock All Locked Accounts\nType 4 to View Accounts with Outstanding Balance\nType 5 to Return to Administrator Menu\n");
  284. scanf("%d",&g);
  285.  
  286. switch(g){
  287. case 1:printf("Here are all the locked accounts\n");
  288. do{
  289. if(pUsers->nlock==1)
  290. printf("%s\n",pUsers->username);
  291. pUsers = pUsers->pNext;
  292. }while(pUsers!=NULL);break;
  293.  
  294. case 2:printf("Type the username of the account you wish to unlock:\n");scanf("%s", &userchk);
  295. do{
  296. pUsers = pUsers->pNext;
  297. if(strcmp(pUsers -> username, userchk)==0)
  298. pUsers->nlock = 0;
  299. printf("User unlocked");
  300. }while(pUsers!=NULL);break;
  301.  
  302. case 3:printf("Unlocking All Locked Accounts");
  303. do{
  304. pUsers = pUsers->pNext;
  305. pUsers->nlock = 0;
  306. }while(pUsers!=NULL && pUsers->nlock==1);break;
  307. case 4:printf("Displaying All Accounts with Outstanding Balance");
  308. do{
  309. pUsers = pUsers->pNext;
  310. if(pUsers->outstanding<0)
  311. printf("%s", pUsers->username);
  312. }while(pUsers!=NULL);break;
  313. case 5:printf("Returning to Admin Menu");break;
  314. }
  315. }while(g!=5);
  316. }
  317.  
  318. void addstock(stocktype *pNewStock, stocktype *pStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  319. int n,nqa,nqs;
  320. float fpp,fusp,fdr;
  321. char cDump;
  322.  
  323. do{
  324. printf("Enter Category:");fgets(pStockcateg->category, 16, stdin);
  325.  
  326. printf("You entered %s Category\n",pStockcateg->category);
  327.  
  328. printf("Input the name of the supplier:");scanf("%s%c",pNewStock->supplier,&cDump);
  329.  
  330. printf("Input name of product:");scanf("%s%c",pNewStock->product,&cDump);
  331.  
  332. printf("Enter Available Product Quantity: "); scanf("%d%c", &nqa, &cDump);pNewStock->Qavail = nqa;
  333.  
  334. printf("Input Price of the Product: ");scanf("%f%c", &fpp, &cDump);pNewStock->purchasePrice = fpp;
  335.  
  336. printf("Input the Unit Selling Price of the product: ");scanf("%f%c", &fusp, &cDump);pNewStock->sellingPrice = fusp;
  337.  
  338. printf("Input the Discount Rate of the product: ");scanf("%f%c", &fdr, &cDump);pNewStock->discountRate = fdr;
  339.  
  340. pNewStock->Qsold = 0;
  341.  
  342. printf("type 1 to add new stock\n0 to return to Manage Stocks Menu:");scanf("%d",&n);
  343. if(n!=1 || n!=0 )
  344. printf("Invalid Input... Returning you to Manage Stocks Menu");
  345.  
  346. }while(n!=0);
  347. }
  348.  
  349. void managestock(stocktype *pStock, stocktype *pNewStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  350. int g,a;
  351.  
  352. printf("Welcome to Manage Stocks Menu");
  353.  
  354. do{
  355. printf("Type 1 to Add new stock\nType 2 to View All Stocks\nType 3 To View stocks by category\nType 4 to View stocks to reorder\nType 5 to Modify Stock Info\n Type 6 to Restock\nType 7 to Save Inventory\n Type 8 to Update Inventory from file\nType 9 to return to Admin Menu:\n");
  356. scanf("%d",&g);
  357.  
  358. switch(g){
  359. case 1:printf("Adding New Stock");
  360. do{
  361. pNewStock = malloc(sizeof(stocktype));//Based almost everything from signup
  362.  
  363. addstock(pNewStock,pStock,pStockcateg,pLastStock,pNewStcateg,pLastStcateg);
  364. pNewStock->pNextstockinfo=NULL;
  365. pNewStcateg->pNextstockcateg=NULL;
  366. if(pStock==NULL && pStockcateg){
  367. pStock=pNewStock;
  368. pStockcateg=pNewStcateg;
  369. }
  370. else
  371. pLastStock->pNextstockinfo = pNewStock;
  372. pLastStcateg->pNextstockcateg = pNewStcateg;
  373. pLastStock = pNewStock;
  374. pLastStcateg = pNewStcateg;
  375. }while(a!=0);break;
  376. case 2:printf("Viewing All Stocks");break;
  377. case 3:printf("Viewing Stocks by Category");break;
  378. case 4:printf("View Stocks to reorder");break;
  379. case 5:printf("Modifying Stock Info");break;
  380. case 6:printf("Restocking");break;
  381. case 7:printf("Saving Inventory");break;
  382. case 8:printf("Updating Inventory from file");break;
  383. case 9:printf("Returning to Admin menu");break;
  384. }
  385. }while(g!=9);
  386. }
  387.  
  388. void AdminMenu (userType *pUsers, userInfoType *pInfo, userType *pNew, userType *pLast, int *end, stocktype *pStock, stocktype *pNewStock, stockcategtype *pStcateg, int *n, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  389. int i;
  390.  
  391. printf("Welcome to admin menu\n");
  392.  
  393. do{
  394. printf("Type 1 to go to Manage Accounts Menu\nType 2 to go to Manage Stocks Menu\nType 3 To Prepare Delivery Receipt\nType 4 to Shutdown Kiosk\nType 5 to Log out");
  395. scanf("%d",&i);
  396.  
  397. switch(i){
  398. case 1:printf("Going to Manage Accounts Menu please wait\n");
  399. manageaccs(pUsers);break;
  400. case 2:printf("Going to Manage Stocks Menu please wait\n");
  401. managestock(pStock, pNewStock, pStcateg, pLastStock,pNewStcateg,pLastStcateg);break;
  402. case 3:printf("Preparing Delivery Receipt Please Wait\n")/*phase 2*/;break;
  403. case 4:printf("Shutting Down\n");*end=1;break;//passes 1 to end so that it'll terminate the program
  404. case 5:printf("Logging Out Goodbye\n");*n=1;break;//assign 1 to n so that it'll return to sign up
  405. }
  406. }while(i!=4 && i!=5);
  407.  
  408. }
  409.  
  410. void ModifyInfo(userInfoType *pInfo){
  411.  
  412. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  413. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  414. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  415.  
  416. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  417.  
  418. printf("Your new name 'Last Name,First Name, Middle Name\n'%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  419. printf("Your new Address\n%s", pInfo->address);
  420.  
  421. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  422. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  423. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  424. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  425. }
  426.  
  427. void ShopperMenu(userType *pUsers, userInfoType *pInfo, int *n){
  428. int g;
  429.  
  430. printf("Welcome to shopper menu\n");
  431.  
  432. printf("Type 1 to Modify User Info\nType 2 to Browse All Products\nType 3 To Browse Products by category\nType 4 to Browse Products on Sale\nType 5 to Add to Cart\n Type 6 to View Cart\nType 7 to Settle OutStanding Balance\n Type 8 to Log Out:");
  433. scanf("%d",&g);
  434.  
  435. switch(g){
  436. case 1:printf("Modify your info\n");
  437. ModifyInfo(&pUsers->info);break;
  438. case 2:printf("Browsing All Products");break;
  439. case 3:printf("Browse Products by Category");break;
  440. case 4:printf("Browse Products on Sale");break;
  441. case 5:printf("Adding to cart");break;
  442. case 6:printf("Viewing Cart");break;
  443. case 7:printf("Settling Outstanding Balance");break;
  444. case 8:printf("Logging Out Goodbye\n");*n=1 ;break;
  445. }
  446. }
  447.  
  448. int main(){
  449.  
  450. /*
  451. userType user;
  452. userType *pUsers = NULL,
  453. *pNew;
  454. */
  455. ptrUser pUsers = NULL,
  456. pNew, pLast;
  457.  
  458. ptrStock pStock = NULL,
  459. pNewStock,pLastStock;
  460.  
  461. ptrStockcateg pStcateg = NULL,
  462. pNewStcateg,pLastStcateg;
  463.  
  464. int end=1,n;
  465. char cDump;
  466. ptrUser pRun, pTrail;
  467.  
  468. do{
  469. printf("Welcome to the shopping kiosk\n\n");
  470. printf("type 2 if you would like to log in\n1 if you would like to sign up\n0 if you would like to exit the program: ");
  471. scanf("%d",&n);
  472.  
  473. switch(n){
  474.  
  475. case 2:if(login(pUsers)==2)
  476. AdminMenu(pUsers, &pUsers->info, pNew, pLast, &end, pStock, pNewStock, pStcateg, &n, pLastStock, pNewStcateg, pLastStcateg);
  477. else if(login(pUsers)==1)
  478. ShopperMenu(pUsers, &pUsers->info, &n);
  479. else if(login(pUsers)==0)
  480. end=0;break;
  481.  
  482.  
  483. default:
  484. /*
  485. pNew = malloc(sizeof(userType));
  486.  
  487. signUp(pNew);
  488. pNew -> pNext = NULL;
  489.  
  490. if (pUsers == NULL)
  491. pUsers = pNew;
  492.  
  493. else
  494. pLast -> pNext = pNew;
  495.  
  496. pLast = pNew;
  497. */
  498.  
  499. pNew = malloc(sizeof(userType));
  500.  
  501. signUp(pNew,pUsers);
  502. pNew -> pNext = NULL;
  503.  
  504. if (pUsers == NULL) //if list is empty
  505. pUsers = pNew;
  506.  
  507. else if (strcmp(pUsers -> username, pNew -> username) > 0){ //conect as first node
  508.  
  509. pNew -> pNext = pUsers;
  510. pUsers = pNew;
  511.  
  512. }
  513.  
  514. else { //modifying middle of list
  515.  
  516. pRun = pUsers;
  517. while (pRun != NULL && strcmp(pRun -> username, pNew -> username) < 0){
  518.  
  519. pTrail = pRun;
  520. pRun = pRun -> pNext;
  521.  
  522. }
  523.  
  524. pTrail -> pNext = pNew;
  525. pNew -> pNext = pRun;
  526.  
  527. }
  528. printf("Type '1' to Return to Log in/Sign up menu or '0' To exit the program. "); scanf("%d%c", &end, &cDump);
  529.  
  530. break;
  531. //use for view locked accounts displayAllRecur(pUsers);
  532. //displayAll(pUsers);
  533.  
  534. }
  535. } while (end != 0);
  536. freeAll(&pUsers);
  537.  
  538. return 0;
  539. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement