Advertisement
luciapineda

STUPID SHIT

Mar 26th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.28 KB | None | 0 0
  1. /**********************************************************************************************
  2. This is to certify that this project is my own work,
  3. based on my personal efforts in studying and applying the concepts learned.
  4. I have constructed the functions and their respective algorithms and corresponding code by myself.
  5. The program was run, tested, and debugged by my own efforts. I further certify that I have not
  6. copied in part or whole or otherwise plagiarized the work of other students and/or persons.
  7.  
  8. PINEDA, Lucia Corazon L., DLSU ID# 11610697
  9. ***********************************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define MAX_CART 100
  15. typedef char string15[16];
  16. typedef char string20[21];
  17. typedef char string50[51];
  18. typedef char string8[9];
  19. typedef struct{
  20. string20 first,
  21. middle,
  22. last;
  23. } nameType;
  24. typedef struct{
  25. nameType name;
  26. string50 address;
  27. }userInfoType;
  28. typedef struct{
  29. string8 code;
  30. int qty;
  31. }prodBoughtType;
  32. typedef prodBoughtType arrBought[MAX_CART];
  33. struct userTag{
  34. string15 username,
  35. password;
  36. userInfoType info;
  37. char type;
  38. float creditlimit,
  39. outstanding;
  40. arrBought cart;
  41. int nItems,
  42. isLocked;
  43. struct userTag *pNext;
  44. };
  45. typedef struct userTag userType;
  46. typedef userType * ptrUser;
  47. struct productListTag{
  48. string15 supplier,
  49. product;
  50. int qtyAvailable;
  51. float purPrice,
  52. unitSellPrice,
  53. discRate;
  54. int qtySold;
  55. string8 prodCode;
  56. };
  57.  
  58. struct stockTag{
  59. string15 category;
  60. struct productListTag prodList;
  61. struct stockTag *pNext;
  62. };
  63. typedef struct stockTag stockType;
  64. typedef stockType * ptrStock;
  65.  
  66. ///////FUNCTIONS:
  67. /*function numDigits counts the number of digits of an integer
  68. @param: Number - input integer
  69. @return: digits - number of of digits of the integer
  70. */
  71. int numDigits(int number){
  72. int digits=0;
  73. if(number<0)
  74. digits=1;
  75. while(number){
  76. number/=10;
  77. digits++;
  78. }
  79.  
  80. return digits;
  81. }
  82.  
  83. /*Function freeAll frees all the User structures of their data and gets rid of all dangling pointers.
  84. @param: *pFirst - structure with data of current user
  85. */
  86. void freeAll(ptrUser *pFirst){
  87. ptrUser pDel;
  88. while(*pFirst!=NULL){
  89. pDel=*pFirst;
  90. *pFirst=(*pFirst)->pNext;
  91. free(pDel);
  92. }
  93. }
  94.  
  95. /*Function freeAllStocks Frees all the Stock structures of their data and gets rid of all dangling pointers.
  96. @param: *pFirst - structure with data of current stock
  97. */
  98.  
  99. void freeAllStocks(ptrStock *pFirst){
  100. ptrStock pDel;
  101. while(*pFirst!=NULL){
  102. pDel=*pFirst;
  103. *pFirst=(*pFirst)->pNext;
  104. free(pDel);
  105. }
  106. }
  107.  
  108. /*Function getUserInfo asks user to input their information, which consists of name and address
  109. @param: *pInfo - structure of “User Information” within the bigger structure with data of current user
  110. */
  111. void getUserInfo(userInfoType *pInfo){
  112. getchar();
  113. printf("[Name]\n");
  114. printf("First name: ");
  115. fgets(pInfo->name.first, 21, stdin);
  116. printf("Middle name: ");
  117. fgets(pInfo->name.middle, 21, stdin);
  118. printf("Last name: ");
  119. fgets(pInfo->name.last, 21, stdin);
  120. printf("Enter address: ");
  121. fgets(pInfo->address, 51, stdin);
  122.  
  123. //Removes the '\n' because fgets takes the enter key and adds it to the string
  124. pInfo->address[strlen(pInfo->address)-1]='\0';
  125. pInfo->name.first[strlen(pInfo->name.first)-1]='\0';
  126. pInfo->name.middle[strlen(pInfo->name.middle)-1]='\0';
  127. pInfo->name.last[strlen(pInfo->name.last)-1]='\0';
  128.  
  129. }
  130.  
  131. /*Function manageAccounts Displays the Manage Account Menu’s options (view locked accounts,
  132. unlock specific account, unlock all locked accounts, unlock all locked accounts, view accounts
  133. with outstanding balance, and return to administrator menu) and lets the user choose an option.
  134. @param: *pUser - structure that holds the information of the current user
  135. */
  136.  
  137. void viewLocked(userType *pCurrent){
  138. printf("\n[LOCKED ACCOUNTS]\n");
  139. while(pCurrent!=NULL){
  140. if(pCurrent->isLocked==1)
  141. printf("%s\n", pCurrent->username);
  142. pCurrent=pCurrent->pNext;
  143. }
  144. }
  145. void manageAccounts(userType *pUser){
  146. userType *pCurrent;
  147. int opt=0;
  148. char cDump;
  149. string15 username;
  150.  
  151. do{
  152. printf("\n------Manage Accounts Menu------\n");
  153. printf("1 - View Locked Accounts\n");
  154. printf("2 - Unlock Specific Account\n");
  155. printf("3 - Unlock All Locked Accounts\n");
  156. printf("4 - View Accounts with Outstanding Balance\n");
  157. printf("5 - Return to Administrator Menu\n\n");
  158.  
  159. printf("Choose option: ");
  160. scanf("%d", &opt);
  161.  
  162. switch(opt){
  163. case 1: viewLocked(pUser);
  164. break;
  165. case 2: viewLocked(pUser);
  166. printf("Input username of account to unlock: ");
  167. scanf("%s%c", username, &cDump);
  168.  
  169. pCurrent=pUser;
  170.  
  171. while(pCurrent!=NULL){
  172. if(pCurrent->isLocked==1 && strcmp(pCurrent->username, username)==0)
  173. pCurrent->isLocked=0;
  174. pCurrent=pCurrent->pNext;
  175. }
  176.  
  177. printf("The account of %s has been unlocked.\n", username);
  178. break;
  179. case 3: viewLocked(pUser);
  180.  
  181. pCurrent=pUser;
  182.  
  183. while(pCurrent!=NULL){
  184. if(pCurrent->isLocked==1)
  185. pCurrent->isLocked=0;
  186. pCurrent=pCurrent->pNext;
  187. }
  188.  
  189. printf("All locked accounts have been unlocked.\n");
  190. break;
  191. case 4: pCurrent=pUser;
  192. printf("\n[ACCOUNTS WITH OUTSTANDING BALANCE]\n");
  193. while(pCurrent!=NULL){
  194. if((pCurrent->type=='s' || pCurrent->type=='S') && pCurrent->outstanding>0.00){
  195. printf("username: %s\n", pCurrent->username);
  196. printf("full name: %s %s %s\n", pCurrent->info.name.first, pCurrent->info.name.middle, pCurrent->info.name.last);
  197. printf("Outstanding Balance: P %.2f\n", pCurrent->outstanding);
  198. }
  199. pCurrent=pCurrent->pNext;
  200. }
  201. break;
  202. case 5: break;
  203. }
  204. printf("\n");
  205. }while(opt!=5);
  206. }
  207.  
  208. /*Function checkStock checks if the input code was already used in a previous stock.
  209. @param: category - input category
  210. *pStock - structure that holds all the stock information
  211. */
  212. int checkStock(string15 category, stockType *pStock){
  213. int check=1;
  214. stockType *pOrig = pStock;
  215.  
  216. while(pOrig!=NULL)
  217. {
  218. if(strcmp(category, pOrig->category)==0)
  219. {
  220. check=0;
  221. pOrig=NULL;
  222. }
  223. else
  224. pOrig=pOrig->pNext;
  225. }
  226.  
  227. return check;
  228. }
  229.  
  230. int checkProd(string15 product, stockType *pStock){
  231. int check=1;
  232. stockType *pOrig = pStock;
  233.  
  234. while(pOrig!=NULL)
  235. {
  236. if(strcmp(product, pOrig->prodList.product)==0)
  237. {
  238. check=0;
  239. pOrig=NULL;
  240. }
  241. else
  242. pOrig=pOrig->pNext;
  243. }
  244.  
  245. return check;
  246. }
  247.  
  248.  
  249. void displayAllStocks(ptrStock pStocks){
  250. while(pStocks!=NULL){
  251. printf("\n%s\n", pStocks->category);
  252.  
  253. pStocks=pStocks->pNext;
  254. }
  255. }
  256.  
  257. /*Function addNewStock will add a new stock to the current list of stocks by asking stock information
  258. @param: *pStock - structure with information of current stock
  259. *pOriginal - structure with information of all stocks*/
  260. void addNewStock(stockType *pStock, stockType *pOriginal, int *categoryCtr){
  261. stockType *pCurrent=NULL,
  262. *pNew;
  263. string15 category,
  264. supplier,
  265. product;
  266. int unique=0,
  267. unique1=0,
  268. unique2=0,
  269. i=0,
  270. newStock=0,
  271. randomNum=0,
  272. uniqueProd=0;
  273. char cDump;
  274.  
  275. getchar();
  276.  
  277. printf("[ADD NEW STOCK]\n");
  278.  
  279. do{
  280. do{
  281. printf("Enter category: ");
  282. scanf("%s%c", &category, &cDump);
  283. }while(strlen(category)<3 || strlen(category)>15);
  284.  
  285. if(strlen(category)>=3 && strlen(category)<=15){
  286.  
  287. pCurrent=pOriginal;
  288.  
  289. unique=checkStock(category, pOriginal);
  290.  
  291. if(unique==1)
  292. { //NEW CATEGORY!!!
  293. (*categoryCtr)++;
  294. printf("\nNew Category! Category number %d\n", *categoryCtr);
  295. }
  296. if(unique!=1) //NOT A NEW CATEGORY
  297. printf("Category already exists.\n");
  298.  
  299. strcpy(pStock->category, category);
  300.  
  301. printf("[Product List]\n");
  302.  
  303. do{
  304. do{
  305. printf("Enter supplier: ");
  306. fgets(supplier, 16, stdin);
  307. supplier[strlen(supplier)-1]='\0';
  308. }while(strlen(supplier)>15);
  309.  
  310. do{
  311. printf("Enter product: ");
  312. fgets(product, 16, stdin);
  313. product[strlen(product)-1]='\0';
  314. }while(strlen(product)>15);
  315.  
  316. if(strlen(product)<=15){
  317.  
  318. uniqueProd=checkProd(product, pOriginal);
  319.  
  320. if(uniqueProd==1)
  321. {
  322. strcpy(pStock->prodList.supplier, supplier);
  323. strcpy(pStock->prodList.product, product);
  324. }
  325. if(uniqueProd!=1)
  326. {
  327. printf("Product already exists.\n");
  328. }
  329. }
  330. }while(uniqueProd!=1);
  331.  
  332. uniqueProd=0;
  333.  
  334. printf("Enter quantity available: ");
  335. scanf("%d", &pStock->prodList.qtyAvailable);
  336. printf("Enter purchase price: ");
  337. scanf("%f", &pStock->prodList.purPrice);
  338. printf("Enter unit selling price: ");
  339. scanf("%f", &pStock->prodList.unitSellPrice);
  340. printf("Enter discount rate: ");
  341. scanf("%f", &pStock->prodList.discRate);
  342.  
  343. pStock->prodList.qtySold=0;
  344.  
  345. pStock->prodList.prodCode[0]=category[0];
  346. pStock->prodList.prodCode[1]=supplier[0];
  347. pStock->prodList.prodCode[2]=product[0];
  348.  
  349. i=1;
  350.  
  351. while(i<=5){
  352. randomNum = rand()%58;
  353. if(randomNum>47 && randomNum<58){
  354. pStock->prodList.prodCode[(i+2)]=randomNum;
  355. i++;
  356. }
  357. }
  358.  
  359. pStock->prodList.prodCode[8]='\0';
  360.  
  361. printf("CHEEEECK\n");
  362. }
  363. }while(strlen(category)<3 || strlen(category)>15);
  364.  
  365. printf("CHECKING OUTSIDE DA LOOOOP\n");
  366. }
  367.  
  368. /*Function viewStocks displays all the stock information for the administrator, and the products for the shoppers
  369. @param: *pStocks - structure that holds all the stock information
  370. opt- number that shows if the user is a shopper or administrator
  371. */
  372. void viewStocks(ptrStock pStocks, int opt){
  373. int i=0,
  374. page=1,
  375. length,
  376. stockNum;
  377. string15 categoryInput;
  378. char nextPage;
  379. stockType *pCurrent;
  380. string15 space=" ",
  381. space2=" ",
  382. space3=" ";
  383. char space4[10]=" ";
  384. char space5[5]=" ";
  385. char space6[9]=" ";
  386. char space7[8]=" ";
  387.  
  388. stockNum=stockCount(pStocks);
  389.  
  390. if(opt==1){
  391. printf("\n[VIEW STOCKS]\n");
  392.  
  393. printf("Category Supplier Product Product Qty Qty Purchase Selling Discount\n");
  394. printf(" Code Available Sold Price Price Rate\n");
  395.  
  396. pCurrent=pStocks;
  397. i=1;
  398.  
  399. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  400. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  401.  
  402. while(i<20 && pCurrent!=NULL){
  403.  
  404. printf("%s", pCurrent->category);
  405. space[(15-strlen(pCurrent->category))]='\0';
  406. printf("%s", space);
  407.  
  408. printf("%s", pCurrent->prodList.supplier);
  409. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  410. printf("%s", space2);
  411.  
  412. printf("%s ", pCurrent->prodList.prodCode);
  413.  
  414. printf("%s", pCurrent->prodList.product);
  415. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  416. printf("%s", space3);
  417.  
  418. printf("%d", pCurrent->prodList.qtyAvailable);
  419. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  420. printf("%s ", space4);
  421.  
  422. printf("%d", pCurrent->prodList.qtySold);
  423. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  424. printf("%s", space5);
  425.  
  426. printf("%.2f", pCurrent->prodList.purPrice);
  427. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  428. printf("%s", space6);
  429.  
  430. printf("%.2f", pCurrent->prodList.unitSellPrice);
  431. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  432. printf("%s", space7);
  433.  
  434. printf("%.1f\n\n", pCurrent->prodList.discRate);
  435.  
  436. i++;
  437. pCurrent=pCurrent->pNext;
  438. }
  439.  
  440. printf("\n\npage %d of %d \n\n", page, stockNum);
  441. page++;
  442. scanf("%c", &nextPage);
  443. }
  444.  
  445. if(opt==2){
  446.  
  447. printf("Enter a category: ");
  448. scanf("%s", categoryInput);
  449.  
  450. printf("\n[VIEW STOCKS BY CATEGORY]\n");
  451.  
  452. printf("Category Supplier Product Product Qty Qty Purchase Selling Discount\n");
  453. printf(" Code Available Sold Price Price Rate\n");
  454.  
  455. pCurrent=pStocks;
  456. i=1;
  457.  
  458. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  459. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  460.  
  461. while(i<20 && pCurrent!=NULL){
  462.  
  463. if(strcmp(categoryInput, pCurrent->category)==0){
  464. printf("%s", pCurrent->category);
  465. space[(15-strlen(pCurrent->category))]='\0';
  466. printf("%s", space);
  467.  
  468. printf("%s", pCurrent->prodList.supplier);
  469. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  470. printf("%s", space2);
  471.  
  472. printf("%s ", pCurrent->prodList.prodCode);
  473.  
  474. printf("%s", pCurrent->prodList.product);
  475. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  476. printf("%s", space3);
  477.  
  478. printf("%d", pCurrent->prodList.qtyAvailable);
  479. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  480. printf("%s ", space4);
  481.  
  482. printf("%d", pCurrent->prodList.qtySold);
  483. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  484. printf("%s", space5);
  485.  
  486. printf("%.2f", pCurrent->prodList.purPrice);
  487. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  488. printf("%s", space6);
  489.  
  490. printf("%.2f", pCurrent->prodList.unitSellPrice);
  491. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  492. printf("%s", space7);
  493.  
  494. printf("%.1f\n\n", pCurrent->prodList.discRate);
  495.  
  496. i++;
  497. }
  498. pCurrent=pCurrent->pNext;
  499. }
  500.  
  501. printf("\n\npage %d of %d \n\n", page, stockNum);
  502. page++;
  503. scanf("%c", &nextPage);
  504. }
  505.  
  506. if(opt==3){
  507.  
  508. printf("Enter a category: ");
  509. scanf("%s", categoryInput);
  510.  
  511. printf("\n[VIEW STOCKS TO REORDER]\n");
  512.  
  513. printf("Category Supplier Product Product Qty Qty Purchase Selling Discount\n");
  514. printf(" Code Available Sold Price Price Rate\n");
  515.  
  516. pCurrent=pStocks;
  517. i=1;
  518.  
  519. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  520. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  521.  
  522. while(i<20 && pCurrent!=NULL){
  523.  
  524. if(pCurrent->prodList.qtyAvailable<=5){
  525. printf("%s", pCurrent->category);
  526. space[(15-strlen(pCurrent->category))]='\0';
  527. printf("%s", space);
  528.  
  529. printf("%s", pCurrent->prodList.supplier);
  530. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  531. printf("%s", space2);
  532.  
  533. printf("%s ", pCurrent->prodList.prodCode);
  534.  
  535. printf("%s", pCurrent->prodList.product);
  536. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  537. printf("%s", space3);
  538.  
  539. printf("%d", pCurrent->prodList.qtyAvailable);
  540. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  541. printf("%s ", space4);
  542.  
  543. printf("%d", pCurrent->prodList.qtySold);
  544. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  545. printf("%s", space5);
  546.  
  547. printf("%.2f", pCurrent->prodList.purPrice);
  548. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  549. printf("%s", space6);
  550.  
  551. printf("%.2f", pCurrent->prodList.unitSellPrice);
  552. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  553. printf("%s", space7);
  554.  
  555. printf("%.1f\n\n", pCurrent->prodList.discRate);
  556.  
  557. i++;
  558. }
  559. pCurrent=pCurrent->pNext;
  560. }
  561.  
  562. printf("\n\npage %d of %d \n\n", page, stockNum);
  563. page++;
  564. scanf("%c", &nextPage);
  565. }
  566.  
  567. if(opt==4){
  568. printf("\n[BROWSE ALL PRODUCTS]\n");
  569.  
  570. printf("Category Brand Product Product Qty Qty Purchase Unit Discount\n");
  571. printf(" Code Available Sold Price Rate\n");
  572.  
  573. pCurrent=pStocks;
  574. i=1;
  575.  
  576. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  577. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  578.  
  579.  
  580. while(i<20 && pCurrent!=NULL){
  581. if(pCurrent->prodList.qtyAvailable>0){
  582. printf("%s", pCurrent->category);
  583. space[(15-strlen(pCurrent->category))]='\0';
  584. printf("%s", space);
  585.  
  586. printf("%s", pCurrent->prodList.supplier);
  587. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  588. printf("%s", space2);
  589.  
  590. printf("%s ", pCurrent->prodList.prodCode);
  591.  
  592. printf("%s", pCurrent->prodList.product);
  593. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  594. printf("%s", space3);
  595.  
  596. printf("%d", pCurrent->prodList.qtyAvailable);
  597. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  598. printf("%s ", space4);
  599.  
  600. printf("%d", pCurrent->prodList.qtySold);
  601. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  602. printf("%s", space5);
  603.  
  604. printf("%.2f", pCurrent->prodList.purPrice);
  605. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  606. printf("%s", space6);
  607.  
  608. printf("%.2f", pCurrent->prodList.unitSellPrice);
  609. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  610. printf("%s", space7);
  611.  
  612. printf("%.1f\n\n", pCurrent->prodList.discRate);
  613.  
  614. i++;
  615. }
  616. pCurrent=pCurrent->pNext;
  617. }
  618.  
  619. printf("\n\n page %d of %d \n\n", page, stockNum);
  620. page++;
  621. scanf("%c", &nextPage);
  622.  
  623. }
  624.  
  625. if(opt==5){
  626. printf("\n[BROWSE PRODUCTS BY CATEGORIES]\n");
  627.  
  628. printf("Category Brand Product Product Qty Qty Purchase Unit Discount\n");
  629. printf(" Code Available Sold Price Rate\n");
  630.  
  631. pCurrent=pStocks;
  632. i=1;
  633.  
  634. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  635. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  636.  
  637. printf("Enter category: ");
  638. scanf("%s", categoryInput);
  639.  
  640. while(i<20 && pCurrent!=NULL){
  641. if(pCurrent->prodList.qtyAvailable>0 && strcmp(categoryInput, pCurrent->category)==0){
  642. printf("%s", pCurrent->category);
  643. space[(15-strlen(pCurrent->category))]='\0';
  644. printf("%s", space);
  645.  
  646. printf("%s", pCurrent->prodList.supplier);
  647. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  648. printf("%s", space2);
  649.  
  650. printf("%s ", pCurrent->prodList.prodCode);
  651.  
  652. printf("%s", pCurrent->prodList.product);
  653. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  654. printf("%s", space3);
  655.  
  656. printf("%d", pCurrent->prodList.qtyAvailable);
  657. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  658. printf("%s ", space4);
  659.  
  660. printf("%d", pCurrent->prodList.qtySold);
  661. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  662. printf("%s", space5);
  663.  
  664. printf("%.2f", pCurrent->prodList.purPrice);
  665. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  666. printf("%s", space6);
  667.  
  668. printf("%.2f", pCurrent->prodList.unitSellPrice);
  669. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  670. printf("%s", space7);
  671.  
  672. printf("%.1f\n\n", pCurrent->prodList.discRate);
  673.  
  674. i++;
  675. }
  676. pCurrent=pCurrent->pNext;
  677. }
  678.  
  679. printf("\n\n page %d of %d \n\n", page, stockNum);
  680. page++;
  681. scanf("%c", &nextPage);
  682.  
  683. }
  684.  
  685. if(opt==6){
  686. printf("\n[BROWSE PRODUCTS ON SALE]\n");
  687.  
  688. printf("Category Brand Product Product Qty Qty Purchase Unit Discount\n");
  689. printf(" Code Available Sold Price Rate\n");
  690.  
  691. pCurrent=pStocks;
  692. i=1;
  693.  
  694. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  695. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  696.  
  697.  
  698. while(i<20 && pCurrent!=NULL){
  699. if(pCurrent->prodList.qtyAvailable>0 && pCurrent->prodList.discRate>0){
  700. printf("%s", pCurrent->category);
  701. space[(15-strlen(pCurrent->category))]='\0';
  702. printf("%s", space);
  703.  
  704. printf("%s", pCurrent->prodList.supplier);
  705. space2[(15-strlen(pCurrent->prodList.supplier))]='\0';
  706. printf("%s", space2);
  707.  
  708. printf("%s ", pCurrent->prodList.prodCode);
  709.  
  710. printf("%s", pCurrent->prodList.product);
  711. space3[(15-strlen(pCurrent->prodList.product))]='\0';
  712. printf("%s", space3);
  713.  
  714. printf("%d", pCurrent->prodList.qtyAvailable);
  715. space4[(9-numDigits(pCurrent->prodList.qtyAvailable))]='\0';
  716. printf("%s ", space4);
  717.  
  718. printf("%d", pCurrent->prodList.qtySold);
  719. space5[(4-numDigits(pCurrent->prodList.qtySold))]='\0';
  720. printf("%s", space5);
  721.  
  722. printf("%.2f", pCurrent->prodList.purPrice);
  723. space6[(6-numDigits(pCurrent->prodList.purPrice))]='\0';
  724. printf("%s", space6);
  725.  
  726. printf("%.2f", pCurrent->prodList.unitSellPrice);
  727. space7[(5-numDigits(pCurrent->prodList.unitSellPrice))]='\0';
  728. printf("%s", space7);
  729.  
  730. printf("%.1f\n\n", pCurrent->prodList.discRate);
  731.  
  732. i++;
  733. }
  734. pCurrent=pCurrent->pNext;
  735. }
  736.  
  737. printf("\n\n page %d of %d \n\n", page, stockNum);
  738. page++;
  739. scanf("%c", &nextPage);
  740.  
  741. }
  742. }
  743. /*Function checkProdCode checks if the input product code was already used in a previous stock.
  744. @param: code - input product code
  745. *pStock - structure that holds all the stock information
  746. @return: check- integer (0 if product code isn't unique, 1 if it is)
  747. */
  748. int checkProdCode(string8 code, stockType *pStock){
  749. int check=1;
  750. stockType *pOrig = pStock;
  751.  
  752. while(pOrig!=NULL)
  753. {
  754. if(strcmp(code, pOrig->prodList.prodCode)==0)
  755. {
  756. check=0;
  757. pOrig=NULL;
  758. }
  759. else
  760. pOrig=pOrig->pNext;
  761. }
  762.  
  763. return check;
  764. }
  765.  
  766.  
  767.  
  768. /*Function modifyStockInfo Asks the user if they want to change the stock information
  769. @param: *pStock - structure that holds all the stocks
  770. */
  771. void modifyStockInfo(stockType *pStock){ //first displays all && then if product code is not found, reurn to managestocksmenu
  772. string8 code,pTemp;
  773. stockType *pCurrent=NULL;
  774.  
  775. int opt=0,
  776. i=0,
  777. j=0,
  778. k=0,
  779. randomNum=0,
  780. found=0;
  781. char cDump;
  782.  
  783. viewStocks(pStock, 1);
  784.  
  785. printf("\n\nEnter product code of item to be modified: ");
  786. scanf("%s%c", code, &cDump);
  787.  
  788. pCurrent=pStock;
  789.  
  790. while(pCurrent!=NULL && found==0){
  791. if(strcmp(pCurrent->prodList.prodCode, code)==0)
  792. found=1;
  793. if(found==0)
  794. pCurrent=pCurrent->pNext;
  795. }
  796.  
  797. if(found==1){
  798. do{
  799. printf("\n[MODIFY STOCK INFO]\n");
  800. printf("1 - Modify Category\n");
  801. printf("2 - Modify Supplier\n");
  802. printf("3 - Modify Product\n");
  803. printf("4 - Modify Purchase Price\n");
  804. printf("5 - Modify Selling Price\n");
  805. printf("6 - Modify Discount\n");
  806. printf("7 - Finish Modification of Product <Product Code>\n\n");
  807.  
  808. printf("Choose option: ");
  809. scanf("%d", &opt);
  810.  
  811. switch(opt){
  812. case 1: do{
  813. printf("New category: ");
  814. scanf("%s%c", pCurrent->category, &cDump);
  815. }while(strlen(pCurrent->category)<3 || strlen(pCurrent->category)>15);
  816. break;
  817. case 2: getchar();
  818. do{
  819. printf("New supplier: ");
  820. fgets(pCurrent->prodList.supplier, 16, stdin);
  821. pCurrent->prodList.supplier[strlen(pCurrent->prodList.supplier)-1]='\0';
  822. }while(strlen(pCurrent->prodList.supplier)>15);
  823. break;
  824. case 3: getchar();
  825. do{
  826. printf("New product: ");
  827. fgets(pCurrent->prodList.product, 16, stdin);
  828. pCurrent->prodList.product[strlen(pCurrent->prodList.product)-1]='\0';
  829. }while(strlen(pCurrent->prodList.product)>15);
  830. break;
  831. case 4: printf("New purchase price: ");
  832. scanf("%f", &pCurrent->prodList.purPrice);
  833. break;
  834. case 5: printf("New unit selling price: ");
  835. scanf("%f", &pCurrent->prodList.unitSellPrice);
  836. break;
  837. case 6: printf("New discount rate: ");
  838. scanf("%f", &pCurrent->prodList.discRate);
  839. break;
  840. case 7: strcpy(pTemp,pCurrent->prodList.prodCode);
  841. if(pCurrent->prodList.prodCode[0]!=pCurrent->category[0])
  842. pTemp[0]=pCurrent->category[0];
  843. if(pCurrent->prodList.prodCode[1]!=pCurrent->prodList.supplier[0])
  844. pTemp[1]=pCurrent->prodList.supplier[0];
  845. if(pCurrent->prodList.prodCode[2]!=pCurrent->prodList.product[0])
  846. pTemp[2]=pCurrent->prodList.product[0];
  847.  
  848. printf("CHECK PROD CODE: %d\n", checkProdCode(pTemp, pStock));
  849.  
  850. if(checkProdCode(pTemp, pStock)!=1){
  851.  
  852. i=1;
  853.  
  854. while(i<=5){
  855. randomNum = rand()%58;
  856. if(randomNum>47 && randomNum<58){
  857. pTemp[(i+2)]=randomNum;
  858. i++;
  859. }
  860. }
  861. pTemp[8]='\0';
  862. }
  863. strcpy(pCurrent->prodList.prodCode,pTemp);
  864. printf("Product code is now %s\n", pCurrent->prodList.prodCode);
  865. break;
  866. }
  867. }while(opt!=7);
  868. }
  869. else
  870. printf("Invalid product code");
  871. }
  872.  
  873. /*Function restock Asks the user which product’s number of items
  874. to they want to modify, then allows them to add to it
  875. @param: *pStock - structure that holds the information of all the stocks
  876. */
  877. void restock(stockType *pStock){
  878. string8 code;
  879. stockType *pCurrent=NULL;
  880. int add=0,
  881. found=0;
  882. char cDump;
  883.  
  884. viewStocks(pStock, 1);
  885.  
  886. printf("\n\nEnter product code of item to be modified: ");
  887. scanf("%s%c", code, &cDump);
  888.  
  889. pCurrent=pStock;
  890.  
  891. while(pCurrent!=NULL && found==0){
  892. if(strcmp(pCurrent->prodList.prodCode, code)==0)
  893. found=1;
  894. if(found==0)
  895. pCurrent=pCurrent->pNext;
  896. }
  897.  
  898. if(found==1){
  899. printf("Additional quantity for this available items: ");
  900. scanf("%d", &add);
  901. pCurrent->prodList.qtyAvailable=pCurrent->prodList.qtyAvailable+add;
  902. }
  903. else
  904. printf("\nLast input product Code not found: Not restocked.\n");
  905. }
  906.  
  907. /*Function stockCount counts the number of stocks
  908. @param: pStocks - structure that holds all the stock information
  909. @return: count - number of stocks
  910. */
  911. int stockCount(ptrStock pStocks){
  912. int count=0;
  913.  
  914. while(pStocks!=NULL){
  915. count++;
  916. pStocks=pStocks->pNext;
  917. }
  918.  
  919. return count;
  920. }
  921.  
  922. void saveStocksToTextFile(ptrStock pFirst){
  923. FILE *pText;
  924. string20 strName;
  925.  
  926. if(pFirst == NULL)
  927. printf("Nothing to save.\n");
  928. else{
  929. printf("Enter filename: ");
  930. scanf("%s", strName);
  931.  
  932. pText=fopen(strName, "wt");
  933.  
  934. if(pText!=NULL){
  935. while(pFirst!=NULL){
  936. fprintf(pText, "%s %s %s\n%d %.2f %s\n%.2f&%.1f %d\n\n",
  937. pFirst->category, pFirst->prodList.prodCode, pFirst->prodList.product, pFirst->prodList.qtyAvailable,
  938. pFirst->prodList.purPrice, pFirst->prodList.supplier, pFirst->prodList.unitSellPrice, pFirst->prodList.discRate,
  939. pFirst->prodList.qtySold);
  940. pFirst=pFirst->pNext;
  941. }
  942. fclose(pText);
  943. }
  944. else
  945. printf("Error creating file.\n");
  946. }
  947. }
  948.  
  949. /*Function manageStocks Displays the Manage Stocks Menu’s
  950. Options (add new stock, view all stocks, view stocks by category, view stocks to reorder, modify stock info,
  951. restock, save inventory, update inventory from file)
  952. and lets the user choose an option.
  953. @param: *pStocks - structure that holds the information of all stocks
  954. *categoryCtr - number of unique categories
  955. */
  956. void manageStocks(ptrStock pStocks, int *categoryCtr){
  957. int opt=0,
  958. choose=0;
  959. char cDump;
  960. ptrStock pNew=NULL,
  961. pRun=NULL,
  962. pTrail=NULL;
  963. string15 category;
  964.  
  965. do{
  966. printf("\n------MANAGE STOCKS MENU------\n");
  967. printf("1 - Add New Stock\n");
  968. printf("2 - View All Stocks\n");
  969. printf("3 - View Stocks by Category\n");
  970. printf("4 - View Stocks to Reorder\n");
  971. printf("5 - Modify Stock Info\n");
  972. printf("6 - Restock\n");
  973. printf("7 - Save Inventory\n");
  974. printf("8 - Update Inventory from File\n");
  975. printf("9 - Return to Admin Menu\n");
  976.  
  977. printf("\nCHECK: \n\n\n");
  978. displayAllStocks(pStocks);
  979.  
  980. printf("\nCHECK: New Category! Category number %d\n", *categoryCtr);
  981.  
  982. printf("Choose option: ");
  983. scanf("%d", &opt);
  984.  
  985. switch(opt){
  986. case 1: if(*categoryCtr<10){ //counts the number of categories (max. 10)
  987. pNew=malloc(sizeof(stockType));
  988. pNew->pNext=NULL;
  989.  
  990. if(pStocks==NULL){
  991.  
  992. addNewStock(pNew, NULL, categoryCtr);
  993. pStocks=pNew;
  994.  
  995. }
  996. else if(strcmp(pStocks->category, pNew->category)<0){ //connect at first node
  997.  
  998. addNewStock(pNew, pStocks, categoryCtr);
  999. pNew->pNext=pStocks;
  1000. pStocks=pNew;
  1001.  
  1002. }
  1003. else{
  1004.  
  1005. addNewStock(pNew, pStocks, categoryCtr);
  1006. pRun=pStocks;
  1007.  
  1008. while(pRun != NULL && strcmp(pRun->category, pNew->category)>0){ //modifying middle of the list
  1009.  
  1010. pTrail=pRun;
  1011. pRun=pRun->pNext;
  1012. }
  1013.  
  1014. pTrail->pNext=pNew;
  1015. pNew->pNext=pRun;
  1016.  
  1017. }
  1018. }
  1019. else
  1020. printf("\nMAXIMUM AMOUNT OF CATEGORIES REACHED. Cannot add any more.\n");
  1021.  
  1022. break;
  1023.  
  1024. case 2: viewStocks(pStocks, 1);
  1025. break;
  1026. case 3: viewStocks(pStocks, 2);
  1027. break;
  1028. case 4: viewStocks(pStocks, 3);
  1029. break;
  1030. case 5: modifyStockInfo(pStocks);
  1031. break;
  1032. case 6: restock(pStocks);
  1033. break;
  1034. case 7: saveStocksToTextFile(pStocks);
  1035. break;
  1036.  
  1037. }
  1038. }while(opt!=9);
  1039.  
  1040.  
  1041. }
  1042.  
  1043. void saveReceiptToTextFile(ptrUser pFirst, ptrStock pStock){
  1044. FILE *pText;
  1045. string20 strName;
  1046. int items=0,
  1047. i=0;
  1048.  
  1049.  
  1050. items=pFirst->nItems;
  1051.  
  1052. if(pFirst == NULL)
  1053. printf("Nothing to save.\n");
  1054. else{
  1055. printf("Enter filename: ");
  1056. scanf("%s", strName);
  1057.  
  1058. pText=fopen(strName, "wt");
  1059.  
  1060. if(pText!=NULL){
  1061. while(pFirst!=NULL){
  1062. fprintf(pText, "%s %d\n", pFirst->username, pFirst->nItems);
  1063. while(items>0){
  1064. fprintf(pText, "%d %s ", pFirst->cart[i].qty, pFirst->cart[i].code);
  1065. while(pStock!=NULL){
  1066. if(strcmp(pFirst->cart[i].code, pStock->prodList.prodCode)==0)
  1067. fprintf(pText, "%s\n", pStock->prodList.product);
  1068. else
  1069. pStock=pStock->pNext;
  1070. }
  1071. fprintf(pText, "@%.2f&%.1f\n", pStock->prodList.unitSellPrice, pStock->prodList.discRate);
  1072.  
  1073. i++;
  1074. items--;
  1075. }
  1076. pFirst=pFirst->pNext;
  1077. }
  1078. fclose(pText);
  1079. }
  1080. else
  1081. printf("Error creating file.\n");
  1082. }
  1083. }
  1084.  
  1085.  
  1086.  
  1087. void receipt(userType *pUser, stockType *pStock){
  1088. int items=0,
  1089. i=0,
  1090. itemNum=0;
  1091. float itemSubtotal=0.0,
  1092. totalDisc=0.0,
  1093. billAmt=0.0;
  1094.  
  1095. items=pUser->nItems;
  1096.  
  1097. printf("DELIVERY RECEIPT:\n");
  1098. printf("User ID: %s\n", pUser->username);
  1099. printf("Customer Name: %s, %s %s\n", pUser->info.name.last, pUser->info.name.first, pUser->info.name.middle);
  1100. printf("Delivery Address: %s\n", pUser->info.address);
  1101. printf("Product Product Quantity Unit Price Total Price Item\n");
  1102. printf("Code Subtotal\n");
  1103.  
  1104. while(items>0){
  1105.  
  1106. printf("%s", pUser->cart[i].code);
  1107. while(pStock!=NULL){
  1108. if(strcmp(pUser->cart[i].code, pStock->prodList.prodCode)==0)
  1109. printf("%s", pStock->prodList.product);
  1110. else
  1111. pStock=pStock->pNext;
  1112. }
  1113. printf("%d", pUser->cart[i].qty);
  1114.  
  1115. printf("P %.2f", pStock->prodList.unitSellPrice);
  1116. printf("P %.2f", (pStock->prodList.unitSellPrice)*(pUser->cart[i].qty));
  1117. printf("- %.1f", pStock->prodList.discRate);
  1118. itemSubtotal=pStock->prodList.unitSellPrice - (((pStock->prodList.discRate)/100)*pUser->cart[i].qty);
  1119. printf("P %.2f", itemSubtotal);
  1120.  
  1121. itemNum=itemNum+pUser->cart[i].qty;
  1122. totalDisc=totalDisc+pStock->prodList.discRate;
  1123. billAmt=billAmt+itemSubtotal;
  1124.  
  1125. i++;
  1126. items--;
  1127. }
  1128.  
  1129. printf("Number of items: %d\n", itemNum);
  1130. printf("Total Discount: Php %.1f\n", totalDisc);
  1131. printf("Bill Amount: Php %.2f\n", billAmt);
  1132. printf("Total Outstanding: Php %.2f\n", billAmt+pUser->outstanding);
  1133.  
  1134. saveReceiptToTextFile(pUser, pStock);
  1135. }
  1136.  
  1137. /*Function adminMenu displays the administrator menu and its options once an administrator has logged in
  1138. Allows the user to choose an option.
  1139. @param: *pUser - structure with information of current user
  1140. *shutdown - option to shutdown (initialized to 0 originally)
  1141. pStock - structure that holds all stock information
  1142. *categoryCtr - number of unique categories
  1143. */
  1144. void adminMenu(userType *pUser, int *shutdown, ptrStock pStock, int *categoryCtr){
  1145. int opt;
  1146.  
  1147. do{
  1148. printf("\n------ADMINISTRATOR MENU------\n");
  1149. printf("1 - Manage Accounts Menu\n");
  1150. printf("2 - Manage Stocks Menu\n");
  1151. printf("3 - Prepare Delivery Receipt\n");
  1152. printf("4 - Shutdown Kiosk\n");
  1153. printf("5 - Log Out\n\n");
  1154.  
  1155. printf("Choose option: ");
  1156. scanf("%d", &opt);
  1157.  
  1158. switch(opt){
  1159. case 1: manageAccounts(pUser);
  1160. break;
  1161. case 2: manageStocks(pStock, categoryCtr);
  1162. break;
  1163. case 3:
  1164. break;
  1165. case 4: *shutdown=1;
  1166. break;
  1167. case 5: break;
  1168. }
  1169. }while(opt!=5 && *shutdown==0);
  1170. }
  1171.  
  1172. /*Function modifyUserInfo Asks the user if they want to change the user info (either name, address, or password).
  1173. @param: *pUser - structure that holds all the users
  1174. */
  1175. void modifyUserInfo(userType *pUser){
  1176. int opt=0,
  1177. choose=0,
  1178. check=0,
  1179. i;
  1180. char cDump;
  1181. do{
  1182. printf("\n[MODIFY USER INFO]\n");
  1183. printf("1 - Change name\n");
  1184. printf("2 - Change address\n");
  1185. printf("3 - Change password\n");
  1186. printf("4 - Exit\n\n");
  1187.  
  1188. printf("Choose option: ");
  1189. scanf("%d", &opt);
  1190.  
  1191.  
  1192. switch(opt){
  1193. case 1: printf("\n[Editing Options]\n");
  1194. printf("1 - First name\n");
  1195. printf("2 - Middle name\n");
  1196. printf("3 - Last name\n\n");
  1197.  
  1198. do{
  1199. printf("Enter option to edit: ");
  1200. scanf("%d", &choose);
  1201. }while(choose!=1 && choose!=2 && choose!=3);
  1202.  
  1203. getchar();
  1204.  
  1205. switch(choose){
  1206. case 1: printf("New first name: ");
  1207. fgets(pUser->info.name.first, 21, stdin);
  1208. pUser->info.name.first[strlen(pUser->info.name.first)-1]='\0';
  1209. break;
  1210. case 2: printf("New middle name: ");
  1211. fgets(pUser->info.name.middle, 21, stdin);
  1212. pUser->info.name.middle[strlen(pUser->info.name.middle)-1]='\0';
  1213. break;
  1214. case 3: printf("New last name: ");
  1215. fgets(pUser->info.name.last, 21, stdin);
  1216. pUser->info.name.last[strlen(pUser->info.name.last)-1]='\0';
  1217. break;
  1218. }
  1219.  
  1220. printf("New name: %s %s %s\n", pUser->info.name.first, pUser->info.name.middle, pUser->info.name.last);
  1221.  
  1222. break;
  1223. case 2: getchar();
  1224. printf("Enter new address: ");
  1225. fgets(pUser->info.address, 51, stdin);
  1226.  
  1227. //Removes the '\n' because fgets takes the enter key and adds it to the string
  1228. pUser->info.address[strlen(pUser->info.address)-1]='\0';
  1229.  
  1230. printf("New address: %s\n", pUser->info.address);
  1231. break;
  1232. case 3: do{
  1233. printf("Enter new password (6-15 characters, at least one must not be a letter): ");
  1234. scanf("%s%c", pUser->password, &cDump);
  1235. for(i=0; i<strlen(pUser->password); i++){
  1236. if(!(pUser->password[i]>='a' && pUser->password[i]<='z'|| pUser->password[i]>='a' && pUser->password[i]<='z'))
  1237. check=1;
  1238. }
  1239. } while(check==0|| strlen(pUser->password)<6 || strlen(pUser->password)>15);
  1240. break;
  1241. case 4: break;
  1242. }
  1243. }while(opt!=4);
  1244.  
  1245. }
  1246.  
  1247. /*Function shopperMenu Shows the user the Shopper Menu’s options (modify user info, browse all products, browse products by category, browse products on sale,
  1248. add to cart, view cart, check out, edit cart items settle outstanding balance, log out) and allows the user to choose an option.
  1249. @param: *pUser - structure that holds the information of all users
  1250. *pStock - structure that holds the information of all stocks
  1251. */
  1252. void shopperMenu(userType *pUser, ptrStock pStock){
  1253. int opt=0,
  1254. quantity=0,
  1255. count=1,
  1256. check=0,
  1257. choice=1,
  1258. i=0,
  1259. choice2=0,
  1260. choice3=0,
  1261. newQty=0,
  1262. items=0,
  1263. itemNum=0,
  1264. checkout=0,
  1265. j=0,
  1266. securityCode=0;
  1267. float itemSubtotal=0.0,
  1268. totalDisc=0.0,
  1269. cartAmt=0.0,
  1270. settle=0.0;
  1271. char cDump;
  1272. string8 code,
  1273. prodCode;
  1274. string20 cardNum;
  1275. stockType *pOrig;
  1276.  
  1277. items=pUser->nItems;
  1278.  
  1279. do{
  1280. printf("\n------SHOPPER MENU------\n");
  1281. printf("1 - Modify User Info\n");
  1282. printf("2 - Browse All Products\n");
  1283. printf("3 - Browse Products by Category\n");
  1284. printf("4 - Browse Products on Sale\n");
  1285. if(checkout==0)
  1286. printf("5 - Add to Cart\n");
  1287. printf("6 - View Cart\n");
  1288. printf("7 - Settle Outstanding Balance\n");
  1289. printf("8 - Log Out\n\n");
  1290.  
  1291. printf("Choose option: ");
  1292. scanf("%d", &opt);
  1293.  
  1294. switch(opt){
  1295. case 1: modifyUserInfo(pUser);
  1296. break;
  1297. case 2: printf("CHECK!!!! %s\n", pStock->category);
  1298. viewStocks(pStock, 4);
  1299. break;
  1300. case 3: viewStocks(pStock, 5);
  1301. break;
  1302. case 4: viewStocks(pStock, 6);
  1303. break;
  1304. case 5: if(pUser->outstanding<pUser->creditlimit && checkout==0){
  1305. do{
  1306. if(check>0){
  1307. printf("Add another item to your cart?(1 for yes, 0 for no): ");
  1308. scanf("%d", &choice);
  1309. }
  1310.  
  1311. if(choice==1){
  1312. printf("Input the ff. details of product you wish to purchase:\n");
  1313. printf("Product code: ");
  1314. scanf("%s%c", code, &cDump);
  1315. printf("Quantity: ");
  1316. scanf("%d", &quantity);
  1317.  
  1318. if(checkProdCode(code, pStock)==1){ //if product code is valid
  1319. pOrig=pStock;
  1320. while(pOrig!=NULL) //if quantity is valid
  1321. {
  1322. if(strcmp(code, pOrig->prodList.prodCode)==0)
  1323. {
  1324. if(pOrig->prodList.qtyAvailable>=quantity){
  1325.  
  1326. strcpy(pUser->cart[count].code, code);
  1327. pUser->cart[count].qty=quantity;
  1328. count++;
  1329. check++;
  1330.  
  1331. }
  1332. pOrig=NULL;
  1333. }
  1334. else
  1335. pOrig=pOrig->pNext;
  1336. }
  1337. }
  1338. else
  1339. choice=0;
  1340. }
  1341. }while(count>=1 && choice!=0);
  1342. }
  1343. break;
  1344. case 6: if(pUser->cart[0].qty>0){
  1345. do{
  1346. printf("Product Product Quantity Unit Price Total Price Item\n");
  1347. printf("Code Subtotal\n");
  1348.  
  1349. while(items>0){
  1350. printf("%s", pUser->cart[i].code);
  1351. while(pStock!=NULL){
  1352. if(strcmp(pUser->cart[i].code, pStock->prodList.prodCode)==0)
  1353. printf("%s", pStock->prodList.product);
  1354. else
  1355. pStock=pStock->pNext;
  1356. }
  1357. printf("%d", pUser->cart[i].qty);
  1358.  
  1359. printf("P %.2f", pStock->prodList.unitSellPrice);
  1360. printf("P %.2f", (pStock->prodList.unitSellPrice)*(pUser->cart[i].qty));
  1361. printf("- %.1f", pStock->prodList.discRate);
  1362. itemSubtotal=pStock->prodList.unitSellPrice - (((pStock->prodList.discRate)/100)*pUser->cart[i].qty);
  1363. printf("P %.2f", itemSubtotal);
  1364.  
  1365. itemNum=itemNum+pUser->cart[i].qty;
  1366. totalDisc=totalDisc+pStock->prodList.discRate;
  1367. cartAmt=cartAmt+itemSubtotal;
  1368.  
  1369. i++;
  1370. items--;
  1371. }
  1372.  
  1373. printf("Number of items: %d\n", itemNum);
  1374. printf("Total Discount: Php %.1f\n", totalDisc);
  1375. printf("Cart Amount: Php %.2f\n", cartAmt);
  1376.  
  1377.  
  1378. ///////after the display above:
  1379. printf("\n[Options]\n");
  1380. printf("1 - Check out\n");
  1381. printf("2 - Edit Cart Items\n");
  1382. printf("3 - Back to Shopper Menu\n");
  1383.  
  1384. printf("Enter option: ");
  1385. scanf("%d", &choice2);
  1386.  
  1387. switch(choice2){
  1388. case 1: //check out
  1389. items=pUser->nItems;
  1390. i=0;
  1391. while(items>0){
  1392. while(pStock!=NULL){
  1393. if(strcmp(pUser->cart[i].code, pStock->prodList.prodCode)==0){
  1394. pStock->prodList.qtySold=pStock->prodList.qtySold+pUser->cart[i].qty;
  1395. pStock->prodList.qtyAvailable=pStock->prodList.qtyAvailable-pUser->cart[i].qty;
  1396. }
  1397. else
  1398. pStock=pStock->pNext;
  1399. }
  1400.  
  1401. i++;
  1402. items--;
  1403. }
  1404.  
  1405. checkout=1;
  1406.  
  1407. break;
  1408. case 2: do{
  1409. printf("\n[Edit Cart Item Options]\n");
  1410. printf("1 - Remove item\n");
  1411. printf("2 - Update quantity\n");
  1412. printf("3 - Back to View Cart Menu\n");
  1413.  
  1414. printf("Enter option: ");
  1415. scanf("%d", &choice3);
  1416.  
  1417. switch(choice3){
  1418. case 1: printf("Enter product code to remove from cart: ");
  1419. scanf("%s%c", prodCode, &cDump);
  1420. //delete product code input from the cart
  1421.  
  1422. items=pUser->nItems;
  1423. i=0;
  1424. while(items>0){
  1425. printf("%s", pUser->cart[i].code);
  1426. while(pStock!=NULL){
  1427. if(strcmp(pUser->cart[i].code, pStock->prodList.prodCode)==0){
  1428. j=i;
  1429. while(pUser->cart[j].qty>0){
  1430. strcpy(pUser->cart[j].code, pUser->cart[j+1].code);
  1431. pUser->cart[j].qty=pUser->cart[j+1].qty;
  1432.  
  1433. j++;
  1434. }
  1435. }
  1436. else
  1437. pStock=pStock->pNext;
  1438. }
  1439. items++;
  1440. i++;
  1441. }
  1442. break;
  1443. case 2: printf("Enter product code: ");
  1444. scanf("%s%c", prodCode, &cDump);
  1445. printf("Enter new quantity: ");
  1446. scanf("%d", &newQty);
  1447. //new quantity should be within limits of qty availalble
  1448. //if new qty = 0, prod is removed from cart
  1449. break;
  1450. case 3: break;
  1451. }
  1452. }while(choice3!=3);
  1453. break;
  1454. case 3: break; //back to shopper menu
  1455. }
  1456. }while(choice2!=3);
  1457. }
  1458. break;
  1459. case 7: if(pUser->outstanding>0.00){
  1460. printf("Outstanding balance: P %.2f\n\n", pUser->outstanding);
  1461. printf("[Credit Card Information]\n");
  1462. printf("Card Number: ");
  1463. scanf("%s", cardNum);
  1464. printf("Security Code: ");
  1465. scanf("%d", &securityCode);
  1466. do{
  1467. if(settle>pUser->outstanding)
  1468. printf("Amount to settle must be at most equal to the outstanding balance, or less.\n");
  1469. printf("Enter amount to settle:");
  1470. scanf("%f", &settle);
  1471. }while(settle>pUser->outstanding);
  1472. printf("Payment successful!\n");
  1473. }
  1474. break;
  1475. case 8: break;
  1476. }
  1477. }while(opt!=8);
  1478. }
  1479.  
  1480. /*Function checkUsername Checks if the input code was already used in a previous stock.
  1481. @param: category - input category
  1482. *pStock = structure that holds all the stock information
  1483. */
  1484. int checkUsername(string15 username, userType *pUser)
  1485. {
  1486. int check=1;
  1487. userType *pOrig = pUser;
  1488.  
  1489. while(pOrig!=NULL)
  1490. {
  1491. if(strcmp(username, pOrig->username)==0)
  1492. {
  1493. check=0;
  1494. pOrig=NULL;
  1495. }
  1496. else
  1497. pOrig=pOrig->pNext;
  1498. }
  1499.  
  1500. return check;
  1501. }
  1502.  
  1503. /*Function signUp Adds a new user to the current list of users by asking for user information.
  1504. @param: *pUser - structure that holds the information of the current user
  1505. *pOriginal - structure that holds the information of all the users
  1506. */
  1507. void signUp(userType *pUser, userType *pOriginal){
  1508. userType *pCurrent=NULL, *pTrail;
  1509. string15 username;
  1510. int unique=0;
  1511. char cDump;
  1512. int check, i;
  1513. string8 authorization;
  1514.  
  1515. printf("\n------SIGN UP------\n");
  1516.  
  1517. do{
  1518. printf("Enter username (3-15 characters): ");
  1519. scanf("%s%c", username, &cDump);
  1520.  
  1521. if(strlen(username)>=3 && strlen(username)<=15){
  1522. pCurrent=pOriginal;
  1523. unique=checkUsername(username, pOriginal);
  1524. if(unique==1)
  1525. {
  1526. strcpy(pUser->username, username);
  1527. }
  1528. if(unique!=1)
  1529. {
  1530. printf("Username already exists.\n");
  1531.  
  1532. }
  1533. }
  1534.  
  1535. }while(strlen(pUser->username)<3 || strlen(pUser->username)>15 || unique!=1);
  1536.  
  1537. unique=0;
  1538.  
  1539. do{
  1540. printf("Enter password (6-15 characters, at least one must not be a letter): ");
  1541. scanf("%s", pUser->password);
  1542. for(i=0; i<strlen(pUser->password); i++){
  1543. if(!(pUser->password[i]>='a' && pUser->password[i]<='z'|| pUser->password[i]>='a' && pUser->password[i]<='z'))
  1544. check=1;
  1545. }
  1546. } while(check==0|| strlen(pUser->password)<6 || strlen(pUser->password)>15);
  1547.  
  1548. getUserInfo(&pUser->info);
  1549.  
  1550. do{
  1551. printf("Account type ([S]hopper or [A]dministrator): ");
  1552. scanf("%c%c", &pUser->type, &cDump);
  1553. if(!(pUser->type=='s' || pUser->type=='S' || pUser->type=='a' || pUser->type=='A'))
  1554. printf("Invalid input.\n");
  1555. } while(!(pUser->type=='s' || pUser->type=='S' || pUser->type=='a' || pUser->type=='A'));
  1556.  
  1557. if(pUser->type == 's' || pUser->type == 'S'){
  1558. //shopper account is created
  1559. pUser->creditlimit=5000.00;
  1560. pUser->outstanding=0.00;
  1561. pUser->nItems=0;
  1562. printf("\nSHOPPER ACCOUNT CREATED.\n\n");
  1563. }
  1564. else if(pUser->type == 'a' || pUser->type == 'A'){
  1565. //administrator account is created
  1566. do{printf("Enter authorization code: ");
  1567. scanf("%s", authorization);
  1568.  
  1569. if(strcmp(authorization,"DLSU2017")==0)
  1570. printf("\nADMINISTRATOR ACCOUNT CREATED.\n\n");
  1571. else
  1572. printf("Invalid authorization code.\n");
  1573.  
  1574. }while(strcmp(authorization,"DLSU2017")!=0);
  1575.  
  1576. }
  1577. }
  1578.  
  1579. /*Function logIn Allows user to log in their account after signing up by asking for their username and password.
  1580. @param: *pUser - structure with information of current user
  1581. accountCount - number of accounts
  1582. *shutdown - shutdown option
  1583. *pStock - structure with list of stocks
  1584. *categoryCtr - number of unique categories
  1585. */
  1586. void logIn(userType *pUser, int accountCount, int *shutdown, ptrStock *pStock, int *categoryCtr){
  1587. userType *pCurrent;
  1588. char cDump;
  1589. string15 username,
  1590. password;
  1591. int found=0,
  1592. i=0,
  1593. correct=0,
  1594. tries=1,
  1595. exit=0;
  1596. printf("\n------LOG IN------\n");
  1597. //if(pStock!=NULL)
  1598. // viewStocks(pStock, 1);
  1599. do{
  1600. printf("Username: ");
  1601. scanf("%s%c", username, &cDump);
  1602. pCurrent=pUser;
  1603. while(pCurrent!=NULL && i<=accountCount && found==0){
  1604. if(strcmp(pCurrent->username, username)==0)
  1605. found=1;
  1606. else{
  1607. pCurrent=pCurrent->pNext;
  1608. i++;
  1609. }
  1610. }
  1611. if(found==0)
  1612. printf("Invalid username.\n");
  1613. }while(found==0);
  1614.  
  1615. do{
  1616. printf("Password: ");
  1617. scanf("%s%c", password, &cDump);
  1618. if(strcmp(pCurrent->password, password)!=0){
  1619. correct=0;
  1620. tries++;
  1621. }
  1622. if(tries>3 || pCurrent->isLocked==1){
  1623. pCurrent->isLocked=1;
  1624. printf("\nYou have exceeded the amount of tries to log in.\n");
  1625. printf("Your account is now locked.\n");
  1626. printf("To unlock your account, please contact an administrator.\n\n");
  1627. exit=1;
  1628. }
  1629. else{
  1630. if(strcmp(pCurrent->password, password)==0)
  1631. correct=1;
  1632.  
  1633. if(correct==1)
  1634. if(pCurrent->type=='S' || pCurrent->type=='s'){
  1635. printf("\nGreetings shopper: %s %s %s!\n",
  1636. pCurrent->info.name.first, pCurrent->info.name.middle, pCurrent->info.name.last);
  1637. ////SHOPPER MENU!
  1638. shopperMenu(pUser, pStock);
  1639. }
  1640. else{
  1641. printf("\nGreetings administrator: %s %s %s!\n",
  1642. pCurrent->info.name.first, pCurrent->info.name.middle, pCurrent->info.name.last);
  1643. /////ADMIN MENU!!!
  1644. adminMenu(pUser, shutdown, pStock, categoryCtr);
  1645. }
  1646. else
  1647. printf("Incorrect password. You have %d attempts left.\n", 4-tries);
  1648. }
  1649. }while((tries>=3 || correct==0) && exit==0);
  1650. }
  1651.  
  1652. /*Function main Shows the main menu’s options to the user and allows the user to choose which option (log in or sign up).
  1653. */
  1654.  
  1655. /*
  1656. void saveToTextFile(ptrUser pFirst){
  1657. FILE *pText;
  1658. string20 strName;
  1659.  
  1660. if(pFirst == NULL)
  1661. printf("Nothing to save.\n");
  1662. else{
  1663. printf("Enter filename: ");
  1664. scanf("%s", strName);
  1665.  
  1666. pText=fopen(strName, "wt");
  1667.  
  1668. if(pText!=NULL){
  1669. while(pFirst!=NULL){
  1670. fprintf(pText, "%s%s\n", pFirst->username, pFirst->password);
  1671. pFirst=pFirst->pNext;
  1672. }
  1673. fclose(pText);
  1674. }
  1675. else
  1676. printf("Error creating file.\n");
  1677. }
  1678. }
  1679.  
  1680. void saveStocksToTextFile(ptrStock pFirst){
  1681. FILE *pText;
  1682. string20 strName;
  1683.  
  1684. if(pFirst == NULL)
  1685. printf("Nothing to save.\n");
  1686. else{
  1687. printf("Enter filename: ");
  1688. scanf("%s", strName);
  1689.  
  1690. pText=fopen(strName, "wt");
  1691.  
  1692. if(pText!=NULL){
  1693. while(pFirst!=NULL){
  1694. fprintf(pText, "%s %s %s\n%d %.2f %s\n%.2f&%.1f %d\n\n",
  1695. pFirst->category, pFirst->prodList.prodCode, pFirst->prodList.product, pFirst->prodList.qtyAvailable,
  1696. pFirst->prodList.purPrice, pFirst->prodList.supplier, pFirst->prodList.unitSellPrice, pFirst->prodList.discRate,
  1697. pFirst->prodList.qtySold);
  1698. pFirst=pFirst->pNext;
  1699. }
  1700. fclose(pText);
  1701. }
  1702. else
  1703. printf("Error creating file.\n");
  1704. }
  1705. }
  1706.  
  1707. void loadFromText(string20 strFile, ptrUser *pFirst){
  1708. FILE *pFile;
  1709. string20 user,
  1710. pass;
  1711. ptrUser pNew;
  1712.  
  1713. if((pFile=fopen(strFile, "rt"))!=NULL){
  1714. while(fscanf(pFile, "%s%s", user, pass) == 2){
  1715. printf("%s%s\n", user, pass);
  1716. //creation of the linked list
  1717. pNew=malloc(sizeof(userType));
  1718. strcpy(pNew->username, user);
  1719. strcpy(pNew->password, pass);
  1720. //always insert at head of list
  1721. pNew->pNext=*pFirst;
  1722. *pFirst=pNew;
  1723. }
  1724. fclose(pFile);
  1725. }
  1726. else
  1727. printf("Error opening file.\n");
  1728. }
  1729. */
  1730.  
  1731. int main(){
  1732. int categoryCtr=0;
  1733. ptrUser pUsers=NULL,
  1734. pNew=NULL,
  1735. pLast=NULL,
  1736. pRun=NULL,
  1737. pTrail=NULL;
  1738. ptrStock pStocks=NULL;
  1739.  
  1740. int opt=0,
  1741. choose=0,
  1742. accountCount=0,
  1743. shutdown=0,
  1744. check=0;
  1745. char cDump;
  1746. string20 strFile;
  1747.  
  1748. do{
  1749. printf("\n------[MAIN MENU]------\n");
  1750. printf("1 - Log In\n");
  1751. printf("2 - Sign Up\n");
  1752. printf("3 - Shutdown\n\n");
  1753. printf("Choose option: ");
  1754. scanf("%d", &choose);
  1755.  
  1756. if(choose==2){
  1757. do{
  1758. pNew=malloc(sizeof(userType));
  1759. pNew->pNext=NULL;
  1760.  
  1761.  
  1762. if(pUsers==NULL) { //list is empty
  1763. signUp(pNew, NULL);
  1764. pUsers=pNew;
  1765.  
  1766. }
  1767. else if(strcmp(pUsers->username, pNew->username)>0){ //connect at first node
  1768. signUp(pNew, pUsers);
  1769. pNew->pNext=pUsers;
  1770. pUsers=pNew;
  1771. }
  1772. else{ //modifying middle of the list
  1773. signUp(pNew, pUsers);
  1774. pRun=pUsers;
  1775. while(pRun != NULL && strcmp(pRun->username, pNew->username)<0){
  1776. pTrail=pRun;
  1777. pRun=pRun->pNext;
  1778. }
  1779. pTrail->pNext=pNew;
  1780. pNew->pNext=pRun;
  1781. }
  1782.  
  1783. accountCount++;
  1784. printf("Another user? (1 for yes, 0 for no): ");
  1785. scanf("%d%c", &opt, &cDump);
  1786. }while(opt==1);
  1787. }
  1788.  
  1789. if(choose==1)
  1790. if(pUsers==NULL){
  1791. printf("No accounts made yet.\n");
  1792. check=1;
  1793. }
  1794. else
  1795. logIn(pUsers, accountCount, &shutdown, &pStocks, &categoryCtr);
  1796.  
  1797. if(choose==3)
  1798. opt=1;
  1799. }while((opt==0 && shutdown==0) && choose>2 || check==1);
  1800.  
  1801. freeAll(&pUsers);
  1802. freeAllStocks(&pStocks);
  1803.  
  1804. /*printf("Enter filename: ");
  1805. scanf("%s", strFile);
  1806.  
  1807. saveToTextFile(pUsers);
  1808. loadFromText(strFile, &pUsers);*/
  1809.  
  1810. printf("\nShutting down Kiosk...");
  1811. return 0;
  1812. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement