Advertisement
Guest User

MP

a guest
Mar 26th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 29.18 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.     srand(time(NULL));
  319.     stocktemp = malloc(sizeof(categNode));
  320.     printf("\nAdding New Stocks: \n");
  321.  
  322.     do
  323.     {
  324.         printf("Would you like to add a new category?\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 categories yet!\n\n");
  335.     } while (cChoice!=1 && (*stockctr)<11);
  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.             if(strlen(addCateg)>=3 && strlen(addCateg)<=15)
  345.             {
  346.                 compare=uniqueCateg(addCateg, stocktemp);
  347.  
  348.                 if (compare==1)
  349.                 {
  350.                     accept=1;
  351.                     strcpy (stocktemp->category, addCateg);
  352.                     stocktemp->category[strlen(stocktemp->category)-1]='\0';
  353.                     (*stockctr)++;
  354.                     printf("You have created a new category! Category #%d\n", *stockctr);
  355.                 }
  356.                 if (compare!=1)
  357.                     printf("Choose a unique category!\n");
  358.             }
  359.             else
  360.                 printf("Enter another category that contains 3-15 characters\n");
  361.             if (*stockctr==10)
  362.                 printf("You have reached the maximum amout of categories!\n");
  363.         } while ((strlen(addCateg)<3 && strlen(addCateg)>15) && accept!=1 && (*stockctr)<11);
  364.     }
  365.    
  366.     accept=0;
  367.     compare=2;
  368.    
  369.     if (cChoice==1 && accept!=1)
  370.     {  
  371.         do
  372.         {
  373.             printf("Enter Supplier: ");
  374.             fflush(stdin);
  375.             fgets(supplier, 16, stdin);
  376.            
  377.             if (strlen(supplier)<16)
  378.                 accept=1;
  379.             else
  380.                 printf("Invalid input. Try again!\n");
  381.         } while (accept!=1);
  382.        
  383.         if (accept==1)
  384.         {
  385.             strcpy(stocktemp->productInfo.supplier, supplier);
  386.             stocktemp->productInfo.supplier[strlen(stocktemp->productInfo.supplier)-1]='\0';
  387.             accept=0;
  388.         }
  389.        
  390.         do
  391.         {
  392.             printf("Enter Product: ");
  393.             fflush(stdin);
  394.             fgets(prod, 16, stdin);
  395.            
  396.             compare=uniqueProd(prod, stocktemp);
  397.            
  398.             if (strlen(prod)<16 && compare==1)
  399.                 accept=1;
  400.             else
  401.                 printf("Invalid input. Try again!\n");
  402.         } while (accept!=1 && compare!=1);
  403.        
  404.         if (accept==1)
  405.         {
  406.             strcpy(stocktemp->productInfo.product, prod);
  407.             stocktemp->productInfo.product[strlen(stocktemp->productInfo.product)-1]='\0';
  408.             accept=0;
  409.         }
  410.            
  411.         do
  412.         {
  413.             printf("Enter Quantity Available: ");
  414.             fflush(stdin);
  415.             scanf("%d", &avbl);
  416.             if (avbl>0)
  417.                 accept=1;
  418.             else
  419.                 printf("Invalid input. Try again!\n");
  420.         } while (accept!=1);
  421.        
  422.         if (accept==1)
  423.         {
  424.             stocktemp->productInfo.avbl=avbl;
  425.             accept=0;
  426.         }
  427.        
  428.         do
  429.         {
  430.             printf("Enter Purchase Price: ");
  431.             fflush(stdin);
  432.             scanf("%f", &purchasePrice);
  433.             if (purchasePrice>0)
  434.                 accept=1;
  435.             else
  436.                 printf("Invalid input. Try again!\n");
  437.         } while (accept!=1);
  438.        
  439.         if (accept==1)
  440.         {
  441.             stocktemp->productInfo.purchasePrice=purchasePrice;
  442.             accept=0;
  443.         }
  444.            
  445.         do
  446.         {
  447.             printf("Enter Unit Selling Price: ");
  448.             fflush(stdin);
  449.             scanf("%f", &sellingPrice);
  450.             if (sellingPrice>0)
  451.                 accept=1;
  452.             else
  453.                 printf("Invalid input. Try again!\n");
  454.         } while (accept!=1);
  455.        
  456.         if (accept==1)
  457.         {
  458.             stocktemp->productInfo.sellingPrice=sellingPrice;
  459.             accept=0;
  460.         }
  461.        
  462.         do
  463.         {
  464.             printf("Enter Discount Rate: ");
  465.             fflush(stdin);
  466.             scanf("%f", &discountRate);
  467.             if (discountRate>0)
  468.                 accept=1;
  469.             else
  470.                 printf("Invalid input. Try again!\n");
  471.         } while (accept!=1);
  472.        
  473.         if (accept==1)
  474.         {
  475.             stocktemp->productInfo.discountRate=discountRate;
  476.             accept=0;
  477.         }
  478.     }
  479.    
  480.     strcpy(newcat, addCateg);
  481.     for(i=0; i<strlen(newcat); i++)
  482.     {
  483.         if (newcat[i]>='a' && newcat[i]<='z')
  484.             newcat[i]-=32;
  485.     }
  486.  
  487.     strcpy(newsup, supplier);
  488.     for(i=0; i<strlen(newsup); i++)
  489.     {
  490.         if (newsup[i]>='a' && newsup[i]<='z')
  491.             newsup[i]-=32;
  492.     }
  493.  
  494.     strcpy(newprod, prod);
  495.     for(i=0; i<strlen(newprod); i++)
  496.     {
  497.         if (newprod[i]>='a' && newprod[i]<='z')
  498.             newprod[i]-=32;
  499.     }
  500.  
  501.     stocktemp->productInfo.sold=0;
  502.     stocktemp->productInfo.pCode[0]=newcat[0];
  503.     stocktemp->productInfo.pCode[1]=newsup[0];
  504.     stocktemp->productInfo.pCode[2]=newprod[0];
  505.    
  506.     i=0;
  507.    
  508.     while (i<5)
  509.     {
  510.         rNum=rand()%58;
  511.         if(rNum>47 && rNum<57)
  512.         {
  513.             stocktemp->productInfo.pCode[(i+3)]=rNum;
  514.             i++;
  515.         }
  516.         stocktemp->productInfo.pCode[8]='\0';
  517.     }
  518.    
  519.     printf("Product code: %s\n", stocktemp->productInfo.pCode);
  520.         stocktemp->next = *stemp;
  521.         *stemp = stocktemp;    
  522. }
  523.  
  524.  
  525. void viewAllStocks (stockNode *stockInfo, int *stockctr, categNode *stemp)
  526. {
  527.     int i;
  528.     int ctr=0;
  529.       printf("\nCATEGORY       |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.             printf("|%d", stemp->productInfo.sold);
  551.             printf("|%.2f", stemp->productInfo.purchasePrice);
  552.             printf("|%.2f", stemp->productInfo.sellingPrice);
  553.             printf("|%.2f", stemp->productInfo.discountRate);
  554.             printf("\n");
  555.             ctr++;
  556.         }else{
  557.             printf("\nPress Enter to continue...\n");
  558.             fflush(stdin);
  559.             while(getchar() != '\n');  
  560.         }
  561.         stemp = stemp->next;
  562.     }
  563.     printf("\nPress Enter to continue...\n");
  564.     fflush(stdin);
  565.     while(getchar() != '\n');  
  566. }
  567. /*
  568. void modStockInfo ()
  569. {
  570.    
  571. }
  572.  
  573. void restock ()
  574. {
  575.    
  576. }*/
  577.  
  578.  
  579. /*
  580. FUNCTION: manageStocks asks the admin what they want to do with all the stock information
  581. @param: stockNode *stockInfo: structure with stock information
  582. @param: int *stockctr: to check the number of categories, which but be max of 10
  583. @param: categNode *stemp: structure with the category information
  584. @return: void
  585. */
  586. void manageStocks (stockNode *stockInfo, int *stockctr,categNode *stemp)
  587. {
  588.     int aChoice=0;
  589.     do
  590.     {
  591.         printf("\n === MANAGE STOCKS MENU === \n");
  592.         printf("1. Add New Stock\n");
  593.         printf("2. View All Stocks\n");
  594.         printf("3. View Stocks by Category (PHASE 2)\n");
  595.         printf("4. View Stocks to Reorder (PHASE 2)\n");
  596.         printf("5. Modify Stock Info\n");
  597.         printf("6. Restock\n");
  598.         printf("7. Save Inventory (PHASE 2)\n");
  599.         printf("8. Update Inventory from File (PHASE 2)\n");
  600.         printf("Enter choice: ");
  601.         fflush(stdin);
  602.         scanf("%d", &aChoice);
  603.        
  604.         switch (aChoice)
  605.         {
  606.             case 1: addNewStock (stockInfo, stockctr, &stemp);
  607.                     break;
  608.             case 2: viewAllStocks (stockInfo, stockctr, stemp);
  609.                     break;
  610.             case 3: break;
  611.             case 4: break;
  612.         //  case 5: modStockInfo ();
  613.                     break;
  614.         //  case 6: restock ();
  615.                     break;
  616.             case 7: break;
  617.             case 8: break;
  618.         }
  619.     } while (aChoice>0 && aChoice<9);
  620. }
  621.  
  622. /*
  623. FUNCTION: adminMenu displays only when an administrator is able to log in successfully
  624.           It then asks the admin what (s)he wants to do
  625. @param: userNode *user: structure with the user information
  626. @param: int *close: option to exit
  627. @param: stockNode *stockInfo: structure with stock information
  628. @param: int *stockctr: to check the number of categories, which but be max of 10
  629. @param: categNode *stemp: structure with the category information
  630. @return: void
  631. */
  632. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr, categNode *stemp)
  633. {
  634.     int adChoice=0;
  635.    
  636.     do
  637.     {          
  638.         printf("\n === ADMINISTRATOR MENU === \n");
  639.         printf("1. Manage Accounts Menu\n");
  640.         printf("2. Manage Stocks Menu\n");
  641.         printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  642.         printf("4. Shutdown Kiosk \n");
  643.         printf("5. Log Out \n");
  644.         printf("Enter choice: ");
  645.         scanf("%d", &adChoice);
  646.        
  647.         switch (adChoice)
  648.         {
  649.             case 1: manageAccounts(user);
  650.                     break;
  651.             case 2: manageStocks(stockInfo, stockctr,stemp);
  652.                     break;
  653.             case 3: break;
  654.             case 4: *close=1;
  655.                     break;
  656.             case 5: break;
  657.             default: printf("Invalid input. Try again.\n");
  658.         }  
  659.     } while (adChoice!=4 && adChoice!=5);
  660. }
  661.  
  662. /*
  663. FUNCTION: modUserInfo allows the shopper to change his/her account details
  664. @param: userNode *user: structure about user information
  665. @return: void
  666. */
  667. void modUserInfo (userNode *user)
  668. {
  669.     int uChoice=0, accept=0, i=0, nameChoice=0;
  670.     string15 newpass;
  671.    
  672.     printf("\nModifying User Info:\n");
  673.    
  674.     do
  675.     {
  676.         printf("\nDo you want to...\n");
  677.         printf("1. Change Name\n");
  678.         printf("2. Change Address\n");
  679.         printf("3. Change Password\n");
  680.         printf("4. Return to Shopper Menu\n");
  681.         printf("Enter choice: ");
  682.         scanf("%d", &uChoice);
  683.        
  684.         switch (uChoice)
  685.         {
  686.             case 1: do
  687.                     {
  688.                         printf("\nWhat would you like to change? \n");
  689.                         printf("1. First Name\n");
  690.                         printf("2. Middle Name\n");
  691.                         printf("3. Last Name\n");
  692.                         printf("4. All Names\n");
  693.                         printf("Enter choice: ");
  694.                         scanf("%d", &nameChoice);
  695.                     } while (nameChoice<1 && nameChoice>4);
  696.                    
  697.                     switch (nameChoice)
  698.                     {
  699.                         case 1: printf("Enter New Name: \n");
  700.                                 printf("First name: ");
  701.                                 fflush(stdin);
  702.                                 fgets(user->info.name.first, 21, stdin);
  703.                            
  704.                                 break;
  705.                                
  706.                         case 2: printf("Enter New Name: \n");
  707.                                 printf("Middle name: ");
  708.                                 fflush(stdin);
  709.                                 fgets(user->info.name.middle, 21, stdin);
  710.                                 break;
  711.                        
  712.                         case 3: printf("Enter New Name: \n");
  713.                                 printf("Last name: ");
  714.                                 fflush(stdin);
  715.                                 fgets(user->info.name.last, 21, stdin);
  716.                                 break;
  717.                        
  718.                         case 4: printf("Enter New Name: \n");
  719.                                 printf("First name: ");
  720.                                 fflush(stdin);
  721.                                 fgets(user->info.name.first, 21, stdin);
  722.                                 printf("Middle name: ");
  723.                                 fflush(stdin);
  724.                                 fgets(user->info.name.middle, 21, stdin);
  725.                                 printf("Last name: ");
  726.                                 fflush(stdin);
  727.                                 fgets(user->info.name.last, 21, stdin);
  728.                                 break;                                     
  729.                     }
  730.                     break;
  731.                    
  732.             case 2: printf("Enter New Address: \n");
  733.                     printf("Address: ");
  734.                     fflush(stdin);
  735.                     fgets (user->info.address, 51, stdin);
  736.                     break;
  737.                    
  738.             case 3: do
  739.                     {                  
  740.                         printf("Enter New Password: ");
  741.                         scanf("%s", newpass);
  742.                    
  743.                         for (i=0; i<strlen(newpass); i++)
  744.                         {
  745.                             if (!((newpass[i] >= 'A' && newpass[i] <= 'Z') || (newpass[i] >= 'a' && newpass[i] <= 'z')))
  746.                             {
  747.                                 accept=1;
  748.                                 strcpy (user->password, newpass);
  749.                             }
  750.                         }
  751.                     } while (strlen(newpass) < 6 || strlen(newpass) > 15 || accept!=1);                
  752.                     break;
  753.                    
  754.             case 4: break;
  755.             default: printf("Invalid input. Try again.\n");
  756.         }
  757.     } while (uChoice!=4);
  758. }
  759.  
  760. void browseAllProd ()
  761. {
  762.    
  763. }
  764.  
  765. /*
  766. FUNCTION: shopperMenu displays when a shopper logs in successfully
  767.           It then asks the shopper what he/she wants to do
  768. @param: userNode *user: structure with the user information
  769. @param: stockNode *stockInfo: structure with stock information
  770. @param: int *stockctr: to check the number of categories, which but be max of 10
  771. @return: void
  772. */
  773. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr)
  774. {
  775.     int sChoice=0;
  776.    
  777.     do
  778.     {
  779.         printf("\n === SHOPPER MENU === \n");
  780.         printf("1. Modify User Info\n");
  781.         printf("2. Browse All Products\n");
  782.         printf("3. Browse Products by Category\n");
  783.         printf("4. Browse Products on Sale\n");
  784.         printf("5. Add to Cart\n");
  785.         printf("6. View Cart\n");
  786.         printf("7. Settle Outstanding Balance\n");
  787.         printf("8. Log Out\n");
  788.         printf("Enter choice: ");
  789.         scanf("%d", &sChoice);
  790.        
  791.         switch (sChoice)
  792.         {
  793.             case 1: modUserInfo (user);
  794.                     break;
  795.         //  case 2: browseAllProd ();
  796.                     break;
  797.             case 3: break;
  798.             case 4: break;
  799.             case 5: break;
  800.             case 6: break;
  801.             case 7: break;
  802.             case 8: break;
  803.         }      
  804.     } while (sChoice!=8);
  805. }
  806.  
  807. /*
  808. FUNCTION: login allows the user to log in to a specific account
  809. @param: userNode *user: structure with the user information
  810. @return: void
  811. */
  812. void login (userNode *user)
  813. {
  814.     int pchk=1, compare=2, found=0, close=0, stockctr=0, attempt=0;
  815.     char cDump;
  816.     string15 uname, pword;
  817.     userNode *temp=NULL;
  818.     stockNode *stockInfo=NULL;
  819.     categNode *stemp=NULL;
  820.     temp=user;
  821.    
  822.     printf(" === LOGIN === \n");
  823.     printf("Enter username: ");
  824.     scanf("%s%c", uname, &cDump);
  825.    
  826.     while (found==0 && temp!=NULL)
  827.     {
  828.         compare=strcmp(uname, temp->username);
  829.        
  830.         if (compare==0)
  831.             found=1;
  832.         else
  833.             temp=temp->next;
  834.     }
  835.  
  836.     if (found==1 && temp->isLocked==0) 
  837.     {  
  838.         do
  839.         {
  840.             printf("Enter password: ");
  841.             scanf("%s%c", pword, &cDump);
  842.            
  843.             compare=strcmp(pword, temp->password);
  844.            
  845.             if (compare==0)
  846.             {              
  847.                 switch (temp->type)
  848.                 {
  849.                     case 1:     printf("\nWelcome back, Admin!\n");
  850.                                 close=1;
  851.                                 adminMenu (user, &close, stockInfo, &stockctr,stemp);
  852.                                 break;
  853.                                
  854.                     case 2:     printf("\nWelcome back, Shopper!\n");
  855.                                 close=1;
  856.                                 shopperMenu(user, stockInfo, &stockctr);
  857.                                 break; 
  858.                 }
  859.             }
  860.             else
  861.             {
  862.                 attempt++;
  863.                 printf("Incorrect password. Try again.\n");
  864.                 printf("%d out of 3 tries\n", attempt);
  865.                    
  866.                 if (attempt==3)
  867.                 {
  868.                     user->isLocked=1;
  869.                     printf("\nYou have tried logging in too many times.\n");
  870.                     printf("Please contact the administrator.\n");
  871.                 }
  872.             }
  873.         } while (!close && attempt<=3 && temp->isLocked==0);
  874.     }
  875.     else
  876.         printf("\nSorry, username does not exist.\n");
  877. }
  878.  
  879. /*
  880. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  881.           as well as their address
  882. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  883. @return: void
  884. */
  885. void getUserInfo (userInfoType  *pInfo)
  886. {
  887.     getchar();
  888.     printf("Enter name: \n");
  889.     printf("First name: ");
  890.     fgets(pInfo->name.first, 21, stdin);
  891.     printf("Middle name: ");
  892.     fgets(pInfo->name.middle, 21, stdin);
  893.     printf("Last name: ");
  894.     fgets(pInfo->name.last, 21, stdin);
  895.     printf("Enter address: ");
  896.     fgets (pInfo->address, 51, stdin);
  897.    
  898.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  899.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  900.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  901.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  902.    
  903.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  904. }
  905.  
  906. /*
  907. FUNCTION: checkUnique is to verify if the username is unique or not
  908. @param: string15 uname: chosen username of new user
  909. @param: userNode *user: structure with the user information
  910. @return: int: 0 if not unique, 1 if unique
  911. */
  912. int checkUnique (string15 uname, userNode *user)
  913. {
  914.     int unique=1;
  915.     userNode *temp=NULL;
  916.    
  917.     temp=user;
  918.    
  919.     while(temp!=NULL)
  920.     {
  921.         if (strcmp(temp->username, uname)==0)
  922.         {
  923.             unique=0;
  924.             temp=NULL;
  925.         }
  926.         else
  927.             temp=temp->next;
  928.     }
  929.    
  930.     return unique;
  931. }
  932.  
  933. /*
  934. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  935.          list of users
  936. @param: userNode *user: structure which contains infomation of all users
  937. @param: userNode *pNew: structure which contains information of the current user
  938. @return: void
  939. */
  940. void signUp (userNode *user, userNode *pNew)
  941. {
  942.     int go=0, chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  943.     char cDump;
  944.     /* for authorization code */
  945.     string8 ac;
  946.     string15 uname, pword;
  947.  
  948.     if (user==NULL)
  949.     {
  950.         /* first account to sign up */
  951.         do
  952.         {
  953.             printf("=== SIGN UP ===\n");
  954.             printf("Enter username: ");
  955.             scanf("%s%c", uname, &cDump);
  956.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  957.        
  958.         strcpy (pNew->username, uname);
  959.     }
  960.        
  961.     else
  962.     {
  963.         /* second account and onwards to sign up */
  964.         do
  965.         {
  966.             printf("=== SIGN UP ===\n");
  967.             printf("Enter username: ");
  968.             scanf("%s%c", uname, &cDump);
  969.            
  970.             if (strlen(uname) >=3 && strlen(uname)<=15)
  971.             {
  972.                 compare=checkUnique(uname, user);
  973.                
  974.                 if (compare==1)
  975.                 {
  976.                     accept=1;
  977.                     strcpy (pNew->username, uname);
  978.                 }
  979.                
  980.                 else if (compare!=1)
  981.                 {
  982.                     accept=0;
  983.                     printf("Username unavailable. Try again.\n");
  984.                 }  
  985.             }
  986.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  987.     }
  988.                                
  989.     printf("Username is: %s\n", pNew->username);
  990.    
  991.     accept=0;
  992.        
  993.     do
  994.     {
  995.         printf("Enter password: ");
  996.         scanf("%s", pword);
  997.        
  998.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  999.         {
  1000.             for (i=0; i<strlen(pword); i++)
  1001.             {
  1002.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1003.                 {
  1004.                     accept=1;
  1005.                     chk=1;
  1006.                     strcpy (pNew->password, pword);
  1007.                 }
  1008.             }
  1009.            
  1010.             if (accept!=1)
  1011.                 printf("Invalid password. Input at least 1 non-letter.\n");
  1012.         }
  1013.         else
  1014.             accept=0;
  1015.                    
  1016.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1017.    
  1018.     printf("Password is: %s\n", pNew->password);
  1019.    
  1020.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  1021.  
  1022.     /* to ask the user to ask which type of account (s)he prefers to make */
  1023.     do
  1024.     {
  1025.         printf("\nChoose your type of account: \n");
  1026.         printf("1. Administrator\n");
  1027.         printf("2. Shopper\n");
  1028.         printf("Enter choice: ");
  1029.         scanf("%d", &choice);
  1030.                
  1031.         while (choice!=1 && choice!=2)
  1032.         {
  1033.             printf("Invalid choice. Try again.\n");
  1034.             printf("Choose your type of account: \n");
  1035.             printf("1. Administrator\n");
  1036.             printf("2. Shopper\n");
  1037.             printf("Enter choice: ");
  1038.             scanf("%d", &choice);
  1039.         }
  1040.         pNew->type=choice;
  1041.        
  1042.         switch (pNew->type)
  1043.         {
  1044.             /* if user chooses to be an administrator */
  1045.             case 1: do
  1046.                     {
  1047.                         printf("Input authorization code: ");
  1048.                         scanf("%s", ac);
  1049.                        
  1050.                         /* to check if authorization code is valid */
  1051.                         if (strcmp(ac, "DLSU2017")==0)
  1052.                             printf("\nHello New Admin!\n");
  1053.  
  1054.                         else
  1055.                         {
  1056.                             printf("\nSorry, invalid authorization code.\n");
  1057.                             printf("1. Try again\n");
  1058.                             printf("2. Change account type\n");
  1059.                             printf("Enter choice: ");
  1060.                             scanf("%d", &change);
  1061.                            
  1062.                             while (change!=1 && change!=2)
  1063.                                 printf("\nInvalid input!\n");
  1064.                                                        
  1065.                             switch (change)
  1066.                             {
  1067.                                 case 1: close=1;
  1068.                                         break;
  1069.                                 case 2: close=0;
  1070.                                         break;
  1071.                             }
  1072.                         }
  1073.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1074.                     break; 
  1075.            
  1076.             /* if user chooses to be a shopper */
  1077.             case 2: printf("\nHello Shopper!\n\n");
  1078.                     pNew->creditLimit=5000.00;
  1079.                     pNew->outstanding=0.00;
  1080.                     pNew->nItems=0;
  1081.                     pNew->isLocked=0;
  1082.                    
  1083.                     /* to tell the user more information about his/her account */
  1084.                     printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1085.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1086.                     printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1087.                     break;
  1088.                    
  1089.             default: printf("Invalid input!\n");
  1090.         }
  1091.     } while (close==0 && change==2);
  1092.    
  1093.     /* recap for the user */       
  1094.     printf("\n === REMEMBER ===\n");
  1095.     printf("Username is: %s\n", pNew->username);
  1096.     printf("Password is: %s\n\n", pNew->password);
  1097. }
  1098.  
  1099. /*
  1100. FUNCTION: displayAllRecur displays all the usernames available
  1101. @param: userNode *user: structure which contains infomation of all users
  1102. @return: void
  1103. */
  1104. void displayAllRecur (userNode *user)
  1105. {
  1106.     if (user!=NULL)
  1107.     {
  1108.         printf("%s \n", user->username);
  1109.         user=user->next;
  1110.     }
  1111. }
  1112.  
  1113. /*
  1114. FUNCTION: freeAll frees all the data from the user structures
  1115. @param: userNode **user: structure containing the data of the all users
  1116. @return: void
  1117. */
  1118. void freeAll (userNode **user)
  1119. {
  1120.     userNode *pDel;
  1121.    
  1122.     pDel=NULL;
  1123.    
  1124.     while (user!=NULL)
  1125.     {
  1126.         pDel=*user;
  1127.         *user=(*user)->next;
  1128.         free(pDel);
  1129.     }
  1130. }
  1131.  
  1132. /*
  1133. FUNCTION: freeAllStocks frees all the data from the stock structures
  1134. @param: stockNode **pStocks: structure containing the data of the stocks
  1135. @return: void
  1136. */
  1137. void freeAllStocks (stockNode **pStocks)
  1138. {
  1139.     stockNode *pDel;
  1140.    
  1141.     pDel=NULL;
  1142.    
  1143.     while(pStocks!=NULL)
  1144.     {
  1145.         pDel=*pStocks;
  1146.         *pStocks=(*pStocks)->next;
  1147.         free(pDel);
  1148.     }
  1149. }
  1150.  
  1151. int main()
  1152. {
  1153.     int choice=0, close=0, nChoice=0;
  1154.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1155.     stockNode *pStocks;
  1156.    
  1157.     user=NULL;
  1158.     pNew=NULL;
  1159.     pLast=NULL;
  1160.     pRun=NULL;
  1161.     pTrail=NULL;
  1162.     pStocks=NULL;
  1163.    
  1164.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1165.  
  1166.     do
  1167.     {
  1168.         printf("\n=== MAIN MENU ===\n");
  1169.         printf("1. Login\n");
  1170.         printf("2. Sign-up\n");
  1171.         printf("3. Exit\n");
  1172.         printf("Enter choice: ");
  1173.         scanf("%d", &choice);
  1174.         printf("\n");
  1175.  
  1176.         while(choice!=1 && choice!=2 && choice!=3)
  1177.         {
  1178.             fflush(stdin);
  1179.             printf("Try Again!\n");
  1180.             printf("\n=== MAIN MENU ===\n");
  1181.             printf("1. Login\n");
  1182.             printf("2. Sign-up\n");
  1183.             printf("3. Exit\n");
  1184.             printf("Enter choice: ");
  1185.             scanf("%d", &choice);
  1186.             printf("\n");
  1187.         }
  1188.  
  1189.         switch (choice)
  1190.         {
  1191.             case 1:     if (user==NULL)
  1192.                             printf("There are not any users yet! Sign up first.\n");
  1193.                         else
  1194.                         {
  1195.                             printf("You chose to login!\n\n");         
  1196.                             login(user);
  1197.                         }
  1198.                         break;
  1199.                        
  1200.             case 2:     printf("You chose to sign-up!\n\n");
  1201.                         do
  1202.                         {
  1203.                             pNew=malloc(sizeof(userNode));
  1204.                             signUp(user, pNew);                                                                    
  1205.                             pNew->next=NULL;
  1206.                                                                                
  1207.                             if (user==NULL)
  1208.                                 user=pNew;     
  1209.                            
  1210.                             else if (strcmp(user->username, pNew->username)>0)
  1211.                             {
  1212.                                 pNew->next=user;
  1213.                                 user=pNew;
  1214.                             }
  1215.                            
  1216.                             else
  1217.                             {
  1218.                                 pRun=user;
  1219.                                 while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1220.                                 {
  1221.                                     pTrail=pRun;
  1222.                                     pRun=pRun->next;
  1223.                                 }                      
  1224.                                 pTrail->next=pNew;
  1225.                                 pNew->next=pRun;
  1226.                             }
  1227.                            
  1228.                             do
  1229.                             {
  1230.                                 printf("Would you like to add another user?\n");
  1231.                                 printf("1. Yes\n");
  1232.                                 printf("2. No\n");
  1233.                                 printf("Enter choice: ");
  1234.                                 scanf("%d", &nChoice);         
  1235.                             } while (nChoice!=1 && nChoice!=2);
  1236.    
  1237.                         } while (nChoice==1);
  1238.                         break;
  1239.                        
  1240.             case 3:     printf("Closing program...\n");
  1241.                         close=1;
  1242.                         break;
  1243.                                            
  1244.             default: printf("Invalid input.\n");
  1245.         }  
  1246.     } while (!close&&choice!=3);
  1247.    
  1248.     if (user!=NULL)
  1249.         freeAll(&user);
  1250.    
  1251.     if (pStocks!=NULL)
  1252.         freeAllStocks (&pStocks);
  1253.        
  1254.     return 0;
  1255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement