This library is a part of a system that I am doing.
It's to control the item in stock of a sort of business.
If someone misses some variable and could suggest I
would be thankfull.
Item.h
Code:
#ifndef ITEM_H
#define ITEM_H
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif /// __cplusplus
typedef enum {
Off, On
} STATUS;
typedef struct Date DATE;
struct Date {
char day, month;
int year;
};
DATE date_set(char day, char month, int year);
typedef struct Item ITEM;
struct Item {
char * description;
double price, interest;
long int barcode, lot, amount;
DATE manufacture, maturity, buy;
ITEM * preview, *next;
};
ITEM * item_set(char * description, double price, double interest, long int lot, long int amount, long int barcode, DATE manufacture, DATE maturity, DATE buy);
STATUS item_next_front(ITEM ** to, ITEM * next);
STATUS item_next_back(ITEM ** to, ITEM * next);
ITEM * item_search(ITEM * to, long int barcode);
void item_show_console(ITEM * to);
STATUS item_remove(ITEM ** to, long int barcode);
STATUS item_destroy(ITEM * to);
#ifdef __cplusplus
}
#endif /// __cplusplus
#endif /// ITEM_H
Item.c
Code:
#include "item.h"
DATE date_set(char day, char month, int year) {
DATE to;
to.day = day;
to.month = month;
to.year = year;
return (to);
}
ITEM * item_set(char * description, double price, double interest, long int lot, long int amount, long int barcode, DATE manufacture, DATE maturity, DATE buy) {
ITEM * to = (ITEM *) malloc(sizeof (ITEM));
to->description = (char *) calloc((1 + strlen(description)), sizeof (char));
sprintf(to->description, "%s", description);
to->price = price;
to->interest = interest;
to->barcode = barcode;
to->amount = amount;
to->lot = lot;
to->manufacture = manufacture;
to->maturity = maturity;
to->buy = buy;
to->preview = to->next = NULL;
return (to);
}
STATUS item_next_front(ITEM ** to, ITEM * next) {
if (next == NULL || item_search(*to, next->barcode))return (Off);
next->next = *to;
if (*to != NULL) {
(*to)->preview = next;
}
*to = next;
return (On);
}
STATUS item_next_back(ITEM ** to, ITEM * next) {
if (next == NULL || item_search(*to, next->barcode))return (Off);
if (*to != NULL) {
ITEM * update = *to, * preview = NULL;
while (update->next != NULL) {
preview = update;
update = update->next;
}
update->preview = preview;
update->next = next;
return (On);
}
*to = next;
return (On);
}
ITEM * item_search(ITEM * to, long int barcode) {
if (to != NULL) {
ITEM * update;
for (update = to; update != NULL; update = update->next) {
/// printf("###%li %li\n",update->id,id);
if (update->barcode == barcode) {
return (update);
}
}
}
return (NULL);
}
void item_show_console(ITEM * to) {
printf("Description: %s\n"
"Interest(%%): %lf\n"
"Price: %lf\n"
"Lot: %ld\n"
"Amount: %li\n"
"Subtotal: %lf\n"
"Manufacture: (%d/%d/%d)\n"
"Maturity: (%d/%d/%d)\n"
"Buy: (%d/%d/%d)\n", to->description, to->interest, to->price, to->lot, to->amount,(to->price*to->amount)
, to->manufacture.day, to->manufacture.month, to->manufacture.year
, to->maturity.day, to->maturity.month, to->maturity.year
, to->buy.day, to->buy.month, to->buy.year);
}
STATUS item_remove(ITEM ** to, long int barcode) {
ITEM * preview = NULL, *update = *to;
while (update != NULL && update->barcode != barcode) {
preview = update;
update = update->next;
}
if (update == NULL)return (Off);
if (preview == NULL)*to = update->next;
else preview->next = update->next;
return (On);
}
STATUS item_destroy(ITEM * to) {
if (to == NULL)return (Off);
ITEM * next = NULL;
while (to != NULL) {
next = to;
free(to), to = to->next;
}
return (On);
}
Main.c
Code:
/*
* File: main.c
* Author: Bean Dragon
*
* Created on 5 de Junho de 2015, 21:57
*/
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
int main(int c, char ** v) {
double price = 5.0, interest = 30.0;
long int lot = 0, amount = 10, barcode = 0;
ITEM * item = item_set("Item 0", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)), * next;
interest = 20.0,amount = 25,barcode = 1;
item_next_front(&item, item_set("Item 1", price, interest, lot, amount, barcode, date_set(5, 6, 2015), date_set(5, 7, 2015), date_set(5, 6, 2015)));
for (next = item; next != NULL; next = next->next)
item_show_console(next), printf("\n");
return (0);
}