Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- struct product {
- unsigned long long id;
- char name[51];
- double price;
- int quantity;
- char date[11];
- };
- struct products {
- struct product val;
- struct products *next;
- };
- struct products* find_by_id(struct products*, unsigned long long id);
- int isUniqueID(struct products *products, unsigned long long id);
- void delete_by_id(struct products*, unsigned long long id);
- int change_quantity_of_product(struct products*);
- int print_all_expired_products(struct products*);
- int print_product_with_id(struct products*);
- int add_new_product(struct products*);
- int load_products(struct products*);
- int save_products(struct products*);
- int showproducts(struct products*);
- void free_list(struct products*);
- time_t string_to_time(char*);
- int main()
- {
- int option = 0,exit_flag = 1;
- struct products *products = NULL;
- products = malloc(sizeof(struct products));
- if (products == NULL)
- {
- return 1;
- }
- products->next = NULL;
- if (load_products(products) == 1)
- {
- return 1;
- }
- while(exit_flag)
- {
- system("@cls||clear");
- printf(" *****************/MENU\\*****************\n");
- printf("* *\n");
- printf("* 1.Add new item in the warehouse *\n");
- printf("* 2.Change the quantity *\n");
- printf("* 3.Display the expired items *\n");
- printf("* 4.Display information for item *\n");
- printf("* 5.Exit *\n");
- printf("* *\n");
- printf("******************************************\n");
- printf("Insert option: ");
- if (scanf("%d", &option) == EOF)
- {
- break;
- }
- switch (option) {
- case 1:
- if (add_new_product(products)) {
- return -1;
- }
- break;
- case 2:
- if (change_quantity_of_product(products)) {
- return -1;
- }
- break;
- case 3:
- if (print_all_expired_products(products)) {
- return -1;
- }
- break;
- case 4:
- if (print_product_with_id(products))
- {
- return -1;
- }
- break;
- case 5:
- exit_flag = 0;
- break;
- default:
- break;
- }
- system("pause");
- }
- if (save_products(products))
- {
- return -1;
- }
- free_list(products);
- return 0;
- }
- void free_list(struct products *products)
- {
- struct products *curr_item=products;
- while(products!=NULL)
- {
- products=products->next;
- free(curr_item);
- curr_item=products;
- }
- }
- int isUniqueID(struct products *products, unsigned long long id)
- {
- while(products)
- {
- if(products->val.id == id)
- {
- printf("ID already exist!\n");
- return 0;
- }
- products = products->next;
- }
- return 1;
- }
- int load_products(struct products *products)
- {
- FILE *fp;
- if ((fp = fopen("data", "rb")) == NULL)
- {
- printf("The file cannot be opened");
- exit(1);
- }
- struct products *prev_products = products;
- struct products *curr_products = NULL;
- while (1)
- {
- struct product product;
- if (fread(&product, sizeof(struct product), 1, fp) != 1)
- {
- if (feof(fp))
- {
- break;
- }
- printf("reading error");
- break;
- }
- curr_products = malloc(sizeof(struct products));
- if (curr_products == NULL)
- {
- printf("Memory allocation error");
- return 1;
- }
- curr_products->val = product;
- curr_products->next = NULL;
- prev_products->next = curr_products;
- prev_products = curr_products;
- }
- fclose(fp);
- return 0;
- }
- int save_products(struct products *products)
- {
- FILE *fp = NULL;
- if ((fp = fopen("data", "wb")) == NULL)
- {
- printf("The file cannot be opened");
- return 2;
- }
- while (1)
- {
- products = products->next;
- if (products == NULL)
- {
- break;
- }
- if (fwrite(&products->val, sizeof(struct product), 1, fp) != 1)
- {
- printf("writening error");
- break;
- }
- }
- fclose(fp);
- return 0;
- }
- struct products* find_by_id(struct products *products,unsigned long long id)
- {
- struct products *iterate_products = products->next;
- while(1)
- {
- if (iterate_products == NULL)
- {
- break;
- }
- if (iterate_products->val.id == id)
- {
- return iterate_products;
- }
- iterate_products = iterate_products->next;
- }
- return NULL;
- }
- void delete_by_id(struct products *products, unsigned long long id)
- {
- struct products *prev_products = products;
- struct products *iterate_products = products->next;
- while(1)
- {
- if (iterate_products == NULL)
- {
- printf("The warehouse is empty.");
- break;
- }
- if (iterate_products->val.id == id)
- {
- prev_products->next = iterate_products->next;
- break;
- }
- iterate_products = iterate_products->next;
- prev_products = prev_products->next;
- }
- }
- time_t string_to_time(char *string_time)
- {
- time_t result = 0;
- int day = 0, month = 0, year = 0;
- if (sscanf(string_time, "%2d.%2d.%4d", &day, &month, &year) == 3)
- {
- struct tm time = {0};
- time.tm_year = year - 1900;
- time.tm_mon = month - 1;
- time.tm_mday = day;
- if ((result = mktime(&time)) == (time_t) - 1)
- {
- perror("mktime");
- return 1;
- }
- }
- return result;
- }
- int add_new_product(struct products *products)
- {
- struct product product;
- unsigned long long id=0;
- char name[51] = "",date[11] = "";
- double price=0;
- int quantity=0;
- do{
- printf("Insert id: ");
- fflush(stdin);
- scanf("%llu", &id);
- }while(!(isUniqueID(products, id)) || !id || id > 999999999999|| id<100000000000);
- printf("Insert name: ");
- scanf("%50s",name);
- printf("Insert price: ");
- scanf("%lf", &price);
- printf("Insert quantity: ");
- scanf("%d", &quantity);
- printf("Insert date /in dd.mm.yyyy format/: ");
- scanf("%s", date);
- product.id = id;
- strcpy(product.name, name);
- product.price = price;
- product.quantity = quantity;
- strcpy(product.date, date);
- struct products *last_products = products;
- while(last_products->next)
- {
- last_products = last_products->next;
- }
- struct products *next_products = NULL;
- next_products = malloc(sizeof(struct products));
- if (!next_products)
- {
- printf("Memory allocation error.");
- return -1;
- }
- next_products->val = product;
- next_products->next = NULL;
- last_products->next = next_products;
- return 0;
- }
- int print_product_with_id(struct products *products)
- {
- unsigned long long id=0;
- printf("Insert id: ");
- scanf("%llu", &id);
- struct products *products_with_id = find_by_id(products, id);
- if (products_with_id == NULL)
- printf("Missing product!\n");
- else
- {
- printf("id: %llu\n", products_with_id->val.id);
- printf("name: %s\n", products_with_id->val.name);
- printf("price: %.2f\n", products_with_id->val.price);
- printf("quantity: %d\n", products_with_id->val.quantity);
- printf("date: %s\n", products_with_id->val.date);
- }
- return 0;
- }
- int print_all_expired_products(struct products *products)
- {
- char date[11];
- printf("Insert date to compare /in dd.mm.yyyy format/: ");
- scanf("%s",date);
- time_t date_t = string_to_time(date);
- if (date_t == -1)
- {
- return -1;
- }
- struct products *iterate_products = products->next;
- while(1)
- {
- if (iterate_products == NULL)
- {
- break;
- }
- time_t product_date_t = string_to_time(iterate_products->val.date);
- if (product_date_t == -1)
- {
- return -1;
- }
- if (difftime(product_date_t, date_t) < 0)
- {
- printf("id: %llu\n", iterate_products->val.id);
- printf("name: %s\n", iterate_products->val.name);
- printf("price: %.2f\n", iterate_products->val.price);
- printf("quantity: %d\n", iterate_products->val.quantity);
- printf("date: %s\n", iterate_products->val.date);
- }
- iterate_products = iterate_products->next;
- }
- return 0;
- }
- int change_quantity_of_product(struct products *products)
- {
- unsigned long long id=0;int quantity=0;
- printf("Insert id: ");
- scanf(" %llu", &id);
- printf("Insert quantity to change[(+)for adding and (-)for subtraction]:");
- scanf("%d", &quantity);
- struct products *products_with_id = find_by_id(products, id);
- if (products_with_id == NULL)
- printf("Missing product!\n");
- else
- {
- int new_quantity = products_with_id->val.quantity + quantity;
- if (new_quantity < 0)
- printf("Quantity to change is bigger than product quantity!\n");
- else if (new_quantity > 0)
- products_with_id->val.quantity = new_quantity;
- else if (new_quantity == 0)
- delete_by_id(products, id);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement