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>
- struct product {
- unsigned long long id;
- char name[51];
- double price;
- int quantity;
- int day;
- int month;
- int year;
- };
- struct products {
- struct product val;
- struct products *next;
- };
- int comparedate(int day1, int month1, int year1, int day2, int month2, int year2);
- 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*);
- int main()
- {
- int option = 0,exit_flag = 1;
- struct products *products = NULL;
- //products = malloc(sizeof(struct products));
- // if (products == NULL)
- //{
- // printf("Memory allocation error");
- // return 1;
- // }
- // products->next;
- if (load_products(products) == 1)
- {
- printf("Memory allocation error");
- 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.Show products in the warehouse *\n");
- printf("* 6.Exit *\n");
- printf("* *\n");
- printf("******************************************\n");
- printf("Insert option: ");
- if (scanf("%d", &option) == EOF)
- {
- break;
- }
- fflush(stdin);
- 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:
- if (showproducts(products)) {
- return ;
- }
- break;
- case 6:
- exit_flag = 0;
- break;
- default:
- break;
- }
- fflush(stdin);
- 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 comparedate(int day1, int month1, int year1, int day2, int month2, int year2)
- {
- if(year2>year1)
- {
- return 1;
- }
- else if(year2==year1 && month2>month1)
- {
- return 1;
- }
- else if(year2==year1 && month2==month1 && day2>=day1)
- {
- return 1;
- }
- return 0;
- }
- int print_all_expired_products(struct products *products)
- {
- if(products==NULL)
- {
- printf("List is empty!\n");
- return;
- }
- struct products* current_product;
- int day,month,year;
- printf("Enter date(DD/MM/YYYY): ");
- scanf("%d/%d/%d",&day,&month,&year);
- //struct products *iterate_products = products->next;
- for(current_product=products;current_product!=NULL;current_product=current_product->next)
- {
- if(comparedate(current_product->val.day,current_product->val.month,current_product->val.year,day,month,year))
- {
- printf("id: %llu\n", current_product->val.id);
- printf("name: %s\n", current_product->val.name);
- printf("price: %.2f\n",current_product->val.price);
- printf("quantity: %d\n", current_product->val.quantity);
- //printf("date: %s\n", iterate_products->val.date);
- }
- }
- //iterate_products = iterate_products->next;
- return 0;
- }
- 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 showproducts(struct products* products)
- {
- if(products==NULL)
- {
- printf("List is empty!\n");
- return 1;
- }
- struct products* current_product;
- for(current_product=products;current_product!=NULL;current_product=current_product->next)
- {
- printf("id: %llu\n", current_product->val.id);
- printf("name: %s\n", current_product->val.name);
- printf("price: %.2f\n", current_product->val.price);
- printf("quantity: %d\n", current_product->val.quantity);
- printf("date: %d.%d.%d\n", current_product->val.day,current_product->val.month,current_product->val.year);
- }
- return 0;
- }
- int save_products(struct products *products)
- {
- FILE *fp = NULL;
- if ((fp = fopen("data", "ab")) == 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)
- {
- printf("The warehouse is empty.");
- exit(212);
- }
- 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;
- }
- }
- int add_new_product(struct products *products)
- {
- struct product product;
- unsigned long long id=0;
- do{
- printf("Insert id: ");
- fflush(stdin);
- scanf("%llu", &product.id);
- }while(!(isUniqueID(products, product.id)) || product.id > 999999999999|| product.id<100000000000);
- printf("Insert name: ");
- scanf("%50s",product.name);
- printf("Insert price: ");
- scanf("%lf", &product.price);
- printf("Insert quantity: ");
- scanf("%d", &product.quantity);;
- printf("Insert date /in dd.mm.yyyy format/: ");
- scanf("%d/%d/%d",&product.day,&product.month,&product.year);
- if(product.month<1 || product.month>12 || product.day<1 || product.day>31 || product.year<0)
- {
- printf("The date is incorrect.");
- return 1;
- }
- 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==NULL)
- {
- 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: %.2d/%.2d/%.4d;\n", products_with_id->val.day,products_with_id->val.month,products_with_id->val.year);
- }
- 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