Advertisement
Guest User

Inventory Management System (Ignacio, OJ)

a guest
Jan 7th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.81 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.  
  26. int main()
  27. {
  28.     system("cls");
  29.     int log;
  30.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  31.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t [1] Login ");
  32.     printf("\n\t\t\t\t [2] Exit Program ");
  33.     printf("\n\t\t\t\t Enter your choice: ");
  34.     scanf("%d", &log);
  35.     switch(log)
  36.     {
  37.         case 1:
  38.         login();
  39.         break;
  40.         case 2:
  41.         exit(-1);
  42.         break;
  43.         default:
  44.         system("cls");
  45.         printf("\n Wrong input. Please try again.");
  46.         main();
  47.  
  48.     }
  49.     return 0;
  50. }
  51.  
  52. void error()
  53. {
  54.     system("cls");
  55.     int log;
  56.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  57.     printf("\n\n\n\n\n\n\t\t\tLogin Failed. Please try again...");
  58.     printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
  59.     printf("\n\t\t\t\t [2] Exit Program ");
  60.     printf("\n\t\t\t\t Enter your choice: ");
  61.     scanf("%d", &log);
  62.     switch(log)
  63.     {
  64.         case 1:
  65.         login();
  66.         break;
  67.         case 2:
  68.         exit(-1);
  69.         break;
  70.         default:
  71.         system("cls");
  72.         printf("\n Wrong input. Please try again.");
  73.         error();
  74.  
  75.     }
  76. }
  77.  
  78.  
  79. void login()
  80. {
  81.     char username[10];
  82.     char password[10];
  83.     system("cls");
  84.     printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
  85.     printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
  86.     scanf("%s", &username);
  87.     printf("\t\t\t\t PASSWORD : ");
  88.     scanf("%s", &password);
  89.  
  90.     if(strcmp(username, "Omsss") == 0)
  91.     {
  92.         if(strcmp(password, "123456") == 0)
  93.         {
  94.             menu();
  95.         }
  96.         else
  97.         {
  98.         printf("\nPassword is Incorrect.");
  99.         }
  100.     }
  101.     else
  102.     {
  103.         printf("\nUsername is Incorrect.");
  104.     }
  105.     error();
  106. }
  107.  
  108. //add new item
  109. void add() {
  110.     system("cls");
  111.     ITEM item;
  112.  
  113.     FILE *ptr;
  114.     ptr  =  fopen("sample1.txt", "a");
  115.  
  116.     //file checking. Check if file existed or not
  117.     if (!ptr) {
  118.         printf("ERROR OPENING FILE\n");
  119.         exit(-1);
  120.     }
  121.  
  122.     printf("Enter Barcode: ");
  123.     scanf(" %[^\n]", &item.barcode);
  124.     printf("Enter item name:: ");
  125.     scanf(" %[^\n]", &item.name);
  126.     printf("Enter Quantity: ");
  127.     scanf(" %d", &item.quantity);
  128.     printf("Enter price: ");
  129.     scanf(" %[^\n]", &item.price);
  130.     printf("Enter category: ");
  131.     scanf(" %[^\n]", &item.category);
  132.  
  133.     fprintf(ptr,"%s|%s|%d|%s|%s\n",
  134.      item.barcode, item.name, item.quantity, item.price, item.category);
  135.     fclose(ptr);
  136.  
  137.     printf("record added\n");
  138.     system("pause");
  139. }
  140.  
  141. //deduct item's quantity
  142. void ded()
  143. {
  144.     char userInputBarcode[5];
  145.     int i=0, arrCount=0, quantity=0;
  146.     ITEM item[1000];
  147.     FILE *ptr;
  148.     ptr = fopen("sample1.txt","r");
  149.  
  150.     //file checking.Check if file existed or not
  151.     if(!ptr) {
  152.         printf("ERROR OPENING FILE\n");
  153.         exit(-1);
  154.     }
  155.  
  156.     //read the content of the file and store the content to the structure array
  157.     while (!feof(ptr))
  158.     {
  159.         fscanf(ptr,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  160.             &item[i].barcode,&item[i].name,
  161.             &item[i].quantity,&item[i].price,&item[i].category);
  162.         i++;
  163.         arrCount++;
  164.     }
  165.     fclose(ptr);
  166.  
  167.     printf("Enter barcode: ");
  168.     scanf(" %[^\n]", &userInputBarcode);
  169.  
  170.     //read the content of the structure array
  171.     for (i=0;i<arrCount;i++){
  172.         //compare user input barcode with structure barcode
  173.         //if is true then minus the quantity
  174.         if(strcmp(userInputBarcode, item[i].barcode) == 0){
  175.             //check if user input quantity is valid or not
  176.             //if is not valid, it will loop for user to reenter the value
  177.  
  178.             do{
  179.                 printf("Enter quantity you wanted: ");
  180.                 scanf(" %d",&quantity);
  181.             }while(quantity > item[i].quantity || quantity <= 0); //to prevent user for enter value bigger than item's quantity and negative value
  182.             printf("record edited\n");
  183.             system("pause");
  184.  
  185.             item[i].quantity=item[i].quantity-quantity;
  186.             break;
  187.         }
  188.     }
  189.     fclose(ptr);
  190.  
  191.     //clear the text file
  192.     ptr = fopen("sample1.txt","w");
  193.     fclose(ptr);
  194.  
  195.     ptr = fopen("sample1.txt","a");
  196.     //write the text file (updated content)
  197.     for (i=0;i<arrCount;i++){
  198.         fprintf(ptr,"%s|%s|%d|%s|%s\n",
  199.             item[i].barcode,item[i].name,
  200.             item[i].quantity,item[i].price,item[i].category);
  201.     }
  202.     fclose(ptr);
  203. }
  204.  
  205. //search for item's information by using barcode
  206. void search()
  207. {
  208.     char userInputSearch[20];
  209.     int i = 0, arrCount= 0, existed = 0;
  210.     FILE *ptr;
  211.     ITEM item[200];
  212.     ptr = fopen("sample1.txt", "r");
  213.     //file checking. Check if file existed or not
  214.     if(!ptr){
  215.         printf("ERROR OPENING FILE");
  216.         exit(-1);
  217.     }
  218.  
  219.     //read the file content and store inside structure array
  220.     i = 0;
  221.     while (!feof(ptr))
  222.     {
  223.         fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
  224.             ,&item[i].barcode
  225.             ,&item[i].name
  226.             ,&item[i].quantity
  227.             ,&item[i].price, &item[i].category);
  228.         i++;
  229.         arrCount++;
  230.     }
  231.     fclose(ptr);
  232.  
  233.     printf("Please enter barcode: ");
  234.     scanf(" %[^\n]", &userInputSearch);
  235.  
  236.     printf("SEARCH RESULT\n");
  237.  
  238.     for(i = 0; i < 62; i++)printf("=");
  239.     printf("\n");
  240.     printf("|%-7s |%-20s |%-8s |%-7s |%-10s|\n"
  241.         ,"BARCODE"
  242.         ,"NAME"
  243.         ,"QUANTITY"
  244.         ,"PRICE"
  245.         ,"CATEGORY");
  246.  
  247.     for(i = 0; i < 62; i++)printf("=");
  248.     printf("\n");
  249.  
  250.     for(i = 0; i < arrCount; i++){
  251.         if(strcmp(userInputSearch, item[i].barcode) == 0){
  252.             printf("|%-7s |%-20s |%-8d |%-7s |%-10s|\n"
  253.                 ,item[i].barcode
  254.                 ,item[i].name
  255.                 ,item[i].quantity
  256.                 ,item[i].price, item[i].category);
  257.             existed = 1;
  258.         }
  259.     }
  260.     if(!existed){
  261.         printf("%-62s\n", "NO RESULT");
  262.     }
  263.  
  264.     for(i = 0; i < 62; i++)printf("=");
  265.     printf("\n");
  266.     system("pause");
  267. }
  268.  
  269. //display all the items information
  270. void display(){
  271.     ITEM item;
  272.     int i = 0, count = 0, page = 1; //count for item count
  273.     FILE *ptr;
  274.     ptr = fopen("sample1.txt", "r");
  275.  
  276.     //file checking. Check file exist or not
  277.     if(!ptr){
  278.         printf("ERROR OPENING FILE");
  279.         exit(-1);
  280.     }
  281.     printf("PAGE %d\n", page);
  282.  
  283.     for(i = 0; i < 62; i++)printf("=");
  284.     printf("\n");
  285.     printf("|%-7s |%-20s |%-8s |%-7s |%-10s|\n"
  286.         ,"BARCODE"
  287.         ,"NAME"
  288.         ,"QUANTITY"
  289.         ,"PRICE"
  290.         ,"CATEGORY");
  291.  
  292.     for(i = 0; i < 62; i++)printf("=");
  293.     printf("\n");
  294.  
  295.     while(!feof(ptr)){
  296.         if (count == ITEM_PER_PAGE) {
  297.             count = 0; //reset the value of count
  298.             //output end of line
  299.             for (i = 0; i < 62; i++)printf("=");
  300.             printf("\n");
  301.             //pause for user to enter any key to continue for next page
  302.             system("pause");
  303.             system("cls");
  304.             page++;
  305.             //output page number
  306.             printf("PAGE %d\n", page);
  307.  
  308.             for (i = 0; i < 62; i++)printf("=");
  309.             printf("\n");
  310.  
  311.             printf("|%-7s |%-20s |%-8s |%-7s |%-10s|\n"
  312.                 , "BARCODE"
  313.                 , "NAME"
  314.                 , "QUANTITY"
  315.                 , "PRICE"
  316.                 , "CATEGORY");
  317.  
  318.             for (i = 0; i < 62; i++)printf("=");
  319.             printf("\n");
  320.  
  321.         }
  322.         fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
  323.             ,&item.barcode
  324.             ,&item.name
  325.             ,&item.quantity
  326.             ,&item.price, &item.category);
  327.         printf("|%-7s |%-20s |%-8d |%-7s |%-10s|\n"
  328.             ,item.barcode
  329.             ,item.name
  330.             ,item.quantity
  331.             ,item.price, item.category);
  332.         count++;
  333.     }
  334.  
  335.     for(i = 0; i < 62; i++)printf("=");
  336.     printf("\n");
  337.  
  338.     fclose(ptr);
  339.     system("pause");
  340. }
  341.  
  342. //update the item's quantity (adding)
  343. void update() {
  344.     char userInputBarcode[5];
  345.     int i = 0, arrCount = 0, quantity = 0;
  346.     ITEM item[1000];
  347.     FILE *ptr;
  348.     ptr = fopen("sample1.txt", "r");
  349.  
  350.     //file checking.Check if file existed or not
  351.     if (!ptr) {
  352.         printf("ERROR OPENING FILE\n");
  353.         exit(-1);
  354.     }
  355.  
  356.     //read the content of the file and store the content to the structure array
  357.     while (!feof(ptr))
  358.     {
  359.         fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  360.             &item[i].barcode, &item[i].name,
  361.             &item[i].quantity, &item[i].price, &item[i].category);
  362.         i++;
  363.         arrCount++;
  364.     }
  365.     fclose(ptr);
  366.  
  367.     printf("Enter barcode: ");
  368.     scanf(" %[^\n]", &userInputBarcode);
  369.  
  370.     //read the content of the structure array
  371.     for (i = 0; i<arrCount; i++) {
  372.         //compare user input barcode with structure barcode
  373.         //if is true then minus the quantity
  374.         if (strcmp(userInputBarcode, item[i].barcode) == 0) {
  375.             //check if user input quantity is valid or not
  376.             //if is not valid, it will loop for user to reenter the value
  377.  
  378.             do {
  379.                 printf("Enter quantity you wanted: ");
  380.                 scanf(" %d", &quantity);
  381.             } while (quantity <= 0); //to prevent user to enter negative value or 0
  382.             printf("record edited\n");
  383.             system("pause");
  384.  
  385.             item[i].quantity = item[i].quantity + quantity;
  386.             break;
  387.         }
  388.     }
  389.     fclose(ptr);
  390.  
  391.     //clear the text file
  392.     ptr = fopen("sample1.txt", "w");
  393.     fclose(ptr);
  394.  
  395.     ptr = fopen("sample1.txt", "a");
  396.     //write the text file (updated content)
  397.     for (i = 0; i<arrCount; i++) {
  398.         fprintf(ptr, "%s|%s|%d|%s|%s\n",
  399.             item[i].barcode, item[i].name,
  400.             item[i].quantity, item[i].price, item[i].category);
  401.     }
  402.     fclose(ptr);
  403.  
  404. }
  405.  
  406. void del()
  407. {
  408.     system("cls");
  409.     char userDelete[5];
  410.     int i = 0, arrCount = 0, quantity = 0;
  411.     ITEM item[1000];
  412.     FILE *ptr, *pt2;
  413.     ptr = fopen("sample1.txt", "r");
  414.  
  415.     //file checking.Check if file existed or not
  416.     if (!ptr) {
  417.         printf("ERROR OPENING FILE\n");
  418.         exit(-1);
  419.     }
  420.     ptr = fopen("sample1.txt","a");
  421.     if (!ptr)
  422.     {
  423.     printf("ERROR OPENING FILE\n");
  424.     exit(-1);
  425.     }
  426.     rewind(ptr);
  427.     printf("\n DELETE ITEM \n");
  428.     printf("Please enter barcode: ");
  429.     scanf(" %[^\n]", &userDelete);
  430.     pt2 = fopen("temp.txt","wb");
  431.     while (!feof(ptr))
  432.     {
  433.         fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  434.             &item[i].barcode, &item[i].name,
  435.             &item[i].quantity, &item[i].price, &item[i].category);
  436.         i++;
  437.         arrCount++;
  438.     }
  439.     if (strcmp(userDelete, item[i].barcode) == 0)
  440.     {
  441.     fprintf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
  442.             &item[i].barcode, &item[i].name,
  443.             &item[i].quantity, &item[i].price, &item[i].category);
  444.     }
  445.     fclose(ptr);
  446.     fclose(pt2);
  447.     remove("sample1.txt");
  448.     rename("temp.txt","sample1.txt");
  449.     ptr = fopen("sample1.txt","rb+");
  450. }
  451. void menu(){
  452.     system("cls");
  453.     int ad=0;
  454.  
  455.     printf("\nWhat would you like to do? \n");
  456.     printf("\n [1] Display All Item");
  457.     printf("\n [2] Search Item");
  458.     printf("\n [3] Add New Item");
  459.     printf("\n [4] Update/Deduct Item's Quantity");
  460.     printf("\n [5] Delete Item");
  461.     printf("\n [6] Exit\n");
  462.     printf("\n Select your choice: ");
  463.     scanf(" %d", &ad);
  464.     switch(ad){
  465.         case 1:
  466.             system("cls");
  467.             display();
  468.             menu();
  469.             break;
  470.            
  471.         case 2:
  472.             system("cls");
  473.             search();
  474.             menu();
  475.             break;
  476.            
  477.         case 3:
  478.             system("cls");
  479.             add();
  480.             menu();
  481.             break;
  482.  
  483.         case 4:
  484.             system("cls");
  485.             ad = 0; //reset the ad variable
  486.  
  487.             printf("\nUPDATE/DEDUCT QUANTITY FUNCTION\n");
  488.             printf("\n [1] Update Quantity");
  489.             printf("\n [2] Deduct Quantity");
  490.             printf("\n [3] Back");
  491.             printf("\n Select your choice: ");
  492.             scanf(" %d", &ad);
  493.             switch (ad)
  494.             {
  495.             case 1: update(); break;
  496.             case 2: ded(); break;
  497.             case 3: main(); break;
  498.             default:
  499.                 printf("\n Wrong Choice. Please select again.");
  500.                 break;
  501.             }
  502.             menu();
  503.             break;
  504.  
  505.         case 5:
  506.             system("cls");
  507.             del();
  508.             break;
  509.         case 6:
  510.             main();
  511.             break;
  512.         default:
  513.             system("cls");
  514.             printf("\n Wrong Choice. Please select again.");
  515.     }
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement