Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.96 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
  3. concepts learned.  I have constructed the functions and their respective algorithms and corresponding code by
  4. myself.  The program was run, tested, and debugged by my own efforts.  I further certify that I have not copied
  5. in part or whole or otherwise plagiarized the work of other students and/or persons.
  6.  
  7.                                                                 DE CASTRO, Julian Paolo Ramirez, DLSU ID# 11612231
  8. ******************************************************************************************************************/
  9. /**** ADDITIONAL STATEMENT: Some/Most variables used below are the ones given in class. Phase 2 is not complete.****/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #define MAX_CART 100
  15.  
  16. typedef char string15[16];
  17. typedef char string20[21];
  18. typedef char string50[51];
  19. typedef char string8[9];
  20. typedef char string9[10];
  21.  
  22. typedef struct userName{
  23.    
  24.     string20    first,
  25.                 middle,
  26.                 last;
  27.    
  28. }nameType;
  29.  
  30. typedef struct{
  31.    
  32.     nameType    name;
  33.     string50    address;
  34.    
  35. }userInfoType;
  36.  
  37. typedef struct cartcontent{
  38.    
  39.     string8 code;
  40.     int qty;
  41.    
  42. }prodBoughtType;
  43.  
  44. typedef prodBoughtType arrBought[MAX_CART];
  45.  
  46. /* FOR BINARY FILES
  47. typedef struct{
  48.     string15    username,
  49.                 password;
  50.    
  51.     userInfoType    info;
  52.    
  53.     char    type;
  54.     float   creditLimit,
  55.             outstanding;
  56.    
  57.     arrBought   cart;
  58.    
  59.     int accountVar,
  60.         nTry;
  61.        
  62.     int items;
  63. }dataType;
  64. */
  65.  
  66. struct userTag{
  67.    
  68.     string15    username,
  69.                 password;
  70.    
  71.     userInfoType    info;
  72.    
  73.     char    type;
  74.     float   creditLimit,
  75.             outstanding;
  76.    
  77.     arrBought   cart;
  78.    
  79.     int accountVar,
  80.         nTry;
  81.        
  82.     int items;
  83.    
  84.     struct userTag  *pNext;
  85.                    
  86.    
  87. };
  88.  
  89. typedef struct userTag userType;
  90.  
  91. typedef struct userTag * ptrUser;
  92.  
  93. typedef struct stocks{
  94.  
  95.     string15 category;
  96.     string15 supplier;
  97.     string15 product;
  98.     string8 prodCode;
  99.    
  100.     int inStock;
  101.     int sold;
  102.    
  103.     float buyingPrice;
  104.     float sellingPrice;
  105.     float discountPercent;
  106.    
  107. }stockInfo;
  108. /* Function displayAll displays all accounts
  109.    
  110.     @param ptrUser pUsers: pointer to the link list
  111. */
  112. void displayAll(ptrUser pUsers){//displays all accounts made during the session
  113.    
  114.     printf("\n -All Accounts Made- \n");
  115.    
  116.     while (pUsers != NULL){
  117.         printf("%s\n", pUsers -> username);
  118.         pUsers = pUsers -> pNext;
  119.     }
  120.        
  121. }
  122. /* Function freeAll frees all malloc used
  123.    
  124.     @param ptrUser * pFirst: pointer to the first node in
  125.     link list created
  126. */
  127. void freeAll(ptrUser * pFirst){//Frees all malloc used
  128.    
  129.     ptrUser pDel;
  130.    
  131.         while (*pFirst != NULL){
  132.             pDel = *pFirst;
  133.             *pFirst = (*pFirst) -> pNext;
  134.             free(pDel);
  135.         }
  136. }
  137. /* Function getUserInfo obtains the user information by asking the
  138.     current user to input his/her data
  139.    
  140.     userInfoType * pInfo: stores info into its structure
  141. */
  142. void getUserInfo(userInfoType * pInfo){//Contains User Lists
  143.    
  144.     printf("Enter Name \n");
  145.         printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  146.         printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  147.         printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  148.    
  149.     printf("%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  150.    
  151.    
  152.     printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  153.    
  154.     pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  155.     pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  156.     pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  157.     pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  158.    
  159. }
  160.  
  161. /*void signUp(userType *pUser){
  162.    
  163.     char cDump;
  164.     do{
  165.    
  166.     printf("Enter username: "); scanf("%s%c", pUser->username, &cDump);
  167.  
  168.     }while (strlen(pUser -> username) < 3 || strlen(pUser -> username) > 15);
  169.    
  170.     do{
  171.    
  172.     printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
  173.    
  174.     }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
  175.  
  176.     getUserInfo(&pUser -> info);
  177.    
  178. }*/
  179.  
  180. /* Function signUp allows the user to create an admin or shopper account
  181.    
  182.     @param userType * pSignUp: node that will contain the current users sign up sheet and info
  183.     @param ptrUser pUsers: pointer to the link list
  184. */
  185.  
  186. void signUp (userType * pSignUp, ptrUser pUsers){//Sign-Up Function; adds a new user to the list
  187.  
  188.     char cDump, type = '\0', specialChar = 0, sameUser;
  189.     char adminInput[9], adminCode[9] = {"DLSU2017"} ;
  190.  
  191.     ptrUser pTemp, pFirst = pUsers;
  192.    
  193.     do{//Username Creator
  194.    
  195.         sameUser = 0;
  196.        
  197.         printf("Enter a username (must have 3-15 characters): "); scanf("%s%c", pSignUp -> username, &cDump);
  198.         if ((strlen(pSignUp -> username)) < 3 || (strlen(pSignUp -> username)) > 15)
  199.        
  200.             printf("Your Username is either too short or too long, please try again. \n");
  201.            
  202.         while(pUsers != NULL && !sameUser){
  203.            
  204.             pTemp = pUsers;
  205.            
  206.             pUsers = pUsers -> pNext;
  207.            
  208.             if (!strcmp(pTemp -> username, pSignUp -> username)){
  209.                
  210.                 printf("Username taken, please think of a new one. \n");
  211.                 sameUser = 1;
  212.                
  213.             }
  214.            
  215.         }
  216.        
  217.         pUsers = pFirst;
  218.        
  219.     }while((strlen(pSignUp -> username)) < 3 || (strlen(pSignUp -> username)) > 15 || sameUser);
  220.    
  221.     do{//Password Creator for a new user
  222.        
  223.         printf("Enter a password (must have 6-15 character \nand must have at least 1 non-letter): \n ");
  224.        
  225.         scanf("%s%c", pSignUp -> password, &cDump);
  226.        
  227.         specialChar=passwordVal(pSignUp);
  228.        
  229.         if (!specialChar)
  230.        
  231.             printf("Invalid. Please have at least one non-letter in your password \n");
  232.            
  233.         if ((strlen(pSignUp->password)) < 6 || (strlen(pSignUp->password)) > 15 )
  234.        
  235.             printf("Invalid length. Please try again \n");
  236.            
  237.     }while(((strlen(pSignUp->password)) < 6 || (strlen(pSignUp->password)) > 15) || !specialChar);
  238.    
  239.     getUserInfo(&pSignUp -> info);
  240.    
  241.     do{//Asks the user which type of account he/she wants to make
  242.    
  243.         pSignUp -> accountVar = -1;
  244.        
  245.         printf("Choose account type. [A]dministrator or [S]hopper: "); scanf("%c%c", &type, &cDump);
  246.        
  247.         if (type == 'A' || type == 'a'){
  248.            
  249.             printf("Enter authorization code: "); scanf("%s%c", adminInput, &cDump);
  250.            
  251.             if (strcmp(adminInput, adminCode) == 0){
  252.                
  253.                 printf("Code Accepted \n");
  254.                 pSignUp -> accountVar = 1;
  255.                
  256.             }
  257.            
  258.             else printf("Wrong Code, try again \n");
  259.            
  260.         }
  261.        
  262.         else if (type == 'S' || type == 's' ){
  263.            
  264.                 pSignUp -> accountVar = 0;
  265.                 pSignUp -> creditLimit = 5000.0;
  266.                 pSignUp -> outstanding = 0.0;
  267.                 pSignUp -> items = 0;
  268.                
  269.              }
  270.              
  271.              else
  272.              
  273.                 printf("Invalid Input, please try again \n");
  274.                
  275.     }while(pSignUp -> accountVar != 1 && pSignUp -> accountVar != 0);
  276.    
  277.     if(pSignUp -> accountVar)//Displays Admin Account Info
  278.    
  279.         printf(" \n Admin Account Made. \n Username: %s \n Password: %s \n Account Type: Administrator \n Name: %s %s %s \n Address: %s \n \n" ,
  280.            
  281.             pSignUp -> username,
  282.             pSignUp -> password,
  283.             pSignUp -> info.name.first,
  284.             pSignUp -> info.name.middle,
  285.             pSignUp -> info.name.last,
  286.             pSignUp -> info.address);
  287.            
  288.     else//Displays Shopper Account Info
  289.    
  290.         printf(" \n Shopper Account Made. \n Username: %s \n Password: %s \n Account Type: Shopper \n Name: %s %s %s \n Address: %s \n Credit Limit: P%.2f \n Outstanding Balance: P%.2f \n Cart is empty. \n \n",
  291.  
  292.             pSignUp -> username,
  293.             pSignUp -> password,
  294.             pSignUp -> info.name.first,
  295.             pSignUp -> info.name.middle,
  296.             pSignUp -> info.name.last,
  297.             pSignUp -> info.address,
  298.             pSignUp -> creditLimit,
  299.             pSignUp -> outstanding);
  300.    
  301. }
  302. /* Function search is used to find a specific username
  303.    
  304.     @param ptrUser pFirst: points to the first node of the link list
  305.     @param string15 username: contains the usernames of all made accounts
  306.     @return pRun: returns if the username was found or not
  307. */
  308. ptrUser search(ptrUser pFirst, string15 username){
  309.    
  310.     ptrUser pRun;
  311.     pRun = pFirst;
  312.    
  313.     while (pRun != NULL && strcmp(username, pRun -> username) != 0){
  314.        
  315.         pRun = pRun -> pNext;
  316.        
  317.     }
  318.    
  319.     return pRun;
  320.  
  321. }
  322.  
  323. /* Function passwordVal checks if the user has inputed a valid password
  324.    
  325.     @param userType * user: points to the list of usernames
  326.     @return result: returns if the password is valid or not
  327. */
  328. int passwordVal(userType * user){//Password Validator
  329.     int i;
  330.     int result = 0;
  331.    
  332.     for(i=0; i<(strlen(user -> password)); i++)
  333.         if (user -> password[i] < 'a' || user -> password[i] >'z')
  334.             if (user -> password[i] < 'A' || user -> password[i] > 'Z')
  335.                 result = 1;
  336.    
  337.     return result;
  338. }
  339.  
  340. /* Function userLogin allows the current user to login into an existing account
  341.    
  342.     @param ptrUser pUsers: points to the link list of users
  343.     @param ptrUser * pCurrentUser: points to the current user using the computer
  344.     @return account: checks and reaturns if the account is valid and was be able to logged into
  345. */
  346.  
  347. int userLogin (ptrUser pUsers, ptrUser * pCurrentUser){//Log-In fucntion; allows the user to access his/her account
  348.    
  349.     int validUser = 0, validPassword = 0, account = -1;
  350.    
  351.     string15 LogInInput;
  352.     string15 passwordInput;
  353.    
  354.     ptrUser pTemp, pFirst = pUsers;
  355.    
  356.     do{
  357.         printf("Username: "); scanf("%s", LogInInput);
  358.         printf("Password: "); scanf("%s", passwordInput);
  359.        
  360.         while (pUsers != NULL && !validUser){
  361.            
  362.             pTemp = pUsers;
  363.             pUsers = pUsers -> pNext;
  364.            
  365.             if (!strcmp(LogInInput, pTemp -> username) && (pTemp->nTry)!=3)
  366.            
  367.                 validUser = 1;
  368.                
  369.             if (!strcmp(passwordInput, pTemp -> password) && (pTemp->nTry)!=3)
  370.            
  371.                 validPassword = 1;
  372.                
  373.             if (validPassword && validUser && (pTemp->nTry)!=3){
  374.                
  375.                 *pCurrentUser = pTemp;
  376.                 account = pTemp -> accountVar;
  377.                
  378.                
  379.             }
  380.            
  381.         }
  382.        
  383.         if ((pTemp->nTry)>=3)
  384.        
  385.             printf("\n +++Exeeded number of tries. Account has been locked. \n Please Contact an Administrator.+++ \n");
  386.            
  387.         else if (!validUser)
  388.        
  389.             printf("\n Wrong Username, please try again. \n");
  390.            
  391.         else if (!validPassword){
  392.            
  393.             (pTemp -> nTry)++;
  394.            
  395.             printf("\n Wrong password, you have %d attemp/s left. \n", 3 - (pTemp -> nTry));          
  396.            
  397.         }
  398.        
  399.         else
  400.        
  401.             return account;
  402.        
  403.         pUsers = pFirst;
  404.         validUser = 0;
  405.         validPassword = 0;
  406.        
  407.     } while ((!validUser && !validPassword) || (pTemp -> nTry)<3);
  408.    
  409.     return -1;
  410. }
  411. /* Function deleteNode is used to delete unwanted accounts from the list
  412.    
  413.     @param ptrUser * pFirst: points to the first node of the link list
  414.     @param string15 username: deletes unwanted username
  415. */
  416. void deleteNode(ptrUser *pFirst, string15 username){//a fucntion where you can search and delete a username
  417.    
  418.     ptrUser pFind, pRun;
  419.    
  420.     if (*pFirst == NULL)
  421.         printf("The List is empty");
  422.        
  423.     else {
  424.        
  425.         pFind = search(*pFirst, username); 
  426.         if (pFind == NULL)
  427.        
  428.             printf("%s is not in the List\n", username);
  429.            
  430.         else { //found the node to be deleted
  431.            
  432.             if(pFind == *pFirst){ //deleting first node
  433.                
  434.                 *pFirst = (*pFirst) -> pNext;
  435.                                
  436.             }
  437.            
  438.             else{ //delete from middle or end
  439.                
  440.                 pRun = *pFirst;
  441.                 while (pRun -> pNext == pFind)
  442.                     pRun = pRun -> pNext;  
  443.                
  444.                 pRun -> pNext = pFind -> pNext;
  445.                
  446.             }
  447.            
  448.             free(pFind);
  449.             pFind = NULL;
  450.        
  451.         }
  452.        
  453.     }
  454.    
  455. }
  456. /* Function saveToTextFile allows the user to save the usernames and passwords of the users who made accounts during the
  457.     current session
  458.    
  459.     @param ptrUser pFirst: the first node of the link list
  460.  
  461. */
  462. void saveToTextFile(ptrUser pFirst){
  463.    
  464.     FILE *pText;
  465.     string20 strName;
  466.    
  467.     if (pFirst == NULL)
  468.         printf("There is nothing to save.\n");
  469.    
  470.     else{
  471.        
  472.         printf("\nEnter Filename: "); scanf("%s", strName);
  473.        
  474.         pText = fopen(strName, "wt"); // fopen(filename, filemade);
  475.         if (pText != NULL){
  476.             while (pFirst != NULL){
  477.                 fprintf(pText, "%s %s\n", pFirst -> username, pFirst -> password); //fprintf file formatted printing
  478.                 pFirst = pFirst -> pNext;
  479.             }
  480.            
  481.             fclose(pText);
  482.        
  483.         }
  484.        
  485.         else printf("Error in Saving File...\n");
  486.        
  487.     }
  488.    
  489. }
  490. /* Function loadToText allows the user to load the usernames and passwords of the users who made accounts during the
  491.     current session
  492.    
  493.     @param string20 strFile: allows the user to open a external txt file to load from
  494.     @param ptrUser * pFirst: points to the first node of the link list
  495.  
  496. */
  497. void loadFromText(string20 strFile, ptrUser * pFirst){
  498.    
  499.     FILE * pFile;
  500.    
  501.     string20 user, pass;
  502.    
  503.     ptrUser pNew;
  504.    
  505.     char ch;
  506.    
  507.     if ((pFile = fopen(strFile, "rt")) != NULL){
  508.        
  509.         fscanf (pFile, "%s%s", user, pass);
  510.         printf("user = %s, pass = %s\n", user, pass);
  511.        
  512.       while(fscanf(pFile, "%s %s", user, pass) == 2){
  513.         //creation of linked list
  514.         pNew = malloc(sizeof(userType));
  515.         strcpy(pNew -> username, user);
  516.         strcpy(pNew -> password, pass);
  517.         //name
  518.         fgets(pNew -> info.name.first, 21, pFile);
  519.         pNew -> info.name.first[strlen(pNew -> info.name.first) - 1] = '\0';
  520.         //type
  521.         ch = fgetc(pFile);
  522.         pNew -> type = ch;
  523.         fgetc(pFile);
  524.         //credit limit
  525.         fscanf(pFile, "%f%f", &pNew -> creditLimit, &pNew -> outstanding);
  526.         fgetc(pFile); //extra new line
  527.        
  528.         //always insert at head of list
  529.         pNew -> pNext = *pFirst;
  530.         *pFirst = pNew;
  531.         }
  532.        
  533.         fclose(pFile);
  534.        
  535.     }
  536.    
  537.     else printf("Error in Loading File...\n");
  538.    
  539. }
  540. /* Function adminMainMenu allows the admin to view the admin menu
  541.    
  542.  
  543.  
  544. */
  545. void adminMainMenu(){
  546.    
  547.     int choice;
  548.  
  549.     do{
  550.         printf("\n\n ADMIN MENU: \n");
  551.         printf("1 - Manage Accounts\n");
  552.         printf("2 - Manage Stocks\n");
  553.         printf("3 - Prepare Delivery Receipt\n");
  554.         printf("4 - Shutdown\n");
  555.         printf("5 - Log-Out\n");
  556.         printf("Choice: "); scanf("%d", &choice); fflush(stdin);
  557.  
  558.         switch(choice){
  559.             case 1: //manageAccount();
  560.                     break;
  561.             case 2: //manageStocks();
  562.                     break;
  563.             case 3: break;
  564.             case 4: //*shutdown=1;
  565.                     break;
  566.             case 5: break;
  567.         }
  568.        
  569.         printf("\n");
  570.        
  571.     }while(choice != 4 && choice != 5);
  572.  
  573. }
  574. /* Function shopperMainMenu allows the shopper to view the shopper menu
  575.    
  576.  
  577.  
  578. */
  579. void shopperMainMenu(){
  580.    
  581.     int choice;
  582.  
  583.     do{
  584.        
  585.    
  586.         printf("\n\nSHOPPER MENU:\n");
  587.         printf("1 - Modify User Info\n");
  588.         printf("2 - Browse All Products\n");
  589.         printf("3 - Browse Products by Category\n");
  590.         printf("4 - Browse Products on Sale\n");
  591.         printf("5 - Add to Cart\n");
  592.         printf("6 - View Cart\n");
  593.         printf("7 - Settle Outstanding Balance\n");
  594.         printf("8 - Log Out\n");
  595.        
  596.         printf("Choice: "); scanf("%d", &choice);  fflush(stdin);
  597.  
  598.         switch(choice){
  599.            
  600.             case 1: //modifyInfo();
  601.                     break;
  602.                    
  603.             case 2: //displayStocks();
  604.                     break;
  605.                    
  606.             case 3: break;
  607.            
  608.             case 4: break;
  609.            
  610.             case 5: break;
  611.            
  612.             case 6: break;
  613.            
  614.             case 7: break;
  615.            
  616.             case 8: break;
  617.            
  618.             default: ;
  619.         }
  620.        
  621.         printf("\n");
  622.        
  623.     }while(choice!=8);
  624.    
  625. }
  626.  
  627. int main(){
  628.  
  629.     ptrUser pUsers = NULL,
  630.             pNew, pLast;
  631.    
  632.     int opt = 1, userInput = 0, LogIn;
  633.     char cDump;
  634.     ptrUser pRun, pTrail,
  635.             pCurrentUser;
  636.     string20 strFile;
  637.  
  638.     do{//Prints the main menu abd asks for the users input
  639.    
  640.     printf("\n Main Menu \n");
  641.     printf("Please input the number of your selection: \n");
  642.     printf("1 - Sign-Up \n");
  643.     printf("2 - Log-In \n");
  644.     printf("3 - Exit Program \n");
  645.     printf("Your Option: "); scanf("%d%c", &userInput, &cDump);
  646.  
  647.     switch (userInput){
  648.        
  649.         case 1: pNew = malloc(sizeof(userType)); //Sign-Up Case
  650.    
  651.                 pNew -> pNext = NULL;
  652.                 signUp(pNew, pUsers);
  653.                    
  654.                 if (pUsers == NULL) //first user
  655.                 pUsers = pNew;
  656.        
  657.                 else if (strcmp(pUsers -> username, pNew -> username) > 0){ //new users
  658.        
  659.                         pNew -> pNext = pUsers;
  660.                         pUsers = pNew; break;
  661.                        
  662.         case 2: LogIn = userLogin(pUsers, &pCurrentUser);//Log-In Case
  663.        
  664.                 if (LogIn == -1)
  665.                
  666.                     printf("Failed to Login \n");
  667.                    
  668.                 else if (LogIn){
  669.                    
  670.                     printf("-Welcome Admin- \n \n");
  671.                     adminMainMenu();
  672.                    
  673.                 }
  674.                 else {
  675.                    
  676.                     printf("-Welcome Shopper- \n \n");
  677.                     shopperMainMenu();
  678.                    
  679.                 }break;
  680.                
  681.         case 3: printf("Shutting Down Program..........");//Shutdown Case (for convinience)
  682.        
  683.                 //freeAll(&pUsers);
  684.        
  685.                 opt = 0;
  686.                
  687.             }
  688.            
  689.         }
  690.        
  691.     } while (opt == 1);
  692.    
  693.     saveToTextFile(pUsers);
  694.        
  695.     printf("Enter Filename: "); scanf("%s", strFile);
  696.  
  697.     loadFromText(strFile, &pUsers);
  698.    
  699.     freeAll(&pUsers);
  700.    
  701.     displayAll(pUsers);
  702.    
  703.     pUsers = NULL;
  704.  
  705.     return 0;
  706. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement