Advertisement
Guest User

FINAL

a guest
Mar 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.27 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.  
  7. Alexandra Rotor Reyes, DLSU ID# 11622105
  8. **********************************************************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14.  
  15. #define MAX_ITEMS 100
  16.  
  17. typedef char string8[9];
  18. typedef char string15[16];
  19. typedef char string20[21];
  20. typedef char string50[51];
  21.  
  22. /* structures needed for the program */
  23. typedef struct
  24. {
  25. string20 first,
  26. middle,
  27. last;
  28. } nameType;
  29.  
  30. typedef struct
  31. {
  32. nameType name;
  33. string50 address;
  34. } userInfoType;
  35.  
  36. typedef struct
  37. {
  38. string8 code;
  39. int qty;
  40. } prodBoughtType;
  41.  
  42. typedef prodBoughtType arrBought[MAX_ITEMS];
  43.  
  44. /* linked list about the user */
  45. typedef struct aboutUser
  46. {
  47. string15 username,
  48. password;
  49. userInfoType info;
  50. int type; /* administrator or shopper? */
  51. float creditLimit,
  52. outstanding;
  53. arrBought cart;
  54. int nItems,
  55. isLocked;
  56. struct aboutUser *next;
  57. } userNode;
  58.  
  59. /* linked list about the stocks */
  60. typedef struct aboutStock
  61. {
  62. string8 pCode;
  63. string15 supplier,
  64. product;
  65. int avbl,
  66. sold;
  67. float purchasePrice,
  68. sellingPrice,
  69. discountRate;
  70. struct aboutStock *next;
  71. } stockNode;
  72.  
  73. /* linked list about the category */
  74. typedef struct categ
  75. {
  76. string15 category;
  77. stockNode productInfo;
  78. struct categ *next;
  79. } categNode;
  80.  
  81. /*
  82. FUNCTION: viewLocked allows the administrator to see all locked accounts
  83. @param: userNode *user: structure with user information
  84. @return: void
  85. */
  86. void viewLocked (userNode *user)
  87. {
  88. int ctr=1;
  89. userNode *temp=NULL;
  90.  
  91. temp=user;
  92.  
  93. printf("\nView Locked Accounts:\n");
  94.  
  95. do
  96. {
  97. if (temp->isLocked==1 && temp->type==2)
  98. {
  99. printf("Account #%d: %s\n", ctr, temp->username);
  100. printf("Name: %s, %s %s\n", temp->info.name.last, temp->info.name.first, temp->info.name.middle);
  101. }
  102. else
  103. {
  104. printf("No locked accounts!\n");
  105. break;
  106. }
  107. temp=temp->next;
  108. ctr++;
  109. } while (temp!=NULL);
  110. }
  111.  
  112. /*
  113. FUNCTION: unlockSpecific allows the admin to unlock a specific account by selecting
  114. which account to unlock
  115. @param: userNode *user: structure with user information
  116. @return: void
  117. */
  118. void unlockSpecific (userNode *user)
  119. {
  120. string15 iUser;
  121. userNode *temp=NULL;
  122. int compare=2, choice=0, close=0;
  123.  
  124. temp=user;
  125.  
  126. printf("\nUnlock a specific account: \n");
  127.  
  128. do
  129. {
  130. printf("Enter username of the account you wish to unlock: \n");
  131. scanf("%s", iUser);
  132.  
  133. compare=strcmp(iUser, temp->username);
  134.  
  135. do
  136. {
  137. if (temp->isLocked==1 && compare==0)
  138. {
  139. temp->isLocked=0;
  140. temp=temp->next;
  141. }
  142. } while (temp!=NULL);
  143.  
  144. printf("Account has been unlocked.\n");
  145. printf("Would you like to unlock another user? \n");
  146. printf("1. Yes\n");
  147. printf("2. No\n");
  148.  
  149. do
  150. {
  151. switch (choice)
  152. {
  153. case 1: close=0;
  154. break;
  155. case 2: close=1;
  156. break;
  157. default: printf("Invalid input. Try again.\n");
  158. }
  159. } while (choice!=1 && choice!=2);
  160.  
  161. } while (close==1);
  162. }
  163.  
  164. /*
  165. FUNCTION: unlockAll allows the admin to unlock all the accounts
  166. @param: userNode *user: structure with user information
  167. @return: void
  168. */
  169. void unlockAll (userNode *user)
  170. {
  171. userNode *temp=NULL;
  172.  
  173. temp=user;
  174.  
  175. printf("\nUnlock all accounts: \n");
  176. printf("Loading...\n");
  177.  
  178. while (temp!=NULL)
  179. {
  180. if (temp->isLocked==1)
  181. temp->isLocked=0;
  182.  
  183. temp=temp->next;
  184. }
  185.  
  186. printf("All accounts have been unlocked!\n");
  187. }
  188.  
  189. /*
  190. FUNCTION: viewOutBal allows the admin to check which users have outstanding balance
  191. @param: userNode *user: structure with user information
  192. @return: void
  193. */
  194. void viewOutBal (userNode *user)
  195. {
  196. userNode *temp=NULL;
  197.  
  198. temp=user;
  199.  
  200. printf("\nViewAccounts With Outstanding Balance: \n");
  201.  
  202. do
  203. {
  204. if (temp->outstanding <= 0.00)
  205. printf("Sorry, your outstanding balance is empty!\n");
  206. else if (temp->outstanding > 0.00)
  207. {
  208. printf("Username: %s", temp->username);
  209. printf("Outstanding Balance is: %.2f\n", temp->outstanding);
  210. }
  211. temp=temp->next;
  212.  
  213. } while (temp!=NULL);
  214. }
  215.  
  216. /*
  217. FUNCTION: manageAccounts displays the admin's options for Manage Account Menu
  218. @param: userNode *user: structure with user information
  219. @return: void
  220. */
  221. void manageAccounts (userNode *user)
  222. {
  223. int aChoice;
  224.  
  225. do
  226. {
  227. printf("\n === MANAGE ACCOUNTS MENU === \n");
  228. printf("1. View Locked Accounts\n");
  229. printf("2. Unlock Specific Account\n");
  230. printf("3. Unlock All Locked Accounts\n");
  231. printf("4. View Accounts with Outstanding Balance\n");
  232. printf("5. Return to Administrator Menu\n");
  233. printf("Enter choice: ");
  234. scanf("%d", &aChoice);
  235.  
  236. switch (aChoice)
  237. {
  238. case 1: viewLocked (user);
  239. break;
  240. case 2: unlockSpecific (user);
  241. break;
  242. case 3: unlockAll (user);
  243. break;
  244. case 4: viewOutBal (user);
  245. break;
  246. case 5: break;
  247. }
  248. } while (aChoice!=5);
  249. }
  250.  
  251. /*
  252. FUNCTION: uniqueCateg checks if the category is unique or not
  253. @param: string15 addCateg: string that contains the name of the new category
  254. @param: categNode *temp: structure with the category information
  255. @return: int
  256. */
  257. int uniqueCateg (string15 addCateg, categNode *temp)
  258. {
  259. int unique=1;
  260. categNode *check=NULL;
  261.  
  262. check=temp;
  263.  
  264. while(check!=NULL)
  265. {
  266. if (strcmp(check->category, addCateg)==0)
  267. {
  268. unique=0;
  269. check=NULL;
  270. }
  271. else
  272. check=check->next;
  273. }
  274. return unique;
  275. }
  276.  
  277. /*
  278. FUNCTION: uniqueProd checks if the product is unique or not
  279. @param: string15 prod: string that contains the name of the new product
  280. @param: categNode *temp: structure with the category information
  281. @return: int
  282. */
  283. int uniqueProd (string15 prod, categNode *temp)
  284. {
  285. int unique=1;
  286. categNode *check=NULL;
  287.  
  288. check=temp;
  289.  
  290. while (check!=NULL)
  291. {
  292. if (strcmp(check->productInfo.product, prod)==0)
  293. {
  294. unique=0;
  295. check=NULL;
  296. }
  297. else
  298. check=check->next;
  299. }
  300. return unique;
  301. }
  302. /*
  303. FUNCTION: addNewStock allows the admin to add a new stock to the current list of stocks
  304. and goes back to the Manage Stocks Menu after
  305. @param: stockNode *stockInfo: structure with stock information
  306. @param: int *stockctr: to check the number of categories, which but be max of 10
  307. @param: categNode *stemp: structure with the category information
  308. @return: categNode *
  309. */
  310. void addNewStock(stockNode *stockInfo, int *stockctr, categNode **stemp)
  311. {
  312. string15 addCateg, supplier, prod, newcat, newsup, newprod;
  313. int cChoice=0, accept=0, compare=2, avbl=0, i=0, rNum=0;
  314. float purchasePrice=0, sellingPrice=0, discountRate=0;
  315. categNode *stocktemp=NULL;
  316. stocktemp = malloc(sizeof(categNode));
  317. srand(time(NULL));
  318. printf("\nAdding New Stocks: \n");
  319.  
  320. do
  321. {
  322. printf("Would you like to add a new stock?\n");
  323. printf("1. Yes\n");
  324. printf("2. No\n");
  325. printf("Enter choice: ");
  326. fflush(stdin);
  327. scanf("%d", &cChoice);
  328.  
  329. if (cChoice!=1 && cChoice!=2)
  330. printf("Invalid input. Try again\n");
  331. if ((*stockctr)==0 && cChoice==2)
  332. printf("\nNo stocks yet!\n\n");
  333. } while ((cChoice!=1 && (*stockctr)<11)&&(cChoice!=2 && (*stockctr)>0));
  334.  
  335. while (cChoice==1 && accept!=1 && (*stockctr)<11)
  336. {
  337. do
  338. {
  339. printf("Enter Category: ");
  340. fflush(stdin);
  341. fgets(addCateg, 16, stdin);
  342. addCateg[strlen(addCateg)-1]='\0';
  343. if(strlen(addCateg)>=3 && strlen(addCateg)<=15)
  344. {
  345. compare=uniqueCateg(addCateg,*stemp);
  346. if (compare==1)
  347. {
  348. accept=1;
  349. strcpy (stocktemp->category, addCateg);
  350. (*stockctr)++;
  351. printf("You have created a new category! Category #%d\n", *stockctr);
  352. }else{
  353. accept=1;
  354. strcpy (stocktemp->category, addCateg);
  355. }
  356. }else
  357. printf("Enter another category that contains 3-15 characters\n");
  358. if (*stockctr==10)
  359. printf("You have reached the maximum amount of categories!\n");
  360. } while ((strlen(addCateg)<3 && strlen(addCateg)>15) && accept!=1 && (*stockctr)<10);
  361. }
  362.  
  363. accept=0;
  364. compare=2;
  365.  
  366. if (cChoice==1 && accept!=1)
  367. {
  368. do
  369. {
  370. printf("Enter Supplier: ");
  371. fflush(stdin);
  372. fgets(supplier, 16, stdin);
  373.  
  374. if (strlen(supplier)<16)
  375. accept=1;
  376. else
  377. printf("Invalid input. Try again!\n");
  378. } while (accept!=1);
  379.  
  380. if (accept==1)
  381. {
  382. strcpy(stocktemp->productInfo.supplier, supplier);
  383. stocktemp->productInfo.supplier[strlen(stocktemp->productInfo.supplier)-1]='\0';
  384. accept=0;
  385. }
  386.  
  387. do
  388. {
  389. printf("Enter Product: ");
  390. fflush(stdin);
  391. fgets(prod, 16, stdin);
  392. prod[strlen(prod)-1]='\0';
  393. compare=uniqueProd(prod, *stemp);
  394.  
  395. if (strlen(prod)<16 && compare==1)
  396. accept=1;
  397. else
  398. printf("Invalid input. Try again!\n");
  399. } while (accept!=1 && compare!=1);
  400.  
  401. if (accept==1)
  402. {
  403. strcpy(stocktemp->productInfo.product, prod);
  404. accept=0;
  405. }
  406.  
  407. do
  408. {
  409. printf("Enter Quantity Available: ");
  410. fflush(stdin);
  411. scanf("%d", &avbl);
  412. if (avbl>=0)
  413. accept=1;
  414. else
  415. printf("Invalid input. Try again!\n");
  416. } while (accept!=1);
  417.  
  418. if (accept==1)
  419. {
  420. stocktemp->productInfo.avbl=avbl;
  421. accept=0;
  422. }
  423.  
  424. do
  425. {
  426. printf("Enter Purchase Price: ");
  427. fflush(stdin);
  428. scanf("%f", &purchasePrice);
  429. if (purchasePrice>0)
  430. accept=1;
  431. else
  432. printf("Invalid input. Try again!\n");
  433. } while (accept!=1);
  434.  
  435. if (accept==1)
  436. {
  437. stocktemp->productInfo.purchasePrice=purchasePrice;
  438. accept=0;
  439. }
  440.  
  441. do
  442. {
  443. printf("Enter Unit Selling Price: ");
  444. fflush(stdin);
  445. scanf("%f", &sellingPrice);
  446. if (sellingPrice>0)
  447. accept=1;
  448. else
  449. printf("Invalid input. Try again!\n");
  450. } while (accept!=1);
  451.  
  452. if (accept==1)
  453. {
  454. stocktemp->productInfo.sellingPrice=sellingPrice;
  455. accept=0;
  456. }
  457.  
  458. do
  459. {
  460. printf("Enter Discount Rate: ");
  461. fflush(stdin);
  462. scanf("%f", &discountRate);
  463. if (discountRate>=0)
  464. accept=1;
  465. else
  466. printf("Invalid input. Try again!\n");
  467. } while (accept!=1);
  468.  
  469. if (accept==1)
  470. {
  471. stocktemp->productInfo.discountRate=discountRate;
  472. accept=0;
  473. }
  474. }
  475.  
  476. strcpy(newcat, addCateg);
  477. for(i=0; i<strlen(newcat); i++)
  478. {
  479. if (newcat[i]>='a' && newcat[i]<='z')
  480. newcat[i]-=32;
  481. }
  482.  
  483. strcpy(newsup, supplier);
  484. for(i=0; i<strlen(newsup); i++)
  485. {
  486. if (newsup[i]>='a' && newsup[i]<='z')
  487. newsup[i]-=32;
  488. }
  489.  
  490. strcpy(newprod, prod);
  491. for(i=0; i<strlen(newprod); i++)
  492. {
  493. if (newprod[i]>='a' && newprod[i]<='z')
  494. newprod[i]-=32;
  495. }
  496.  
  497. stocktemp->productInfo.sold=0;
  498. stocktemp->productInfo.pCode[0]=newcat[0];
  499. stocktemp->productInfo.pCode[1]=newsup[0];
  500. stocktemp->productInfo.pCode[2]=newprod[0];
  501.  
  502. i=0;
  503.  
  504. while (i<5)
  505. {
  506. rNum=rand()%58;
  507. if(rNum>47 && rNum<57)
  508. {
  509. stocktemp->productInfo.pCode[(i+3)]=rNum;
  510. i++;
  511. }
  512. stocktemp->productInfo.pCode[8]='\0';
  513. }
  514. if(cChoice!=2){
  515. printf("Product code: %s\n", stocktemp->productInfo.pCode);
  516. stocktemp->next = *stemp;
  517. *stemp = stocktemp;
  518. }
  519.  
  520. }
  521.  
  522. /*
  523. FUNCTION: viewAllStocks: allows the admin to view all the created stocks in a table
  524. @param: stockNode *stockInfo: structure with stock information
  525. @param: int *stockctr: to check the number of categories, which must be max of 10
  526. @param: categNode *stemp: structure with the category information
  527. @return: void
  528. */
  529. void viewAllStocks (stockNode *stockInfo, int *stockctr, categNode *stemp)
  530. {
  531. int i;
  532. int ctr=0;
  533.  
  534. printf("\n");
  535. printf("| CATEGORY | SUPPLIER | PROD |PRODUCT |QTY |QTY |BUY |SELL |DISC|\n");
  536. printf("| | | CODE | |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  537.  
  538. while (stemp!=NULL)
  539. {
  540. if(ctr!=20)
  541. {
  542. printf("\n");
  543. printf("%s", stemp->category);
  544. if(strlen(stemp->category)<15)
  545. for(i=strlen(stemp->category);i<15;i++)
  546. printf(" ");
  547. printf("|%s", stemp->productInfo.supplier);
  548. if(strlen(stemp->productInfo.supplier)<15)
  549. for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  550. printf(" ");
  551. printf("|%s", stemp->productInfo.pCode);
  552. printf("|%s", stemp->productInfo.product);
  553. if(strlen(stemp->productInfo.product)<10)
  554. for(i=strlen(stemp->productInfo.product);i<10;i++)
  555. printf(" ");
  556. printf("|%d", stemp->productInfo.avbl);
  557. if(stemp->productInfo.avbl<10)
  558. for(i=0;i<3;i++)
  559. printf(" ");
  560. else if(stemp->productInfo.avbl<100)
  561. for(i=0;i<2;i++)
  562. printf(" ");
  563. else if(stemp->productInfo.avbl<1000)
  564. for(i=0;i<1;i++)
  565. printf(" ");
  566. printf("|%d", stemp->productInfo.sold);
  567. if(stemp->productInfo.sold<10)
  568. for(i=0;i<3;i++)
  569. printf(" ");
  570. else if(stemp->productInfo.sold<100)
  571. for(i=0;i<2;i++)
  572. printf(" ");
  573. else if(stemp->productInfo.sold<1000)
  574. for(i=0;i<1;i++)
  575. printf(" ");
  576. printf("|%.2f", stemp->productInfo.purchasePrice);
  577. printf("|%.2f", stemp->productInfo.sellingPrice);
  578. printf("|%.1f", stemp->productInfo.discountRate);
  579. printf("\n");
  580. ctr++;
  581. }
  582. else
  583. {
  584. printf("\nPress Enter to continue...\n");
  585. fflush(stdin);
  586. while(getchar() != '\n');
  587. ctr = 0;
  588. }
  589. stemp = stemp->next;
  590. }
  591. printf("\nPress Enter to continue...\n");
  592. fflush(stdin);
  593. while(getchar() != '\n');
  594. }
  595.  
  596. /*
  597. FUNCTION: viewStocksCategory: allows the admin to search for stocks based on the category input
  598. @param: stockNode *stockInfo: structure with stock information
  599. @param: int *stockctr: to check the number of categories, which must be max of 10
  600. @param: categNode *stemp: structure with the category information
  601. @return: void
  602. */
  603. void viewStockCategory (stockNode *stockInfo, int *stockctr, categNode *stemp)
  604. {
  605. int i,found=0;
  606. categNode *temp;
  607. string15 categ;
  608. int ctr=0;
  609.  
  610. printf("Enter category: ");
  611. fflush(stdin);
  612. fgets(categ, 16, stdin);
  613.  
  614. for(temp=stemp; temp!=NULL&&!found; temp=temp->next)
  615. {
  616. if(strcmp(temp->category,categ)==0)
  617. found = 1;
  618. }
  619.  
  620. if(found)
  621. {
  622. printf("\n");
  623. printf("| CATEGORY | SUPPLIER | PROD |PRODUCT |QTY |QTY |BUY |SELL |DISC|\n");
  624. printf("| | | CODE | |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  625.  
  626. while(stemp!=NULL)
  627. {
  628. if(ctr!=20)
  629. {
  630. if(strcmp(stemp->category,categ)==0)
  631. {
  632. printf("\n");
  633. printf("%s", stemp->category);
  634. if(strlen(stemp->category)<15)
  635. for(i=strlen(stemp->category);i<15;i++)
  636. printf(" ");
  637. printf("|%s", stemp->productInfo.supplier);
  638. if(strlen(stemp->productInfo.supplier)<15)
  639. for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  640. printf(" ");
  641. printf("|%s", stemp->productInfo.pCode);
  642. printf("|%s", stemp->productInfo.product);
  643. if(strlen(stemp->productInfo.product)<10)
  644. for(i=strlen(stemp->productInfo.product);i<10;i++)
  645. printf(" ");
  646. printf("|%d", stemp->productInfo.avbl);
  647. if(stemp->productInfo.avbl<10)
  648. for(i=0;i<3;i++)
  649. printf(" ");
  650. else if(stemp->productInfo.avbl<100)
  651. for(i=0;i<2;i++)
  652. printf(" ");
  653. else if(stemp->productInfo.avbl<1000)
  654. for(i=0;i<1;i++)
  655. printf(" ");
  656. printf("|%d", stemp->productInfo.sold);
  657. if(stemp->productInfo.sold<10)
  658. for(i=0;i<3;i++)
  659. printf(" ");
  660. else if(stemp->productInfo.sold<100)
  661. for(i=0;i<2;i++)
  662. printf(" ");
  663. else if(stemp->productInfo.sold<1000)
  664. for(i=0;i<1;i++)
  665. printf(" ");
  666. printf("|%.2f", stemp->productInfo.purchasePrice);
  667. printf("|%.2f", stemp->productInfo.sellingPrice);
  668. printf("|%.1f", stemp->productInfo.discountRate);
  669. printf("\n");
  670. ctr++;
  671. }
  672. }
  673. else
  674. {
  675. printf("\nPress Enter to continue...\n");
  676. fflush(stdin);
  677. while(getchar() != '\n');
  678. ctr = 0;
  679. }
  680. stemp = stemp->next;
  681. }
  682. printf("\nPress Enter to continue...\n");
  683. fflush(stdin);
  684. while(getchar() != '\n');
  685. }
  686. else
  687. {
  688. printf("\nCategory not found.\n");
  689. printf("\nPress Enter to continue...\n");
  690. fflush(stdin);
  691. while(getchar() != '\n');
  692. }
  693. }
  694.  
  695. /*
  696. FUNCTION: modStockInfo: allows the admin to modify the stock information
  697. @param: stockNode *stockInfo: structure with stock information
  698. @param: int *stockctr: to check the number of categories, which must be max of 10
  699. @param: categNode *stemp: structure with the category information
  700. @return: void
  701. */
  702. void modStockInfo (stockNode *stockInfo, int *stockctr, categNode *stemp)
  703. {
  704. int mChoice=0, rNum, found=0, i=0;
  705. string8 pCode;
  706. string15 categ, supp, prod, newcat, newsup, newprod;
  707. float pPrice=0, sPrice=0, discount=0;
  708.  
  709. viewAllStocks (stockInfo, stockctr, stemp);
  710.  
  711. printf("Enter Product Code: ");
  712. scanf("%s",pCode);
  713.  
  714. while (stemp!=NULL && found!=1)
  715. {
  716. if (strcmp(stemp->productInfo.pCode, pCode)==0)
  717. found=1;
  718. else
  719. stemp=stemp->next;
  720. }
  721.  
  722. if (found!=1)
  723. printf("Product Code not found!\n");
  724.  
  725. if (found==1)
  726. {
  727. do
  728. {
  729.  
  730. printf("\nWhat would you like to modify?\n");
  731. printf("1. Category\n");
  732. printf("2. Supplier\n");
  733. printf("3. Product\n");
  734. printf("4. Purchase Price\n");
  735. printf("5. Selling Price\n");
  736. printf("6. Discount\n");
  737. printf("7. Finish Modification of Product\n");
  738. printf("Enter choice: ");
  739. scanf("%d", &mChoice);
  740.  
  741. switch (mChoice)
  742. {
  743. case 1: do
  744. {
  745. printf("Enter New Category: ");
  746. fflush(stdin);
  747. fgets(categ, 16, stdin);
  748. if (strlen(categ)>=3 && strlen(categ)<=15)
  749. {
  750. strcpy(stemp->category, categ);
  751. stemp->category[strlen(stemp->category)-1]='\0';
  752. }
  753. else
  754. printf("\nInvalid input. Try again!\n");
  755. } while (strlen(stemp->category)<3 && strlen(stemp->category)>15);
  756. break;
  757. case 2: do
  758. {
  759. printf("Enter New Supplier: ");
  760. fflush(stdin);
  761. fgets(supp, 16, stdin);
  762. if (strlen(supp)>=3 && strlen(supp)<=15)
  763. {
  764. strcpy(stemp->productInfo.supplier, supp);
  765. stemp->productInfo.supplier[strlen(stemp->productInfo.supplier)-1]='\0';
  766. }
  767. else
  768. printf("\nInvalid input. Try again!\n");
  769. } while (strlen(stemp->productInfo.supplier)<3 && strlen(stemp->productInfo.supplier)>15);
  770. break;
  771. case 3: do
  772. {
  773. printf("Enter New Product: ");
  774. fflush(stdin);
  775. fgets(prod, 16, stdin);
  776. if (strlen(prod)>=3 && strlen(prod)<=15)
  777. {
  778. strcpy(stemp->productInfo.product, prod);
  779. stemp->productInfo.product[strlen(stemp->productInfo.product)-1]='\0';
  780. }
  781. else
  782. printf("\nInvalid input. Try again!\n");
  783. } while (strlen(stemp->productInfo.product)<3 && strlen(stemp->productInfo.product)>15);
  784. break;
  785. case 4: printf("Enter New Purchase Price: ");
  786. scanf("%f", &pPrice);
  787. stemp->productInfo.purchasePrice=pPrice;
  788. break;
  789. case 5: printf("Enter Selling Price: ");
  790. scanf("%f", &sPrice);
  791. stemp->productInfo.sellingPrice=sPrice;
  792. break;
  793. case 6: printf("Enter New Discount Rate: ");
  794. scanf("%f", &discount);
  795. stemp->productInfo.discountRate=discount;
  796. break;
  797. case 7: strcpy(pCode, stemp->productInfo.pCode);
  798.  
  799. strcpy(newcat, categ);
  800. for(i=0; i<strlen(newcat); i++)
  801. {
  802. if (newcat[i]>='a' && newcat[i]<='z')
  803. newcat[i]-=32;
  804. }
  805.  
  806. strcpy(newsup, supp);
  807. for(i=0; i<strlen(newsup); i++)
  808. {
  809. if (newsup[i]>='a' && newsup[i]<='z')
  810. newsup[i]-=32;
  811. }
  812.  
  813. strcpy(newprod, prod);
  814. for(i=0; i<strlen(newprod); i++)
  815. {
  816. if (newprod[i]>='a' && newprod[i]<='z')
  817. newprod[i]-=32;
  818. }
  819.  
  820. if (stemp->productInfo.pCode[0]!=stemp->category[0])
  821. pCode[0]=newcat[0];
  822. if (stemp->productInfo.pCode[1]!=stemp->productInfo.supplier[0])
  823. pCode[1]=newsup[0];
  824. if (stemp->productInfo.pCode[2]!=stemp->productInfo.product[0])
  825. pCode[2]=newprod[0];
  826.  
  827. i=0;
  828.  
  829. while (i<5)
  830. {
  831. rNum=rand()%58;
  832. if(rNum>47 && rNum<57)
  833. {
  834. pCode[(i+3)]=rNum;
  835. i++;
  836. }
  837. pCode[8]='\0';
  838. }
  839. strcpy(stemp->productInfo.pCode, pCode);
  840. printf("New Product Code: %s", stemp->productInfo.pCode);
  841. break;
  842. }
  843. } while (mChoice!=7);
  844. }
  845. }
  846.  
  847. /*
  848. FUNCTION: restock asks the user which product's number of available items
  849. they want to modify
  850. @param: stockNode *stockInfo: structure with stock information
  851. @param: int *stockctr: to check the number of categories, which must be max of 10
  852. @param: categNode *stemp: structure with the category information
  853. @return: void
  854. */
  855. void restock (stockNode *stockInfo, int *stockctr, categNode *stemp)
  856. {
  857. string8 pCode;
  858. int found=0, amt=0;
  859.  
  860. viewAllStocks (stockInfo, stockctr, stemp);
  861.  
  862. printf("\nRestocking:\n");
  863. printf("Enter Product Code: ");
  864. fflush(stdin);
  865. fgets(pCode, 9, stdin);
  866.  
  867. while (stemp!=NULL && found==0)
  868. {
  869. if (strcmp(stemp->productInfo.pCode, pCode)==0)
  870. found=1;
  871. else
  872. stemp=stemp->next;
  873. }
  874.  
  875. if (found==1)
  876. {
  877. printf("Enter modified amount: ");
  878. scanf("%d", &amt);
  879. stemp->productInfo.avbl=stemp->productInfo.avbl+amt;
  880. }
  881.  
  882. if (found!=1)
  883. printf("Last input product code not found: not restocked!\n");
  884. }
  885.  
  886. /*
  887. FUNCTION: manageStocks asks the admin what they want to do with all the stock information
  888. @param: stockNode *stockInfo: structure with stock information
  889. @param: int *stockctr: to check the number of categories, which but be max of 10
  890. @param: categNode *stemp: structure with the category information
  891. @return: void
  892. */
  893. void manageStocks (stockNode *stockInfo, int *stockctr,categNode **stemp)
  894. {
  895. int aChoice=0;
  896. do
  897. {
  898. printf("\n === MANAGE STOCKS MENU === \n");
  899. printf("1. Add New Stock\n");
  900. printf("2. View All Stocks\n");
  901. printf("3. View Stocks by Category\n");
  902. printf("4. View Stocks to Reorder\n");
  903. printf("5. Modify Stock Info\n");
  904. printf("6. Restock\n");
  905. printf("7. Save Inventory\n");
  906. printf("8. Update Inventory from File\n");
  907. printf("9. Return to Administrator Menu\n");
  908. printf("Enter choice: ");
  909. fflush(stdin);
  910. scanf("%d", &aChoice);
  911.  
  912. switch (aChoice)
  913. {
  914. case 1: addNewStock (stockInfo, stockctr, stemp);
  915. break;
  916. case 2: viewAllStocks (stockInfo, stockctr, *stemp);
  917. break;
  918. case 3: viewStockCategory (stockInfo, stockctr, *stemp);
  919. break;
  920. case 4: break;
  921. case 5: modStockInfo (stockInfo, stockctr, stemp);
  922. break;
  923. case 6: restock (stockInfo, stockctr, stemp);
  924. break;
  925. case 7: break;
  926. case 8: break;
  927. case 9: break;
  928. }
  929. } while (aChoice>0 && aChoice<9 && aChoice!= 9);
  930. }
  931.  
  932. /*
  933. FUNCTION: adminMenu displays only when an administrator is able to log in successfully
  934. It then asks the admin what (s)he wants to do
  935. @param: userNode *user: structure with the user information
  936. @param: int *close: option to exit
  937. @param: stockNode *stockInfo: structure with stock information
  938. @param: int *stockctr: to check the number of categories, which but be max of 10
  939. @param: categNode *stemp: structure with the category information
  940. @return: void
  941. */
  942. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr, categNode **stemp)
  943. {
  944. int adChoice=0;
  945.  
  946. do
  947. {
  948. printf("\n === ADMINISTRATOR MENU === \n");
  949. printf("1. Manage Accounts Menu\n");
  950. printf("2. Manage Stocks Menu\n");
  951. printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  952. printf("4. Shutdown Kiosk \n");
  953. printf("5. Log Out \n");
  954. printf("Enter choice: ");
  955. scanf("%d", &adChoice);
  956.  
  957. switch (adChoice)
  958. {
  959. case 1: manageAccounts(user);
  960. break;
  961. case 2: manageStocks(stockInfo, stockctr,stemp);
  962. break;
  963. case 3: break;
  964. case 4: *close=1;
  965. break;
  966. case 5: break;
  967. default: printf("Invalid input. Try again.\n");
  968. }
  969. } while (adChoice!=4 && adChoice!=5);
  970. }
  971.  
  972. /*
  973. FUNCTION: modUserInfo allows the shopper to change his/her account details
  974. @param: userNode *user: structure about user information
  975. @return: void
  976. */
  977. void modUserInfo (userNode *user)
  978. {
  979. int uChoice=0, accept=0, i=0, nameChoice=0;
  980. string15 newpass;
  981.  
  982. printf("\nModifying User Info:\n");
  983.  
  984. do
  985. {
  986. printf("\nDo you want to...\n");
  987. printf("1. Change Name\n");
  988. printf("2. Change Address\n");
  989. printf("3. Change Password\n");
  990. printf("4. Return to Shopper Menu\n");
  991. printf("Enter choice: ");
  992. scanf("%d", &uChoice);
  993.  
  994. switch (uChoice)
  995. {
  996. case 1: do
  997. {
  998. printf("\nWhat would you like to change? \n");
  999. printf("1. First Name\n");
  1000. printf("2. Middle Name\n");
  1001. printf("3. Last Name\n");
  1002. printf("4. All Names\n");
  1003. printf("Enter choice: ");
  1004. scanf("%d", &nameChoice);
  1005. } while (nameChoice<1 && nameChoice>4);
  1006.  
  1007. switch (nameChoice)
  1008. {
  1009. case 1: printf("Enter New Name: \n");
  1010. printf("First name: ");
  1011. fflush(stdin);
  1012. fgets(user->info.name.first, 21, stdin);
  1013.  
  1014. break;
  1015.  
  1016. case 2: printf("Enter New Name: \n");
  1017. printf("Middle name: ");
  1018. fflush(stdin);
  1019. fgets(user->info.name.middle, 21, stdin);
  1020. break;
  1021.  
  1022. case 3: printf("Enter New Name: \n");
  1023. printf("Last name: ");
  1024. fflush(stdin);
  1025. fgets(user->info.name.last, 21, stdin);
  1026. break;
  1027.  
  1028. case 4: printf("Enter New Name: \n");
  1029. printf("First name: ");
  1030. fflush(stdin);
  1031. fgets(user->info.name.first, 21, stdin);
  1032. printf("Middle name: ");
  1033. fflush(stdin);
  1034. fgets(user->info.name.middle, 21, stdin);
  1035. printf("Last name: ");
  1036. fflush(stdin);
  1037. fgets(user->info.name.last, 21, stdin);
  1038. break;
  1039. }
  1040. break;
  1041.  
  1042. case 2: printf("Enter New Address: \n");
  1043. printf("Address: ");
  1044. fflush(stdin);
  1045. fgets (user->info.address, 51, stdin);
  1046. break;
  1047.  
  1048. case 3: do
  1049. {
  1050. printf("Enter New Password: ");
  1051. scanf("%s", newpass);
  1052.  
  1053. for (i=0; i<strlen(newpass); i++)
  1054. {
  1055. if (!((newpass[i] >= 'A' && newpass[i] <= 'Z') || (newpass[i] >= 'a' && newpass[i] <= 'z')))
  1056. {
  1057. accept=1;
  1058. strcpy (user->password, newpass);
  1059. }
  1060. }
  1061. } while (strlen(newpass) < 6 || strlen(newpass) > 15 || accept!=1);
  1062. break;
  1063.  
  1064. case 4: break;
  1065. default: printf("Invalid input. Try again.\n");
  1066. }
  1067. } while (uChoice!=4);
  1068. }
  1069.  
  1070. /*
  1071. FUNCTION: browseAllProd: allows the shopper to see all the available products
  1072. @param categNode *stemp: structure with all the category information
  1073. @return: void
  1074. */
  1075. void browseAllProd (categNode *stemp)
  1076. {
  1077. int i,found=0;
  1078. categNode *temp;
  1079. int ctr=0;
  1080.  
  1081. for(temp=stemp; temp!=NULL&&!found; temp=temp->next)
  1082. {
  1083. if(temp->productInfo.avbl>0)
  1084. found = 1;
  1085. }
  1086.  
  1087. if(found)
  1088. {
  1089. printf("\n");
  1090. printf("| CATEGORY | SUPPLIER | PROD |PRODUCT |QTY |QTY |BUY |SELL |DISC|\n");
  1091. printf("| | | CODE | |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  1092.  
  1093. while(stemp!=NULL)
  1094. {
  1095. if(ctr!=20)
  1096. {
  1097. if(stemp->productInfo.avbl>0)
  1098. {
  1099. printf("\n");
  1100. printf("%s", stemp->category);
  1101. if(strlen(stemp->category)<15)
  1102. for(i=strlen(stemp->category);i<15;i++)
  1103. printf(" ");
  1104. printf("|%s", stemp->productInfo.supplier);
  1105. if(strlen(stemp->productInfo.supplier)<15)
  1106. for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  1107. printf(" ");
  1108. printf("|%s", stemp->productInfo.pCode);
  1109. printf("|%s", stemp->productInfo.product);
  1110. if(strlen(stemp->productInfo.product)<10)
  1111. for(i=strlen(stemp->productInfo.product);i<10;i++)
  1112. printf(" ");
  1113. printf("|%d", stemp->productInfo.avbl);
  1114. if(stemp->productInfo.avbl<10)
  1115. for(i=0;i<3;i++)
  1116. printf(" ");
  1117. else if(stemp->productInfo.avbl<100)
  1118. for(i=0;i<2;i++)
  1119. printf(" ");
  1120. else if(stemp->productInfo.avbl<1000)
  1121. for(i=0;i<1;i++)
  1122. printf(" ");
  1123. printf("|%d", stemp->productInfo.sold);
  1124. if(stemp->productInfo.sold<10)
  1125. for(i=0;i<3;i++)
  1126. printf(" ");
  1127. else if(stemp->productInfo.sold<100)
  1128. for(i=0;i<2;i++)
  1129. printf(" ");
  1130. else if(stemp->productInfo.sold<1000)
  1131. for(i=0;i<1;i++)
  1132. printf(" ");
  1133. printf("|%.2f", stemp->productInfo.purchasePrice);
  1134. printf("|%.2f", stemp->productInfo.sellingPrice);
  1135. printf("|%.1f", stemp->productInfo.discountRate);
  1136. printf("\n");
  1137. ctr++;
  1138. }
  1139. }
  1140. else
  1141. {
  1142. printf("\nPress Enter to continue...\n");
  1143. fflush(stdin);
  1144. while(getchar() != '\n');
  1145. ctr = 0;
  1146. }
  1147. stemp = stemp->next;
  1148. }
  1149. printf("\nPress Enter to continue...\n");
  1150. fflush(stdin);
  1151. while(getchar() != '\n');
  1152. }
  1153. else
  1154. {
  1155. printf("\nNo available products.\n");
  1156. printf("\nPress Enter to continue...\n");
  1157. fflush(stdin);
  1158. while(getchar() != '\n');
  1159. }
  1160. }
  1161.  
  1162. /*
  1163. FUNCTION: browseAllCateg: allows the shopper to search for a specific category and
  1164. display the following products
  1165. @param categNode *stemp: structure with all the category information
  1166. @return: void
  1167. */
  1168. void browseAllCateg (categNode *stemp)
  1169. {
  1170. int i,found=0;
  1171. categNode *temp;
  1172. int ctr=0;
  1173. string15 categ;
  1174. printf("Enter category: ");
  1175. fflush(stdin);
  1176. scanf("%s",categ);
  1177.  
  1178. for(temp=stemp; temp!=NULL&&!found; temp=temp->next)
  1179. {
  1180. if(strcmp(temp->category,categ)==0)
  1181. found = 1;
  1182. }
  1183.  
  1184. if(found)
  1185. {
  1186. printf("\n");
  1187. printf("| CATEGORY | SUPPLIER | PROD |PRODUCT |QTY |QTY |BUY |SELL |DISC|\n");
  1188. printf("| | | CODE | |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  1189.  
  1190. while(stemp!=NULL)
  1191. {
  1192. if(ctr!=20)
  1193. {
  1194. if(strcmp(stemp->category,categ)==0)
  1195. if(temp->productInfo.avbl>0)
  1196. {
  1197. printf("\n");
  1198. printf("%s", stemp->category);
  1199. if(strlen(stemp->category)<15)
  1200. for(i=strlen(stemp->category); i<15; i++)
  1201. printf(" ");
  1202. printf("|%s", stemp->productInfo.supplier);
  1203. if(strlen(stemp->productInfo.supplier)<15)
  1204. for(i=strlen(stemp->productInfo.supplier); i<15; i++)
  1205. printf(" ");
  1206. printf("|%s", stemp->productInfo.pCode);
  1207. printf("|%s", stemp->productInfo.product);
  1208. if(strlen(stemp->productInfo.product)<10)
  1209. for(i=strlen(stemp->productInfo.product); i<10; i++)
  1210. printf(" ");
  1211. printf("|%d", stemp->productInfo.avbl);
  1212. if(stemp->productInfo.avbl<10)
  1213. for(i=0; i<3; i++)
  1214. printf(" ");
  1215. else if(stemp->productInfo.avbl<100)
  1216. for(i=0; i<2; i++)
  1217. printf(" ");
  1218. else if(stemp->productInfo.avbl<1000)
  1219. for(i=0; i<1; i++)
  1220. printf(" ");
  1221. printf("|%d", stemp->productInfo.sold);
  1222. if(stemp->productInfo.sold<10)
  1223. for(i=0; i<3; i++)
  1224. printf(" ");
  1225. else if(stemp->productInfo.sold<100)
  1226. for(i=0; i<2; i++)
  1227. printf(" ");
  1228. else if(stemp->productInfo.sold<1000)
  1229. for(i=0; i<1; i++)
  1230. printf(" ");
  1231. printf("|%.2f", stemp->productInfo.purchasePrice);
  1232. printf("|%.2f", stemp->productInfo.sellingPrice);
  1233. printf("|%.1f", stemp->productInfo.discountRate);
  1234. printf("\n");
  1235. ctr++;
  1236. }
  1237. }
  1238. else
  1239. {
  1240. printf("\nPress Enter to continue...\n");
  1241. fflush(stdin);
  1242. while(getchar() != '\n');
  1243. ctr=0;
  1244. }
  1245. stemp=stemp->next;
  1246. }
  1247. printf("\nPress Enter to continue...\n");
  1248. fflush(stdin);
  1249. while(getchar() != '\n');
  1250. }
  1251. else
  1252. {
  1253. printf("\nNo available products.\n");
  1254. printf("\nPress Enter to continue...\n");
  1255. fflush(stdin);
  1256. while(getchar() != '\n');
  1257. }
  1258. }
  1259.  
  1260. /*
  1261. FUNCTION: browseAllSale: allows the shopper to see all the available products with discount
  1262. @param categNode *stemp: structure with all the category information
  1263. @return: void
  1264. */
  1265. void browseAllSale (categNode *stemp)
  1266. {
  1267. int i,found=0;
  1268. categNode *temp;
  1269. int ctr=0;
  1270.  
  1271. for(temp=stemp; temp!=NULL&&!found; temp=temp->next)
  1272. {
  1273. if(temp->productInfo.discountRate>0)
  1274. found = 1;
  1275. }
  1276.  
  1277. if(found)
  1278. {
  1279. printf("\n");
  1280. printf("| CATEGORY | SUPPLIER | PROD |PRODUCT |QTY |QTY |BUY |SELL |DISC|\n");
  1281. printf("| | | CODE | |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  1282.  
  1283. while(stemp!=NULL)
  1284. {
  1285. if(ctr!=20)
  1286. {
  1287. if(stemp->productInfo.discountRate>0)
  1288. {
  1289. printf("\n");
  1290. printf("%s", stemp->category);
  1291. if(strlen(stemp->category)<15)
  1292. for(i=strlen(stemp->category);i<15;i++)
  1293. printf(" ");
  1294. printf("|%s", stemp->productInfo.supplier);
  1295. if(strlen(stemp->productInfo.supplier)<15)
  1296. for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  1297. printf(" ");
  1298. printf("|%s", stemp->productInfo.pCode);
  1299. printf("|%s", stemp->productInfo.product);
  1300. if(strlen(stemp->productInfo.product)<10)
  1301. for(i=strlen(stemp->productInfo.product);i<10;i++)
  1302. printf(" ");
  1303. printf("|%d", stemp->productInfo.avbl);
  1304. if(stemp->productInfo.avbl<10)
  1305. for(i=0; i<3; i++)
  1306. printf(" ");
  1307. else if(stemp->productInfo.avbl<100)
  1308. for(i=0; i<2; i++)
  1309. printf(" ");
  1310. else if(stemp->productInfo.avbl<1000)
  1311. for(i=0; i<1; i++)
  1312. printf(" ");
  1313. printf("|%d", stemp->productInfo.sold);
  1314. if(stemp->productInfo.sold<10)
  1315. for(i=0; i<3; i++)
  1316. printf(" ");
  1317. else if(stemp->productInfo.sold<100)
  1318. for(i=0; i<2; i++)
  1319. printf(" ");
  1320. else if(stemp->productInfo.sold<1000)
  1321. for(i=0; i<1; i++)
  1322. printf(" ");
  1323. printf("|%.2f", stemp->productInfo.purchasePrice);
  1324. printf("|%.2f", stemp->productInfo.sellingPrice);
  1325. printf("|%.1f", stemp->productInfo.discountRate);
  1326. printf("\n");
  1327. ctr++;
  1328. }
  1329. }
  1330. else
  1331. {
  1332. printf("\nPress Enter to continue...\n");
  1333. fflush(stdin);
  1334. while(getchar() != '\n');
  1335. ctr = 0;
  1336. }
  1337. stemp = stemp->next;
  1338. }
  1339. printf("\nPress Enter to continue...\n");
  1340. fflush(stdin);
  1341. while(getchar() != '\n');
  1342. }
  1343. else
  1344. {
  1345. printf("\nNo available products.\n");
  1346. printf("\nPress Enter to continue...\n");
  1347. fflush(stdin);
  1348. while(getchar() != '\n');
  1349. }
  1350. }
  1351.  
  1352. /*
  1353. FUNCTION: settleOut: settles the user's outstanding balance
  1354. @param: userNode *user: structure that contains the user information
  1355. @return: void
  1356. */
  1357. void settleOut (userNode *user)
  1358. {
  1359. userNode *temp=NULL;
  1360.  
  1361. temp=user;
  1362.  
  1363. if (temp!=NULL)
  1364. {
  1365. printf("Your Outstanding Balance: %.2f", user->outstanding);
  1366. printf("\nEnter Credit Card Information: \n");
  1367. }
  1368. }
  1369.  
  1370. /*
  1371. FUNCTION: shopperMenu displays when a shopper logs in successfully
  1372. It then asks the shopper what he/she wants to do
  1373. @param: userNode *user: structure with the user information
  1374. @param: stockNode *stockInfo: structure with stock information
  1375. @param: int *stockctr: to check the number of categories, which but be max of 10
  1376. @return: void
  1377. */
  1378. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr,categNode **stemp)
  1379. {
  1380. int sChoice=0;
  1381.  
  1382. do
  1383. {
  1384. printf("\n === SHOPPER MENU === \n");
  1385. printf("1. Modify User Info\n");
  1386. printf("2. Browse All Products\n");
  1387. printf("3. Browse Products by Category\n");
  1388. printf("4. Browse Products on Sale\n");
  1389. printf("5. Add to Cart\n");
  1390. printf("6. View Cart\n");
  1391. printf("7. Settle Outstanding Balance\n");
  1392. printf("8. Log Out\n");
  1393. printf("Enter choice: ");
  1394. scanf("%d", &sChoice);
  1395.  
  1396. switch (sChoice)
  1397. {
  1398. case 1: modUserInfo (user);
  1399. break;
  1400. case 2: browseAllProd (*stemp);
  1401. break;
  1402. case 3: browseAllCateg (*stemp);
  1403. break;
  1404. case 4: browseAllSale (*stemp);
  1405. break;
  1406. case 5: break;
  1407. case 6: break;
  1408. case 7: if (user->outstanding>0)
  1409. settleOut(user);
  1410. else
  1411. break;
  1412. break;
  1413. case 8: break;
  1414. }
  1415. } while (sChoice!=8);
  1416. }
  1417.  
  1418. /*
  1419. FUNCTION: login allows the user to log in to a specific account
  1420. @param: userNode *user: structure with the user information
  1421. categNode **stemp: structure with the stock information
  1422. @return: void
  1423. */
  1424. void login (userNode *user,categNode **stemp)
  1425. {
  1426. int compare=2, found=0, close=0, stockctr=0, attempt=0;
  1427. char cDump;
  1428. string15 uname, pword;
  1429. userNode *temp=NULL;
  1430. stockNode *stockInfo=NULL;
  1431. temp=user;
  1432.  
  1433. printf(" === LOGIN === \n");
  1434. printf("Enter username: ");
  1435. scanf("%s%c", uname, &cDump);
  1436.  
  1437. while (found==0 && temp!=NULL)
  1438. {
  1439. compare=strcmp(uname, temp->username);
  1440.  
  1441. if (compare==0)
  1442. found=1;
  1443. else
  1444. temp=temp->next;
  1445. }
  1446.  
  1447. if (found==1 && temp->isLocked==0)
  1448. {
  1449. do
  1450. {
  1451. printf("Enter password: ");
  1452. scanf("%s%c", pword, &cDump);
  1453.  
  1454. compare=strcmp(pword, temp->password);
  1455.  
  1456. if (compare==0)
  1457. {
  1458. switch (temp->type)
  1459. {
  1460. case 1: printf("\nWelcome back, Admin!\n");
  1461. close=1;
  1462. adminMenu (user, &close, stockInfo, &stockctr, stemp);
  1463. break;
  1464.  
  1465. case 2: printf("\nWelcome back, Shopper!\n");
  1466. close=1;
  1467. shopperMenu(user, stockInfo, &stockctr, stemp);
  1468. break;
  1469. }
  1470. }
  1471. else
  1472. {
  1473. attempt++;
  1474. printf("Incorrect password. Try again.\n");
  1475. printf("%d out of 3 tries\n", attempt);
  1476.  
  1477. if (attempt==3)
  1478. {
  1479. user->isLocked=1;
  1480. printf("\nYou have tried logging in too many times.\n");
  1481. printf("Please contact the administrator.\n");
  1482. }
  1483. }
  1484. } while (!close && attempt<=3 && temp->isLocked==0);
  1485. }
  1486. else
  1487. printf("\nSorry, username does not exist.\n");
  1488. }
  1489.  
  1490. /*
  1491. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  1492. as well as their address
  1493. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  1494. @return: void
  1495. */
  1496. void getUserInfo (userInfoType *pInfo)
  1497. {
  1498. getchar();
  1499. printf("Enter name: \n");
  1500. printf("First name: ");
  1501. fgets(pInfo->name.first, 21, stdin);
  1502. printf("Middle name: ");
  1503. fgets(pInfo->name.middle, 21, stdin);
  1504. printf("Last name: ");
  1505. fgets(pInfo->name.last, 21, stdin);
  1506. printf("Enter address: ");
  1507. fgets (pInfo->address, 51, stdin);
  1508.  
  1509. (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  1510. (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  1511. (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  1512. (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  1513.  
  1514. printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  1515. }
  1516.  
  1517. /*
  1518. FUNCTION: checkUnique is to verify if the username is unique or not
  1519. @param: string15 uname: chosen username of new user
  1520. @param: userNode *user: structure with the user information
  1521. @return: int: 0 if not unique, 1 if unique
  1522. */
  1523. int checkUnique (string15 uname, userNode *user)
  1524. {
  1525. int unique=1;
  1526. userNode *temp=NULL;
  1527.  
  1528. temp=user;
  1529.  
  1530. while(temp!=NULL)
  1531. {
  1532. if (strcmp(temp->username, uname)==0)
  1533. {
  1534. unique=0;
  1535. temp=NULL;
  1536. }
  1537. else
  1538. temp=temp->next;
  1539. }
  1540.  
  1541. return unique;
  1542. }
  1543.  
  1544. /*
  1545. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  1546. list of users
  1547. @param: userNode *user: structure which contains infomation of all users
  1548. @param: userNode *pNew: structure which contains information of the current user
  1549. @return: void
  1550. */
  1551. void signUp (userNode *user, userNode *pNew)
  1552. {
  1553. int chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  1554. char cDump;
  1555. /* for authorization code */
  1556. string8 ac;
  1557. string15 uname, pword;
  1558.  
  1559. if (user==NULL)
  1560. {
  1561. /* first account to sign up */
  1562. do
  1563. {
  1564. printf("=== SIGN UP ===\n");
  1565. printf("Enter username: ");
  1566. scanf("%s%c", uname, &cDump);
  1567. } while ((strlen(uname) < 3 || strlen(uname) > 15));
  1568.  
  1569. strcpy (pNew->username, uname);
  1570. }
  1571.  
  1572. else
  1573. {
  1574. /* second account and onwards to sign up */
  1575. do
  1576. {
  1577. printf("=== SIGN UP ===\n");
  1578. printf("Enter username: ");
  1579. scanf("%s%c", uname, &cDump);
  1580.  
  1581. if (strlen(uname) >=3 && strlen(uname)<=15)
  1582. {
  1583. compare=checkUnique(uname, user);
  1584.  
  1585. if (compare==1)
  1586. {
  1587. accept=1;
  1588. strcpy (pNew->username, uname);
  1589. }
  1590.  
  1591. else if (compare!=1)
  1592. {
  1593. accept=0;
  1594. printf("Username unavailable. Try again.\n");
  1595. }
  1596. }
  1597. } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  1598. }
  1599.  
  1600. printf("Username is: %s\n", pNew->username);
  1601.  
  1602. accept=0;
  1603.  
  1604. do
  1605. {
  1606. printf("Enter password: ");
  1607. scanf("%s", pword);
  1608.  
  1609. if (strlen(pword) >= 6 || strlen(pword) <= 15)
  1610. {
  1611. for (i=0; i<strlen(pword); i++)
  1612. {
  1613. if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1614. {
  1615. accept=1;
  1616. chk=1;
  1617. strcpy (pNew->password, pword);
  1618. }
  1619. }
  1620.  
  1621. if (accept!=1)
  1622. printf("Invalid password. Input at least 1 non-letter.\n");
  1623. }
  1624. else
  1625. accept=0;
  1626.  
  1627. } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1628.  
  1629. printf("Password is: %s\n", pNew->password);
  1630.  
  1631. getUserInfo(&pNew->info); /* (&pUser->info) */
  1632.  
  1633. /* to ask the user to ask which type of account (s)he prefers to make */
  1634. do
  1635. {
  1636. printf("\nChoose your type of account: \n");
  1637. printf("1. Administrator\n");
  1638. printf("2. Shopper\n");
  1639. printf("Enter choice: ");
  1640. scanf("%d", &choice);
  1641.  
  1642. while (choice!=1 && choice!=2)
  1643. {
  1644. printf("Invalid choice. Try again.\n");
  1645. printf("Choose your type of account: \n");
  1646. printf("1. Administrator\n");
  1647. printf("2. Shopper\n");
  1648. printf("Enter choice: ");
  1649. scanf("%d", &choice);
  1650. }
  1651. pNew->type=choice;
  1652.  
  1653. switch (pNew->type)
  1654. {
  1655. /* if user chooses to be an administrator */
  1656. case 1: do
  1657. {
  1658. printf("Input authorization code: ");
  1659. scanf("%s", ac);
  1660.  
  1661. /* to check if authorization code is valid */
  1662. if (strcmp(ac, "DLSU2017")==0)
  1663. printf("\nHello New Admin!\n");
  1664.  
  1665. else
  1666. {
  1667. printf("\nSorry, invalid authorization code.\n");
  1668. printf("1. Try again\n");
  1669. printf("2. Change account type\n");
  1670. printf("Enter choice: ");
  1671. scanf("%d", &change);
  1672.  
  1673. while (change!=1 && change!=2)
  1674. printf("\nInvalid input!\n");
  1675.  
  1676. switch (change)
  1677. {
  1678. case 1: close=1;
  1679. break;
  1680. case 2: close=0;
  1681. break;
  1682. }
  1683. }
  1684. } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1685. break;
  1686.  
  1687. /* if user chooses to be a shopper */
  1688. case 2: printf("\nHello Shopper!\n\n");
  1689. pNew->creditLimit=5000.00;
  1690. pNew->outstanding=0.00;
  1691. pNew->nItems=0;
  1692. pNew->isLocked=0;
  1693.  
  1694. /* to tell the user more information about his/her account */
  1695. printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1696. printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1697. printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1698. break;
  1699.  
  1700. default: printf("Invalid input!\n");
  1701. }
  1702. } while (close==0 && change==2);
  1703.  
  1704. /* recap for the user */
  1705. printf("\n === REMEMBER ===\n");
  1706. printf("Username is: %s\n", pNew->username);
  1707. printf("Password is: %s\n\n", pNew->password);
  1708. }
  1709.  
  1710. /*
  1711. FUNCTION: displayAllRecur displays all the usernames available
  1712. @param: userNode *user: structure which contains infomation of all users
  1713. @return: void
  1714. */
  1715. void displayAllRecur (userNode *user)
  1716. {
  1717. if (user!=NULL)
  1718. {
  1719. printf("%s \n", user->username);
  1720. user=user->next;
  1721. }
  1722. }
  1723.  
  1724. /*
  1725. FUNCTION: freeAll frees all the data from the user structures
  1726. @param: userNode **user: structure containing the data of the all users
  1727. @return: void
  1728. */
  1729. void freeAll (userNode **user)
  1730. {
  1731. userNode *pDel;
  1732.  
  1733. pDel=NULL;
  1734.  
  1735. while (user!=NULL)
  1736. {
  1737. pDel=*user;
  1738. *user=(*user)->next;
  1739. free(pDel);
  1740. }
  1741. }
  1742.  
  1743. /*
  1744. FUNCTION: freeAllStocks frees all the data from the stock structures
  1745. @param: stockNode **pStocks: structure containing the data of the stocks
  1746. @return: void
  1747. */
  1748. void freeAllStocks (stockNode **pStocks)
  1749. {
  1750. stockNode *pDel;
  1751.  
  1752. pDel=NULL;
  1753.  
  1754. while(pStocks!=NULL)
  1755. {
  1756. pDel=*pStocks;
  1757. *pStocks=(*pStocks)->next;
  1758. free(pDel);
  1759. }
  1760. }
  1761.  
  1762. int main()
  1763. {
  1764. int choice=0, close=0, nChoice=0;
  1765. userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1766. categNode *stemp=NULL;
  1767. stockNode *pStocks;
  1768.  
  1769. user=NULL;
  1770. pNew=NULL;
  1771. pLast=NULL;
  1772. pRun=NULL;
  1773. pTrail=NULL;
  1774. pStocks=NULL;
  1775.  
  1776. printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1777.  
  1778. do
  1779. {
  1780. printf("\n=== MAIN MENU ===\n");
  1781. printf("1. Login\n");
  1782. printf("2. Sign-up\n");
  1783. printf("3. Exit\n");
  1784. printf("Enter choice: ");
  1785. scanf("%d", &choice);
  1786. printf("\n");
  1787.  
  1788. while(choice!=1 && choice!=2 && choice!=3)
  1789. {
  1790. fflush(stdin);
  1791. printf("Try Again!\n");
  1792. printf("\n=== MAIN MENU ===\n");
  1793. printf("1. Login\n");
  1794. printf("2. Sign-up\n");
  1795. printf("3. Exit\n");
  1796. printf("Enter choice: ");
  1797. scanf("%d", &choice);
  1798. printf("\n");
  1799. }
  1800.  
  1801. switch (choice)
  1802. {
  1803. case 1: if (user==NULL)
  1804. printf("There are not any users yet! Sign up first.\n");
  1805. else
  1806. {
  1807. printf("You chose to login!\n\n");
  1808. login(user,&stemp);
  1809. }
  1810. break;
  1811.  
  1812. case 2: printf("You chose to sign-up!\n\n");
  1813. do
  1814. {
  1815. pNew=malloc(sizeof(userNode));
  1816. signUp(user, pNew);
  1817. pNew->next=NULL;
  1818.  
  1819. if (user==NULL)
  1820. user=pNew;
  1821.  
  1822. else if (strcmp(user->username, pNew->username)>0)
  1823. {
  1824. pNew->next=user;
  1825. user=pNew;
  1826. }
  1827.  
  1828. else
  1829. {
  1830. pRun=user;
  1831. while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1832. {
  1833. pTrail=pRun;
  1834. pRun=pRun->next;
  1835. }
  1836. pTrail->next=pNew;
  1837. pNew->next=pRun;
  1838. }
  1839.  
  1840. do
  1841. {
  1842. printf("Would you like to add another user?\n");
  1843. printf("1. Yes\n");
  1844. printf("2. No\n");
  1845. printf("Enter choice: ");
  1846. scanf("%d", &nChoice);
  1847. } while (nChoice!=1 && nChoice!=2);
  1848.  
  1849. } while (nChoice==1);
  1850. break;
  1851.  
  1852. case 3: printf("Closing program...\n");
  1853. close=1;
  1854. break;
  1855.  
  1856. default: printf("Invalid input.\n");
  1857. }
  1858. } while (!close&&choice!=3);
  1859.  
  1860. if (user!=NULL)
  1861. freeAll(&user);
  1862.  
  1863. if (pStocks!=NULL)
  1864. freeAllStocks (&pStocks);
  1865.  
  1866. return 0;
  1867. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement