Advertisement
Guest User

YOU CAN DO IT ALEX!

a guest
Mar 26th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 39.24 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("Username: %s 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("Wrong 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.     userNode *temp=NULL;
  225.  
  226.     do
  227.     {      
  228.         printf("\n === MANAGE ACCOUNTS MENU === \n");
  229.         printf("1. View Locked Accounts\n");
  230.         printf("2. Unlock Specific Account\n");
  231.         printf("3. Unlock All Locked Accounts\n");
  232.         printf("4. View Accounts with Outstanding Balance\n");
  233.         printf("5. Return to Administrator Menu\n");
  234.         printf("Enter choice: ");
  235.         scanf("%d", &aChoice);
  236.    
  237.         switch (aChoice)
  238.         {
  239.             case 1: viewLocked (user);
  240.                     break;
  241.             case 2: unlockSpecific (user);
  242.                     break;
  243.             case 3: unlockAll (user);
  244.                     break;
  245.             case 4: viewOutBal (user);
  246.                     break;
  247.             case 5: break;
  248.         }
  249.     } while (aChoice!=5);
  250. }
  251.  
  252. /*
  253. FUNCTION: uniqueCateg checks if the category is unique or not
  254. @param: string15 addCateg: string that contains the name of the new category
  255. @param: categNode *temp: structure with the category information
  256. @return: int
  257. */
  258. int uniqueCateg (string15 addCateg, categNode *temp)
  259. {
  260.     int unique=1;
  261.     categNode *check=NULL;
  262.    
  263.     check=temp;
  264.    
  265.     while(check!=NULL)
  266.     {
  267.         if (strcmp(check->category, addCateg)==0)
  268.         {
  269.             unique=0;
  270.             check=NULL;
  271.         }
  272.         else
  273.             check=check->next;
  274.     }
  275.     return unique;
  276. }
  277.  
  278. /*
  279. FUNCTION: uniqueProd checks if the product is unique or not
  280. @param: string15 prod: string that contains the name of the new product
  281. @param: categNode *temp: structure with the category information
  282. @return: int
  283. */
  284. int uniqueProd (string15 prod, categNode *temp)
  285. {
  286.     int unique=1;
  287.     categNode *check=NULL;
  288.    
  289.     check=temp;
  290.    
  291.     while (check!=NULL)
  292.     {
  293.         if (strcmp(check->productInfo.product, prod)==0)
  294.         {
  295.             unique=0;
  296.             check=NULL;
  297.         }
  298.         else
  299.             check=check->next;
  300.     }
  301.     return unique;
  302. }
  303.  
  304. /*
  305. FUNCTION: addNewStock allows the admin to add a new stock to the current list of stocks
  306.           and goes back to the Manage Stocks Menu after
  307. @param: stockNode *stockInfo: structure with stock information
  308. @param: int *stockctr: to check the number of categories, which but be max of 10
  309. @param: categNode *stemp: structure with the category information
  310. @return: categNode *
  311. */
  312. void addNewStock(stockNode *stockInfo, int *stockctr, categNode **stemp)
  313. {
  314.     string15 addCateg, supplier, prod, newcat, newsup, newprod;
  315.     int cChoice=0, accept=0, compare=2, avbl=0, sold=0, i=0, add=0, rNum=0;
  316.     float purchasePrice=0, sellingPrice=0, discountRate=0;
  317.     categNode *stocktemp=NULL;
  318.     stocktemp = malloc(sizeof(categNode));
  319.     srand(time(NULL));
  320.     printf("\nAdding New Stocks: \n");
  321.  
  322.     do
  323.     {
  324.         printf("Would you like to add a new stock?\n");
  325.         printf("1. Yes\n");
  326.         printf("2. No\n");
  327.         printf("Enter choice: ");
  328.         fflush(stdin);
  329.         scanf("%d", &cChoice);
  330.        
  331.         if (cChoice!=1 && cChoice!=2)
  332.             printf("Invalid input. Try again\n");
  333.         if ((*stockctr)==0 && cChoice==2)
  334.             printf("\nNo stocks yet!\n\n");
  335.     } while ((cChoice!=1 && (*stockctr)<11)&&(cChoice!=2 && (*stockctr)>0));
  336.    
  337.     while (cChoice==1 && accept!=1 && (*stockctr)<11)
  338.     {
  339.         do
  340.         {
  341.             printf("Enter Category: ");
  342.             fflush(stdin);
  343.             fgets(addCateg, 16, stdin);
  344.             addCateg[strlen(addCateg)-1]='\0';
  345.             if(strlen(addCateg)>=3 && strlen(addCateg)<=15)
  346.             {
  347.                     compare=uniqueCateg(addCateg,*stemp);
  348.                     if (compare==1)
  349.                         {
  350.                         accept=1;
  351.                         strcpy (stocktemp->category, addCateg);
  352.                         (*stockctr)++;
  353.                         printf("You have created a new category! Category #%d\n", *stockctr);
  354.                         }else{
  355.                         accept=1;
  356.                         strcpy (stocktemp->category, addCateg);            
  357.                         }
  358.             }else
  359.                 printf("Enter another category that contains 3-15 characters\n");
  360.             if (*stockctr==10)
  361.                 printf("You have reached the maximum amount of categories!\n");
  362.         } while ((strlen(addCateg)<3 && strlen(addCateg)>15) && accept!=1 && (*stockctr)<10);
  363.     }
  364.    
  365.     accept=0;
  366.     compare=2;
  367.    
  368.     if (cChoice==1 && accept!=1)
  369.     {  
  370.         do
  371.         {
  372.             printf("Enter Supplier: ");
  373.             fflush(stdin);
  374.             fgets(supplier, 16, stdin);
  375.            
  376.             if (strlen(supplier)<16)
  377.                 accept=1;
  378.             else
  379.                 printf("Invalid input. Try again!\n");
  380.         } while (accept!=1);
  381.        
  382.         if (accept==1)
  383.         {
  384.             strcpy(stocktemp->productInfo.supplier, supplier);
  385.             stocktemp->productInfo.supplier[strlen(stocktemp->productInfo.supplier)-1]='\0';
  386.             accept=0;
  387.         }
  388.        
  389.         do
  390.         {
  391.             printf("Enter Product: ");
  392.             fflush(stdin);
  393.             fgets(prod, 16, stdin);
  394.             prod[strlen(prod)-1]='\0';
  395.             compare=uniqueProd(prod, *stemp);
  396.            
  397.             if (strlen(prod)<16 && compare==1)
  398.                 accept=1;
  399.             else
  400.                 printf("Invalid input. Try again!\n");
  401.         } while (accept!=1 && compare!=1);
  402.        
  403.         if (accept==1)
  404.         {
  405.             strcpy(stocktemp->productInfo.product, prod);
  406.             accept=0;
  407.         }
  408.            
  409.         do
  410.         {
  411.             printf("Enter Quantity Available: ");
  412.             fflush(stdin);
  413.             scanf("%d", &avbl);
  414.             if (avbl>=0)
  415.                 accept=1;
  416.             else
  417.                 printf("Invalid input. Try again!\n");
  418.         } while (accept!=1);
  419.        
  420.         if (accept==1)
  421.         {
  422.             stocktemp->productInfo.avbl=avbl;
  423.             accept=0;
  424.         }
  425.        
  426.         do
  427.         {
  428.             printf("Enter Purchase Price: ");
  429.             fflush(stdin);
  430.             scanf("%f", &purchasePrice);
  431.             if (purchasePrice>0)
  432.                 accept=1;
  433.             else
  434.                 printf("Invalid input. Try again!\n");
  435.         } while (accept!=1);
  436.        
  437.         if (accept==1)
  438.         {
  439.             stocktemp->productInfo.purchasePrice=purchasePrice;
  440.             accept=0;
  441.         }
  442.            
  443.         do
  444.         {
  445.             printf("Enter Unit Selling Price: ");
  446.             fflush(stdin);
  447.             scanf("%f", &sellingPrice);
  448.             if (sellingPrice>0)
  449.                 accept=1;
  450.             else
  451.                 printf("Invalid input. Try again!\n");
  452.         } while (accept!=1);
  453.        
  454.         if (accept==1)
  455.         {
  456.             stocktemp->productInfo.sellingPrice=sellingPrice;
  457.             accept=0;
  458.         }
  459.        
  460.         do
  461.         {
  462.             printf("Enter Discount Rate: ");
  463.             fflush(stdin);
  464.             scanf("%f", &discountRate);
  465.             if (discountRate>=0)
  466.                 accept=1;
  467.             else
  468.                 printf("Invalid input. Try again!\n");
  469.         } while (accept!=1);
  470.        
  471.         if (accept==1)
  472.         {
  473.             stocktemp->productInfo.discountRate=discountRate;
  474.             accept=0;
  475.         }
  476.     }
  477.    
  478.     strcpy(newcat, addCateg);
  479.     for(i=0; i<strlen(newcat); i++)
  480.     {
  481.         if (newcat[i]>='a' && newcat[i]<='z')
  482.             newcat[i]-=32;
  483.     }
  484.  
  485.     strcpy(newsup, supplier);
  486.     for(i=0; i<strlen(newsup); i++)
  487.     {
  488.         if (newsup[i]>='a' && newsup[i]<='z')
  489.             newsup[i]-=32;
  490.     }
  491.  
  492.     strcpy(newprod, prod);
  493.     for(i=0; i<strlen(newprod); i++)
  494.     {
  495.         if (newprod[i]>='a' && newprod[i]<='z')
  496.             newprod[i]-=32;
  497.     }
  498.  
  499.     stocktemp->productInfo.sold=0;
  500.     stocktemp->productInfo.pCode[0]=newcat[0];
  501.     stocktemp->productInfo.pCode[1]=newsup[0];
  502.     stocktemp->productInfo.pCode[2]=newprod[0];
  503.    
  504.     i=0;
  505.    
  506.     while (i<5)
  507.     {
  508.         rNum=rand()%58;
  509.         if(rNum>47 && rNum<57)
  510.         {
  511.             stocktemp->productInfo.pCode[(i+3)]=rNum;
  512.             i++;
  513.         }
  514.         stocktemp->productInfo.pCode[8]='\0';
  515.     }
  516.     if(cChoice!=2){
  517.         printf("Product code: %s\n", stocktemp->productInfo.pCode);
  518.         stocktemp->next = *stemp;
  519.         *stemp = stocktemp;
  520.     }
  521.    
  522. }
  523.  
  524.  
  525. void viewAllStocks (stockNode *stockInfo, int *stockctr, categNode *stemp)
  526. {
  527.     int i;
  528.     int ctr=0;
  529.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  530.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  531.     while (stemp!=NULL)
  532.     {
  533.         if(ctr!=20)
  534.         {  
  535.             printf("\n");
  536.             printf("%s", stemp->category);
  537.             if(strlen(stemp->category)<15)
  538.                 for(i=strlen(stemp->category);i<15;i++)
  539.                     printf(" ");
  540.             printf("|%s", stemp->productInfo.supplier);
  541.             if(strlen(stemp->productInfo.supplier)<15)
  542.                 for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  543.                     printf(" ");
  544.             printf("|%s", stemp->productInfo.pCode);
  545.             printf("|%s", stemp->productInfo.product);
  546.             if(strlen(stemp->productInfo.product)<10)
  547.                 for(i=strlen(stemp->productInfo.product);i<10;i++)
  548.                     printf(" ");
  549.             printf("|%d", stemp->productInfo.avbl);
  550.             if(stemp->productInfo.avbl<10)
  551.                 for(i=0;i<3;i++)
  552.                     printf(" ");
  553.                 else if(stemp->productInfo.avbl<100)
  554.                     for(i=0;i<2;i++)
  555.                         printf(" ");
  556.                     else if(stemp->productInfo.avbl<1000)
  557.                         for(i=0;i<1;i++)
  558.                             printf(" ");
  559.             printf("|%d", stemp->productInfo.sold);
  560.             if(stemp->productInfo.sold<10)
  561.                 for(i=0;i<3;i++)
  562.                     printf(" ");
  563.                 else if(stemp->productInfo.sold<100)
  564.                     for(i=0;i<2;i++)
  565.                         printf(" ");
  566.                     else if(stemp->productInfo.sold<1000)
  567.                         for(i=0;i<1;i++)
  568.                             printf(" ");
  569.             printf("|%.2f", stemp->productInfo.purchasePrice);
  570.             printf("|%.2f", stemp->productInfo.sellingPrice);
  571.             printf("|%.1f", stemp->productInfo.discountRate);
  572.             printf("\n");
  573.             ctr++;
  574.         }else{
  575.             printf("\nPress Enter to continue...\n");
  576.             fflush(stdin);
  577.             while(getchar() != '\n');  
  578.             ctr = 0;
  579.         }
  580.         stemp = stemp->next;
  581.     }
  582.     printf("\nPress Enter to continue...\n");
  583.     fflush(stdin);
  584.     while(getchar() != '\n');  
  585. }
  586.  
  587. void viewStockCategory (stockNode *stockInfo, int *stockctr, categNode *stemp)
  588. {
  589.     int i,found=0;
  590.     categNode *temp;
  591.     int ctr=0;
  592.     string15 categ;
  593.         printf("Enter category: ");
  594.         fflush(stdin);
  595.         scanf("%s",categ);
  596.     for(temp=stemp;temp!=NULL&&!found;temp=temp->next){
  597.         if(strcmp(temp->category,categ)==0)
  598.             found = 1;
  599.         }
  600.     if(found){
  601.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  602.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  603.             while(stemp!=NULL){
  604.                 if(ctr!=20)
  605.                 {          
  606.                 if(strcmp(stemp->category,categ)==0){
  607.                     printf("\n");
  608.                     printf("%s", stemp->category);
  609.                     if(strlen(stemp->category)<15)
  610.                         for(i=strlen(stemp->category);i<15;i++)
  611.                             printf(" ");
  612.                     printf("|%s", stemp->productInfo.supplier);
  613.                     if(strlen(stemp->productInfo.supplier)<15)
  614.                         for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  615.                                 printf(" ");
  616.                     printf("|%s", stemp->productInfo.pCode);
  617.                     printf("|%s", stemp->productInfo.product);
  618.                     if(strlen(stemp->productInfo.product)<10)
  619.                         for(i=strlen(stemp->productInfo.product);i<10;i++)
  620.                             printf(" ");
  621.                     printf("|%d", stemp->productInfo.avbl);
  622.                     if(stemp->productInfo.avbl<10)
  623.                         for(i=0;i<3;i++)
  624.                             printf(" ");
  625.                         else if(stemp->productInfo.avbl<100)
  626.                             for(i=0;i<2;i++)
  627.                                 printf(" ");
  628.                             else if(stemp->productInfo.avbl<1000)
  629.                                 for(i=0;i<1;i++)
  630.                                     printf(" ");
  631.                     printf("|%d", stemp->productInfo.sold);
  632.                     if(stemp->productInfo.sold<10)
  633.                         for(i=0;i<3;i++)
  634.                             printf(" ");
  635.                         else if(stemp->productInfo.sold<100)
  636.                             for(i=0;i<2;i++)
  637.                                 printf(" ");
  638.                             else if(stemp->productInfo.sold<1000)
  639.                                 for(i=0;i<1;i++)
  640.                                     printf(" ");
  641.                     printf("|%.2f", stemp->productInfo.purchasePrice);
  642.                     printf("|%.2f", stemp->productInfo.sellingPrice);
  643.                     printf("|%.1f", stemp->productInfo.discountRate);
  644.                     printf("\n");
  645.                     ctr++;
  646.                     }  
  647.                 }else{
  648.                     printf("\nPress Enter to continue...\n");
  649.                     fflush(stdin);
  650.                     while(getchar() != '\n');  
  651.                     ctr = 0;
  652.                 }
  653.                 stemp = stemp->next;           
  654.             }
  655.         printf("\nPress Enter to continue...\n");
  656.         fflush(stdin);
  657.         while(getchar() != '\n');
  658.     }else{
  659.         printf("\nCategory not found.\n");
  660.         printf("\nPress Enter to continue...\n");
  661.         fflush(stdin);
  662.         while(getchar() != '\n');
  663.     }
  664. }
  665.  
  666. /*
  667. void modStockInfo ()
  668. {
  669.    
  670. }
  671.  
  672. void restock ()
  673. {
  674.    
  675. }*/
  676.  
  677.  
  678. /*
  679. FUNCTION: manageStocks asks the admin what they want to do with all the stock information
  680. @param: stockNode *stockInfo: structure with stock information
  681. @param: int *stockctr: to check the number of categories, which but be max of 10
  682. @param: categNode *stemp: structure with the category information
  683. @return: void
  684. */
  685. void manageStocks (stockNode *stockInfo, int *stockctr,categNode **stemp)
  686. {
  687.     int aChoice=0;
  688.     do
  689.     {
  690.         printf("\n === MANAGE STOCKS MENU === \n");
  691.         printf("1. Add New Stock\n");
  692.         printf("2. View All Stocks\n");
  693.         printf("3. View Stocks by Category (PHASE 2)\n");
  694.         printf("4. View Stocks to Reorder (PHASE 2)\n");
  695.         printf("5. Modify Stock Info\n");
  696.         printf("6. Restock\n");
  697.         printf("7. Save Inventory (PHASE 2)\n");
  698.         printf("8. Update Inventory from File (PHASE 2)\n");
  699.         printf("9. Go back\n");
  700.         printf("Enter choice: ");
  701.         fflush(stdin);
  702.         scanf("%d", &aChoice);
  703.        
  704.         switch (aChoice)
  705.         {
  706.             case 1: addNewStock (stockInfo, stockctr, stemp);
  707.                     break;
  708.             case 2: viewAllStocks (stockInfo, stockctr, *stemp);
  709.                     break;
  710.             case 3: viewStockCategory (stockInfo, stockctr, *stemp);
  711.                     break;
  712.             case 4: break;
  713.         //  case 5: modStockInfo ();
  714.                     break;
  715.         //  case 6: restock ();
  716.                     break;
  717.             case 7: break;
  718.             case 8: break;
  719.         }
  720.     } while (aChoice>0 && aChoice<9 && aChoice!= 9);
  721. }
  722.  
  723. /*
  724. FUNCTION: adminMenu displays only when an administrator is able to log in successfully
  725.           It then asks the admin what (s)he wants to do
  726. @param: userNode *user: structure with the user information
  727. @param: int *close: option to exit
  728. @param: stockNode *stockInfo: structure with stock information
  729. @param: int *stockctr: to check the number of categories, which but be max of 10
  730. @param: categNode *stemp: structure with the category information
  731. @return: void
  732. */
  733. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr, categNode **stemp)
  734. {
  735.     int adChoice=0;
  736.    
  737.     do
  738.     {          
  739.         printf("\n === ADMINISTRATOR MENU === \n");
  740.         printf("1. Manage Accounts Menu\n");
  741.         printf("2. Manage Stocks Menu\n");
  742.         printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  743.         printf("4. Shutdown Kiosk \n");
  744.         printf("5. Log Out \n");
  745.         printf("Enter choice: ");
  746.         scanf("%d", &adChoice);
  747.        
  748.         switch (adChoice)
  749.         {
  750.             case 1: manageAccounts(user);
  751.                     break;
  752.             case 2: manageStocks(stockInfo, stockctr,stemp);
  753.                     break;
  754.             case 3: break;
  755.             case 4: *close=1;
  756.                     break;
  757.             case 5: break;
  758.             default: printf("Invalid input. Try again.\n");
  759.         }  
  760.     } while (adChoice!=4 && adChoice!=5);
  761. }
  762.  
  763. /*
  764. FUNCTION: modUserInfo allows the shopper to change his/her account details
  765. @param: userNode *user: structure about user information
  766. @return: void
  767. */
  768. void modUserInfo (userNode *user)
  769. {
  770.     int uChoice=0, accept=0, i=0, nameChoice=0;
  771.     string15 newpass;
  772.    
  773.     printf("\nModifying User Info:\n");
  774.    
  775.     do
  776.     {
  777.         printf("\nDo you want to...\n");
  778.         printf("1. Change Name\n");
  779.         printf("2. Change Address\n");
  780.         printf("3. Change Password\n");
  781.         printf("4. Return to Shopper Menu\n");
  782.         printf("Enter choice: ");
  783.         scanf("%d", &uChoice);
  784.        
  785.         switch (uChoice)
  786.         {
  787.             case 1: do
  788.                     {
  789.                         printf("\nWhat would you like to change? \n");
  790.                         printf("1. First Name\n");
  791.                         printf("2. Middle Name\n");
  792.                         printf("3. Last Name\n");
  793.                         printf("4. All Names\n");
  794.                         printf("Enter choice: ");
  795.                         scanf("%d", &nameChoice);
  796.                     } while (nameChoice<1 && nameChoice>4);
  797.                    
  798.                     switch (nameChoice)
  799.                     {
  800.                         case 1: printf("Enter New Name: \n");
  801.                                 printf("First name: ");
  802.                                 fflush(stdin);
  803.                                 fgets(user->info.name.first, 21, stdin);
  804.                            
  805.                                 break;
  806.                                
  807.                         case 2: printf("Enter New Name: \n");
  808.                                 printf("Middle name: ");
  809.                                 fflush(stdin);
  810.                                 fgets(user->info.name.middle, 21, stdin);
  811.                                 break;
  812.                        
  813.                         case 3: printf("Enter New Name: \n");
  814.                                 printf("Last name: ");
  815.                                 fflush(stdin);
  816.                                 fgets(user->info.name.last, 21, stdin);
  817.                                 break;
  818.                        
  819.                         case 4: printf("Enter New Name: \n");
  820.                                 printf("First name: ");
  821.                                 fflush(stdin);
  822.                                 fgets(user->info.name.first, 21, stdin);
  823.                                 printf("Middle name: ");
  824.                                 fflush(stdin);
  825.                                 fgets(user->info.name.middle, 21, stdin);
  826.                                 printf("Last name: ");
  827.                                 fflush(stdin);
  828.                                 fgets(user->info.name.last, 21, stdin);
  829.                                 break;                                     
  830.                     }
  831.                     break;
  832.                    
  833.             case 2: printf("Enter New Address: \n");
  834.                     printf("Address: ");
  835.                     fflush(stdin);
  836.                     fgets (user->info.address, 51, stdin);
  837.                     break;
  838.                    
  839.             case 3: do
  840.                     {                  
  841.                         printf("Enter New Password: ");
  842.                         scanf("%s", newpass);
  843.                    
  844.                         for (i=0; i<strlen(newpass); i++)
  845.                         {
  846.                             if (!((newpass[i] >= 'A' && newpass[i] <= 'Z') || (newpass[i] >= 'a' && newpass[i] <= 'z')))
  847.                             {
  848.                                 accept=1;
  849.                                 strcpy (user->password, newpass);
  850.                             }
  851.                         }
  852.                     } while (strlen(newpass) < 6 || strlen(newpass) > 15 || accept!=1);                
  853.                     break;
  854.                    
  855.             case 4: break;
  856.             default: printf("Invalid input. Try again.\n");
  857.         }
  858.     } while (uChoice!=4);
  859. }
  860.  
  861. void browseAllProd (categNode *stemp)
  862. {
  863.     int i,found=0;
  864.     categNode *temp;
  865.     int ctr=0;
  866.     for(temp=stemp;temp!=NULL&&!found;temp=temp->next){
  867.         if(temp->productInfo.avbl>0)
  868.             found = 1;
  869.         }
  870.     if(found){
  871.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  872.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  873.             while(stemp!=NULL){
  874.                 if(ctr!=20)
  875.                 {          
  876.                 if(stemp->productInfo.avbl>0){
  877.                     printf("\n");
  878.                     printf("%s", stemp->category);
  879.                     if(strlen(stemp->category)<15)
  880.                         for(i=strlen(stemp->category);i<15;i++)
  881.                             printf(" ");
  882.                     printf("|%s", stemp->productInfo.supplier);
  883.                     if(strlen(stemp->productInfo.supplier)<15)
  884.                         for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  885.                                 printf(" ");
  886.                     printf("|%s", stemp->productInfo.pCode);
  887.                     printf("|%s", stemp->productInfo.product);
  888.                     if(strlen(stemp->productInfo.product)<10)
  889.                         for(i=strlen(stemp->productInfo.product);i<10;i++)
  890.                             printf(" ");
  891.                     printf("|%d", stemp->productInfo.avbl);
  892.                     if(stemp->productInfo.avbl<10)
  893.                         for(i=0;i<3;i++)
  894.                             printf(" ");
  895.                         else if(stemp->productInfo.avbl<100)
  896.                             for(i=0;i<2;i++)
  897.                                 printf(" ");
  898.                             else if(stemp->productInfo.avbl<1000)
  899.                                 for(i=0;i<1;i++)
  900.                                     printf(" ");
  901.                     printf("|%d", stemp->productInfo.sold);
  902.                     if(stemp->productInfo.sold<10)
  903.                         for(i=0;i<3;i++)
  904.                             printf(" ");
  905.                         else if(stemp->productInfo.sold<100)
  906.                             for(i=0;i<2;i++)
  907.                                 printf(" ");
  908.                             else if(stemp->productInfo.sold<1000)
  909.                                 for(i=0;i<1;i++)
  910.                                     printf(" ");
  911.                     printf("|%.2f", stemp->productInfo.purchasePrice);
  912.                     printf("|%.2f", stemp->productInfo.sellingPrice);
  913.                     printf("|%.1f", stemp->productInfo.discountRate);
  914.                     printf("\n");
  915.                     ctr++;
  916.                     }  
  917.                 }else{
  918.                     printf("\nPress Enter to continue...\n");
  919.                     fflush(stdin);
  920.                     while(getchar() != '\n');  
  921.                     ctr = 0;
  922.                 }
  923.                 stemp = stemp->next;           
  924.             }
  925.         printf("\nPress Enter to continue...\n");
  926.         fflush(stdin);
  927.         while(getchar() != '\n');
  928.     }else{
  929.         printf("\nNo available products.\n");
  930.         printf("\nPress Enter to continue...\n");
  931.         fflush(stdin);
  932.         while(getchar() != '\n');
  933.     }  
  934. }
  935.  
  936. void browseAllCateg (categNode *stemp)
  937. {
  938.     int i,found=0;
  939.     categNode *temp;
  940.     int ctr=0;
  941.     string15 categ;
  942.         printf("Enter category: ");
  943.         fflush(stdin);
  944.         scanf("%s",categ);
  945.     for(temp=stemp;temp!=NULL&&!found;temp=temp->next){
  946.         if(strcmp(temp->category,categ)==0)
  947.             found = 1;
  948.         }
  949.     if(found){
  950.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  951.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  952.             while(stemp!=NULL){
  953.                 if(ctr!=20)
  954.                 {  
  955.             if(strcmp(stemp->category,categ)==0)       
  956.                 if(temp->productInfo.avbl>0){
  957.                     printf("\n");
  958.                     printf("%s", stemp->category);
  959.                     if(strlen(stemp->category)<15)
  960.                         for(i=strlen(stemp->category);i<15;i++)
  961.                             printf(" ");
  962.                     printf("|%s", stemp->productInfo.supplier);
  963.                     if(strlen(stemp->productInfo.supplier)<15)
  964.                         for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  965.                                 printf(" ");
  966.                     printf("|%s", stemp->productInfo.pCode);
  967.                     printf("|%s", stemp->productInfo.product);
  968.                     if(strlen(stemp->productInfo.product)<10)
  969.                         for(i=strlen(stemp->productInfo.product);i<10;i++)
  970.                             printf(" ");
  971.                     printf("|%d", stemp->productInfo.avbl);
  972.                     if(stemp->productInfo.avbl<10)
  973.                         for(i=0;i<3;i++)
  974.                             printf(" ");
  975.                         else if(stemp->productInfo.avbl<100)
  976.                             for(i=0;i<2;i++)
  977.                                 printf(" ");
  978.                             else if(stemp->productInfo.avbl<1000)
  979.                                 for(i=0;i<1;i++)
  980.                                     printf(" ");
  981.                     printf("|%d", stemp->productInfo.sold);
  982.                     if(stemp->productInfo.sold<10)
  983.                         for(i=0;i<3;i++)
  984.                             printf(" ");
  985.                         else if(stemp->productInfo.sold<100)
  986.                             for(i=0;i<2;i++)
  987.                                 printf(" ");
  988.                             else if(stemp->productInfo.sold<1000)
  989.                                 for(i=0;i<1;i++)
  990.                                     printf(" ");
  991.                     printf("|%.2f", stemp->productInfo.purchasePrice);
  992.                     printf("|%.2f", stemp->productInfo.sellingPrice);
  993.                     printf("|%.1f", stemp->productInfo.discountRate);
  994.                     printf("\n");
  995.                     ctr++;
  996.                     }  
  997.                 }else{
  998.                     printf("\nPress Enter to continue...\n");
  999.                     fflush(stdin);
  1000.                     while(getchar() != '\n');  
  1001.                     ctr = 0;
  1002.                 }
  1003.                 stemp = stemp->next;           
  1004.             }
  1005.         printf("\nPress Enter to continue...\n");
  1006.         fflush(stdin);
  1007.         while(getchar() != '\n');
  1008.     }else{
  1009.         printf("\nNo available products.\n");
  1010.         printf("\nPress Enter to continue...\n");
  1011.         fflush(stdin);
  1012.         while(getchar() != '\n');
  1013.     }  
  1014. }
  1015.  
  1016. void browseAllSale (categNode *stemp)
  1017. {
  1018.     int i,found=0;
  1019.     categNode *temp;
  1020.     int ctr=0;
  1021.     string15 categ;
  1022.     for(temp=stemp;temp!=NULL&&!found;temp=temp->next){
  1023.         if(temp->productInfo.discountRate>0)
  1024.             found = 1;
  1025.         }
  1026.     if(found){
  1027.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  1028.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  1029.             while(stemp!=NULL){
  1030.                 if(ctr!=20)
  1031.                 {          
  1032.                 if(stemp->productInfo.discountRate>0){
  1033.                     printf("\n");
  1034.                     printf("%s", stemp->category);
  1035.                     if(strlen(stemp->category)<15)
  1036.                         for(i=strlen(stemp->category);i<15;i++)
  1037.                             printf(" ");
  1038.                     printf("|%s", stemp->productInfo.supplier);
  1039.                     if(strlen(stemp->productInfo.supplier)<15)
  1040.                         for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  1041.                                 printf(" ");
  1042.                     printf("|%s", stemp->productInfo.pCode);
  1043.                     printf("|%s", stemp->productInfo.product);
  1044.                     if(strlen(stemp->productInfo.product)<10)
  1045.                         for(i=strlen(stemp->productInfo.product);i<10;i++)
  1046.                             printf(" ");
  1047.                     printf("|%d", stemp->productInfo.avbl);
  1048.                     if(stemp->productInfo.avbl<10)
  1049.                         for(i=0;i<3;i++)
  1050.                             printf(" ");
  1051.                         else if(stemp->productInfo.avbl<100)
  1052.                             for(i=0;i<2;i++)
  1053.                                 printf(" ");
  1054.                             else if(stemp->productInfo.avbl<1000)
  1055.                                 for(i=0;i<1;i++)
  1056.                                     printf(" ");
  1057.                     printf("|%d", stemp->productInfo.sold);
  1058.                     if(stemp->productInfo.sold<10)
  1059.                         for(i=0;i<3;i++)
  1060.                             printf(" ");
  1061.                         else if(stemp->productInfo.sold<100)
  1062.                             for(i=0;i<2;i++)
  1063.                                 printf(" ");
  1064.                             else if(stemp->productInfo.sold<1000)
  1065.                                 for(i=0;i<1;i++)
  1066.                                     printf(" ");
  1067.                     printf("|%.2f", stemp->productInfo.purchasePrice);
  1068.                     printf("|%.2f", stemp->productInfo.sellingPrice);
  1069.                     printf("|%.1f", stemp->productInfo.discountRate);
  1070.                     printf("\n");
  1071.                     ctr++;
  1072.                     }  
  1073.                 }else{
  1074.                     printf("\nPress Enter to continue...\n");
  1075.                     fflush(stdin);
  1076.                     while(getchar() != '\n');  
  1077.                     ctr = 0;
  1078.                 }
  1079.                 stemp = stemp->next;           
  1080.             }
  1081.         printf("\nPress Enter to continue...\n");
  1082.         fflush(stdin);
  1083.         while(getchar() != '\n');
  1084.     }else{
  1085.         printf("\nNo available products.\n");
  1086.         printf("\nPress Enter to continue...\n");
  1087.         fflush(stdin);
  1088.         while(getchar() != '\n');
  1089.     }  
  1090. }
  1091.  
  1092. /*
  1093. FUNCTION: shopperMenu displays when a shopper logs in successfully
  1094.           It then asks the shopper what he/she wants to do
  1095. @param: userNode *user: structure with the user information
  1096. @param: stockNode *stockInfo: structure with stock information
  1097. @param: int *stockctr: to check the number of categories, which but be max of 10
  1098. @return: void
  1099. */
  1100. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr,categNode **stemp)
  1101. {
  1102.     int sChoice=0;
  1103.    
  1104.     do
  1105.     {
  1106.         printf("\n === SHOPPER MENU === \n");
  1107.         printf("1. Modify User Info\n");
  1108.         printf("2. Browse All Products\n");
  1109.         printf("3. Browse Products by Category\n");
  1110.         printf("4. Browse Products on Sale\n");
  1111.         printf("5. Add to Cart\n");
  1112.         printf("6. View Cart\n");
  1113.         printf("7. Settle Outstanding Balance\n");
  1114.         printf("8. Log Out\n");
  1115.         printf("Enter choice: ");
  1116.         scanf("%d", &sChoice);
  1117.        
  1118.         switch (sChoice)
  1119.         {
  1120.             case 1: modUserInfo (user);
  1121.                     break;
  1122.             case 2: browseAllProd (*stemp);
  1123.                     break;
  1124.             case 3: browseAllCateg (*stemp);
  1125.                     break;
  1126.             case 4: browseAllSale (*stemp);
  1127.                     break;
  1128.             case 5: break;
  1129.             case 6: break;
  1130.             case 7: break;
  1131.             case 8: break;
  1132.         }      
  1133.     } while (sChoice!=8);
  1134. }
  1135.  
  1136. /*
  1137. FUNCTION: login allows the user to log in to a specific account
  1138. @param: userNode *user: structure with the user information
  1139.         categNode **stemp: structure with the stock information
  1140. @return: void
  1141. */
  1142. void login (userNode *user,categNode **stemp)
  1143. {
  1144.     int pchk=1, compare=2, found=0, close=0, stockctr=0, attempt=0;
  1145.     char cDump;
  1146.     string15 uname, pword;
  1147.     userNode *temp=NULL;
  1148.     stockNode *stockInfo=NULL;
  1149.     temp=user;
  1150.    
  1151.     printf(" === LOGIN === \n");
  1152.     printf("Enter username: ");
  1153.     scanf("%s%c", uname, &cDump);
  1154.    
  1155.     while (found==0 && temp!=NULL)
  1156.     {
  1157.         compare=strcmp(uname, temp->username);
  1158.        
  1159.         if (compare==0)
  1160.             found=1;
  1161.         else
  1162.             temp=temp->next;
  1163.     }
  1164.  
  1165.     if (found==1 && temp->isLocked==0) 
  1166.     {  
  1167.         do
  1168.         {
  1169.             printf("Enter password: ");
  1170.             scanf("%s%c", pword, &cDump);
  1171.            
  1172.             compare=strcmp(pword, temp->password);
  1173.            
  1174.             if (compare==0)
  1175.             {              
  1176.                 switch (temp->type)
  1177.                 {
  1178.                     case 1:     printf("\nWelcome back, Admin!\n");
  1179.                                 close=1;
  1180.                                 adminMenu (user, &close, stockInfo, &stockctr,stemp);
  1181.                                 break;
  1182.                                
  1183.                     case 2:     printf("\nWelcome back, Shopper!\n");
  1184.                                 close=1;
  1185.                                 shopperMenu(user, stockInfo, &stockctr,stemp);
  1186.                                 break; 
  1187.                 }
  1188.             }
  1189.             else
  1190.             {
  1191.                 attempt++;
  1192.                 printf("Incorrect password. Try again.\n");
  1193.                 printf("%d out of 3 tries\n", attempt);
  1194.                    
  1195.                 if (attempt==3)
  1196.                 {
  1197.                     user->isLocked=1;
  1198.                     printf("\nYou have tried logging in too many times.\n");
  1199.                     printf("Please contact the administrator.\n");
  1200.                 }
  1201.             }
  1202.         } while (!close && attempt<=3 && temp->isLocked==0);
  1203.     }
  1204.     else
  1205.         printf("\nSorry, username does not exist.\n");
  1206. }
  1207.  
  1208. /*
  1209. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  1210.           as well as their address
  1211. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  1212. @return: void
  1213. */
  1214. void getUserInfo (userInfoType  *pInfo)
  1215. {
  1216.     getchar();
  1217.     printf("Enter name: \n");
  1218.     printf("First name: ");
  1219.     fgets(pInfo->name.first, 21, stdin);
  1220.     printf("Middle name: ");
  1221.     fgets(pInfo->name.middle, 21, stdin);
  1222.     printf("Last name: ");
  1223.     fgets(pInfo->name.last, 21, stdin);
  1224.     printf("Enter address: ");
  1225.     fgets (pInfo->address, 51, stdin);
  1226.    
  1227.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  1228.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  1229.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  1230.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  1231.    
  1232.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  1233. }
  1234.  
  1235. /*
  1236. FUNCTION: checkUnique is to verify if the username is unique or not
  1237. @param: string15 uname: chosen username of new user
  1238. @param: userNode *user: structure with the user information
  1239. @return: int: 0 if not unique, 1 if unique
  1240. */
  1241. int checkUnique (string15 uname, userNode *user)
  1242. {
  1243.     int unique=1;
  1244.     userNode *temp=NULL;
  1245.    
  1246.     temp=user;
  1247.    
  1248.     while(temp!=NULL)
  1249.     {
  1250.         if (strcmp(temp->username, uname)==0)
  1251.         {
  1252.             unique=0;
  1253.             temp=NULL;
  1254.         }
  1255.         else
  1256.             temp=temp->next;
  1257.     }
  1258.    
  1259.     return unique;
  1260. }
  1261.  
  1262. /*
  1263. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  1264.          list of users
  1265. @param: userNode *user: structure which contains infomation of all users
  1266. @param: userNode *pNew: structure which contains information of the current user
  1267. @return: void
  1268. */
  1269. void signUp (userNode *user, userNode *pNew)
  1270. {
  1271.     int go=0, chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  1272.     char cDump;
  1273.     /* for authorization code */
  1274.     string8 ac;
  1275.     string15 uname, pword;
  1276.  
  1277.     if (user==NULL)
  1278.     {
  1279.         /* first account to sign up */
  1280.         do
  1281.         {
  1282.             printf("=== SIGN UP ===\n");
  1283.             printf("Enter username: ");
  1284.             scanf("%s%c", uname, &cDump);
  1285.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  1286.        
  1287.         strcpy (pNew->username, uname);
  1288.     }
  1289.        
  1290.     else
  1291.     {
  1292.         /* second account and onwards to sign up */
  1293.         do
  1294.         {
  1295.             printf("=== SIGN UP ===\n");
  1296.             printf("Enter username: ");
  1297.             scanf("%s%c", uname, &cDump);
  1298.            
  1299.             if (strlen(uname) >=3 && strlen(uname)<=15)
  1300.             {
  1301.                 compare=checkUnique(uname, user);
  1302.                
  1303.                 if (compare==1)
  1304.                 {
  1305.                     accept=1;
  1306.                     strcpy (pNew->username, uname);
  1307.                 }
  1308.                
  1309.                 else if (compare!=1)
  1310.                 {
  1311.                     accept=0;
  1312.                     printf("Username unavailable. Try again.\n");
  1313.                 }  
  1314.             }
  1315.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  1316.     }
  1317.                                
  1318.     printf("Username is: %s\n", pNew->username);
  1319.    
  1320.     accept=0;
  1321.        
  1322.     do
  1323.     {
  1324.         printf("Enter password: ");
  1325.         scanf("%s", pword);
  1326.        
  1327.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  1328.         {
  1329.             for (i=0; i<strlen(pword); i++)
  1330.             {
  1331.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1332.                 {
  1333.                     accept=1;
  1334.                     chk=1;
  1335.                     strcpy (pNew->password, pword);
  1336.                 }
  1337.             }
  1338.            
  1339.             if (accept!=1)
  1340.                 printf("Invalid password. Input at least 1 non-letter.\n");
  1341.         }
  1342.         else
  1343.             accept=0;
  1344.                    
  1345.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1346.    
  1347.     printf("Password is: %s\n", pNew->password);
  1348.    
  1349.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  1350.  
  1351.     /* to ask the user to ask which type of account (s)he prefers to make */
  1352.     do
  1353.     {
  1354.         printf("\nChoose your type of account: \n");
  1355.         printf("1. Administrator\n");
  1356.         printf("2. Shopper\n");
  1357.         printf("Enter choice: ");
  1358.         scanf("%d", &choice);
  1359.                
  1360.         while (choice!=1 && choice!=2)
  1361.         {
  1362.             printf("Invalid choice. Try again.\n");
  1363.             printf("Choose your type of account: \n");
  1364.             printf("1. Administrator\n");
  1365.             printf("2. Shopper\n");
  1366.             printf("Enter choice: ");
  1367.             scanf("%d", &choice);
  1368.         }
  1369.         pNew->type=choice;
  1370.        
  1371.         switch (pNew->type)
  1372.         {
  1373.             /* if user chooses to be an administrator */
  1374.             case 1: do
  1375.                     {
  1376.                         printf("Input authorization code: ");
  1377.                         scanf("%s", ac);
  1378.                        
  1379.                         /* to check if authorization code is valid */
  1380.                         if (strcmp(ac, "DLSU2017")==0)
  1381.                             printf("\nHello New Admin!\n");
  1382.  
  1383.                         else
  1384.                         {
  1385.                             printf("\nSorry, invalid authorization code.\n");
  1386.                             printf("1. Try again\n");
  1387.                             printf("2. Change account type\n");
  1388.                             printf("Enter choice: ");
  1389.                             scanf("%d", &change);
  1390.                            
  1391.                             while (change!=1 && change!=2)
  1392.                                 printf("\nInvalid input!\n");
  1393.                                                        
  1394.                             switch (change)
  1395.                             {
  1396.                                 case 1: close=1;
  1397.                                         break;
  1398.                                 case 2: close=0;
  1399.                                         break;
  1400.                             }
  1401.                         }
  1402.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1403.                     break; 
  1404.            
  1405.             /* if user chooses to be a shopper */
  1406.             case 2: printf("\nHello Shopper!\n\n");
  1407.                     pNew->creditLimit=5000.00;
  1408.                     pNew->outstanding=0.00;
  1409.                     pNew->nItems=0;
  1410.                     pNew->isLocked=0;
  1411.                    
  1412.                     /* to tell the user more information about his/her account */
  1413.                     printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1414.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1415.                     printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1416.                     break;
  1417.                    
  1418.             default: printf("Invalid input!\n");
  1419.         }
  1420.     } while (close==0 && change==2);
  1421.    
  1422.     /* recap for the user */       
  1423.     printf("\n === REMEMBER ===\n");
  1424.     printf("Username is: %s\n", pNew->username);
  1425.     printf("Password is: %s\n\n", pNew->password);
  1426. }
  1427.  
  1428. /*
  1429. FUNCTION: displayAllRecur displays all the usernames available
  1430. @param: userNode *user: structure which contains infomation of all users
  1431. @return: void
  1432. */
  1433. void displayAllRecur (userNode *user)
  1434. {
  1435.     if (user!=NULL)
  1436.     {
  1437.         printf("%s \n", user->username);
  1438.         user=user->next;
  1439.     }
  1440. }
  1441.  
  1442. /*
  1443. FUNCTION: freeAll frees all the data from the user structures
  1444. @param: userNode **user: structure containing the data of the all users
  1445. @return: void
  1446. */
  1447. void freeAll (userNode **user)
  1448. {
  1449.     userNode *pDel;
  1450.    
  1451.     pDel=NULL;
  1452.    
  1453.     while (user!=NULL)
  1454.     {
  1455.         pDel=*user;
  1456.         *user=(*user)->next;
  1457.         free(pDel);
  1458.     }
  1459. }
  1460.  
  1461. /*
  1462. FUNCTION: freeAllStocks frees all the data from the stock structures
  1463. @param: stockNode **pStocks: structure containing the data of the stocks
  1464. @return: void
  1465. */
  1466. void freeAllStocks (stockNode **pStocks)
  1467. {
  1468.     stockNode *pDel;
  1469.    
  1470.     pDel=NULL;
  1471.    
  1472.     while(pStocks!=NULL)
  1473.     {
  1474.         pDel=*pStocks;
  1475.         *pStocks=(*pStocks)->next;
  1476.         free(pDel);
  1477.     }
  1478. }
  1479.  
  1480. int main()
  1481. {
  1482.     int choice=0, close=0, nChoice=0;
  1483.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1484.     categNode *stemp=NULL;
  1485.     stockNode *pStocks;
  1486.    
  1487.     user=NULL;
  1488.     pNew=NULL;
  1489.     pLast=NULL;
  1490.     pRun=NULL;
  1491.     pTrail=NULL;
  1492.     pStocks=NULL;
  1493.    
  1494.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1495.  
  1496.     do
  1497.     {
  1498.         printf("\n=== MAIN MENU ===\n");
  1499.         printf("1. Login\n");
  1500.         printf("2. Sign-up\n");
  1501.         printf("3. Exit\n");
  1502.         printf("Enter choice: ");
  1503.         scanf("%d", &choice);
  1504.         printf("\n");
  1505.  
  1506.         while(choice!=1 && choice!=2 && choice!=3)
  1507.         {
  1508.             fflush(stdin);
  1509.             printf("Try Again!\n");
  1510.             printf("\n=== MAIN MENU ===\n");
  1511.             printf("1. Login\n");
  1512.             printf("2. Sign-up\n");
  1513.             printf("3. Exit\n");
  1514.             printf("Enter choice: ");
  1515.             scanf("%d", &choice);
  1516.             printf("\n");
  1517.         }
  1518.  
  1519.         switch (choice)
  1520.         {
  1521.             case 1:     if (user==NULL)
  1522.                             printf("There are not any users yet! Sign up first.\n");
  1523.                         else
  1524.                         {
  1525.                             printf("You chose to login!\n\n");         
  1526.                             login(user,&stemp);
  1527.                         }
  1528.                         break;
  1529.                        
  1530.             case 2:     printf("You chose to sign-up!\n\n");
  1531.                         do
  1532.                         {
  1533.                             pNew=malloc(sizeof(userNode));
  1534.                             signUp(user, pNew);                                                                    
  1535.                             pNew->next=NULL;
  1536.                                                                                
  1537.                             if (user==NULL)
  1538.                                 user=pNew;     
  1539.                            
  1540.                             else if (strcmp(user->username, pNew->username)>0)
  1541.                             {
  1542.                                 pNew->next=user;
  1543.                                 user=pNew;
  1544.                             }
  1545.                            
  1546.                             else
  1547.                             {
  1548.                                 pRun=user;
  1549.                                 while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1550.                                 {
  1551.                                     pTrail=pRun;
  1552.                                     pRun=pRun->next;
  1553.                                 }                      
  1554.                                 pTrail->next=pNew;
  1555.                                 pNew->next=pRun;
  1556.                             }
  1557.                            
  1558.                             do
  1559.                             {
  1560.                                 printf("Would you like to add another user?\n");
  1561.                                 printf("1. Yes\n");
  1562.                                 printf("2. No\n");
  1563.                                 printf("Enter choice: ");
  1564.                                 scanf("%d", &nChoice);         
  1565.                             } while (nChoice!=1 && nChoice!=2);
  1566.    
  1567.                         } while (nChoice==1);
  1568.                         break;
  1569.                        
  1570.             case 3:     printf("Closing program...\n");
  1571.                         close=1;
  1572.                         break;
  1573.                                            
  1574.             default: printf("Invalid input.\n");
  1575.         }  
  1576.     } while (!close&&choice!=3);
  1577.    
  1578.     if (user!=NULL)
  1579.         freeAll(&user);
  1580.    
  1581.     if (pStocks!=NULL)
  1582.         freeAllStocks (&pStocks);
  1583.        
  1584.     return 0;
  1585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement