#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZEMAX 10 //Indicamos el numero maximo de registros
struct stRegistro {
int ID;
char nombre[25];
int cantidad;
};
int Menu(void);
void ChangeStock(char *operacion, struct stRegistro *reg);
void MostrarCabecera(void);
void MostrarProducto(long n, struct stRegistro *reg);
long LeeNumero(void);
int Borrar(struct stRegistro *reg, long numero);
int Confirmar(char *operacion);
int CountReg(struct stRegistro *reg);
//--------------------------------------------------------------------------
int main()
{
struct stRegistro reg[SIZEMAX];
struct stRegistro aux;
char nombre[25];
int i, opcion, nRegistros, encontrado, ID, retval;
long numero;
memset(reg, 0, sizeof(reg));
do {
opcion = Menu();
switch(opcion) {
case '1': // Añadir o incrementar el stock
encontrado = 0;
nRegistros = CountReg(reg);
ChangeStock("Añadir al stock", &aux);
for(i = 0; i<nRegistros; i++)
{
if(strcmp(aux.nombre, reg[i].nombre) == 0 &&
Confirmar("incremenar la existencia del producto indicado") == 1)
{
reg[i].cantidad += aux.cantidad;
encontrado = 1;
printf("\nSe a incrementado satisfactoriamente el stockaje del producto.\n\n");
break;
}
}
if(encontrado == 0 && Confirmar("incorporar un nuevo producto al stock") == 1)
{
if(nRegistros == 0)
ID = 1;
else
{
ID = reg[i-1].ID + 1;
}
reg[i].ID = ID;
strcpy(reg[i].nombre, aux.nombre);
reg[i].cantidad = aux.cantidad;
printf("\nSe ha añadido satisfactoriamente una nueva entrada al stockage.\n\n");
}
system("PAUSE");
break;
case '2': // Ventas de stockaje
encontrado = 0;
nRegistros = CountReg(reg);
ChangeStock("Venta de stockaje", &aux);
for(i = 0; i < nRegistros; i++)
{
if(strcmp(aux.nombre, reg[i].nombre) == 0)
{
encontrado = 1;
if(aux.cantidad <= reg[i].cantidad)
{
reg[i].cantidad -= aux.cantidad;
printf("\nLa venta se ha realizado satisfactoriamente.\n\n");
}else{
printf("\nNo hay suficiente stockaje de ese producto para esta venta.\n");
MostrarCabecera();
MostrarProducto(i, ®[i]);
printf("|---------|-------------------------|--------|\n\n");
}
break;
}
}
if(encontrado == 0)
{
printf("\nNo se puede encontrar el producto indicado.\n\n");
}
system("PAUSE");
break;
case '3': // Mostrar stock de un producto por su ID
encontrado = 0;
system("cls");
printf("Buscar stock por ID: ");
ID = LeeNumero();
nRegistros = CountReg(reg);
for(i = 0; i < nRegistros; i++)
{
if(ID == reg[i].ID)
{
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®[i]);
break;
}
}
if(encontrado == 0)
{
printf("\nNo se puede encontrar el producto indicado.\n\n");
}
else
{
printf("|---------|-------------------------|--------|\n\n");
}
system("PAUSE");
break;
case '4': // Mostrar stock de un producto por su nombre
encontrado = 0;
system("cls");
printf("Buscar stock por nombre: ");
fgets(nombre, 25, stdin);
for(i = strlen(nombre) - 1; i && nombre[i] < ' '; i--)
nombre[i] = 0;
nRegistros = CountReg(reg);
for(i = 0; i < nRegistros; i++)
{
if(strcmp(nombre, reg[i].nombre) == 0)
{
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®[i]);
break;
}
}
if(encontrado == 0)
{
printf("\nNo se puede encontrar el producto indicado.\n\n");
system("PAUSE");
}
else
{
printf("|---------|-------------------------|--------|\n\n");
}
system("PAUSE");
break;
case '5': // Mostrar todo el stockaje
encontrado = 0;
numero = 0;
system("cls");
nRegistros = CountReg(reg);
if(nRegistros > 0)
{
encontrado = 1;
MostrarCabecera();
for(i = 0; i < nRegistros; i++)
{
MostrarProducto(numero++, ®[i]);
}
printf("|---------|-------------------------|--------|\n\n");
}
if(encontrado == 0)
{
printf("\nNo existen entradas en el registro.\n");
}
system("PAUSE");
break;
case '6': // Eliminar del stock
system("cls");
printf("Eliminar del stock: ");
numero = LeeNumero();
retval = Borrar(reg, numero);
if(retval == 0)
{
printf("\nNo existe ese ID de producto.\n\n");
}
else if(retval == 1)
{
printf("\nEntrada borrada satisfactoriamente.\n\n");
}
else if(retval == -1)
{
printf("\nNo se ha realizado ningun cambio.\n\n");
}
system("PAUSE");
break;
}
} while(opcion != '0');
return 0;
}
//--------------------------------------------------------------------------
// Muestra un menú con las opciones disponibles y captura una opción del usuario
int Menu(void)
{
char resp[20];
do {
system("cls");
printf("MENU PRINCIPAL\n");
printf("--------------\n\n");
printf("1- Añadir al stock\n");
printf("2- Venta\n");
printf("3- Buscar stock por su ID\n");
printf("4- Buscar stock por su nombre\n");
printf("5- Mostrar todo el stockaje\n");
printf("6- Eliminar producto del stock por su ID\n");
printf("0- Salir\n");
fgets(resp, 20, stdin);
} while(resp[0] < '0' && resp[0] > '6');
return resp[0];
}
//--------------------------------------------------------------------------
// Permite que el usuario introduzca un producto por pantalla
void ChangeStock(char *operacion, struct stRegistro *reg)
{
int i;
char numero[6];
system("cls");
printf("%s:", operacion);
printf("\n\n");
printf("Nombre: ");
fgets(reg->nombre, 25, stdin);
// la función fgets captura el retorno de línea, hay que eliminarlo:
for(i = strlen(reg->nombre) - 1; i && reg->nombre[i] < ' '; i--)
reg->nombre[i] = 0;
printf("Cantidad: ");
fgets(numero, 6, stdin);
reg->cantidad = atoi(numero);
}
//--------------------------------------------------------------------------
// Muestra la cabecera de la tabla por pantalla
void MostrarCabecera(void)
{
printf("\n");
printf("|---------|-------------------------|--------|\n"
"|ID |Nombre |Cantidad|\n"
"|---------|-------------------------|--------|\n");
}
//--------------------------------------------------------------------------
// Muestra un producto por pantalla
void MostrarProducto(long n, struct stRegistro *reg)
{
printf("|[%6ld] |%-25s| %4d |\n", reg->ID, reg->nombre, reg->cantidad);
}
//--------------------------------------------------------------------------
// Lee un número suministrado por el usuario
long LeeNumero()
{
char numero[6];
fgets(numero, 6, stdin);
return atoi(numero);
}
//--------------------------------------------------------------------------
// Borra una entrada del archivo
int Borrar(struct stRegistro *reg, long numero)
{
struct stRegistro temp[SIZEMAX];
int nRegistros, i, j, encontrado = 0;
nRegistros = CountReg(reg);
for(i = 0, j = 0; i < nRegistros; i++)
{
if(numero != reg[i].ID)
{
temp[j].ID = reg[i].ID;
strcpy(temp[j].nombre,reg[i].nombre);
temp[j].cantidad = reg[i].cantidad;
j++;
}
else
{
encontrado = 1;
}
}
if(encontrado == 1)
{
if(Confirmar("eliminar la entrada indicada"))
{
memset(reg, 0, sizeof(struct stRegistro) * nRegistros);
for(i = 0; i < nRegistros - 1; i++)
{
reg[i].ID = temp[i].ID;
strcpy(reg[i].nombre, temp[i].nombre);
reg[i].cantidad = temp[i].cantidad;
}
}
else
{
encontrado = -1;
}
}
return encontrado;
}
//--------------------------------------------------------------------------
int Confirmar(char *operacion)
{
char valor;
int salida = 0;
printf("Esta apunto de %s. Pulse 'S' y pulse intro para confirmar: ", operacion);
valor = getchar();
if(valor == 's' || valor == 'S')
{
salida = 1;
}
return salida;
}
//--------------------------------------------------------------------------
int CountReg(struct stRegistro *reg)
{
int i;
for(i = 0; i < SIZEMAX; i++)
if(strcmp(reg[i].nombre, "")==0)
break;
return i;
}
//--------------------------------------------------------------------------