Advertisement
Guest User

INVENTORY

a guest
Jan 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. //define constant for item per page in display()
  6. #define ITEM_PER_PAGE 10
  7.  
  8. typedef struct{
  9.     char barcode[5];
  10.     char name[20];
  11.     int quantity;
  12.     char price[10];
  13.     char category[10];
  14. }ITEM;
  15.  
  16. int main();
  17. void menu();
  18. void add();
  19. void ded();
  20. void display();
  21. void search();
  22. void update();
  23. void login();
  24. void error();
  25. void login1();
  26. void error2();
  27. void login2();
  28. void error2();
  29. void login3();
  30.  
  31.  
  32.  
  33. //add new item
  34. void add() {
  35.     system("cls");
  36.     ITEM item;
  37.  
  38.     FILE *ptr;
  39.     ptr  =  fopen("sample1.txt", "a");
  40.  
  41.     //file checking. Check if file existed or not
  42.     if (!ptr) {
  43.         printf("ERROR OPENING FILE\n");
  44.         exit(-1);
  45.     }
  46.  
  47.     printf("\n\n\n\t\tEnter Barcode: ");
  48.     scanf(" %[^\n]", &item.barcode);
  49.     printf("\t\tEnter Item name: ");
  50.     scanf(" %[^\n]", &item.name);
  51.     printf("\t\tEnter Quantity: ");
  52.     scanf(" %d", &item.quantity);
  53.     printf("\t\tEnter Price(Php): ");
  54.     scanf(" %[^\n]", &item.price);
  55.     printf("\t\tEnter Category: ");
  56.     scanf(" %[^\n]", &item.category);
  57.  
  58.     fprintf(ptr,"\n%s|%s|%d|%s|%s\n",
  59.      item.barcode, item.name, item.quantity, item.price, item.category);
  60.     fclose(ptr);
  61.  
  62.     printf("\n\t\tItem Successfully Added!\n");
  63.     system("pause");
  64. }
  65.  
  66.  
  67. //search for item's information by using barcode
  68. void search()
  69. {
  70.     char userInputSearch[20];
  71.     int i = 0, arrCount= 0, existed = 0;
  72.     FILE *ptr;
  73.     ITEM item[1000];
  74.     ptr = fopen("sample1.txt", "r");
  75.     //file checking. Check if file existed or not
  76.     if(!ptr){
  77.         printf("ERROR OPENING FILE");
  78.         exit(-1);
  79.     }
  80.  
  81.     //read the file content and store inside structure array
  82.     i = 0;
  83.     while (!feof(ptr))
  84.     {
  85.         fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
  86.             ,&item[i].barcode
  87.             ,&item[i].name
  88.             ,&item[i].quantity
  89.             ,&item[i].price, &item[i].category);
  90.         i++;
  91.         arrCount++;
  92.     }
  93.     fclose(ptr);
  94.  
  95.     printf("\n\n\tPlease enter barcode: ");
  96.     scanf(" %[^\n]", &userInputSearch);
  97.  
  98.     printf("\tSEARCH RESULT\n");
  99.  
  100.     printf("\n\n\n\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
  101.     printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
  102.         ,"BARCODE"
  103.         ,"NAME"
  104.         ,"QUANTITY"
  105.         ,"PRICE"
  106.         ,"CATEGORY");
  107.     printf("\tณฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤฤณ\n");
  108.  
  109.  
  110.     for(i = 0; i < arrCount; i++){
  111.         if(strcmp(userInputSearch, item[i].barcode) == 0){
  112.             printf("\tณ%-7s ณ%-20s ณ%-8d ณ%-7s ณ%-10sณ\n"
  113.                 ,item[i].barcode
  114.                 ,item[i].name
  115.                 ,item[i].quantity
  116.                 ,item[i].price, item[i].category);
  117.             existed = 1;
  118.         }
  119.     }
  120.     if(!existed){
  121.         printf("%-62s\n", "NO RESULT");
  122.     }
  123.     printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
  124.  
  125.     printf("\n\n\n");
  126.     system("\t\tpause");
  127. }
  128.  
  129. //display all the items information
  130. void display(){
  131.     ITEM item;
  132.     int i = 0, count = 0, page = 1; //count for item count
  133.     FILE *ptr;
  134.     ptr = fopen("sample1.txt", "r");
  135.  
  136.     //file checking. Check file exist or not
  137.     if(!ptr){
  138.         printf("ERROR OPENING FILE");
  139.         exit(-1);
  140.     }
  141.     printf("\n\n\n\n\tPAGE %d\n", page);
  142.  
  143.     printf("\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
  144.     printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
  145.         ,"BARCODE"
  146.         ,"NAME"
  147.         ,"QUANTITY"
  148.         ,"PRICE"
  149.         ,"CATEGORY");
  150.  
  151.     printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
  152.  
  153.     while(!feof(ptr)){
  154.         if (count == ITEM_PER_PAGE) {
  155.             count = 0; //reset the value of count
  156.             //output end of line
  157.             printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
  158.             //pause for user to enter any key to continue for next page
  159.             system("pause");
  160.             system("cls");
  161.             page++;
  162.             //output page number
  163.             printf("\n\n\n\n\tPAGE %d\n", page);
  164.  
  165.             printf("\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
  166.  
  167.             printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
  168.                 , "BARCODE"
  169.                 , "NAME"
  170.                 , "QUANTITY"
  171.                 , "PRICE"
  172.                 , "CATEGORY");
  173.  
  174.             printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
  175.  
  176.         }
  177.         fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
  178.             ,&item.barcode
  179.             ,&item.name
  180.             ,&item.quantity
  181.             ,&item.price, &item.category);
  182.         printf("\tณ%-7s ณ%-20s ณ%-8d ณ%-7s ณ%-10sณ\n"
  183.             ,item.barcode
  184.             ,item.name
  185.             ,item.quantity
  186.             ,item.price, item.category);
  187.         count++;
  188.     }
  189.  
  190.     printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n\n\n");
  191.  
  192.     fclose(ptr);
  193.     system("\t\tpause");
  194. }
  195.  
  196. //update the item's quantity (adding)
  197. void update() {
  198.     char userInputBarcode[5];
  199.     int i = 0, arrCount = 0, quantity = 0;
  200.     ITEM item[1000];
  201.     FILE *ptr;
  202.     ptr = fopen("sample1.txt", "r");
  203.  
  204.     //file checking.Check if file existed or not
  205.     if (!ptr) {
  206.         printf("ERROR OPENING FILE\n");
  207.         exit(-1);
  208.     }
  209.  
  210.     //read the content of the file and store the content to the structure array
  211.     while (!feof(ptr))
  212.     {
  213.         fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  214.             &item[i].barcode, &item[i].name,
  215.             &item[i].quantity, &item[i].price, &item[i].category);
  216.         i++;
  217.         arrCount++;
  218.     }
  219.     fclose(ptr);
  220.  
  221.     printf("\n\n\n\t\tEnter barcode: ");
  222.     scanf(" %[^\n]", &userInputBarcode);
  223.  
  224.     //read the content of the structure array
  225.     for (i = 0; i<arrCount; i++) {
  226.         //compare user input barcode with structure barcode
  227.         //if is true then minus the quantity
  228.         if (strcmp(userInputBarcode, item[i].barcode) == 0) {
  229.             //check if user input quantity is valid or not
  230.             //if is not valid, it will loop for user to reenter the value
  231.  
  232.             do {
  233.                 printf("\n\t\t\tEnter Quantity you wanted: ");
  234.                 scanf(" %d", &quantity);
  235.             } while (quantity <= 0); //to prevent user to enter negative value or 0
  236.             printf("\t\t\tItem Quantity Updated! \n\n\n\n");
  237.             system("pause");
  238.  
  239.             item[i].quantity = item[i].quantity + quantity;
  240.             break;
  241.         }
  242.     }
  243.     fclose(ptr);
  244.  
  245.     //clear the text file
  246.     ptr = fopen("sample1.txt", "w");
  247.     fclose(ptr);
  248.  
  249.     ptr = fopen("sample1.txt", "a");
  250.     //write the text file (updated content)
  251.     for (i = 0; i<arrCount; i++) {
  252.         fprintf(ptr, "%s|%s|%d|%s|%s\n",
  253.             item[i].barcode, item[i].name,
  254.             item[i].quantity, item[i].price, item[i].category);
  255.     }
  256.     fclose(ptr);
  257.  
  258. }
  259.  
  260. //deduct item's quantity
  261. void ded()
  262. {
  263.     char userInputBarcode[5];
  264.     int i=0, arrCount=0, quantity=0;
  265.     ITEM item[1000];
  266.     FILE *ptr;
  267.     ptr = fopen("sample1.txt","r");
  268.  
  269.     //file checking.Check if file existed or not
  270.     if(!ptr) {
  271.         printf("ERROR OPENING FILE\n");
  272.         exit(-1);
  273.     }
  274.  
  275.     //read the content of the file and store the content to the structure array
  276.     while (!feof(ptr))
  277.     {
  278.         fscanf(ptr,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  279.             &item[i].barcode,&item[i].name,
  280.             &item[i].quantity,&item[i].price,&item[i].category);
  281.         i++;
  282.         arrCount++;
  283.     }
  284.     fclose(ptr);
  285.  
  286.     printf("\n\t\tEnter barcode: ");
  287.     scanf(" %[^\n]", &userInputBarcode);
  288.  
  289.     //read the content of the structure array
  290.     for (i=0;i<arrCount;i++){
  291.         //compare user input barcode with structure barcode
  292.         //if is true then minus the quantity
  293.         if(strcmp(userInputBarcode, item[i].barcode) == 0){
  294.             //check if user input quantity is valid or not
  295.             //if is not valid, it will loop for user to reenter the value
  296.  
  297.             do{
  298.                 printf("\n\t\tEnter Quantity you wanted: ");
  299.                 scanf(" %d",&quantity);
  300.             }while(quantity > item[i].quantity || quantity <= 0); //to prevent user for enter value bigger than item's quantity and negative value
  301.             printf("Item's Quantity Updated!\n");
  302.             system("\t\tpause");
  303.  
  304.             item[i].quantity=item[i].quantity-quantity;
  305.             break;
  306.         }
  307.     }
  308.     fclose(ptr);
  309.  
  310.     //clear the text file
  311.     ptr = fopen("sample1.txt","w");
  312.     fclose(ptr);
  313.  
  314.     ptr = fopen("sample1.txt","a");
  315.     //write the text file (updated content)
  316.     for (i=0;i<arrCount;i++){
  317.         fprintf(ptr,"%s|%s|%d|%s|%s\n",
  318.             item[i].barcode,item[i].name,
  319.             item[i].quantity,item[i].price,item[i].category);
  320.     }
  321.     fclose(ptr);
  322. }
  323.  
  324. void del()
  325. {
  326.     printf("\n\n\n\t\tNot Available. Sorry\n\n\n");
  327.     system("pause");
  328. }
  329. void menu(){
  330.     system("cls");
  331.     int ad=0;
  332.  
  333.     printf("\n\n\n\t\tWhat would you like to do? \n");
  334.     printf("\n\t\t [1] Display All Item");
  335.     printf("\n\t\t [2] Search Item");
  336.     printf("\n\t\t [3] Add New Item");
  337.     printf("\n\t\t [4] Update/Deduct Item's Quantity");
  338.     printf("\n\t\t [5] Delete Item");
  339.     printf("\n\t\t [6] Exit\n");
  340.     printf("\n\t\t Select your choice: ");
  341.     scanf(" %d", &ad);
  342.     switch(ad){
  343.         case 1:
  344.             system("cls");
  345.             display();
  346.             menu();
  347.             break;
  348.            
  349.         case 2:
  350.             system("cls");
  351.             search();
  352.             menu();
  353.             break;
  354.            
  355.         case 3:
  356.             system("cls");
  357.             add();
  358.             menu();
  359.             break;
  360.  
  361.         case 4:
  362.             system("cls");
  363.             ad = 0; //reset the ad variable
  364.  
  365.             printf("\n\n\n\tUPDATE/DEDUCT QUANTITY FUNCTION\n");
  366.             printf("\n\t\t [1] Update Quantity");
  367.             printf("\n\t\t [2] Deduct Quantity");
  368.             printf("\n\t\t [3] Back");
  369.             printf("\n\t\t Select your choice: ");
  370.             scanf(" %d", &ad);
  371.             switch (ad)
  372.             {
  373.             case 1: update(); break;
  374.             case 2: ded(); break;
  375.             case 3: main(); break;
  376.             default:
  377.                 printf("\n Wrong Choice. Please select again.");
  378.                 break;
  379.             }
  380.             menu();
  381.             break;
  382.  
  383.         case 5:
  384.             system("cls");
  385.             del();
  386.             menu();
  387.             break;
  388.         case 6:
  389.             main();
  390.             break;
  391.         default:
  392.             system("cls");
  393.             printf("\n Wrong Choice. Please select again.");
  394.     }
  395. }
  396.  
  397. int main()
  398. {
  399.     system("cls");
  400.     int log;
  401.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  402.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t [1] Login ");
  403.     printf("\n\t\t\t\t [2] Exit Program ");
  404.     printf("\n\t\t\t\t Enter your choice: ");
  405.     scanf("%d", &log);
  406.     switch(log)
  407.     {
  408.         case 1:
  409.         login();
  410.         break;
  411.         case 2:
  412.         exit(-1);
  413.         break;
  414.         default:
  415.         system("cls");
  416.         printf("\n Wrong input. Please try again.");
  417.         main();
  418.  
  419.     }
  420.     return 0;
  421. }
  422.  
  423. void error()
  424. {
  425.     system("cls");
  426.     int log;
  427.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  428.     printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again... (3 Attempts Left)");
  429.     printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
  430.     printf("\n\t\t\t\t [2] Exit Program ");
  431.     printf("\n\t\t\t\t Enter your choice: ");
  432.     scanf("%d", &log);
  433.     switch(log)
  434.     {
  435.         case 1:
  436.         login1();
  437.         break;
  438.         case 2:
  439.         exit(-1);
  440.         break;
  441.         default:
  442.         system("cls");
  443.         printf("\n Wrong input. Please try again.");
  444.         main();
  445.  
  446.     }
  447. }
  448.  
  449. void error1()
  450. {
  451.     system("cls");
  452.     int log;
  453.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  454.     printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again...(2 Attempts Left)");
  455.     printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
  456.     printf("\n\t\t\t\t [2] Exit Program ");
  457.     printf("\n\t\t\t\t Enter your choice: ");
  458.     scanf("%d", &log);
  459.     switch(log)
  460.     {
  461.         case 1:
  462.         login2();
  463.         break;
  464.         case 2:
  465.         exit(-1);
  466.         break;
  467.         default:
  468.         system("cls");
  469.         printf("\n Wrong input. Please try again.");
  470.         main();
  471.  
  472.     }
  473. }
  474.  
  475. void error2()
  476. {
  477.     system("cls");
  478.     int log;
  479.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  480.     printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again...(1 Attempts Left)");
  481.     printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
  482.     printf("\n\t\t\t\t [2] Exit Program ");
  483.     printf("\n\t\t\t\t Enter your choice: ");
  484.     scanf("%d", &log);
  485.     switch(log)
  486.     {
  487.         case 1:
  488.         login3();
  489.         break;
  490.         case 2:
  491.         exit(-1);
  492.         break;
  493.         default:
  494.         system("cls");
  495.         printf("\n Wrong input. Please try again.");
  496.         main;
  497.  
  498.     }
  499. }
  500.  
  501.  
  502.  
  503.  
  504. void login()
  505. {
  506.     char username[10];
  507.     char password[10];
  508.     system("cls");
  509.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  510.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
  511.     scanf("%s", &username);
  512.     printf("\t\t\t\t PASSWORD : ");
  513.     scanf("%s", &password);
  514.  
  515.     if(strcmp(username, "Omsss") == 0)
  516.     {
  517.         if(strcmp(password, "123456") == 0)
  518.         {
  519.             menu();
  520.         }
  521.         else
  522.         {
  523.         printf("\nPassword is Incorrect.");
  524.         }
  525.     }
  526.     else
  527.     {
  528.         printf("\nUsername is Incorrect.");
  529.     }
  530.     error();
  531. }
  532.  
  533. void login1()
  534. {
  535.     char username[10];
  536.     char password[10];
  537.     system("cls");
  538.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  539.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
  540.     scanf("%s", &username);
  541.     printf("\t\t\t\t PASSWORD : ");
  542.     scanf("%s", &password);
  543.  
  544.     if(strcmp(username, "Omsss") == 0)
  545.     {
  546.         if(strcmp(password, "123456") == 0)
  547.         {
  548.             menu();
  549.         }
  550.         else
  551.         {
  552.         printf("\nPassword is Incorrect.");
  553.         }
  554.     }
  555.     else
  556.     {
  557.         printf("\nUsername is Incorrect.");
  558.     }
  559.     error1();
  560. }
  561.  
  562. void login2()
  563. {
  564.     char username[10];
  565.     char password[10];
  566.     system("cls");
  567.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  568.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
  569.     scanf("%s", &username);
  570.     printf("\t\t\t\t PASSWORD : ");
  571.     scanf("%s", &password);
  572.  
  573.     if(strcmp(username, "Omsss") == 0)
  574.     {
  575.         if(strcmp(password, "123456") == 0)
  576.         {
  577.             menu();
  578.         }
  579.         else
  580.         {
  581.         printf("\nPassword is Incorrect.");
  582.         }
  583.     }
  584.     else
  585.     {
  586.         printf("\nUsername is Incorrect.");
  587.     }
  588.     error2();
  589. }
  590.  
  591. void login3()
  592. {
  593.     char username[10];
  594.     char password[10];
  595.     system("cls");
  596.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  597.     printf("\n\n\n\n\n\n\t\t\t\t(WARNING: Last Attempt!)\n");
  598.     printf("\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
  599.     scanf("%s", &username);
  600.     printf("\t\t\t\t PASSWORD : ");
  601.     scanf("%s", &password);
  602.  
  603.     if(strcmp(username, "Omsss") == 0)
  604.     {
  605.         if(strcmp(password, "123456") == 0)
  606.         {
  607.             menu();
  608.         }
  609.         else
  610.         {
  611.         printf("\nPassword is Incorrect.");
  612.         }
  613.     }
  614.     else
  615.     {
  616.         printf("\nUsername is Incorrect.");
  617.     }
  618.     exit(-1);
  619. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement