Advertisement
Guest User

you can do it alex

a guest
Mar 26th, 2017
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 32.17 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.            
  395.             compare=uniqueProd(prod, stocktemp);
  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.             stocktemp->productInfo.product[strlen(stocktemp->productInfo.product)-1]='\0';
  407.             accept=0;
  408.         }
  409.            
  410.         do
  411.         {
  412.             printf("Enter Quantity Available: ");
  413.             fflush(stdin);
  414.             scanf("%d", &avbl);
  415.             if (avbl>0)
  416.                 accept=1;
  417.             else
  418.                 printf("Invalid input. Try again!\n");
  419.         } while (accept!=1);
  420.        
  421.         if (accept==1)
  422.         {
  423.             stocktemp->productInfo.avbl=avbl;
  424.             accept=0;
  425.         }
  426.        
  427.         do
  428.         {
  429.             printf("Enter Purchase Price: ");
  430.             fflush(stdin);
  431.             scanf("%f", &purchasePrice);
  432.             if (purchasePrice>0)
  433.                 accept=1;
  434.             else
  435.                 printf("Invalid input. Try again!\n");
  436.         } while (accept!=1);
  437.        
  438.         if (accept==1)
  439.         {
  440.             stocktemp->productInfo.purchasePrice=purchasePrice;
  441.             accept=0;
  442.         }
  443.            
  444.         do
  445.         {
  446.             printf("Enter Unit Selling Price: ");
  447.             fflush(stdin);
  448.             scanf("%f", &sellingPrice);
  449.             if (sellingPrice>0)
  450.                 accept=1;
  451.             else
  452.                 printf("Invalid input. Try again!\n");
  453.         } while (accept!=1);
  454.        
  455.         if (accept==1)
  456.         {
  457.             stocktemp->productInfo.sellingPrice=sellingPrice;
  458.             accept=0;
  459.         }
  460.        
  461.         do
  462.         {
  463.             printf("Enter Discount Rate: ");
  464.             fflush(stdin);
  465.             scanf("%f", &discountRate);
  466.             if (discountRate>0)
  467.                 accept=1;
  468.             else
  469.                 printf("Invalid input. Try again!\n");
  470.         } while (accept!=1);
  471.        
  472.         if (accept==1)
  473.         {
  474.             stocktemp->productInfo.discountRate=discountRate;
  475.             accept=0;
  476.         }
  477.     }
  478.    
  479.     strcpy(newcat, addCateg);
  480.     for(i=0; i<strlen(newcat); i++)
  481.     {
  482.         if (newcat[i]>='a' && newcat[i]<='z')
  483.             newcat[i]-=32;
  484.     }
  485.  
  486.     strcpy(newsup, supplier);
  487.     for(i=0; i<strlen(newsup); i++)
  488.     {
  489.         if (newsup[i]>='a' && newsup[i]<='z')
  490.             newsup[i]-=32;
  491.     }
  492.  
  493.     strcpy(newprod, prod);
  494.     for(i=0; i<strlen(newprod); i++)
  495.     {
  496.         if (newprod[i]>='a' && newprod[i]<='z')
  497.             newprod[i]-=32;
  498.     }
  499.  
  500.     stocktemp->productInfo.sold=0;
  501.     stocktemp->productInfo.pCode[0]=newcat[0];
  502.     stocktemp->productInfo.pCode[1]=newsup[0];
  503.     stocktemp->productInfo.pCode[2]=newprod[0];
  504.    
  505.     i=0;
  506.    
  507.     while (i<5)
  508.     {
  509.         rNum=rand()%58;
  510.         if(rNum>47 && rNum<57)
  511.         {
  512.             stocktemp->productInfo.pCode[(i+3)]=rNum;
  513.             i++;
  514.         }
  515.         stocktemp->productInfo.pCode[8]='\0';
  516.     }
  517.     if(cChoice!=2){
  518.         printf("Product code: %s\n", stocktemp->productInfo.pCode);
  519.         stocktemp->next = *stemp;
  520.         *stemp = stocktemp;
  521.     }
  522.    
  523. }
  524.  
  525.  
  526. void viewAllStocks (stockNode *stockInfo, int *stockctr, categNode *stemp)
  527. {
  528.     int i;
  529.     int ctr=0;
  530.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  531.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  532.     while (stemp!=NULL)
  533.     {
  534.         if(ctr!=20)
  535.         {  
  536.             printf("\n");
  537.             printf("%s", stemp->category);
  538.             if(strlen(stemp->category)<15)
  539.                 for(i=strlen(stemp->category);i<15;i++)
  540.                     printf(" ");
  541.             printf("|%s", stemp->productInfo.supplier);
  542.             if(strlen(stemp->productInfo.supplier)<15)
  543.                 for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  544.                     printf(" ");
  545.             printf("|%s", stemp->productInfo.pCode);
  546.             printf("|%s", stemp->productInfo.product);
  547.             if(strlen(stemp->productInfo.product)<10)
  548.                 for(i=strlen(stemp->productInfo.product);i<10;i++)
  549.                     printf(" ");
  550.             printf("|%d", stemp->productInfo.avbl);
  551.             if(stemp->productInfo.avbl<10)
  552.                 for(i=0;i<3;i++)
  553.                     printf(" ");
  554.                 else if(stemp->productInfo.avbl<100)
  555.                     for(i=0;i<2;i++)
  556.                         printf(" ");
  557.                     else if(stemp->productInfo.avbl<1000)
  558.                         for(i=0;i<1;i++)
  559.                             printf(" ");
  560.             printf("|%d", stemp->productInfo.sold);
  561.             if(stemp->productInfo.sold<10)
  562.                 for(i=0;i<3;i++)
  563.                     printf(" ");
  564.                 else if(stemp->productInfo.sold<100)
  565.                     for(i=0;i<2;i++)
  566.                         printf(" ");
  567.                     else if(stemp->productInfo.sold<1000)
  568.                         for(i=0;i<1;i++)
  569.                             printf(" ");
  570.             printf("|%.2f", stemp->productInfo.purchasePrice);
  571.             printf("|%.2f", stemp->productInfo.sellingPrice);
  572.             printf("|%.1f", stemp->productInfo.discountRate);
  573.             printf("\n");
  574.             ctr++;
  575.         }else{
  576.             printf("\nPress Enter to continue...\n");
  577.             fflush(stdin);
  578.             while(getchar() != '\n');  
  579.             ctr = 0;
  580.         }
  581.         stemp = stemp->next;
  582.     }
  583.     printf("\nPress Enter to continue...\n");
  584.     fflush(stdin);
  585.     while(getchar() != '\n');  
  586. }
  587.  
  588. void viewStockCategory (stockNode *stockInfo, int *stockctr, categNode *stemp)
  589. {
  590.     int i,found=0;
  591.     categNode *temp;
  592.     string15 categ;
  593.     int ctr=0;
  594.         printf("Enter category: ");
  595.         fflush(stdin);
  596.         scanf("%s",categ);
  597.     for(temp=stemp;temp!=NULL&&!found;temp=temp->next){
  598.         if(strcmp(temp->category,categ)==0)
  599.             found = 1;
  600.         }
  601.     if(found){
  602.       printf("\n   CATEGORY    |   SUPPLIER    |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  603.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  604.             while(stemp!=NULL){
  605.                 if(ctr!=20)
  606.                 {          
  607.                 if(strcmp(stemp->category,categ)==0){
  608.                     printf("\n");
  609.                     printf("%s", stemp->category);
  610.                     if(strlen(stemp->category)<15)
  611.                         for(i=strlen(stemp->category);i<15;i++)
  612.                             printf(" ");
  613.                     printf("|%s", stemp->productInfo.supplier);
  614.                     if(strlen(stemp->productInfo.supplier)<15)
  615.                         for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  616.                                 printf(" ");
  617.                     printf("|%s", stemp->productInfo.pCode);
  618.                     printf("|%s", stemp->productInfo.product);
  619.                     if(strlen(stemp->productInfo.product)<10)
  620.                         for(i=strlen(stemp->productInfo.product);i<10;i++)
  621.                             printf(" ");
  622.                     printf("|%d", stemp->productInfo.avbl);
  623.                     if(stemp->productInfo.avbl<10)
  624.                         for(i=0;i<3;i++)
  625.                             printf(" ");
  626.                         else if(stemp->productInfo.avbl<100)
  627.                             for(i=0;i<2;i++)
  628.                                 printf(" ");
  629.                             else if(stemp->productInfo.avbl<1000)
  630.                                 for(i=0;i<1;i++)
  631.                                     printf(" ");
  632.                     printf("|%d", stemp->productInfo.sold);
  633.                     if(stemp->productInfo.sold<10)
  634.                         for(i=0;i<3;i++)
  635.                             printf(" ");
  636.                         else if(stemp->productInfo.sold<100)
  637.                             for(i=0;i<2;i++)
  638.                                 printf(" ");
  639.                             else if(stemp->productInfo.sold<1000)
  640.                                 for(i=0;i<1;i++)
  641.                                     printf(" ");
  642.                     printf("|%.2f", stemp->productInfo.purchasePrice);
  643.                     printf("|%.2f", stemp->productInfo.sellingPrice);
  644.                     printf("|%.1f", stemp->productInfo.discountRate);
  645.                     printf("\n");
  646.                     ctr++;
  647.                     }  
  648.                 }else{
  649.                     printf("\nPress Enter to continue...\n");
  650.                     fflush(stdin);
  651.                     while(getchar() != '\n');  
  652.                     ctr = 0;
  653.                 }
  654.                 stemp = stemp->next;           
  655.             }
  656.         printf("\nPress Enter to continue...\n");
  657.         fflush(stdin);
  658.         while(getchar() != '\n');
  659.     }else{
  660.         printf("\nCategory not found.\n");
  661.         printf("\nPress Enter to continue...\n");
  662.         fflush(stdin);
  663.         while(getchar() != '\n');
  664.     }
  665. }
  666.  
  667. /*
  668. void modStockInfo ()
  669. {
  670.    
  671. }
  672.  
  673. void restock ()
  674. {
  675.    
  676. }*/
  677.  
  678.  
  679. /*
  680. FUNCTION: manageStocks asks the admin what they want to do with all the stock information
  681. @param: stockNode *stockInfo: structure with stock information
  682. @param: int *stockctr: to check the number of categories, which but be max of 10
  683. @param: categNode *stemp: structure with the category information
  684. @return: void
  685. */
  686. void manageStocks (stockNode *stockInfo, int *stockctr,categNode *stemp)
  687. {
  688.     int aChoice=0;
  689.     do
  690.     {
  691.         printf("\n === MANAGE STOCKS MENU === \n");
  692.         printf("1. Add New Stock\n");
  693.         printf("2. View All Stocks\n");
  694.         printf("3. View Stocks by Category (PHASE 2)\n");
  695.         printf("4. View Stocks to Reorder (PHASE 2)\n");
  696.         printf("5. Modify Stock Info\n");
  697.         printf("6. Restock\n");
  698.         printf("7. Save Inventory (PHASE 2)\n");
  699.         printf("8. Update Inventory from File (PHASE 2)\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);
  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 ()
  862. {
  863.    
  864. }
  865.  
  866. /*
  867. FUNCTION: shopperMenu displays when a shopper logs in successfully
  868.           It then asks the shopper what he/she wants to do
  869. @param: userNode *user: structure with the user information
  870. @param: stockNode *stockInfo: structure with stock information
  871. @param: int *stockctr: to check the number of categories, which but be max of 10
  872. @return: void
  873. */
  874. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr)
  875. {
  876.     int sChoice=0;
  877.    
  878.     do
  879.     {
  880.         printf("\n === SHOPPER MENU === \n");
  881.         printf("1. Modify User Info\n");
  882.         printf("2. Browse All Products\n");
  883.         printf("3. Browse Products by Category\n");
  884.         printf("4. Browse Products on Sale\n");
  885.         printf("5. Add to Cart\n");
  886.         printf("6. View Cart\n");
  887.         printf("7. Settle Outstanding Balance\n");
  888.         printf("8. Log Out\n");
  889.         printf("Enter choice: ");
  890.         scanf("%d", &sChoice);
  891.        
  892.         switch (sChoice)
  893.         {
  894.             case 1: modUserInfo (user);
  895.                     break;
  896.         //  case 2: browseAllProd ();
  897.                     break;
  898.             case 3: break;
  899.             case 4: break;
  900.             case 5: break;
  901.             case 6: break;
  902.             case 7: break;
  903.             case 8: break;
  904.         }      
  905.     } while (sChoice!=8);
  906. }
  907.  
  908. /*
  909. FUNCTION: login allows the user to log in to a specific account
  910. @param: userNode *user: structure with the user information
  911. @return: void
  912. */
  913. void login (userNode *user)
  914. {
  915.     int pchk=1, compare=2, found=0, close=0, stockctr=0, attempt=0;
  916.     char cDump;
  917.     string15 uname, pword;
  918.     userNode *temp=NULL;
  919.     stockNode *stockInfo=NULL;
  920.     categNode *stemp=NULL;
  921.     temp=user;
  922.    
  923.     printf(" === LOGIN === \n");
  924.     printf("Enter username: ");
  925.     scanf("%s%c", uname, &cDump);
  926.    
  927.     while (found==0 && temp!=NULL)
  928.     {
  929.         compare=strcmp(uname, temp->username);
  930.        
  931.         if (compare==0)
  932.             found=1;
  933.         else
  934.             temp=temp->next;
  935.     }
  936.  
  937.     if (found==1 && temp->isLocked==0) 
  938.     {  
  939.         do
  940.         {
  941.             printf("Enter password: ");
  942.             scanf("%s%c", pword, &cDump);
  943.            
  944.             compare=strcmp(pword, temp->password);
  945.            
  946.             if (compare==0)
  947.             {              
  948.                 switch (temp->type)
  949.                 {
  950.                     case 1:     printf("\nWelcome back, Admin!\n");
  951.                                 close=1;
  952.                                 adminMenu (user, &close, stockInfo, &stockctr,stemp);
  953.                                 break;
  954.                                
  955.                     case 2:     printf("\nWelcome back, Shopper!\n");
  956.                                 close=1;
  957.                                 shopperMenu(user, stockInfo, &stockctr);
  958.                                 break; 
  959.                 }
  960.             }
  961.             else
  962.             {
  963.                 attempt++;
  964.                 printf("Incorrect password. Try again.\n");
  965.                 printf("%d out of 3 tries\n", attempt);
  966.                    
  967.                 if (attempt==3)
  968.                 {
  969.                     user->isLocked=1;
  970.                     printf("\nYou have tried logging in too many times.\n");
  971.                     printf("Please contact the administrator.\n");
  972.                 }
  973.             }
  974.         } while (!close && attempt<=3 && temp->isLocked==0);
  975.     }
  976.     else
  977.         printf("\nSorry, username does not exist.\n");
  978. }
  979.  
  980. /*
  981. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  982.           as well as their address
  983. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  984. @return: void
  985. */
  986. void getUserInfo (userInfoType  *pInfo)
  987. {
  988.     getchar();
  989.     printf("Enter name: \n");
  990.     printf("First name: ");
  991.     fgets(pInfo->name.first, 21, stdin);
  992.     printf("Middle name: ");
  993.     fgets(pInfo->name.middle, 21, stdin);
  994.     printf("Last name: ");
  995.     fgets(pInfo->name.last, 21, stdin);
  996.     printf("Enter address: ");
  997.     fgets (pInfo->address, 51, stdin);
  998.    
  999.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  1000.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  1001.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  1002.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  1003.    
  1004.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  1005. }
  1006.  
  1007. /*
  1008. FUNCTION: checkUnique is to verify if the username is unique or not
  1009. @param: string15 uname: chosen username of new user
  1010. @param: userNode *user: structure with the user information
  1011. @return: int: 0 if not unique, 1 if unique
  1012. */
  1013. int checkUnique (string15 uname, userNode *user)
  1014. {
  1015.     int unique=1;
  1016.     userNode *temp=NULL;
  1017.    
  1018.     temp=user;
  1019.    
  1020.     while(temp!=NULL)
  1021.     {
  1022.         if (strcmp(temp->username, uname)==0)
  1023.         {
  1024.             unique=0;
  1025.             temp=NULL;
  1026.         }
  1027.         else
  1028.             temp=temp->next;
  1029.     }
  1030.    
  1031.     return unique;
  1032. }
  1033.  
  1034. /*
  1035. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  1036.          list of users
  1037. @param: userNode *user: structure which contains infomation of all users
  1038. @param: userNode *pNew: structure which contains information of the current user
  1039. @return: void
  1040. */
  1041. void signUp (userNode *user, userNode *pNew)
  1042. {
  1043.     int go=0, chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  1044.     char cDump;
  1045.     /* for authorization code */
  1046.     string8 ac;
  1047.     string15 uname, pword;
  1048.  
  1049.     if (user==NULL)
  1050.     {
  1051.         /* first account to sign up */
  1052.         do
  1053.         {
  1054.             printf("=== SIGN UP ===\n");
  1055.             printf("Enter username: ");
  1056.             scanf("%s%c", uname, &cDump);
  1057.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  1058.        
  1059.         strcpy (pNew->username, uname);
  1060.     }
  1061.        
  1062.     else
  1063.     {
  1064.         /* second account and onwards to sign up */
  1065.         do
  1066.         {
  1067.             printf("=== SIGN UP ===\n");
  1068.             printf("Enter username: ");
  1069.             scanf("%s%c", uname, &cDump);
  1070.            
  1071.             if (strlen(uname) >=3 && strlen(uname)<=15)
  1072.             {
  1073.                 compare=checkUnique(uname, user);
  1074.                
  1075.                 if (compare==1)
  1076.                 {
  1077.                     accept=1;
  1078.                     strcpy (pNew->username, uname);
  1079.                 }
  1080.                
  1081.                 else if (compare!=1)
  1082.                 {
  1083.                     accept=0;
  1084.                     printf("Username unavailable. Try again.\n");
  1085.                 }  
  1086.             }
  1087.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  1088.     }
  1089.                                
  1090.     printf("Username is: %s\n", pNew->username);
  1091.    
  1092.     accept=0;
  1093.        
  1094.     do
  1095.     {
  1096.         printf("Enter password: ");
  1097.         scanf("%s", pword);
  1098.        
  1099.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  1100.         {
  1101.             for (i=0; i<strlen(pword); i++)
  1102.             {
  1103.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1104.                 {
  1105.                     accept=1;
  1106.                     chk=1;
  1107.                     strcpy (pNew->password, pword);
  1108.                 }
  1109.             }
  1110.            
  1111.             if (accept!=1)
  1112.                 printf("Invalid password. Input at least 1 non-letter.\n");
  1113.         }
  1114.         else
  1115.             accept=0;
  1116.                    
  1117.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1118.    
  1119.     printf("Password is: %s\n", pNew->password);
  1120.    
  1121.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  1122.  
  1123.     /* to ask the user to ask which type of account (s)he prefers to make */
  1124.     do
  1125.     {
  1126.         printf("\nChoose your type of account: \n");
  1127.         printf("1. Administrator\n");
  1128.         printf("2. Shopper\n");
  1129.         printf("Enter choice: ");
  1130.         scanf("%d", &choice);
  1131.                
  1132.         while (choice!=1 && choice!=2)
  1133.         {
  1134.             printf("Invalid choice. Try again.\n");
  1135.             printf("Choose your type of account: \n");
  1136.             printf("1. Administrator\n");
  1137.             printf("2. Shopper\n");
  1138.             printf("Enter choice: ");
  1139.             scanf("%d", &choice);
  1140.         }
  1141.         pNew->type=choice;
  1142.        
  1143.         switch (pNew->type)
  1144.         {
  1145.             /* if user chooses to be an administrator */
  1146.             case 1: do
  1147.                     {
  1148.                         printf("Input authorization code: ");
  1149.                         scanf("%s", ac);
  1150.                        
  1151.                         /* to check if authorization code is valid */
  1152.                         if (strcmp(ac, "DLSU2017")==0)
  1153.                             printf("\nHello New Admin!\n");
  1154.  
  1155.                         else
  1156.                         {
  1157.                             printf("\nSorry, invalid authorization code.\n");
  1158.                             printf("1. Try again\n");
  1159.                             printf("2. Change account type\n");
  1160.                             printf("Enter choice: ");
  1161.                             scanf("%d", &change);
  1162.                            
  1163.                             while (change!=1 && change!=2)
  1164.                                 printf("\nInvalid input!\n");
  1165.                                                        
  1166.                             switch (change)
  1167.                             {
  1168.                                 case 1: close=1;
  1169.                                         break;
  1170.                                 case 2: close=0;
  1171.                                         break;
  1172.                             }
  1173.                         }
  1174.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1175.                     break; 
  1176.            
  1177.             /* if user chooses to be a shopper */
  1178.             case 2: printf("\nHello Shopper!\n\n");
  1179.                     pNew->creditLimit=5000.00;
  1180.                     pNew->outstanding=0.00;
  1181.                     pNew->nItems=0;
  1182.                     pNew->isLocked=0;
  1183.                    
  1184.                     /* to tell the user more information about his/her account */
  1185.                     printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1186.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1187.                     printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1188.                     break;
  1189.                    
  1190.             default: printf("Invalid input!\n");
  1191.         }
  1192.     } while (close==0 && change==2);
  1193.    
  1194.     /* recap for the user */       
  1195.     printf("\n === REMEMBER ===\n");
  1196.     printf("Username is: %s\n", pNew->username);
  1197.     printf("Password is: %s\n\n", pNew->password);
  1198. }
  1199.  
  1200. /*
  1201. FUNCTION: displayAllRecur displays all the usernames available
  1202. @param: userNode *user: structure which contains infomation of all users
  1203. @return: void
  1204. */
  1205. void displayAllRecur (userNode *user)
  1206. {
  1207.     if (user!=NULL)
  1208.     {
  1209.         printf("%s \n", user->username);
  1210.         user=user->next;
  1211.     }
  1212. }
  1213.  
  1214. /*
  1215. FUNCTION: freeAll frees all the data from the user structures
  1216. @param: userNode **user: structure containing the data of the all users
  1217. @return: void
  1218. */
  1219. void freeAll (userNode **user)
  1220. {
  1221.     userNode *pDel;
  1222.    
  1223.     pDel=NULL;
  1224.    
  1225.     while (user!=NULL)
  1226.     {
  1227.         pDel=*user;
  1228.         *user=(*user)->next;
  1229.         free(pDel);
  1230.     }
  1231. }
  1232.  
  1233. /*
  1234. FUNCTION: freeAllStocks frees all the data from the stock structures
  1235. @param: stockNode **pStocks: structure containing the data of the stocks
  1236. @return: void
  1237. */
  1238. void freeAllStocks (stockNode **pStocks)
  1239. {
  1240.     stockNode *pDel;
  1241.    
  1242.     pDel=NULL;
  1243.    
  1244.     while(pStocks!=NULL)
  1245.     {
  1246.         pDel=*pStocks;
  1247.         *pStocks=(*pStocks)->next;
  1248.         free(pDel);
  1249.     }
  1250. }
  1251.  
  1252. int main()
  1253. {
  1254.     int choice=0, close=0, nChoice=0;
  1255.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1256.     stockNode *pStocks;
  1257.    
  1258.     user=NULL;
  1259.     pNew=NULL;
  1260.     pLast=NULL;
  1261.     pRun=NULL;
  1262.     pTrail=NULL;
  1263.     pStocks=NULL;
  1264.    
  1265.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1266.  
  1267.     do
  1268.     {
  1269.         printf("\n=== MAIN MENU ===\n");
  1270.         printf("1. Login\n");
  1271.         printf("2. Sign-up\n");
  1272.         printf("3. Exit\n");
  1273.         printf("Enter choice: ");
  1274.         scanf("%d", &choice);
  1275.         printf("\n");
  1276.  
  1277.         while(choice!=1 && choice!=2 && choice!=3)
  1278.         {
  1279.             fflush(stdin);
  1280.             printf("Try Again!\n");
  1281.             printf("\n=== MAIN MENU ===\n");
  1282.             printf("1. Login\n");
  1283.             printf("2. Sign-up\n");
  1284.             printf("3. Exit\n");
  1285.             printf("Enter choice: ");
  1286.             scanf("%d", &choice);
  1287.             printf("\n");
  1288.         }
  1289.  
  1290.         switch (choice)
  1291.         {
  1292.             case 1:     if (user==NULL)
  1293.                             printf("There are not any users yet! Sign up first.\n");
  1294.                         else
  1295.                         {
  1296.                             printf("You chose to login!\n\n");         
  1297.                             login(user);
  1298.                         }
  1299.                         break;
  1300.                        
  1301.             case 2:     printf("You chose to sign-up!\n\n");
  1302.                         do
  1303.                         {
  1304.                             pNew=malloc(sizeof(userNode));
  1305.                             signUp(user, pNew);                                                                    
  1306.                             pNew->next=NULL;
  1307.                                                                                
  1308.                             if (user==NULL)
  1309.                                 user=pNew;     
  1310.                            
  1311.                             else if (strcmp(user->username, pNew->username)>0)
  1312.                             {
  1313.                                 pNew->next=user;
  1314.                                 user=pNew;
  1315.                             }
  1316.                            
  1317.                             else
  1318.                             {
  1319.                                 pRun=user;
  1320.                                 while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1321.                                 {
  1322.                                     pTrail=pRun;
  1323.                                     pRun=pRun->next;
  1324.                                 }                      
  1325.                                 pTrail->next=pNew;
  1326.                                 pNew->next=pRun;
  1327.                             }
  1328.                            
  1329.                             do
  1330.                             {
  1331.                                 printf("Would you like to add another user?\n");
  1332.                                 printf("1. Yes\n");
  1333.                                 printf("2. No\n");
  1334.                                 printf("Enter choice: ");
  1335.                                 scanf("%d", &nChoice);         
  1336.                             } while (nChoice!=1 && nChoice!=2);
  1337.    
  1338.                         } while (nChoice==1);
  1339.                         break;
  1340.                        
  1341.             case 3:     printf("Closing program...\n");
  1342.                         close=1;
  1343.                         break;
  1344.                                            
  1345.             default: printf("Invalid input.\n");
  1346.         }  
  1347.     } while (!close&&choice!=3);
  1348.    
  1349.     if (user!=NULL)
  1350.         freeAll(&user);
  1351.    
  1352.     if (pStocks!=NULL)
  1353.         freeAllStocks (&pStocks);
  1354.        
  1355.     return 0;
  1356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement