Advertisement
Guest User

Untitled

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.94 KB | None | 0 0
  1.   #define _CRT_SECURE_NO_WARNINGS
  2.   #include <stdio.h>
  3.   #include <string.h>
  4.   #include <stdlib.h>
  5.   #include <time.h>
  6.  
  7.   struct product {
  8.      unsigned long long id;
  9.      char name[51];
  10.      double price;
  11.      int quantity;
  12.      char date[11];
  13.   };
  14.  
  15.   struct products {
  16.       struct product val;    
  17.       struct products *next;
  18.   };
  19.  
  20.   struct products* find_by_id(struct products*, unsigned long long id);
  21.   int isUniqueID(struct products *products, unsigned long long id);
  22.   void delete_by_id(struct products*, unsigned long long id);
  23.   int change_quantity_of_product(struct products*);
  24.   int print_all_expired_products(struct products*);
  25.   int print_product_with_id(struct products*);
  26.   int add_new_product(struct products*);
  27.   int load_products(struct products*);
  28.   int save_products(struct products*);
  29.   int showproducts(struct products*);
  30.   void free_list(struct products*);
  31.   time_t string_to_time(char*);
  32.  
  33.   int main()
  34.    {
  35.     int option = 0,exit_flag = 1;
  36.     struct products *products = NULL;
  37.     products = malloc(sizeof(struct products));
  38.     if (products == NULL)
  39.     {
  40.         return 1;
  41.     }
  42.     products->next = NULL;
  43.     if (load_products(products) == 1)
  44.     {
  45.       return 1;
  46.     }
  47.  
  48.        while(exit_flag)
  49.     {
  50.         system("@cls||clear");
  51.     printf(" *****************/MENU\\*****************\n");
  52.     printf("*                                        *\n");
  53.     printf("*   1.Add new item in the warehouse      *\n");
  54.     printf("*   2.Change the quantity                *\n");
  55.     printf("*   3.Display the expired items          *\n");
  56.     printf("*   4.Display information for item       *\n");
  57.     printf("*   5.Exit                               *\n");
  58.     printf("*                                        *\n");
  59.     printf("******************************************\n");
  60.         printf("Insert option: ");
  61.         if (scanf("%d", &option) == EOF)
  62.         {
  63.             break;
  64.         }
  65.  
  66.         switch (option) {
  67.             case 1:
  68.                 if (add_new_product(products)) {
  69.                     return -1;
  70.                 }
  71.                 break;
  72.             case 2:
  73.                 if (change_quantity_of_product(products)) {
  74.                     return -1;
  75.                 }
  76.                 break;
  77.             case 3:
  78.                 if (print_all_expired_products(products)) {
  79.                     return -1;
  80.                 }
  81.                 break;
  82.             case 4:
  83.                 if (print_product_with_id(products))
  84.               {
  85.                     return -1;
  86.                 }
  87.                 break;
  88.             case 5:
  89.                 exit_flag = 0;
  90.                 break;
  91.             default:
  92.                 break;
  93.         }
  94.  
  95.       system("pause");
  96.     }
  97.  
  98.    if (save_products(products))
  99.     {
  100.         return -1;
  101.     }
  102.     free_list(products);
  103.     return 0;
  104.  }
  105.     void free_list(struct products *products)
  106.     {
  107.         struct products *curr_item=products;
  108.         while(products!=NULL)
  109.         {
  110.             products=products->next;
  111.             free(curr_item);
  112.             curr_item=products;
  113.         }
  114.     }
  115.  
  116.    int isUniqueID(struct products *products, unsigned long long id)
  117.   {  
  118.     while(products)
  119.     {
  120.         if(products->val.id == id)
  121.         {
  122.             printf("ID already exist!\n");
  123.             return 0;
  124.         }    
  125.     products = products->next;
  126.         }
  127.   return 1;
  128.  }
  129.  
  130.    int load_products(struct products *products)
  131.    {
  132.       FILE *fp;
  133.       if ((fp = fopen("data", "rb")) == NULL)
  134.         {
  135.           printf("The file cannot be opened");
  136.           exit(1);
  137.         }
  138.       struct products *prev_products = products;
  139.       struct products *curr_products = NULL;
  140.       while (1)
  141.       {
  142.           struct product product;
  143.           if (fread(&product, sizeof(struct product), 1, fp) != 1)
  144.            {
  145.               if (feof(fp))
  146.               {
  147.                   break;
  148.               }
  149.              printf("reading error");
  150.              break;
  151.            }
  152.         curr_products = malloc(sizeof(struct products));
  153.         if (curr_products == NULL)
  154.         {
  155.             printf("Memory allocation error");
  156.             return 1;
  157.         }
  158.         curr_products->val = product;
  159.         curr_products->next = NULL;
  160.         prev_products->next = curr_products;
  161.         prev_products = curr_products;
  162.     }
  163.       fclose(fp);
  164.       return 0;
  165.   }
  166.  
  167.     int save_products(struct products *products)
  168.     {
  169.       FILE *fp  = NULL;
  170.       if ((fp = fopen("data", "wb")) == NULL)
  171.       {
  172.        printf("The file cannot be opened");
  173.        return 2;
  174.       }
  175.         while (1)
  176.       {
  177.          products = products->next;
  178.          if (products == NULL)
  179.          {
  180.             break;
  181.          }
  182.          if (fwrite(&products->val, sizeof(struct product), 1, fp) != 1)
  183.         {
  184.             printf("writening error");
  185.             break;
  186.         }
  187.       }
  188.      fclose(fp);
  189.      return 0;
  190.     }
  191.  
  192.     struct products* find_by_id(struct products *products,unsigned long long id)
  193.   {
  194.     struct products *iterate_products = products->next;
  195.     while(1)
  196.     {
  197.         if (iterate_products == NULL)
  198.         {    
  199.             break;
  200.         }
  201.         if (iterate_products->val.id == id)
  202.         {
  203.             return iterate_products;
  204.         }
  205.        iterate_products = iterate_products->next;
  206.     }
  207.     return NULL;
  208.   }
  209.  
  210.    void delete_by_id(struct products *products, unsigned long long id)
  211.   {
  212.       struct products *prev_products = products;
  213.       struct products *iterate_products = products->next;
  214.       while(1)
  215.       {
  216.         if (iterate_products == NULL)
  217.         {
  218.             printf("The warehouse is empty.");
  219.             break;
  220.         }
  221.         if (iterate_products->val.id == id)
  222.         {
  223.             prev_products->next = iterate_products->next;
  224.             break;
  225.         }
  226.         iterate_products = iterate_products->next;
  227.         prev_products = prev_products->next;
  228.       }
  229.   }
  230.  
  231.   time_t string_to_time(char *string_time)
  232.   {
  233.       time_t result = 0;
  234.       int day = 0, month = 0, year = 0;
  235.  
  236.       if (sscanf(string_time, "%2d.%2d.%4d", &day, &month, &year) == 3)
  237.     {
  238.        struct tm time = {0};
  239.        time.tm_year = year - 1900;
  240.        time.tm_mon = month - 1;
  241.        time.tm_mday = day;
  242.     if ((result = mktime(&time)) == (time_t) - 1)
  243.         {
  244.             perror("mktime");
  245.             return 1;
  246.         }
  247.     }
  248.     return result;
  249. }
  250.  
  251.    int add_new_product(struct products *products)
  252.    {  
  253.         struct product product;
  254.         unsigned long long id=0;
  255.         char name[51] = "",date[11] = "";
  256.         double price=0;
  257.         int quantity=0;
  258.         do{
  259.         printf("Insert id: ");
  260.         fflush(stdin);
  261.         scanf("%llu", &id);
  262.         }while(!(isUniqueID(products, id)) || !id || id > 999999999999|| id<100000000000);
  263.    
  264.         printf("Insert name: ");
  265.         scanf("%50s",name);
  266.         printf("Insert price: ");
  267.         scanf("%lf", &price);
  268.         printf("Insert quantity: ");
  269.         scanf("%d", &quantity);
  270.         printf("Insert date /in dd.mm.yyyy format/: ");
  271.         scanf("%s", date);
  272.    
  273.         product.id = id;
  274.         strcpy(product.name, name);
  275.         product.price = price;
  276.         product.quantity = quantity;
  277.         strcpy(product.date, date);
  278.         struct products *last_products = products;
  279.         while(last_products->next)
  280.         {
  281.         last_products = last_products->next;
  282.         }
  283.         struct products *next_products = NULL;
  284.         next_products = malloc(sizeof(struct products));
  285.         if (!next_products)
  286.         {
  287.         printf("Memory allocation error.");
  288.         return -1;
  289.         }
  290.         next_products->val = product;
  291.         next_products->next = NULL;
  292.         last_products->next = next_products;
  293.         return 0;
  294.         }
  295.  
  296. int print_product_with_id(struct products *products)
  297.  {
  298.     unsigned long long id=0;
  299.     printf("Insert id: ");
  300.     scanf("%llu", &id);
  301.  
  302.     struct products *products_with_id = find_by_id(products, id);
  303.     if (products_with_id == NULL)
  304.         printf("Missing product!\n");
  305.     else
  306.     {
  307.         printf("id: %llu\n", products_with_id->val.id);
  308.         printf("name: %s\n", products_with_id->val.name);
  309.         printf("price: %.2f\n", products_with_id->val.price);
  310.         printf("quantity: %d\n", products_with_id->val.quantity);
  311.         printf("date: %s\n", products_with_id->val.date);
  312.     }
  313.     return 0;
  314.    }
  315.  
  316.   int print_all_expired_products(struct products *products)
  317.   {
  318.     char date[11];
  319.     printf("Insert date to compare /in dd.mm.yyyy format/: ");
  320.     scanf("%s",date);
  321.     time_t date_t = string_to_time(date);
  322.     if (date_t == -1)
  323.     {
  324.         return -1;
  325.     }
  326.      
  327.     struct products *iterate_products = products->next;
  328.        
  329.     while(1)
  330.     {      
  331.          if  (iterate_products == NULL)
  332.         {
  333.             break;
  334.         }
  335.         time_t product_date_t = string_to_time(iterate_products->val.date);
  336.         if (product_date_t == -1)
  337.         {
  338.             return -1;
  339.         }
  340.         if (difftime(product_date_t, date_t) < 0)
  341.         {
  342.             printf("id: %llu\n", iterate_products->val.id);
  343.             printf("name: %s\n", iterate_products->val.name);
  344.             printf("price: %.2f\n", iterate_products->val.price);
  345.             printf("quantity: %d\n", iterate_products->val.quantity);
  346.             printf("date: %s\n", iterate_products->val.date);
  347.         }
  348.         iterate_products = iterate_products->next;
  349.     }
  350.    return 0;
  351. }
  352.    int change_quantity_of_product(struct products *products)
  353. {
  354.     unsigned long long id=0;int quantity=0;
  355.     printf("Insert id: ");
  356.     scanf(" %llu", &id);
  357.     printf("Insert quantity to change[(+)for adding and (-)for subtraction]:");
  358.     scanf("%d", &quantity);
  359.     struct products *products_with_id = find_by_id(products, id);
  360.     if (products_with_id == NULL)
  361.     printf("Missing product!\n");
  362.     else
  363.     {
  364.        int new_quantity = products_with_id->val.quantity + quantity;
  365.        if (new_quantity < 0)
  366.              printf("Quantity to change is bigger than product quantity!\n");
  367.        else if (new_quantity > 0)
  368.                products_with_id->val.quantity = new_quantity;
  369.        else if (new_quantity == 0)
  370.                delete_by_id(products, id);
  371.     }
  372.  return 0;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement