Advertisement
Guest User

w t f

a guest
Mar 12th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.28 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.  
  14. #define MAX_ITEMS 100
  15.  
  16. typedef char string8[9];
  17. typedef char string15[16];
  18. typedef char string20[21];
  19. typedef char string50[51];
  20.  
  21. typedef struct
  22. {
  23.     string20    first,
  24.                 middle,
  25.                 last;
  26. }   nameType;
  27.  
  28. typedef struct
  29. {
  30.     nameType    name;
  31.     string50    address;
  32. }   userInfoType;
  33.  
  34. typedef struct
  35. {
  36.     string8     code;
  37.     int         qty;
  38. } prodBoughtType;
  39.  
  40. typedef prodBoughtType arrBought[MAX_ITEMS];
  41.  
  42. typedef struct aboutUser
  43. {
  44.     string15        username,
  45.                     password;
  46.     userInfoType    info;
  47.     int             type;           /* administrator or shopper? */
  48.     float           creditLimit,
  49.                     outstanding;
  50.     arrBought       cart;
  51.     int             nItems,
  52.                     ctr,
  53.                     isLocked;
  54.     struct          aboutUser *next;
  55. }   userNode;
  56.  
  57. typedef struct aboutStock
  58. {
  59.     string8     pCode;
  60.     string15    category,
  61.                 supplier,
  62.                 product;
  63.     int         avbl,
  64.                 sold;
  65.     float       purchasPrice,
  66.                 sellingPrice,
  67.                 discountRate;
  68.     struct      aboutStock *next;
  69. } stockNode;
  70.  
  71. void viewLocked (userNode *user)
  72. {
  73.     int ctr=0;
  74.     userNode *temp=NULL;
  75.    
  76.     temp=user;
  77.    
  78.     printf("View Locked Accounts:\n");
  79.    
  80.     do
  81.     {
  82.         if (temp->isLocked==1 && temp->type==2)
  83.             printf("Account #%d: %s\n", ctr, temp->username);
  84.         temp=temp->next;       
  85.     } while (temp!=NULL);
  86.    
  87. }
  88.  
  89. void unlockSpecific (userNode *user)
  90. {
  91.     string15 iUser;
  92.     userNode *temp=NULL;
  93.     int compare=2, choice=0, close=0;
  94.    
  95.     temp=user;
  96.    
  97.     printf("Unlock a specific account: \n");
  98.    
  99.     do
  100.     {
  101.        
  102.         printf("Enter username of the account you wish to unlock: \n");
  103.         scanf("%s", iUser);
  104.        
  105.         compare=strcmp(iUser, temp->username);
  106.        
  107.         do
  108.         {
  109.             if (temp->isLocked==1 && compare==0)
  110.             {
  111.                 temp->isLocked=0;
  112.                 temp=temp->next;
  113.             }
  114.         } while (temp!=NULL);
  115.        
  116.         printf("Username: %s has been unlocked.\n");
  117.         printf("Would you like to unlock another user? \n");
  118.         printf("1. Yes\n");
  119.         printf("2. No\n");
  120.        
  121.         do
  122.         {
  123.             switch (choice)
  124.             {
  125.                 case 1: close=0;
  126.                         break;
  127.                 case 2: close=1;
  128.                         break;
  129.                 default: printf("Wrong input. Try again.\n");
  130.             }
  131.         } while (choice!=1 && choice!=2);
  132.        
  133.     } while (close==1);
  134. }
  135.  
  136. void unlockAll (userNode *user)
  137. {
  138.     userNode *temp=NULL;
  139.    
  140.     temp=user;
  141.    
  142.     printf("Unlock all accounts: \n");
  143.     printf("Loading...\n");
  144.    
  145.     do
  146.     {
  147.         if (temp->isLocked==1)
  148.             temp->isLocked=0;
  149.            
  150.         temp=temp->next;   
  151.     } while (temp!=NULL);
  152.    
  153.     printf("All accounts have been unlocked!\n");
  154. }
  155.  
  156. void viewOutBal (userNode *user)
  157. {
  158.     userNode *temp=NULL;
  159.    
  160.     printf("View Outstanding Balance: \n");
  161.    
  162.     do
  163.     {
  164.         if (temp->outstanding <= 0.00)
  165.             printf("Sorry, your outstanding balance is empty!\n");
  166.         else if (temp->outstanding > 0.00)
  167.         {
  168.             printf("Username: %s", temp->username);
  169.             printf("Outstanding Balance is: %.2f\n", temp->outstanding);
  170.         }
  171.         temp=temp->next;
  172.        
  173.     } while (temp!=NULL);
  174. }
  175.  
  176. void manageAccounts (userNode *user)
  177. {
  178.     int aChoice;
  179.     userNode *temp=NULL;
  180.  
  181.     do
  182.     {      
  183.         printf(" === MANAGE ACCOUNTS MENU === \n");
  184.         printf("1. View Locked Accounts\n");
  185.         printf("2. Unlock Specific Account\n");
  186.         printf("3. Unlock All Locked Accounts\n");
  187.         printf("4. View Accounts with Outstanding Balance\n");
  188.         printf("5. Return to Administrator Menu\n");
  189.         scanf("%d", &aChoice);
  190.    
  191.         switch (aChoice)
  192.         {
  193.             case 1: viewLocked (user);
  194.                     break;
  195.             case 2: unlockSpecific (user);
  196.                     break;
  197.             case 3: unlockAll (user);
  198.                     break;
  199.             case 4: viewOutBal (user);
  200.                     break;
  201.             case 5: break;
  202.         }
  203.     } while (aChoice!=5);
  204. }
  205.  
  206. void addNewStock (stockNode *stockInfo, int *stockctr)
  207. {
  208.    
  209.     printf("Add new stocks: \n");
  210.    
  211.    
  212. }
  213.  
  214. void viewAllStocks (stockNode *stockInfo, int *stockctr)
  215. {
  216.    
  217. }
  218.  
  219. void modStockInfo ()
  220. {
  221.    
  222. }
  223.  
  224. void restock ()
  225. {
  226.    
  227. }
  228.  
  229. void manageStocks (stockNode *stockInfo, int *stockctr)
  230. {
  231.     int aChoice=0;
  232.     do
  233.     {
  234.         printf("\n === MANAGE STOCKS MENU === \n");
  235.         printf("1. Add New Stock\n");
  236.         printf("2. View All Stocks\n");
  237.         printf("3. View Stocks by Category (PHASE 2)\n");
  238.         printf("4. View Stocks to Reorder (PHASE 2)\n");
  239.         printf("5. Modify Stock Info\n");
  240.         printf("6. Restock\n");
  241.         printf("7. Save Inventory (PHASE 2)\n");
  242.         printf("8. Update Inventory from File (PHASE 2)\n");
  243.         scanf("%d", &aChoice);
  244.         /*
  245.         switch (aChoice)
  246.         {
  247.             case 1: addNewStock (stockInfo, stockctr);
  248.                     break;
  249.             case 2: viewAllStocks ();
  250.                     break;
  251.             case 3: break;
  252.             case 4: break;
  253.             case 5: modStockInfo ();
  254.                     break;
  255.             case 6: restock ();
  256.                     break;
  257.             case 7: break;
  258.             case 8: break;
  259.         } */
  260.     } while (aChoice>0 && aChoice<9);
  261. }
  262.  
  263. void adminMenu (userNode *user, int *close, stockNode *stockInfo, int *stockctr)
  264. {
  265.     int adChoice=0;
  266.    
  267.     do
  268.     {          
  269.         printf("\n === ADMINISTRATOR MENU === \n");
  270.         printf("1. Manage Accounts Menu\n");
  271.         printf("2. Manage Stocks Menu\n");
  272.         printf("3. Prepare Delivery Receipt (PHASE 2)\n");
  273.         printf("4. Shutdown Kiosk \n");
  274.         printf("5. Log Out \n");
  275.         scanf("%d", &adChoice);
  276.        
  277.         switch (adChoice)
  278.         {
  279.             case 1: manageAccounts(user);
  280.                     break;
  281.             case 2: manageStocks(stockInfo, stockctr);
  282.                     break;
  283.             case 3: break;
  284.             case 4: *close=1;
  285.                     break;
  286.             case 5: break;
  287.             default: printf("Invalid input. Please try again!\n");
  288.         }  
  289.     } while (adChoice!=4 && adChoice!=5);
  290. }
  291.  
  292. void modUserInfo (userNode *user)
  293. {
  294.    
  295. }
  296.  
  297. void browseAllProd ()
  298. {
  299.    
  300. }
  301.  
  302. /*
  303. void shopperMenu (userNode *user, int *close)
  304. {
  305.     int sChoice=0;
  306.    
  307.     do
  308.     {
  309.         printf("\n === SHOPPER MENU === \n");
  310.         printf("1. Modify User Info\n");
  311.         printf("2. Browse All Products\n");
  312.         printf("3. Browse Products by Category\n");
  313.         printf("4. Browse Products on Sale\n");
  314.         printf("5. Add to Cart\n");
  315.         printf("6. View Cart\n");
  316.         printf("7. Settle Outstanding Balance\n");
  317.         printf("8. Log Out\n");
  318.        
  319.         switch (sChoice)
  320.         {
  321.             case 1: modUserInfo ();
  322.                     break;
  323.             case 2: browseAllProd ();
  324.                     break;
  325.             case 3: break;
  326.             case 4: break;
  327.             case 5: break;
  328.             case 6: break;
  329.             case 7: break;
  330.             case 8: break;
  331.         }      
  332.     } while (choice!=8);
  333. } */
  334.  
  335.  
  336. void login (userNode *user)
  337. {
  338.     int pchk=1, compare=2, found=0, close=1, stockctr=0;
  339.     char cDump;
  340.     string15 uname, pword;
  341.     userNode *temp=NULL;
  342.     stockNode *stockInfo=NULL;
  343.    
  344.     temp=user;
  345.    
  346.     if (temp!=NULL)
  347.     {  
  348.         do
  349.         {
  350.             printf(" === LOGIN === \n");
  351.             printf("Enter username: ");
  352.             scanf("%s%c", uname, &cDump);  
  353.                    
  354.             while (temp!=NULL && found==0)
  355.             {
  356.                 compare=strcmp(uname, temp->username);
  357.                 if (compare==0)
  358.                     found=1;
  359.                 else if (compare!=0)
  360.                     temp=temp->next;
  361.             }
  362.    
  363.             while (temp->isLocked==1 && found==0)
  364.             {
  365.                 printf("\nUsername does not exist. Try again.\n\n");
  366.                 printf(" === LOGIN === \n");
  367.                 printf("Enter username: ");
  368.                 scanf("%s%c", uname, &cDump);
  369.             }      
  370.         } while (found!=1);
  371.        
  372.         compare=2;
  373.         found=0;
  374.         temp->ctr=0;
  375.        
  376.         do
  377.         {
  378.             printf("Enter password: ");
  379.             scanf("%s%c", temp->password, &cDump);
  380.            
  381.             compare=strcmp(temp->password, user->password);
  382.            
  383.             if (compare==0)
  384.             {
  385.                 switch (temp->type)
  386.                 {
  387.                     case 1:     printf("Welcome back, Admin!\n");
  388.                                 close=1;
  389.                                 adminMenu (user, &close, stockInfo, &stockctr);
  390.                                 break;
  391.                     case 2:     printf("Welcome back, Shopper!\n");
  392.                                 close=1;
  393.                             //  shopperMenu(user, &close);
  394.                                 break; 
  395.                 }
  396.             }
  397.             else
  398.             {
  399.                 temp->ctr++;
  400.                 printf("Incorrect password. Try again. %1 out of 3 tries\n", temp->ctr);
  401.                 printf("Enter password: ");
  402.                 scanf("%s%c", temp->password, &cDump);
  403.                 if (temp->ctr>=3 && temp->isLocked==0 && close==0)
  404.                 {
  405.                     user->isLocked=1;
  406.                     printf("You have tried logging in too many times.\n");
  407.                     printf("Please contact the administrator.\n");
  408.                 }
  409.             }
  410.         } while (close==0 && temp->ctr>=3);
  411.     }
  412.     else
  413.         printf("There are not any users yet! Sign up first.\n");
  414. }
  415.  
  416. /* to ask which type of account the user prefers */
  417. void typeofAccount (userNode *pNew)
  418. {  
  419.     /* for authorization code */
  420.     string8 ac;
  421.     int choice=0, close=1, change=0;   
  422.    
  423.     do
  424.     {
  425.         printf("\nChoose your type of account: \n");
  426.         printf("1. Administrator\n");
  427.         printf("2. Shopper\n");
  428.         printf("Enter choice: ");
  429.         scanf("%d", &choice);
  430.                
  431.         while (choice!=1 && choice!=2)
  432.         {
  433.         //  system("cls");
  434.             printf("Invalid choice. Try again.\n");
  435.             printf("Choose your type of account: \n");
  436.             printf("1. Administrator\n");
  437.             printf("2. Shopper\n");
  438.             printf("Enter choice: ");
  439.             scanf("%d", &choice);
  440.         }
  441.         pNew->type=choice;
  442.        
  443.         switch (pNew->type)
  444.         {
  445.             case 1: do
  446.                     {
  447.                         printf("Input authorization code: ");
  448.                         scanf("%s", ac);
  449.                        
  450.                         if (strcmp(ac, "DLSU2017")==0)
  451.                         {
  452.                             printf("\nHello New Admin!\n");
  453.                         //  printf("Opening Administrator Menu...\n");
  454.                         //  adminMenu(pNew, close);
  455.                         }
  456.                         else
  457.                         {
  458.                             printf("\nSorry, invalid authorization code.\n");
  459.                             printf("1. Try again\n");
  460.                             printf("2. Change account type\n");
  461.                             printf("Enter choice: ");
  462.                             scanf("%d", &change);
  463.                            
  464.                             while (change!=1 && change!=2)
  465.                                 printf("\nInvalid input!\n");
  466.                                                        
  467.                             switch (change)
  468.                             {
  469.                                 case 1: close=1;
  470.                                         break;
  471.                                 case 2: close=0;
  472.                                         break;
  473.                             }
  474.                         }
  475.                     } while (strcmp(ac, "DLSU2017")!=0 && close!=0);
  476.                     break; 
  477.             case 2: printf("Hello Shopper!");
  478.                     pNew->creditLimit=5000.00;
  479.                     pNew->outstanding=0.00;
  480.                     pNew->nItems=0;
  481.                     pNew->isLocked=0;
  482.                    
  483.                     /* to tell the user more information about his/her account */
  484.                     printf("\nYour Credit Limit is: PhP %.2f.\n", pNew->creditLimit);
  485.                     printf("Your Outstanding Balance is: PhP %.2f.\n", pNew->outstanding);
  486.                     printf("Your Cart currently contains: %d items.\n\n", pNew->nItems);
  487.                     break;
  488.             default: printf("Invalid input!\n");
  489.         }
  490.     } while (close==0 && change==2);
  491.    
  492.     /* recap for the user */       
  493.     printf("\n === REMEMBER ===\n");
  494.     printf("Username is: %s\n", pNew->username);
  495.     printf("Password is: %s\n\n", pNew->password);
  496. }
  497.  
  498. void getUserInfo (userInfoType  *pInfo)
  499. {
  500.     getchar();
  501.     printf("Enter name: \n");
  502.     printf("First name: ");
  503.     fgets(pInfo->name.first, 21, stdin);
  504.     printf("Middle name: ");
  505.     fgets(pInfo->name.middle, 21, stdin);
  506.     printf("Last name: ");
  507.     fgets(pInfo->name.last, 21, stdin);
  508.     printf("Enter address: ");
  509.     fgets (pInfo->address, 51, stdin);
  510.    
  511.     (*pInfo).address[strlen((*pInfo).address)-1]='\0';
  512.     (*pInfo).name.first[strlen((*pInfo).name.first)-1]='\0';
  513.     (*pInfo).name.middle[strlen((*pInfo).name.middle)-1]='\0';
  514.     (*pInfo).name.last[strlen((*pInfo).name.last)-1]='\0';
  515.    
  516.     printf("%s, %s %s \n", pInfo->name.last, pInfo->name.first, pInfo->name.middle);
  517. }
  518.  
  519. void signUp (userNode *user, userNode *pNew)
  520. {
  521.     int go=0, chk=0, i=0, compare=2, accept=0;
  522.     char cDump;
  523.     string15 uname, pword;
  524.  
  525.     if (user==NULL)
  526.     {
  527.         /* first account to sign up */
  528.         do
  529.         {
  530.             printf("=== SIGN UP ===\n");
  531.             printf("Enter username: ");
  532.             scanf("%s%c", uname, &cDump);
  533.         } while ((strlen(uname) < 3 || strlen(uname) > 15));
  534.        
  535.         strcpy (pNew->username, uname);
  536.     }
  537.        
  538.     else
  539.     {
  540.         /* second account and onwards to sign up */
  541.         do
  542.         {
  543.             printf("=== SIGN UP ===\n");
  544.             printf("Enter username: ");
  545.             scanf("%s%c", uname, &cDump);
  546.            
  547.             if (strlen(uname) >=3 && strlen(uname)<=15)
  548.             {
  549.                 pNew=user;
  550.                 compare=strcmp(pNew->username, uname);
  551.             }
  552.            
  553.             if (compare!=0) //either 1 or -1
  554.             {
  555.                 accept=1;
  556.                 pNew->next=pNew;
  557.                 strcpy (pNew->username, uname);
  558.                 //printf("Username is: %s\n", pNew->username);
  559.             }
  560.             else if (compare==0)
  561.             {
  562.                 accept=0;
  563.                 printf("Username unavailable. Try again.\n");
  564.             }
  565.            
  566.         } while (strlen(uname) < 3 || strlen(uname) > 15 || accept==0);
  567.     }
  568.                                
  569.     printf("Username is: %s\n", pNew->username);
  570.    
  571.     accept=0;
  572.        
  573.     do
  574.     {
  575.         printf("Enter password: ");
  576.         scanf("%s", pword);
  577.        
  578.         if (strlen(pword) >= 6 || strlen(pword) <= 15)
  579.         {
  580.             for (i=0; i<strlen(pword); i++)
  581.             {
  582.                 if (!((pword[i] >= 'A' && pword[i] <= 'Z') || (pword[i] >= 'a' && pword[i] <= 'z')))
  583.                 {
  584.                     accept=1;
  585.                     chk=1;
  586.                     strcpy (pNew->password, pword);
  587.                 }
  588.             }
  589.            
  590.             if (accept!=1)
  591.                 printf("Invalid password. Input at least 1 non-letter.\n");
  592.         }
  593.         else
  594.             accept=0;
  595.                    
  596.     } while (chk==0 || accept!=1 || strlen(pword) < 6 || strlen(pword) > 15);
  597.     printf("Password is: %s\n", pNew->password);
  598.    
  599.     getUserInfo(&pNew->info);   /* (&pUser->info) */
  600. }
  601.  
  602. void displayAllRecur (userNode *pUser)
  603. {
  604.     if (pUser!=NULL)
  605.     {
  606.         printf("%s \n", pUser->next);
  607.         displayAllRecur (pUser->next);
  608.     }
  609. }
  610.  
  611. void freeAll (userNode *user)
  612. {
  613.     userNode *pDel;
  614.    
  615.     pDel=NULL;
  616.    
  617.     while (user != NULL)
  618.     {
  619.         pDel=user;
  620.         user=user->next;
  621.         free(pDel);
  622.     }
  623. }
  624.  
  625. int main()
  626. {
  627.     int choice=0, close=0, nChoice=0;
  628.     userNode *user, *pNew, *pLast, *pRun, *pTrail;
  629.     stockNode *pStocks;
  630.    
  631.     user=NULL;
  632.     pNew=NULL; //pFirst
  633.     pLast=NULL;
  634.     pRun=NULL;
  635.     pTrail=NULL;
  636.     pStocks=NULL;
  637.    
  638.     printf("\t\t\t\t\t\t========== WELCOME! ==========\n\n");
  639.  
  640.     do
  641.     {
  642.         printf("=== MAIN MENU ===\n");
  643.         printf("1. Login\n");
  644.         printf("2. Sign-up\n");
  645.         printf("3. Display\n");
  646.         printf("Enter choice: ");
  647.         scanf("%d", &choice);
  648.         printf("\n");
  649.  
  650.         while(choice!=1 && choice!=2)
  651.         {
  652.             system("cls");
  653.             printf("Try Again!\n");
  654.             printf("=== MAIN MENU ===\n");
  655.             printf("1. Login\n");
  656.             printf("2. Sign-up\n");
  657.             printf("3. Display\n");
  658.             printf("Enter choice: ");
  659.             scanf("%d", &choice);
  660.             printf("\n");
  661.         }
  662.  
  663.         switch (choice)
  664.         {
  665.         case 1:     printf("You chose to login!\n\n");
  666.                     login(user);
  667.                     break;
  668.                    
  669.         case 2:     printf("You chose to sign-up!\n\n");
  670.                     do
  671.                     {
  672.                         pNew=malloc(sizeof(userNode));
  673.                         signUp(user, pNew);                                            
  674.                         typeofAccount(pNew);
  675.                         pNew->next=NULL;
  676.                                                
  677.                         if (user==NULL)
  678.                             user=pNew;                             
  679.                        
  680.                         else if (strcmp(user->username, pNew->username)>0)
  681.                         {
  682.                             pNew->next=user;
  683.                             user=pNew;
  684.                         }
  685.                        
  686.                         else
  687.                         {
  688.                             pRun=user;
  689.                             while (pRun!=NULL && strcmp(pRun->username, pNew->username)<0)
  690.                             {
  691.                                 pTrail=pRun;
  692.                                 pRun=pRun->next;
  693.                             }
  694.                         //  if (pTrail!=NULL)
  695.                                 pTrail->next=pNew;
  696.                                 pNew->next=pRun;
  697.                         }
  698.                        
  699.                         do
  700.                         {
  701.                             printf("Would you like to add another user?\n");
  702.                             printf("1. Yes\n");
  703.                             printf("2. No\n");
  704.                             scanf("%d", &nChoice);         
  705.                         } while (nChoice!=1 && nChoice!=2);
  706.  
  707.                     } while (nChoice==1);
  708.                     break;
  709.                    
  710.         case 3:     printf("You chose to display all accounts!\n");
  711.                     displayAllRecur(user);
  712.                     break;
  713.                    
  714.         default: printf("Invalid input.\n");
  715.         }
  716.        
  717.     } while (nChoice==2 || nChoice==0);
  718.    
  719.     return 0;
  720. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement