Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 29.40 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. #include<conio.h>
  15.  
  16. #define MAX_ITEMS 100
  17.  
  18. typedef char string8[9];
  19. typedef char string15[16];
  20. typedef char string20[21];
  21. typedef char string50[51];
  22.  
  23. /* structures needed for the program */
  24. typedef struct
  25. {
  26.     string20    first,
  27.                 middle,
  28.                 last;
  29. }   nameType;
  30.  
  31. typedef struct
  32. {
  33.     nameType    name;
  34.     string50    address;
  35. }   userInfoType;
  36.  
  37. typedef struct
  38. {
  39.     string8     code;
  40.     int         qty;
  41. } prodBoughtType;
  42.  
  43. typedef prodBoughtType arrBought[MAX_ITEMS];
  44.  
  45. /* linked list about the user */
  46. typedef struct aboutUser
  47. {
  48.     string15        username,
  49.                     password;
  50.     userInfoType    info;
  51.     int             type;           /* administrator or shopper? */
  52.     float           creditLimit,
  53.                     outstanding;
  54.     arrBought       cart;
  55.     int             nItems,
  56.                     isLocked;
  57.     struct          aboutUser *next;
  58. }   userNode;
  59.  
  60. /* linked list about the stocks */
  61. typedef struct aboutStock
  62. {
  63.     string8     pCode;
  64.     string15    supplier,
  65.                 product;
  66.     int         avbl,
  67.                 sold;
  68.     float       purchasePrice,
  69.                 sellingPrice,
  70.                 discountRate;
  71.     struct      aboutStock *next;
  72. } stockNode;
  73.  
  74. /* linked list about the category */
  75. typedef struct categ
  76. {
  77.     string15    category;
  78.     stockNode   productInfo;   
  79.     struct      categ *next;
  80. } categNode;
  81.  
  82. /*
  83. FUNCTION: viewLocked allows the administrator to see all locked accounts
  84. @param: userNode *user: structure with user information
  85. @return: void
  86. */
  87. void viewLocked (userNode *user)
  88. {
  89.     int ctr=1;
  90.     userNode *temp=NULL;
  91.    
  92.     temp=user;
  93.    
  94.     printf("\nView Locked Accounts:\n");
  95.    
  96.     do
  97.     {
  98.         if (temp->isLocked==1 && temp->type==2)
  99.         {
  100.             printf("Account #%d: %s\n", ctr, temp->username);
  101.             printf("Name: %s, %s %s\n", temp->info.name.last, temp->info.name.first, temp->info.name.middle);
  102.         }
  103.         else
  104.         {
  105.             printf("No locked accounts!\n");
  106.             break;
  107.         }
  108.         temp=temp->next;
  109.         ctr++;     
  110.     } while (temp!=NULL);
  111. }
  112.  
  113. /*
  114. FUNCTION: unlockSpecific allows the admin to unlock a specific account by selecting
  115.           which account to unlock
  116. @param: userNode *user: structure with user information
  117. @return: void
  118. */
  119. void unlockSpecific (userNode *user)
  120. {
  121.     string15 iUser;
  122.     userNode *temp=NULL;
  123.     int compare=2, choice=0, close=0;
  124.    
  125.     temp=user;
  126.    
  127.     printf("\nUnlock a specific account: \n");
  128.    
  129.     do
  130.     {
  131.         printf("Enter username of the account you wish to unlock: \n");
  132.         scanf("%s", iUser);
  133.        
  134.         compare=strcmp(iUser, temp->username);
  135.        
  136.         do
  137.         {
  138.             if (temp->isLocked==1 && compare==0)
  139.             {
  140.                 temp->isLocked=0;
  141.                 temp=temp->next;
  142.             }
  143.         } while (temp!=NULL);
  144.        
  145.         printf("Username: %s has been unlocked.\n");
  146.         printf("Would you like to unlock another user? \n");
  147.         printf("1. Yes\n");
  148.         printf("2. No\n");
  149.        
  150.         do
  151.         {
  152.             switch (choice)
  153.             {
  154.                 case 1: close=0;
  155.                         break;
  156.                 case 2: close=1;
  157.                         break;
  158.                 default: printf("Wrong input. Try again.\n");
  159.             }
  160.         } while (choice!=1 && choice!=2);
  161.        
  162.     } while (close==1);
  163. }
  164.  
  165. /*
  166. FUNCTION: unlockAll allows the admin to unlock all the accounts
  167. @param: userNode *user: structure with user information
  168. @return: void
  169. */
  170. void unlockAll (userNode *user)
  171. {
  172.     userNode *temp=NULL;
  173.    
  174.     temp=user;
  175.    
  176.     printf("\nUnlock all accounts: \n");
  177.     printf("Loading...\n");
  178.    
  179.     while (temp!=NULL)
  180.     {
  181.         if (temp->isLocked==1)
  182.             temp->isLocked=0;
  183.            
  184.         temp=temp->next;
  185.     }
  186.    
  187.     printf("All accounts have been unlocked!\n");
  188. }
  189.  
  190. /*
  191. FUNCTION: viewOutBal allows the admin to check which users have outstanding balance
  192. @param: userNode *user: structure with user information
  193. @return: void
  194. */
  195. void viewOutBal (userNode *user)
  196. {
  197.     userNode *temp=NULL;
  198.  
  199.     temp=user;
  200.  
  201.     printf("\nViewAccounts With Outstanding Balance: \n");
  202.    
  203.     do
  204.     {
  205.         if (temp->outstanding <= 0.00)
  206.             printf("Sorry, your outstanding balance is empty!\n");
  207.         else if (temp->outstanding > 0.00)
  208.         {
  209.             printf("Username: %s", temp->username);
  210.             printf("Outstanding Balance is: %.2f\n", temp->outstanding);
  211.         }
  212.         temp=temp->next;
  213.        
  214.     } while (temp!=NULL);
  215. }
  216.  
  217. /*
  218. FUNCTION: manageAccounts displays the admin's options for Manage Account Menu
  219. @param: userNode *user: structure with user information
  220. @return: void
  221. */
  222. void manageAccounts (userNode *user)
  223. {
  224.     int aChoice;
  225.     userNode *temp=NULL;
  226.  
  227.     do
  228.     {      
  229.         printf("\n === MANAGE ACCOUNTS MENU === \n");
  230.         printf("1. View Locked Accounts\n");
  231.         printf("2. Unlock Specific Account\n");
  232.         printf("3. Unlock All Locked Accounts\n");
  233.         printf("4. View Accounts with Outstanding Balance\n");
  234.         printf("5. Return to Administrator Menu\n");
  235.         printf("Enter choice: ");
  236.         scanf("%d", &aChoice);
  237.    
  238.         switch (aChoice)
  239.         {
  240.             case 1: viewLocked (user);
  241.                     break;
  242.             case 2: unlockSpecific (user);
  243.                     break;
  244.             case 3: unlockAll (user);
  245.                     break;
  246.             case 4: viewOutBal (user);
  247.                     break;
  248.             case 5: break;
  249.         }
  250.     } while (aChoice!=5);
  251. }
  252.  
  253. /*
  254. FUNCTION: uniqueCateg checks if the category is unique or not
  255. @param: string15 addCateg: string that contains the name of the new category
  256. @param: categNode *temp: structure with the category information
  257. @return: int
  258. */
  259. int uniqueCateg (string15 addCateg, categNode *temp)
  260. {
  261.     int unique=1;
  262.     categNode *check=NULL;
  263.    
  264.     check=temp;
  265.    
  266.     while(check!=NULL)
  267.     {
  268.         if (strcmp(check->category, addCateg)==0)
  269.         {
  270.             unique=0;
  271.             check=NULL;
  272.         }
  273.         else
  274.             check=check->next;
  275.     }
  276.     return unique;
  277. }
  278.  
  279. /*
  280. FUNCTION: uniqueProd checks if the product is unique or not
  281. @param: string15 prod: string that contains the name of the new product
  282. @param: categNode *temp: structure with the category information
  283. @return: int
  284. */
  285. int uniqueProd (string15 prod, categNode *temp)
  286. {
  287.     int unique=1;
  288.     categNode *check=NULL;
  289.    
  290.     check=temp;
  291.    
  292.     while (check!=NULL)
  293.     {
  294.         if (strcmp(check->productInfo.product, prod)==0)
  295.         {
  296.             unique=0;
  297.             check=NULL;
  298.         }
  299.         else
  300.             check=check->next;
  301.     }
  302.     return unique;
  303. }
  304.  
  305. /*
  306. FUNCTION: addNewStock allows the admin to add a new stock to the current list of stocks
  307.           and goes back to the Manage Stocks Menu after
  308. @param: stockNode *stockInfo: structure with stock information
  309. @param: int *stockctr: to check the number of categories, which but be max of 10
  310. @param: categNode *stemp: structure with the category information
  311. @return: categNode *
  312. */
  313. categNode *addNewStock (stockNode *stockInfo, int *stockctr, categNode *stemp)
  314. {
  315.     string15 addCateg, supplier, prod, newcat, newsup, newprod;
  316.     int cChoice=0, accept=0, compare=2, avbl=0, sold=0, i=0, add=0, rNum=0;
  317.     float purchasePrice=0, sellingPrice=0, discountRate=0;
  318.     categNode *stocktemp;
  319.     srand(time(NULL));
  320.     stocktemp = malloc(sizeof(categNode));
  321.     printf("\nAdding New Stocks: \n");
  322.  
  323.     do
  324.     {
  325.         printf("Would you like to add a new category?\n");
  326.         printf("1. Yes\n");
  327.         printf("2. No\n");
  328.         printf("Enter choice: ");
  329.         fflush(stdin);
  330.         scanf("%d", &cChoice);
  331.        
  332.         if (cChoice!=1 && cChoice!=2)
  333.             printf("Invalid input. Try again\n");
  334.         if ((*stockctr)==0 && cChoice==2)
  335.             printf("\nNo categories yet!\n\n");
  336.     } while (cChoice!=1 && (*stockctr)<11);
  337.    
  338.     while (cChoice==1 && accept!=1 && (*stockctr)<11)
  339.     {
  340.         do
  341.         {
  342.             printf("Enter Category: ");
  343.             fflush(stdin);
  344.             fgets(addCateg, 16, stdin);
  345.             if(strlen(addCateg)>=3 && strlen(addCateg)<=15)
  346.             {
  347.                 compare=uniqueCateg(addCateg, stocktemp);
  348.  
  349.                 if (compare==1)
  350.                 {
  351.                     accept=1;
  352.                     strcpy (stocktemp->category, addCateg);
  353.                     stocktemp->category[strlen(stocktemp->category)-1]='\0';
  354.                     (*stockctr)++;
  355.                     printf("You have created a new category! Category #%d\n", *stockctr);
  356.                 }
  357.                 if (compare!=1)
  358.                     printf("Choose a unique category!\n");
  359.             }
  360.             else
  361.                 printf("Enter another category that contains 3-15 characters\n");
  362.             if (*stockctr==10)
  363.                 printf("You have reached the maximum amout of categories!\n");
  364.         } while ((strlen(addCateg)<3 && strlen(addCateg)>15) && accept!=1 && (*stockctr)<11);
  365.     }
  366.    
  367.     accept=0;
  368.     compare=2;
  369.    
  370.     if (cChoice==1 && accept!=1)
  371.     {  
  372.         do
  373.         {
  374.             printf("Enter Supplier: ");
  375.             fflush(stdin);
  376.             fgets(supplier, 16, stdin);
  377.            
  378.             if (strlen(supplier)<16)
  379.                 accept=1;
  380.             else
  381.                 printf("Invalid input. Try again!\n");
  382.         } while (accept!=1);
  383.        
  384.         if (accept==1)
  385.         {
  386.             strcpy(stocktemp->productInfo.supplier, supplier);
  387.             stocktemp->productInfo.supplier[strlen(stocktemp->productInfo.supplier)-1]='\0';
  388.             accept=0;
  389.         }
  390.        
  391.         do
  392.         {
  393.             printf("Enter Product: ");
  394.             fflush(stdin);
  395.             fgets(prod, 16, stdin);
  396.            
  397.             compare=uniqueProd(prod, stocktemp);
  398.            
  399.             if (strlen(prod)<16 && compare==1)
  400.                 accept=1;
  401.             else
  402.                 printf("Invalid input. Try again!\n");
  403.         } while (accept!=1 && compare!=1);
  404.        
  405.         if (accept==1)
  406.         {
  407.             strcpy(stocktemp->productInfo.product, prod);
  408.             stocktemp->productInfo.product[strlen(stocktemp->productInfo.product)-1]='\0';
  409.             accept=0;
  410.         }
  411.            
  412.         do
  413.         {
  414.             printf("Enter Quantity Available: ");
  415.             fflush(stdin);
  416.             scanf("%d", &avbl);
  417.             if (avbl>0)
  418.                 accept=1;
  419.             else
  420.                 printf("Invalid input. Try again!\n");
  421.         } while (accept!=1);
  422.        
  423.         if (accept==1)
  424.         {
  425.             stocktemp->productInfo.avbl=avbl;
  426.             accept=0;
  427.         }
  428.        
  429.         do
  430.         {
  431.             printf("Enter Purchase Price: ");
  432.             fflush(stdin);
  433.             scanf("%f", &purchasePrice);
  434.             if (purchasePrice>0)
  435.                 accept=1;
  436.             else
  437.                 printf("Invalid input. Try again!\n");
  438.         } while (accept!=1);
  439.        
  440.         if (accept==1)
  441.         {
  442.             stocktemp->productInfo.purchasePrice=purchasePrice;
  443.             accept=0;
  444.         }
  445.            
  446.         do
  447.         {
  448.             printf("Enter Unit Selling Price: ");
  449.             fflush(stdin);
  450.             scanf("%f", &sellingPrice);
  451.             if (sellingPrice>0)
  452.                 accept=1;
  453.             else
  454.                 printf("Invalid input. Try again!\n");
  455.         } while (accept!=1);
  456.        
  457.         if (accept==1)
  458.         {
  459.             stocktemp->productInfo.sellingPrice=sellingPrice;
  460.             accept=0;
  461.         }
  462.        
  463.         do
  464.         {
  465.             printf("Enter Discount Rate: ");
  466.             fflush(stdin);
  467.             scanf("%f", &discountRate);
  468.             if (discountRate>0)
  469.                 accept=1;
  470.             else
  471.                 printf("Invalid input. Try again!\n");
  472.         } while (accept!=1);
  473.        
  474.         if (accept==1)
  475.         {
  476.             stocktemp->productInfo.discountRate=discountRate;
  477.             accept=0;
  478.         }
  479.     }
  480.    
  481.     strcpy(newcat, addCateg);
  482.     for(i=0; i<strlen(newcat); i++)
  483.     {
  484.         if (newcat[i]>='a' && newcat[i]<='z')
  485.             newcat[i]-=32;
  486.     }
  487.  
  488.     strcpy(newsup, supplier);
  489.     for(i=0; i<strlen(newsup); i++)
  490.     {
  491.         if (newsup[i]>='a' && newsup[i]<='z')
  492.             newsup[i]-=32;
  493.     }
  494.  
  495.     strcpy(newprod, prod);
  496.     for(i=0; i<strlen(newprod); i++)
  497.     {
  498.         if (newprod[i]>='a' && newprod[i]<='z')
  499.             newprod[i]-=32;
  500.     }
  501.  
  502.     stocktemp->productInfo.sold=0;
  503.     stocktemp->productInfo.pCode[0]=newcat[0];
  504.     stocktemp->productInfo.pCode[1]=newsup[0];
  505.     stocktemp->productInfo.pCode[2]=newprod[0];
  506.    
  507.     i=0;
  508.    
  509.     while (i<5)
  510.     {
  511.         rNum=rand()%58;
  512.         if(rNum>47 && rNum<57)
  513.         {
  514.             stocktemp->productInfo.pCode[(i+3)]=rNum;
  515.             i++;
  516.         }
  517.         stocktemp->productInfo.pCode[8]='\0';
  518.     }
  519.    
  520.     printf("Product code: %s\n", stocktemp->productInfo.pCode);
  521.         stocktemp->next = stemp;
  522.         stemp = stocktemp;     
  523.         return stemp;
  524. }
  525.  
  526.  
  527. void viewAllStocks (stockNode *stockInfo, int *stockctr, categNode *stemp)
  528. {
  529.     int i;
  530.     int ctr=0;
  531.       printf("\nCATEGORY       |SUPPLIER       |  PROD  |PRODUCT   |QTY |QTY |BUY  |SELL |DISC|\n");
  532.         printf("               |               |  CODE  |          |AVBL|SOLD|PRICE|PRICE|RATE|\n");
  533.     while (stemp!=NULL)
  534.     {
  535.         if(ctr!=20)
  536.         {  
  537.             printf("\n");
  538.             printf("%s", stemp->category);
  539.             if(strlen(stemp->category)<15)
  540.                 for(i=strlen(stemp->category);i<15;i++)
  541.                     printf(" ");
  542.             printf("|%s", stemp->productInfo.supplier);
  543.             if(strlen(stemp->productInfo.supplier)<15)
  544.                 for(i=strlen(stemp->productInfo.supplier);i<15;i++)
  545.                     printf(" ");
  546.             printf("|%s", stemp->productInfo.pCode);
  547.             printf("|%s", stemp->productInfo.product);
  548.             if(strlen(stemp->productInfo.product)<10)
  549.                 for(i=strlen(stemp->productInfo.product);i<10;i++)
  550.                     printf(" ");
  551.             printf("|%d", stemp->productInfo.avbl);
  552.             if(stemp->productInfo.avbl<1000)
  553.                 for(i=stemp->productInfo.avbl;i<10;i++)
  554.                     printf(" ");
  555.             printf("|%d", stemp->productInfo.sold);
  556.             if(strlen(stemp->productInfo.product)<10)
  557.                 for(i=strlen(stemp->productInfo.product);i<10;i++)
  558.                     printf(" ");
  559.             printf("|%.2f", stemp->productInfo.purchasePrice);
  560.             printf("|%.2f", stemp->productInfo.sellingPrice);
  561.             printf("|%.2f", stemp->productInfo.discountRate);
  562.             printf("\n");
  563.             ctr++;
  564.         }else{
  565.             printf("\nPress Enter to continue...\n");
  566.             getch();   
  567.         }
  568.         stemp = stemp->next;
  569.     }
  570.     printf("\nPress Enter to continue...\n");
  571.     getch();
  572. }
  573. /*
  574. void modStockInfo ()
  575. {
  576.    
  577. }
  578.  
  579. void restock ()
  580. {
  581.    
  582. }*/
  583.  
  584.  
  585. /*
  586. FUNCTION: manageStocks asks the admin what they want to do with all the stock information
  587. @param: stockNode *stockInfo: structure with stock information
  588. @param: int *stockctr: to check the number of categories, which but be max of 10
  589. @param: categNode *stemp: structure with the category information
  590. @return: void
  591. */
  592. void manageStocks (stockNode *stockInfo, int *stockctr,categNode *stemp)
  593. {
  594.     int aChoice=0;
  595.     do
  596.     {
  597.         printf("\n === MANAGE STOCKS MENU === \n");
  598.         printf("1. Add New Stock\n");
  599.         printf("2. View All Stocks\n");
  600.         printf("3. View Stocks by Category (PHASE 2)\n");
  601.         printf("4. View Stocks to Reorder (PHASE 2)\n");
  602.         printf("5. Modify Stock Info\n");
  603.         printf("6. Restock\n");
  604.         printf("7. Save Inventory (PHASE 2)\n");
  605.         printf("8. Update Inventory from File (PHASE 2)\n");
  606.         printf("Enter choice: ");
  607.         fflush(stdin);
  608.         scanf("%d", &aChoice);
  609.        
  610.         switch (aChoice)
  611.         {
  612.             case 1: stemp = addNewStock (stockInfo, stockctr, stemp);
  613.                     break;
  614.             case 2: viewAllStocks (stockInfo, stockctr, stemp);
  615.                     break;
  616.             case 3: break;
  617.             case 4: break;
  618.         //  case 5: modStockInfo ();
  619.                     break;
  620.         //  case 6: restock ();
  621.                     break;
  622.             case 7: break;
  623.             case 8: break;
  624.         }
  625.     } while (aChoice>0 && aChoice<9);
  626. }
  627.  
  628. /*
  629. FUNCTION: adminMenu displays only when an administrator is able to log in successfully
  630.           It then asks the admin what (s)he wants to do
  631. @param: userNode *user: structure with the user information
  632. @param: int *close: option to exit
  633. @param: stockNode *stockInfo: structure with stock information
  634. @param: int *stockctr: to check the number of categories, which but be max of 10
  635. @param: categNode *stemp: structure with the category information
  636. @return: void
  637. */
  638. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr, categNode *stemp)
  639. {
  640.     int adChoice=0;
  641.    
  642.     do
  643.     {          
  644.         printf("\n === ADMINISTRATOR MENU === \n");
  645.         printf("1. Manage Accounts Menu\n");
  646.         printf("2. Manage Stocks Menu\n");
  647.         printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  648.         printf("4. Shutdown Kiosk \n");
  649.         printf("5. Log Out \n");
  650.         printf("Enter choice: ");
  651.         scanf("%d", &adChoice);
  652.        
  653.         switch (adChoice)
  654.         {
  655.             case 1: manageAccounts(user);
  656.                     break;
  657.             case 2: manageStocks(stockInfo, stockctr,stemp);
  658.                     break;
  659.             case 3: break;
  660.             case 4: *close=1;
  661.                     break;
  662.             case 5: break;
  663.             default: printf("Invalid input. Try again.\n");
  664.         }  
  665.     } while (adChoice!=4 && adChoice!=5);
  666. }
  667.  
  668. /*
  669. FUNCTION: modUserInfo allows the shopper to change his/her account details
  670. @param: userNode *user: structure about user information
  671. @return: void
  672. */
  673. void modUserInfo (userNode *user)
  674. {
  675.     int uChoice=0, accept=0, i=0, nameChoice=0;
  676.     string15 newpass;
  677.    
  678.     printf("\nModifying User Info:\n");
  679.    
  680.     do
  681.     {
  682.         printf("\nDo you want to...\n");
  683.         printf("1. Change Name\n");
  684.         printf("2. Change Address\n");
  685.         printf("3. Change Password\n");
  686.         printf("4. Return to Shopper Menu\n");
  687.         printf("Enter choice: ");
  688.         scanf("%d", &uChoice);
  689.        
  690.         switch (uChoice)
  691.         {
  692.             case 1: do
  693.                     {
  694.                         printf("\nWhat would you like to change? \n");
  695.                         printf("1. First Name\n");
  696.                         printf("2. Middle Name\n");
  697.                         printf("3. Last Name\n");
  698.                         printf("4. All Names\n");
  699.                         printf("Enter choice: ");
  700.                         scanf("%d", &nameChoice);
  701.                     } while (nameChoice<1 && nameChoice>4);
  702.                    
  703.                     switch (nameChoice)
  704.                     {
  705.                         case 1: printf("Enter New Name: \n");
  706.                                 printf("First name: ");
  707.                                 fflush(stdin);
  708.                                 fgets(user->info.name.first, 21, stdin);
  709.                            
  710.                                 break;
  711.                                
  712.                         case 2: printf("Enter New Name: \n");
  713.                                 printf("Middle name: ");
  714.                                 fflush(stdin);
  715.                                 fgets(user->info.name.middle, 21, stdin);
  716.                                 break;
  717.                        
  718.                         case 3: printf("Enter New Name: \n");
  719.                                 printf("Last name: ");
  720.                                 fflush(stdin);
  721.                                 fgets(user->info.name.last, 21, stdin);
  722.                                 break;
  723.                        
  724.                         case 4: printf("Enter New Name: \n");
  725.                                 printf("First name: ");
  726.                                 fflush(stdin);
  727.                                 fgets(user->info.name.first, 21, stdin);
  728.                                 printf("Middle name: ");
  729.                                 fflush(stdin);
  730.                                 fgets(user->info.name.middle, 21, stdin);
  731.                                 printf("Last name: ");
  732.                                 fflush(stdin);
  733.                                 fgets(user->info.name.last, 21, stdin);
  734.                                 break;                                     
  735.                     }
  736.                     break;
  737.                    
  738.             case 2: printf("Enter New Address: \n");
  739.                     printf("Address: ");
  740.                     fflush(stdin);
  741.                     fgets (user->info.address, 51, stdin);
  742.                     break;
  743.                    
  744.             case 3: do
  745.                     {                  
  746.                         printf("Enter New Password: ");
  747.                         scanf("%s", newpass);
  748.                    
  749.                         for (i=0; i<strlen(newpass); i++)
  750.                         {
  751.                             if (!((newpass[i] >= 'A' && newpass[i] <= 'Z') || (newpass[i] >= 'a' && newpass[i] <= 'z')))
  752.                             {
  753.                                 accept=1;
  754.                                 strcpy (user->password, newpass);
  755.                             }
  756.                         }
  757.                     } while (strlen(newpass) < 6 || strlen(newpass) > 15 || accept!=1);                
  758.                     break;
  759.                    
  760.             case 4: break;
  761.             default: printf("Invalid input. Try again.\n");
  762.         }
  763.     } while (uChoice!=4);
  764. }
  765.  
  766. void browseAllProd ()
  767. {
  768.    
  769. }
  770.  
  771. /*
  772. FUNCTION: shopperMenu displays when a shopper logs in successfully
  773.           It then asks the shopper what he/she wants to do
  774. @param: userNode *user: structure with the user information
  775. @param: stockNode *stockInfo: structure with stock information
  776. @param: int *stockctr: to check the number of categories, which but be max of 10
  777. @return: void
  778. */
  779. void shopperMenu (userNode *user, stockNode *stockInfo, int *stockchtr)
  780. {
  781.     int sChoice=0;
  782.    
  783.     do
  784.     {
  785.         printf("\n === SHOPPER MENU === \n");
  786.         printf("1. Modify User Info\n");
  787.         printf("2. Browse All Products\n");
  788.         printf("3. Browse Products by Category\n");
  789.         printf("4. Browse Products on Sale\n");
  790.         printf("5. Add to Cart\n");
  791.         printf("6. View Cart\n");
  792.         printf("7. Settle Outstanding Balance\n");
  793.         printf("8. Log Out\n");
  794.         printf("Enter choice: ");
  795.         scanf("%d", &sChoice);
  796.        
  797.         switch (sChoice)
  798.         {
  799.             case 1: modUserInfo (user);
  800.                     break;
  801.         //  case 2: browseAllProd ();
  802.                     break;
  803.             case 3: break;
  804.             case 4: break;
  805.             case 5: break;
  806.             case 6: break;
  807.             case 7: break;
  808.             case 8: break;
  809.         }      
  810.     } while (sChoice!=8);
  811. }
  812.  
  813. /*
  814. FUNCTION: login allows the user to log in to a specific account
  815. @param: userNode *user: structure with the user information
  816. @return: void
  817. */
  818. void login (userNode *user)
  819. {
  820.     int pchk=1, compare=2, found=0, close=0, stockctr=0, attempt=0;
  821.     char cDump;
  822.     string15 uname, pword;
  823.     userNode *temp=NULL;
  824.     stockNode *stockInfo=NULL;
  825.     categNode *stemp=NULL;
  826.     temp=user;
  827.    
  828.     printf(" === LOGIN === \n");
  829.     printf("Enter username: ");
  830.     scanf("%s%c", uname, &cDump);
  831.    
  832.     while (found==0 && temp!=NULL)
  833.     {
  834.         compare=strcmp(uname, temp->username);
  835.        
  836.         if (compare==0)
  837.             found=1;
  838.         else
  839.             temp=temp->next;
  840.     }
  841.  
  842.     if (found==1 && temp->isLocked==0) 
  843.     {  
  844.         do
  845.         {
  846.             printf("Enter password: ");
  847.             scanf("%s%c", pword, &cDump);
  848.            
  849.             compare=strcmp(pword, temp->password);
  850.            
  851.             if (compare==0)
  852.             {              
  853.                 switch (temp->type)
  854.                 {
  855.                     case 1:     printf("\nWelcome back, Admin!\n");
  856.                                 close=1;
  857.                                 adminMenu (user, &close, stockInfo, &stockctr,stemp);
  858.                                 break;
  859.                                
  860.                     case 2:     printf("\nWelcome back, Shopper!\n");
  861.                                 close=1;
  862.                                 shopperMenu(user, stockInfo, &stockctr);
  863.                                 break; 
  864.                 }
  865.             }
  866.             else
  867.             {
  868.                 attempt++;
  869.                 printf("Incorrect password. Try again.\n");
  870.                 printf("%d out of 3 tries\n", attempt);
  871.                    
  872.                 if (attempt==3)
  873.                 {
  874.                     user->isLocked=1;
  875.                     printf("\nYou have tried logging in too many times.\n");
  876.                     printf("Please contact the administrator.\n");
  877.                 }
  878.             }
  879.         } while (close==0 && attempt<=3 && temp->isLocked==0);
  880.     }
  881.     else
  882.         printf("\nSorry, username does not exist.\n");
  883. }
  884.  
  885. /*
  886. FUNCTION: getUserInfo asks the user for their name (first, middle, last) information
  887.           as well as their address
  888. @param: userInfoType *pInfo: structure of the data of the current user with contains user information
  889. @return: void
  890. */
  891. void getUserInfo (userInfoType  *pInfo)
  892. {
  893.     getchar();
  894.     printf("Enter name: \n");
  895.     printf("First name: ");
  896.     fgets(pInfo->name.first, 21, stdin);
  897.     printf("Middle name: ");
  898.     fgets(pInfo->name.middle, 21, stdin);
  899.     printf("Last name: ");
  900.     fgets(pInfo->name.last, 21, stdin);
  901.     printf("Enter address: ");
  902.     fgets (pInfo->address, 51, stdin);
  903.    
  904.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  905.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  906.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  907.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  908.    
  909.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  910. }
  911.  
  912. /*
  913. FUNCTION: checkUnique is to verify if the username is unique or not
  914. @param: string15 uname: chosen username of new user
  915. @param: userNode *user: structure with the user information
  916. @return: int: 0 if not unique, 1 if unique
  917. */
  918. int checkUnique (string15 uname, userNode *user)
  919. {
  920.     int unique=1;
  921.     userNode *temp=NULL;
  922.    
  923.     temp=user;
  924.    
  925.     while(temp!=NULL)
  926.     {
  927.         if (strcmp(temp->username, uname)==0)
  928.         {
  929.             unique=0;
  930.             temp=NULL;
  931.         }
  932.         else
  933.             temp=temp->next;
  934.     }
  935.    
  936.     return unique;
  937. }
  938.  
  939. /*
  940. FUNCTION: signUp allows the user to create more accounts and adding it to the current
  941.          list of users
  942. @param: userNode *user: structure which contains infomation of all users
  943. @param: userNode *pNew: structure which contains information of the current user
  944. @return: void
  945. */
  946. void signUp (userNode *user, userNode *pNew)
  947. {
  948.     int go=0, chk=0, i=0, compare=2, accept=0, choice=0, close=1, change=0;
  949.     char cDump;
  950.     /* for authorization code */
  951.     string8 ac;
  952.     string15 uname, pword;
  953.  
  954.     if (user==NULL)
  955.     {
  956.         /* first account to sign up */
  957.         do
  958.         {
  959.             printf("=== SIGN UP ===\n");
  960.             printf("Enter username: ");
  961.             scanf("%s%c", uname, &cDump);
  962.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  963.        
  964.         strcpy (pNew->username, uname);
  965.     }
  966.        
  967.     else
  968.     {
  969.         /* second account and onwards to sign up */
  970.         do
  971.         {
  972.             printf("=== SIGN UP ===\n");
  973.             printf("Enter username: ");
  974.             scanf("%s%c", uname, &cDump);
  975.            
  976.             if (strlen(uname) >=3 && strlen(uname)<=15)
  977.             {
  978.                 compare=checkUnique(uname, user);
  979.                
  980.                 if (compare==1)
  981.                 {
  982.                     accept=1;
  983.                     strcpy (pNew->username, uname);
  984.                 }
  985.                
  986.                 else if (compare!=1)
  987.                 {
  988.                     accept=0;
  989.                     printf("Username unavailable. Try again.\n");
  990.                 }  
  991.             }
  992.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0 || compare!=1);
  993.     }
  994.                                
  995.     printf("Username is: %s\n", pNew->username);
  996.    
  997.     accept=0;
  998.        
  999.     do
  1000.     {
  1001.         printf("Enter password: ");
  1002.         scanf("%s", pword);
  1003.        
  1004.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  1005.         {
  1006.             for (i=0; i<strlen(pword); i++)
  1007.             {
  1008.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  1009.                 {
  1010.                     accept=1;
  1011.                     chk=1;
  1012.                     strcpy (pNew->password, pword);
  1013.                 }
  1014.             }
  1015.            
  1016.             if (accept!=1)
  1017.                 printf("Invalid password. Input at least 1 non-letter.\n");
  1018.         }
  1019.         else
  1020.             accept=0;
  1021.                    
  1022.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  1023.    
  1024.     printf("Password is: %s\n", pNew->password);
  1025.    
  1026.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  1027.  
  1028.     /* to ask the user to ask which type of account (s)he prefers to make */
  1029.     do
  1030.     {
  1031.         printf("\nChoose your type of account: \n");
  1032.         printf("1. Administrator\n");
  1033.         printf("2. Shopper\n");
  1034.         printf("Enter choice: ");
  1035.         scanf("%d", &choice);
  1036.                
  1037.         while (choice!=1 && choice!=2)
  1038.         {
  1039.             printf("Invalid choice. Try again.\n");
  1040.             printf("Choose your type of account: \n");
  1041.             printf("1. Administrator\n");
  1042.             printf("2. Shopper\n");
  1043.             printf("Enter choice: ");
  1044.             scanf("%d", &choice);
  1045.         }
  1046.         pNew->type=choice;
  1047.        
  1048.         switch (pNew->type)
  1049.         {
  1050.             /* if user chooses to be an administrator */
  1051.             case 1: do
  1052.                     {
  1053.                         printf("Input authorization code: ");
  1054.                         scanf("%s", ac);
  1055.                        
  1056.                         /* to check if authorization code is valid */
  1057.                         if (strcmp(ac, "DLSU2017")==0)
  1058.                             printf("\nHello New Admin!\n");
  1059.  
  1060.                         else
  1061.                         {
  1062.                             printf("\nSorry, invalid authorization code.\n");
  1063.                             printf("1. Try again\n");
  1064.                             printf("2. Change account type\n");
  1065.                             printf("Enter choice: ");
  1066.                             scanf("%d", &change);
  1067.                            
  1068.                             while (change!=1 && change!=2)
  1069.                                 printf("\nInvalid input!\n");
  1070.                                                        
  1071.                             switch (change)
  1072.                             {
  1073.                                 case 1: close=1;
  1074.                                         break;
  1075.                                 case 2: close=0;
  1076.                                         break;
  1077.                             }
  1078.                         }
  1079.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  1080.                     break; 
  1081.            
  1082.             /* if user chooses to be a shopper */
  1083.             case 2: printf("\nHello Shopper!\n\n");
  1084.                     pNew->creditLimit=5000.00;
  1085.                     pNew->outstanding=0.00;
  1086.                     pNew->nItems=0;
  1087.                     pNew->isLocked=0;
  1088.                    
  1089.                     /* to tell the user more information about his/her account */
  1090.                     printf("Your Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  1091.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  1092.                     printf("Your Cart currently contains: %d items.\n", pNew->nItems);
  1093.                     break;
  1094.                    
  1095.             default: printf("Invalid input!\n");
  1096.         }
  1097.     } while (close==0 && change==2);
  1098.    
  1099.     /* recap for the user */       
  1100.     printf("\n === REMEMBER ===\n");
  1101.     printf("Username is: %s\n", pNew->username);
  1102.     printf("Password is: %s\n\n", pNew->password);
  1103. }
  1104.  
  1105. /*
  1106. FUNCTION: displayAllRecur displays all the usernames available
  1107. @param: userNode *user: structure which contains infomation of all users
  1108. @return: void
  1109. */
  1110. void displayAllRecur (userNode *user)
  1111. {
  1112.     if (user!=NULL)
  1113.     {
  1114.         printf("%s \n", user->username);
  1115.         user=user->next;
  1116.     }
  1117. }
  1118.  
  1119. /*
  1120. FUNCTION: freeAll frees all the data from the user structures
  1121. @param: userNode **user: structure containing the data of the all users
  1122. @return: void
  1123. */
  1124. void freeAll (userNode **user)
  1125. {
  1126.     userNode *pDel;
  1127.    
  1128.     pDel=NULL;
  1129.    
  1130.     while (user!=NULL)
  1131.     {
  1132.         pDel=*user;
  1133.         *user=(*user)->next;
  1134.         free(pDel);
  1135.     }
  1136. }
  1137.  
  1138. /*
  1139. FUNCTION: freeAllStocks frees all the data from the stock structures
  1140. @param: stockNode **pStocks: structure containing the data of the stocks
  1141. @return: void
  1142. */
  1143. void freeAllStocks (stockNode **pStocks)
  1144. {
  1145.     stockNode *pDel;
  1146.    
  1147.     pDel=NULL;
  1148.    
  1149.     while(pStocks!=NULL)
  1150.     {
  1151.         pDel=*pStocks;
  1152.         *pStocks=(*pStocks)->next;
  1153.         free(pDel);
  1154.     }
  1155. }
  1156.  
  1157. int main()
  1158. {
  1159.     int choice=0, close=0, nChoice=0;
  1160.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  1161.     stockNode *pStocks;
  1162.    
  1163.     user=NULL;
  1164.     pNew=NULL;
  1165.     pLast=NULL;
  1166.     pRun=NULL;
  1167.     pTrail=NULL;
  1168.     pStocks=NULL;
  1169.    
  1170.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n");
  1171.  
  1172.     do
  1173.     {
  1174.         printf("\n=== MAIN MENU ===\n");
  1175.         printf("1. Login\n");
  1176.         printf("2. Sign-up\n");
  1177.         printf("3. Exit\n");
  1178.         printf("Enter choice: ");
  1179.         scanf("%d", &choice);
  1180.         printf("\n");
  1181.  
  1182.         while(choice!=1 && choice!=2 && choice!=3)
  1183.         {
  1184.             fflush(stdin);
  1185.             printf("Try Again!\n");
  1186.             printf("\n=== MAIN MENU ===\n");
  1187.             printf("1. Login\n");
  1188.             printf("2. Sign-up\n");
  1189.             printf("3. Exit\n");
  1190.             printf("Enter choice: ");
  1191.             scanf("%d", &choice);
  1192.             printf("\n");
  1193.         }
  1194.  
  1195.         switch (choice)
  1196.         {
  1197.             case 1:     if (user==NULL)
  1198.                             printf("There are not any users yet! Sign up first.\n");
  1199.                         else
  1200.                         {
  1201.                             printf("You chose to login!\n\n");         
  1202.                             login(user);
  1203.                         }
  1204.                         break;
  1205.                        
  1206.             case 2:     printf("You chose to sign-up!\n\n");
  1207.                         do
  1208.                         {
  1209.                             pNew=malloc(sizeof(userNode));
  1210.                             signUp(user, pNew);                                                                    
  1211.                             pNew->next=NULL;
  1212.                                                                                
  1213.                             if (user==NULL)
  1214.                                 user=pNew;     
  1215.                            
  1216.                             else if (strcmp(user->username, pNew->username)>0)
  1217.                             {
  1218.                                 pNew->next=user;
  1219.                                 user=pNew;
  1220.                             }
  1221.                            
  1222.                             else
  1223.                             {
  1224.                                 pRun=user;
  1225.                                 while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  1226.                                 {
  1227.                                     pTrail=pRun;
  1228.                                     pRun=pRun->next;
  1229.                                 }                      
  1230.                                 pTrail->next=pNew;
  1231.                                 pNew->next=pRun;
  1232.                             }
  1233.                            
  1234.                             do
  1235.                             {
  1236.                                 printf("Would you like to add another user?\n");
  1237.                                 printf("1. Yes\n");
  1238.                                 printf("2. No\n");
  1239.                                 printf("Enter choice: ");
  1240.                                 scanf("%d", &nChoice);         
  1241.                             } while (nChoice!=1 && nChoice!=2);
  1242.    
  1243.                         } while (nChoice==1);
  1244.                         break;
  1245.                        
  1246.             case 3:     printf("Closing program...\n");
  1247.                         close=1;
  1248.                         break;
  1249.                                            
  1250.             default: printf("Invalid input.\n");
  1251.         }  
  1252.     } while (close==0 || nChoice==2 || nChoice==0 && choice!=3);
  1253.    
  1254.     if (user!=NULL)
  1255.         freeAll(&user);
  1256.    
  1257.     if (pStocks!=NULL)
  1258.         freeAllStocks (&pStocks);
  1259.        
  1260.     return 0;
  1261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement