Advertisement
Guest User

you can do it!!!

a guest
Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 32.11 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.     string15 categ;
  592.     int ctr=0;
  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("Enter choice: ");
  700.         fflush(stdin);
  701.         scanf("%d", &aChoice);
  702.        
  703.         switch (aChoice)
  704.         {
  705.             case 1: addNewStock (stockInfo, stockctr, &stemp);
  706.                     break;
  707.             case 2: viewAllStocks (stockInfo, stockctr, stemp);
  708.                     break;
  709.             case 3: viewStockCategory (stockInfo, stockctr, stemp);
  710.                     break;
  711.             case 4: break;
  712.         //  case 5: modStockInfo ();
  713.                     break;
  714.         //  case 6: restock ();
  715.                     break;
  716.             case 7: break;
  717.             case 8: break;
  718.         }
  719.     } while (aChoice>0 && aChoice<9);
  720. }
  721.  
  722. /*
  723. FUNCTION: adminMenu displays only when an administrator is able to log in successfully
  724.           It then asks the admin what (s)he wants to do
  725. @param: userNode *user: structure with the user information
  726. @param: int *close: option to exit
  727. @param: stockNode *stockInfo: structure with stock information
  728. @param: int *stockctr: to check the number of categories, which but be max of 10
  729. @param: categNode *stemp: structure with the category information
  730. @return: void
  731. */
  732. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr, categNode *stemp)
  733. {
  734.     int adChoice=0;
  735.    
  736.     do
  737.     {          
  738.         printf("\n === ADMINISTRATOR MENU === \n");
  739.         printf("1. Manage Accounts Menu\n");
  740.         printf("2. Manage Stocks Menu\n");
  741.         printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  742.         printf("4. Shutdown Kiosk \n");
  743.         printf("5. Log Out \n");
  744.         printf("Enter choice: ");
  745.         scanf("%d", &adChoice);
  746.        
  747.         switch (adChoice)
  748.         {
  749.             case 1: manageAccounts(user);
  750.                     break;
  751.             case 2: manageStocks(stockInfo, stockctr,stemp);
  752.                     break;
  753.             case 3: break;
  754.             case 4: *close=1;
  755.                     break;
  756.             case 5: break;
  757.             default: printf("Invalid input. Try again.\n");
  758.         }  
  759.     } while (adChoice!=4 && adChoice!=5);
  760. }
  761.  
  762. /*
  763. FUNCTION: modUserInfo allows the shopper to change his/her account details
  764. @param: userNode *user: structure about user information
  765. @return: void
  766. */
  767. void modUserInfo (userNode *user)
  768. {
  769.     int uChoice=0, accept=0, i=0, nameChoice=0;
  770.     string15 newpass;
  771.    
  772.     printf("\nModifying User Info:\n");
  773.    
  774.     do
  775.     {
  776.         printf("\nDo you want to...\n");
  777.         printf("1. Change Name\n");
  778.         printf("2. Change Address\n");
  779.         printf("3. Change Password\n");
  780.         printf("4. Return to Shopper Menu\n");
  781.         printf("Enter choice: ");
  782.         scanf("%d", &uChoice);
  783.        
  784.         switch (uChoice)
  785.         {
  786.             case 1: do
  787.                     {
  788.                         printf("\nWhat would you like to change? \n");
  789.                         printf("1. First Name\n");
  790.                         printf("2. Middle Name\n");
  791.                         printf("3. Last Name\n");
  792.                         printf("4. All Names\n");
  793.                         printf("Enter choice: ");
  794.                         scanf("%d", &nameChoice);
  795.                     } while (nameChoice<1 && nameChoice>4);
  796.                    
  797.                     switch (nameChoice)
  798.                     {
  799.                         case 1: printf("Enter New Name: \n");
  800.                                 printf("First name: ");
  801.                                 fflush(stdin);
  802.                                 fgets(user->info.name.first, 21, stdin);
  803.                            
  804.                                 break;
  805.                                
  806.                         case 2: printf("Enter New Name: \n");
  807.                                 printf("Middle name: ");
  808.                                 fflush(stdin);
  809.                                 fgets(user->info.name.middle, 21, stdin);
  810.                                 break;
  811.                        
  812.                         case 3: printf("Enter New Name: \n");
  813.                                 printf("Last name: ");
  814.                                 fflush(stdin);
  815.                                 fgets(user->info.name.last, 21, stdin);
  816.                                 break;
  817.                        
  818.                         case 4: printf("Enter New Name: \n");
  819.                                 printf("First name: ");
  820.                                 fflush(stdin);
  821.                                 fgets(user->info.name.first, 21, stdin);
  822.                                 printf("Middle name: ");
  823.                                 fflush(stdin);
  824.                                 fgets(user->info.name.middle, 21, stdin);
  825.                                 printf("Last name: ");
  826.                                 fflush(stdin);
  827.                                 fgets(user->info.name.last, 21, stdin);
  828.                                 break;                                     
  829.                     }
  830.                     break;
  831.                    
  832.             case 2: printf("Enter New Address: \n");
  833.                     printf("Address: ");
  834.                     fflush(stdin);
  835.                     fgets (user->info.address, 51, stdin);
  836.                     break;
  837.                    
  838.             case 3: do
  839.                     {                  
  840.                         printf("Enter New Password: ");
  841.                         scanf("%s", newpass);
  842.                    
  843.                         for (i=0; i<strlen(newpass); i++)
  844.                         {
  845.                             if (!((newpass[i] >= 'A' && newpass[i] <= 'Z') || (newpass[i] >= 'a' && newpass[i] <= 'z')))
  846.                             {
  847.                                 accept=1;
  848.                                 strcpy (user->password, newpass);
  849.                             }
  850.                         }
  851.                     } while (strlen(newpass) < 6 || strlen(newpass) > 15 || accept!=1);                
  852.                     break;
  853.                    
  854.             case 4: break;
  855.             default: printf("Invalid input. Try again.\n");
  856.         }
  857.     } while (uChoice!=4);
  858. }
  859.  
  860. void browseAllProd ()
  861. {
  862.    
  863. }
  864.  
  865. /*
  866. FUNCTION: shopperMenu displays when a shopper logs in successfully
  867.           It then asks the shopper what he/she wants to do
  868. @param: userNode *user: structure with the user information
  869. @param: stockNode *stockInfo: structure with stock information
  870. @param: int *stockctr: to check the number of categories, which but be max of 10
  871. @return: void
  872. */
  873. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr)
  874. {
  875.     int sChoice=0;
  876.    
  877.     do
  878.     {
  879.         printf("\n === SHOPPER MENU === \n");
  880.         printf("1. Modify User Info\n");
  881.         printf("2. Browse All Products\n");
  882.         printf("3. Browse Products by Category\n");
  883.         printf("4. Browse Products on Sale\n");
  884.         printf("5. Add to Cart\n");
  885.         printf("6. View Cart\n");
  886.         printf("7. Settle Outstanding Balance\n");
  887.         printf("8. Log Out\n");
  888.         printf("Enter choice: ");
  889.         scanf("%d", &sChoice);
  890.        
  891.         switch (sChoice)
  892.         {
  893.             case 1: modUserInfo (user);
  894.                     break;
  895.         //  case 2: browseAllProd ();
  896.                     break;
  897.             case 3: break;
  898.             case 4: break;
  899.             case 5: break;
  900.             case 6: break;
  901.             case 7: break;
  902.             case 8: break;
  903.         }      
  904.     } while (sChoice!=8);
  905. }
  906.  
  907. /*
  908. FUNCTION: login allows the user to log in to a specific account
  909. @param: userNode *user: structure with the user information
  910. @return: void
  911. */
  912. void login (userNode *user)
  913. {
  914.     int pchk=1, compare=2, found=0, close=0, stockctr=0, attempt=0;
  915.     char cDump;
  916.     string15 uname, pword;
  917.     userNode *temp=NULL;
  918.     stockNode *stockInfo=NULL;
  919.     categNode *stemp=NULL;
  920.     temp=user;
  921.    
  922.     printf(" === LOGIN === \n");
  923.     printf("Enter username: ");
  924.     scanf("%s%c", uname, &cDump);
  925.    
  926.     while (found==0 && temp!=NULL)
  927.     {
  928.         compare=strcmp(uname, temp->username);
  929.        
  930.         if (compare==0)
  931.             found=1;
  932.         else
  933.             temp=temp->next;
  934.     }
  935.  
  936.     if (found==1 && temp->isLocked==0) 
  937.     {  
  938.         do
  939.         {
  940.             printf("Enter password: ");
  941.             scanf("%s%c", pword, &cDump);
  942.            
  943.             compare=strcmp(pword, temp->password);
  944.            
  945.             if (compare==0)
  946.             {              
  947.                 switch (temp->type)
  948.                 {
  949.                     case 1:     printf("\nWelcome back, Admin!\n");
  950.                                 close=1;
  951.                                 adminMenu (user, &close, stockInfo, &stockctr,stemp);
  952.                                 break;
  953.                                
  954.                     case 2:     printf("\nWelcome back, Shopper!\n");
  955.                                 close=1;
  956.                                 shopperMenu(user, stockInfo, &stockctr);
  957.                                 break; 
  958.                 }
  959.             }
  960.             else
  961.             {
  962.                 attempt++;
  963.                 printf("Incorrect password. Try again.\n");
  964.                 printf("%d out of 3 tries\n", attempt);
  965.                    
  966.                 if (attempt==3)
  967.                 {
  968.                     user->isLocked=1;
  969.                     printf("\nYou have tried logging in too many times.\n");
  970.                     printf("Please contact the administrator.\n");
  971.                 }
  972.             }
  973.         } while (!close && attempt<=3 && temp->isLocked==0);
  974.     }
  975.     else
  976.         printf("\nSorry, username does not exist.\n");
  977. }
  978.  
  979. /*
  980. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  981.           as well as their address
  982. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  983. @return: void
  984. */
  985. void getUserInfo (userInfoType  *pInfo)
  986. {
  987.     getchar();
  988.     printf("Enter name: \n");
  989.     printf("First name: ");
  990.     fgets(pInfo->name.first, 21, stdin);
  991.     printf("Middle name: ");
  992.     fgets(pInfo->name.middle, 21, stdin);
  993.     printf("Last name: ");
  994.     fgets(pInfo->name.last, 21, stdin);
  995.     printf("Enter address: ");
  996.     fgets (pInfo->address, 51, stdin);
  997.    
  998.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  999.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  1000.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  1001.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  1002.    
  1003.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  1004. }
  1005.  
  1006. /*
  1007. FUNCTION: checkUnique is to verify if the username is unique or not
  1008. @param: string15 uname: chosen username of new user
  1009. @param: userNode *user: structure with the user information
  1010. @return: int: 0 if not unique, 1 if unique
  1011. */
  1012. int checkUnique (string15 uname, userNode *user)
  1013. {
  1014.     int unique=1;
  1015.     userNode *temp=NULL;
  1016.    
  1017.     temp=user;
  1018.    
  1019.     while(temp!=NULL)
  1020.     {
  1021.         if (strcmp(temp->username, uname)==0)
  1022.         {
  1023.             unique=0;
  1024.             temp=NULL;
  1025.         }
  1026.         else
  1027.             temp=temp->next;
  1028.     }
  1029.    
  1030.     return unique;
  1031. }
  1032.  
  1033. /*
  1034. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  1035.          list of users
  1036. @param: userNode *user: structure which contains infomation of all users
  1037. @param: userNode *pNew: structure which contains information of the current user
  1038. @return: void
  1039. */
  1040. void signUp (userNode *user, userNode *pNew)
  1041. {
  1042.     int go=0, chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  1043.     char cDump;
  1044.     /* for authorization code */
  1045.     string8 ac;
  1046.     string15 uname, pword;
  1047.  
  1048.     if (user==NULL)
  1049.     {
  1050.         /* first account to sign up */
  1051.         do
  1052.         {
  1053.             printf("=== SIGN UP ===\n");
  1054.             printf("Enter username: ");
  1055.             scanf("%s%c", uname, &cDump);
  1056.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  1057.        
  1058.         strcpy (pNew->username, uname);
  1059.     }
  1060.        
  1061.     else
  1062.     {
  1063.         /* second account and onwards to sign up */
  1064.         do
  1065.         {
  1066.             printf("=== SIGN UP ===\n");
  1067.             printf("Enter username: ");
  1068.             scanf("%s%c", uname, &cDump);
  1069.            
  1070.             if (strlen(uname) >=3 && strlen(uname)<=15)
  1071.             {
  1072.                 compare=checkUnique(uname, user);
  1073.                
  1074.                 if (compare==1)
  1075.                 {
  1076.                     accept=1;
  1077.                     strcpy (pNew->username, uname);
  1078.                 }
  1079.                
  1080.                 else if (compare!=1)
  1081.                 {
  1082.                     accept=0;
  1083.                     printf("Username unavailable. Try again.\n");
  1084.                 }  
  1085.             }
  1086.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  1087.     }
  1088.                                
  1089.     printf("Username is: %s\n", pNew->username);
  1090.    
  1091.     accept=0;
  1092.        
  1093.     do
  1094.     {
  1095.         printf("Enter password: ");
  1096.         scanf("%s", pword);
  1097.        
  1098.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  1099.         {
  1100.             for (i=0; i<strlen(pword); i++)
  1101.             {
  1102.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1103.                 {
  1104.                     accept=1;
  1105.                     chk=1;
  1106.                     strcpy (pNew->password, pword);
  1107.                 }
  1108.             }
  1109.            
  1110.             if (accept!=1)
  1111.                 printf("Invalid password. Input at least 1 non-letter.\n");
  1112.         }
  1113.         else
  1114.             accept=0;
  1115.                    
  1116.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1117.    
  1118.     printf("Password is: %s\n", pNew->password);
  1119.    
  1120.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  1121.  
  1122.     /* to ask the user to ask which type of account (s)he prefers to make */
  1123.     do
  1124.     {
  1125.         printf("\nChoose your type of account: \n");
  1126.         printf("1. Administrator\n");
  1127.         printf("2. Shopper\n");
  1128.         printf("Enter choice: ");
  1129.         scanf("%d", &choice);
  1130.                
  1131.         while (choice!=1 && choice!=2)
  1132.         {
  1133.             printf("Invalid choice. Try again.\n");
  1134.             printf("Choose your type of account: \n");
  1135.             printf("1. Administrator\n");
  1136.             printf("2. Shopper\n");
  1137.             printf("Enter choice: ");
  1138.             scanf("%d", &choice);
  1139.         }
  1140.         pNew->type=choice;
  1141.        
  1142.         switch (pNew->type)
  1143.         {
  1144.             /* if user chooses to be an administrator */
  1145.             case 1: do
  1146.                     {
  1147.                         printf("Input authorization code: ");
  1148.                         scanf("%s", ac);
  1149.                        
  1150.                         /* to check if authorization code is valid */
  1151.                         if (strcmp(ac, "DLSU2017")==0)
  1152.                             printf("\nHello New Admin!\n");
  1153.  
  1154.                         else
  1155.                         {
  1156.                             printf("\nSorry, invalid authorization code.\n");
  1157.                             printf("1. Try again\n");
  1158.                             printf("2. Change account type\n");
  1159.                             printf("Enter choice: ");
  1160.                             scanf("%d", &change);
  1161.                            
  1162.                             while (change!=1 && change!=2)
  1163.                                 printf("\nInvalid input!\n");
  1164.                                                        
  1165.                             switch (change)
  1166.                             {
  1167.                                 case 1: close=1;
  1168.                                         break;
  1169.                                 case 2: close=0;
  1170.                                         break;
  1171.                             }
  1172.                         }
  1173.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1174.                     break; 
  1175.            
  1176.             /* if user chooses to be a shopper */
  1177.             case 2: printf("\nHello Shopper!\n\n");
  1178.                     pNew->creditLimit=5000.00;
  1179.                     pNew->outstanding=0.00;
  1180.                     pNew->nItems=0;
  1181.                     pNew->isLocked=0;
  1182.                    
  1183.                     /* to tell the user more information about his/her account */
  1184.                     printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1185.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1186.                     printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1187.                     break;
  1188.                    
  1189.             default: printf("Invalid input!\n");
  1190.         }
  1191.     } while (close==0 && change==2);
  1192.    
  1193.     /* recap for the user */       
  1194.     printf("\n === REMEMBER ===\n");
  1195.     printf("Username is: %s\n", pNew->username);
  1196.     printf("Password is: %s\n\n", pNew->password);
  1197. }
  1198.  
  1199. /*
  1200. FUNCTION: displayAllRecur displays all the usernames available
  1201. @param: userNode *user: structure which contains infomation of all users
  1202. @return: void
  1203. */
  1204. void displayAllRecur (userNode *user)
  1205. {
  1206.     if (user!=NULL)
  1207.     {
  1208.         printf("%s \n", user->username);
  1209.         user=user->next;
  1210.     }
  1211. }
  1212.  
  1213. /*
  1214. FUNCTION: freeAll frees all the data from the user structures
  1215. @param: userNode **user: structure containing the data of the all users
  1216. @return: void
  1217. */
  1218. void freeAll (userNode **user)
  1219. {
  1220.     userNode *pDel;
  1221.    
  1222.     pDel=NULL;
  1223.    
  1224.     while (user!=NULL)
  1225.     {
  1226.         pDel=*user;
  1227.         *user=(*user)->next;
  1228.         free(pDel);
  1229.     }
  1230. }
  1231.  
  1232. /*
  1233. FUNCTION: freeAllStocks frees all the data from the stock structures
  1234. @param: stockNode **pStocks: structure containing the data of the stocks
  1235. @return: void
  1236. */
  1237. void freeAllStocks (stockNode **pStocks)
  1238. {
  1239.     stockNode *pDel;
  1240.    
  1241.     pDel=NULL;
  1242.    
  1243.     while(pStocks!=NULL)
  1244.     {
  1245.         pDel=*pStocks;
  1246.         *pStocks=(*pStocks)->next;
  1247.         free(pDel);
  1248.     }
  1249. }
  1250.  
  1251. int main()
  1252. {
  1253.     int choice=0, close=0, nChoice=0;
  1254.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1255.     stockNode *pStocks;
  1256.    
  1257.     user=NULL;
  1258.     pNew=NULL;
  1259.     pLast=NULL;
  1260.     pRun=NULL;
  1261.     pTrail=NULL;
  1262.     pStocks=NULL;
  1263.    
  1264.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1265.  
  1266.     do
  1267.     {
  1268.         printf("\n=== MAIN MENU ===\n");
  1269.         printf("1. Login\n");
  1270.         printf("2. Sign-up\n");
  1271.         printf("3. Exit\n");
  1272.         printf("Enter choice: ");
  1273.         scanf("%d", &choice);
  1274.         printf("\n");
  1275.  
  1276.         while(choice!=1 && choice!=2 && choice!=3)
  1277.         {
  1278.             fflush(stdin);
  1279.             printf("Try Again!\n");
  1280.             printf("\n=== MAIN MENU ===\n");
  1281.             printf("1. Login\n");
  1282.             printf("2. Sign-up\n");
  1283.             printf("3. Exit\n");
  1284.             printf("Enter choice: ");
  1285.             scanf("%d", &choice);
  1286.             printf("\n");
  1287.         }
  1288.  
  1289.         switch (choice)
  1290.         {
  1291.             case 1:     if (user==NULL)
  1292.                             printf("There are not any users yet! Sign up first.\n");
  1293.                         else
  1294.                         {
  1295.                             printf("You chose to login!\n\n");         
  1296.                             login(user);
  1297.                         }
  1298.                         break;
  1299.                        
  1300.             case 2:     printf("You chose to sign-up!\n\n");
  1301.                         do
  1302.                         {
  1303.                             pNew=malloc(sizeof(userNode));
  1304.                             signUp(user, pNew);                                                                    
  1305.                             pNew->next=NULL;
  1306.                                                                                
  1307.                             if (user==NULL)
  1308.                                 user=pNew;     
  1309.                            
  1310.                             else if (strcmp(user->username, pNew->username)>0)
  1311.                             {
  1312.                                 pNew->next=user;
  1313.                                 user=pNew;
  1314.                             }
  1315.                            
  1316.                             else
  1317.                             {
  1318.                                 pRun=user;
  1319.                                 while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1320.                                 {
  1321.                                     pTrail=pRun;
  1322.                                     pRun=pRun->next;
  1323.                                 }                      
  1324.                                 pTrail->next=pNew;
  1325.                                 pNew->next=pRun;
  1326.                             }
  1327.                            
  1328.                             do
  1329.                             {
  1330.                                 printf("Would you like to add another user?\n");
  1331.                                 printf("1. Yes\n");
  1332.                                 printf("2. No\n");
  1333.                                 printf("Enter choice: ");
  1334.                                 scanf("%d", &nChoice);         
  1335.                             } while (nChoice!=1 && nChoice!=2);
  1336.    
  1337.                         } while (nChoice==1);
  1338.                         break;
  1339.                        
  1340.             case 3:     printf("Closing program...\n");
  1341.                         close=1;
  1342.                         break;
  1343.                                            
  1344.             default: printf("Invalid input.\n");
  1345.         }  
  1346.     } while (!close&&choice!=3);
  1347.    
  1348.     if (user!=NULL)
  1349.         freeAll(&user);
  1350.    
  1351.     if (pStocks!=NULL)
  1352.         freeAllStocks (&pStocks);
  1353.        
  1354.     return 0;
  1355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement