Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <DOS.H>
- // Definicion de Constantes
- #define FILE_CLIENTES "clientes.dat"
- #define FILE_PELICULAS "cula.dat"
- #define FILE_CLAVES "claves.dat"
- #define FILE_ALQUILER "Alquila.dat"
- #define FILE_CONFIG "config.dat"
- #define FILE_PELICULAS_INDICE "peli_ind.dat"
- #define FILE_LOG_BAJAS "bajas_pelis.log"
- #define FILE_ESTADISTICA "estadistica.dat"
- #define p printf
- #define s scanf
- #define g getch
- // Declaracion de Estructuras
- typedef struct
- {
- int codigo_pel;
- char comentario[200];
- }comen;
- typedef struct
- {
- int cod_cli;
- int cod_pel;
- } devolucion;
- typedef struct {
- char propiedad[30];
- char valor[30];
- } Config;
- typedef struct {
- int codigo;
- char nombre[30];
- char apellido[30];
- char direccion[30];
- char telefono[20];
- char estado; // Admite A (Activo) D(desactivado)
- int alquileresrealizados;
- } Cliente;
- typedef struct {
- int codigo;
- char titulo[30];
- int copias;
- int alquiladas;
- char descripcion[100];
- int vecesalquilada;
- int estado;
- } Pelicula;
- typedef struct {
- int valido;
- char clave[11];
- } Validacion;
- typedef struct
- {
- char nombre[11];
- char clave[11];
- }Usuario;
- typedef struct {
- int codigo;
- long posicion;
- }Indice;
- typedef struct date dia;
- typedef struct
- {
- int codcli;
- int codpel;
- dia fecha;
- char estado;
- }Alquiler;
- typedef struct
- {
- int anio;
- float contmeses[12];
- float porcmeses[12];
- float max;
- }Estadistica;
- typedef struct
- {
- int codpel;
- dia fecha;
- }peli_baja;
- // Funcion Helper
- int ExisteArchivo(char* FileName) ;
- // Archivo de configuracion
- Config LeerPropiedad(char* propiedad);
- void EscribirPropiedad(char* propiedad, char* valor);
- // Validacion de Usuario
- void AgregarUsuario(void);
- void alta_usuario(void);
- int RealizarValidacion(void);
- void ingreso_de_clave();
- char menu(void);
- Validacion validar_usuario(char*);
- // Funciones para Manejo de Clientes
- Cliente* ObtenerCliente_MemoriaDinamica(Cliente,int);
- void ConsultarCliente_MemoriaDinamica(void);
- long ObtenerPosicionCliente(int); // Recibe el codigo de cliente y devuelve el numero de registro en el archivo o -1
- Cliente ObtenerCliente(Cliente,int);
- void AgregarCliente(void);
- void EliminarFisicaCliente(int);
- void ModificarCliente(void);
- void EliminarCliente(void); // Se encarga de la baja logica y fisica, cuando si pide la baja de un cliente Activo hace baja logica, y cuando se pide baja de un Cliente Desactivado hace baja fisica.
- void ListarClientes(void);
- void ConsultarCliente(void);
- // Funciones de Menu para Peliculas
- void AgregarPelicula(void);
- void EliminarPelicula(void);
- void EliminarFisicaPelicula(int);
- void ModificarPelicula(void);
- long ObtenerPosicionPelicula(int);
- void ListarPeliculas(void);
- //void ListarClientes(void);
- void ConsultarPelicula(void);
- void Listar_EncaClientes(void);
- // Funciones para Manejo de Alquileres
- int Menu_Alquiler(void);
- void RegistrarAlquiler(void);
- void ListarAlquileres(void);
- void PeliculasAlquiladas(void);
- int Menu_Devolucion(void);
- void cargar(void);
- void mostrar(void);
- // Funciones para Menues
- int Menu_Reporte(void);
- int Menu_Principal(void);
- int Menu_Clientes(void);
- int Menu_Peliculas(void);
- int Menu_Busqueda_NomApeCodigo(void);
- // Funciones para Registro de Bajas
- void registrar_bajas (int);
- void ListarBajas(void);
- // Funciones para información estadística
- void Crear_Estad_Alq (void);
- Pelicula ObtenerPelicula(Pelicula,int);
- void ListarEstadistica(void);
- // MAIN - Inicio
- void main()
- {
- int op, opsubmenu;
- // Parte de validacion
- int intentosFallidos = 0;
- do
- {
- if (RealizarValidacion() == 0)
- break;
- else
- intentosFallidos++;
- if (intentosFallidos == 3) exit(0);
- }
- while (intentosFallidos < 3);
- if(ExisteArchivo(FILE_CONFIG)==0)
- {
- EscribirPropiedad("codclien","1");
- EscribirPropiedad("codpeli","1");
- }
- do
- {
- op=Menu_Principal();
- switch(op)
- {
- case 1:
- do
- {
- opsubmenu = Menu_Clientes();
- if (opsubmenu == 1) AgregarCliente();
- if (opsubmenu == 2) EliminarCliente();
- if (opsubmenu == 3) ModificarCliente();
- if (opsubmenu == 4) ConsultarCliente_MemoriaDinamica();//ConsultarCliente();
- }
- while(opsubmenu!=5);
- break;
- case 2:
- do
- {
- opsubmenu = Menu_Peliculas();
- if (opsubmenu == 1) AgregarPelicula();
- if (opsubmenu == 2) EliminarPelicula();
- if (opsubmenu == 3) ModificarPelicula();
- break;
- }
- while(opsubmenu!=4);
- break;
- case 3:
- opsubmenu = Menu_Alquiler();
- if (opsubmenu == 1) RegistrarAlquiler();
- if (opsubmenu == 2) ListarAlquileres();
- break;
- case 4:
- Menu_Devolucion();
- break;
- case 5:
- do
- {
- opsubmenu = Menu_Reporte();
- if (opsubmenu == 1) ListarClientes();
- if (opsubmenu == 2) ListarPeliculas();
- if (opsubmenu == 3) PeliculasAlquiladas();
- if (opsubmenu == 4) ListarBajas();
- if (opsubmenu == 5) ListarEstadistica();
- }
- while(opsubmenu<6);//AGREGAR
- break;
- case 6:
- if (RealizarValidacion() == 0)
- {
- alta_usuario();
- break;
- }
- else
- break;
- case 7:
- Crear_Estad_Alq();
- break;
- }
- }while(op<8);
- return;
- }
- // MAIN - Fin
- // INICIO - Manejo de Claves
- int RealizarValidacion()
- {
- // 0 Usuario OK, 1 - Clave Incorrecta - 1 Usuario Inexistente
- char pass[11],car;
- char usu[11];
- int i=0;
- Validacion x;
- clrscr();
- printf("Usuario: ");
- fflush(stdin);
- gets(usu);
- x=validar_usuario(usu);
- if(x.valido==1)
- {
- printf("Ingrese la clave: ");
- while((car=getch())!='\r'&&i<10)
- {
- pass[i++]=car;
- printf("%c",'*');
- }
- pass[i]=NULL;
- if(strcmp(x.clave,pass)==0)
- return 0;
- else
- return 1;
- }
- else
- return -1;
- }
- void alta_usuario(void)
- {
- FILE *ptr;
- char aux[200],cla1[11],cla2[11];
- Usuario reg;
- ptr=fopen(FILE_CLAVES,"a+b");
- clrscr();
- printf("Ingrese nombre de usuario: ");
- fflush(stdin);
- gets(aux);
- aux[10]=NULL;
- strcpy(reg.nombre,aux);
- printf("Ingrese clave: ");
- gets(cla1);
- printf("Confirme clave: ");
- gets(cla2);
- if(strcmp(cla1,cla2)==0)
- {
- strcpy(reg.clave,cla1);
- fwrite(®,sizeof(reg),1,ptr);
- }
- fclose(ptr);
- }
- Validacion validar_usuario(char*usu)
- {
- FILE *ptr;
- Usuario reg;
- Validacion reg1;
- int x=0;
- if ( strcmpi(usu,"admin") == 0)
- {
- reg1.valido = 1;
- strcpy(reg1.clave,"admin123");
- }
- else
- {
- ptr=fopen(FILE_CLAVES,"rb");
- fread(®,sizeof(reg),1,ptr);
- while(!feof(ptr))
- {
- if(strcmp(reg.nombre,usu)==0)
- {
- strcpy(reg1.clave,reg.clave);
- x=1;
- break;
- }
- fread(®,sizeof(reg),1,ptr);
- }
- reg1.valido=x;
- fclose(ptr);
- }
- return reg1;
- }
- // FIN - Manejo de Claves
- // Menu_Principal - Inicio
- int Menu_Principal(void)
- {
- int op;
- do
- {
- clrscr();
- gotoxy(38,5);
- p("Video Club El LP3");
- gotoxy(36,8);
- p("1 - Admistracion de Clientes");
- gotoxy(36,9);
- p("2 - Administracion de Peliculas");
- gotoxy(36,10);
- p("3 - Alquileres");
- gotoxy(36,11);
- p("4 - Devoluciones");
- gotoxy(36,12);
- p("5 - Reportes");
- gotoxy(36,13);
- p("6 - Usuarios");
- gotoxy(36,14);
- p("7 - Crear Estadistica"); //AGREGAR
- gotoxy(36,15);
- p("8 - Salir");
- gotoxy(36,17); //AGREGAR
- p("Ingrese una opcion: ");
- s("%d",&op);
- }while(op<1||op>8);
- return op;
- }
- // Menu_Principal - Fin
- // Menu_Clientes - Inicio
- int Menu_Clientes(void)
- {
- int op;
- flushall();
- do
- {
- clrscr();
- gotoxy(38,5);
- printf("Administracion de Clientes");
- gotoxy(36,8);
- printf("1 - Altas");
- gotoxy(36,9);
- printf("2 - Bajas");
- gotoxy(36,10);
- printf("3 - Modificaciones");
- gotoxy(36,11);
- printf("4 - Consultas");
- gotoxy(36,12);
- printf("5 - Salir");
- gotoxy(36,13);
- printf("Ingrese una opcion: ");
- scanf("%d",&op);
- }while(op<1||op>5);
- return op;
- }
- // Menu_Clientes - Fin
- // Menu_Peliculas - Inicio
- int Menu_Peliculas(void)
- {
- int op;
- do
- {
- clrscr();
- gotoxy(38,5);
- printf("Administracion de Peliculas");
- gotoxy(36,8);
- printf("1- Altas");
- gotoxy(36,9);
- printf("2- Bajas");
- gotoxy(36,10);
- printf("3- Modificaciones");
- gotoxy(36,11);
- printf("4- Consultas");
- gotoxy(36,12);
- printf("5- Salir");
- gotoxy(36,13);
- printf("Ingrese una opcion: ");
- scanf("%d",&op);
- }while(op<1||op>5);
- return op;
- }
- // Menu_Peliculas - Fin
- // Menu_Alquiler - Inicio
- int Menu_Alquiler(void)
- {
- int op;
- do
- {
- clrscr();
- gotoxy(38,5);
- p("Registro del Alquiler");
- gotoxy(36,9);
- p("1 - Registro de Alquiler");
- gotoxy(36,10);
- p("2 - Listado de Alquileres");
- gotoxy(36,11);
- p("3 - Salir");
- gotoxy(36,13);
- p("Ingrese una opcion: ");
- s("%d",&op);
- }while(op<1||op>3);
- return op;
- }
- // Menu_Alquiler - Fin
- // Menu_Devolucion - Inicio
- int Menu_Devolucion(void)
- {
- int cond;
- // char op;
- clrscr();
- {
- devolucion reg;
- FILE *pf;
- pf = fopen("c:\\devoluciones.txt","wb");
- if (pf==NULL)
- {
- clrscr();
- printf("error de apertura");
- getch();
- exit(1);
- }
- //carga del archivo
- clrscr();
- gotoxy(20,10);
- p("SELECCIONO INGRESAR UNA DEVOLUCION");
- printf("\n\n\n\nIngrese el c¢digo de la pelicula devuelta: ");
- scanf("%d", ®.cod_pel);
- while (reg.cod_pel)
- {
- printf("\n\nIngrese el c¢digo del cliente: ");
- fflush(stdin);
- scanf("%d",®.cod_cli);
- fwrite(®,sizeof(reg),1,pf);
- clrscr();
- printf("Ingrese el c¢digo de la pelicula devuelta<0= salir>: ");
- scanf("%d", ®.cod_pel);
- }
- fclose(pf);
- }
- printf("\n \nDesea agregar un comentario? \n SI=1, NO=2 ->");
- fflush(stdin);
- scanf("%d",&cond);
- if (cond==1)
- {
- cargar();
- }
- // mostrar();
- return 0;
- }
- // Menu_Devolucion - Fin
- // Menu_Reporte - Inicio
- int Menu_Reporte(void)
- {
- int op;
- do
- {
- clrscr();
- gotoxy(38,5);
- printf("Reportes");
- gotoxy(36,8);
- printf("1 - Listado de Clientes");
- gotoxy(36,9);
- printf("2 - Listado de Peliculas");
- gotoxy(36,10);
- printf("3 - Peliculas Alquiladas");
- gotoxy(36,11);
- printf("4 - Listar Peliculas Dadas de Baja"); //AGREGAR
- gotoxy(36,12);
- printf("5 - Listar Informacion Estadistica"); //AGREGAR
- gotoxy(36,13);
- printf("6 - Salir");
- gotoxy(36,14);
- printf("Ingrese una opcion: ");
- scanf("%d",&op);
- }while(op<1||op>6); //AGREGAR
- return op;
- }
- // Menu_Reporte - Fin
- void ModificarCliente(void)
- {
- int codigo;
- char continua,otro,conf;
- Cliente cliente,cliente_NuevosDatos,aux;
- long posicion;
- FILE *p1;
- clrscr();
- gotoxy(24,1);
- printf("Ingrese el Codigo del Cliente a Buscar: ");
- scanf("%d",&codigo);
- posicion=ObtenerPosicionCliente(codigo);
- clrscr();
- if(posicion == -1)
- {
- gotoxy(25,20);
- printf("CLIENTE NO ENCONTRADO");
- getch();
- return;
- }
- // Cargamos el cliente para mostrarlo antes de pedir los nuevos datos
- if((p1 = fopen(FILE_CLIENTES,"r+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL GENERAR ARCHIVO DE CLIENTE %s",FILE_CLIENTES);
- getch();
- return;
- }
- fseek(p1,posicion,SEEK_SET);
- if (fread(&cliente,sizeof(Cliente),1,p1) != 1)
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- clrscr();
- gotoxy(25,2);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(25,3);
- printf("Codigo : %d ",cliente.codigo);
- gotoxy(25,4);
- printf("Nombre : %s ",cliente.nombre);
- gotoxy(25,5);
- printf("Apellido : %s ",cliente.apellido);
- gotoxy(25,6);
- printf("Direccion : %s ",cliente.direccion);
- gotoxy(25,7);
- printf("Telefono : %s ",cliente.telefono);
- gotoxy(25,8);
- printf("Alquileres : %d ",cliente.alquileresrealizados);
- gotoxy(25,9);
- printf("Estado : %c ",cliente.estado);
- getch();
- /////////////////////////////////////////////
- // Pido los nuevos datos para el cliente
- /////////////////////////////////////////////
- do
- {
- gotoxy(25,10);
- printf("====================");
- do
- {
- gotoxy(25,11);
- printf("NOMBRE: ");
- fflush(stdin);
- gets(cliente_NuevosDatos.nombre);
- }
- while(strlen(cliente_NuevosDatos.nombre)==0);
- do
- {
- gotoxy(25,12);
- printf("APELLIDO: ");
- fflush(stdin);
- gets(cliente_NuevosDatos.apellido);
- }
- while(strlen(cliente_NuevosDatos.apellido)==0);
- //VALIDA QUE EL NOMBRE NO SE REPITA
- aux = ObtenerCliente(cliente_NuevosDatos,2);
- if(aux.codigo!=-1)
- {
- gotoxy(25,20);
- printf("YA HAY UN CLIENTE CON ESE NOMBRE");
- gotoxy(25,21);
- printf("DESEA CONTINUAR? (S/N)");
- continua = getch();
- continua = toupper(continua);
- if (continua!='S')return;
- }
- //FIN DE VALIDACION
- cliente_NuevosDatos.codigo = cliente.codigo;
- gotoxy(25,13);
- printf("CODIGO: %d", cliente.codigo);
- gotoxy(25,14);
- printf("DIRECCION: ");
- fflush(stdin);
- gets(cliente_NuevosDatos.direccion);
- gotoxy(25,15);
- printf("TELEFONO: ");
- fflush(stdin);
- gets(cliente_NuevosDatos.telefono);
- // Inicializa Estado y Alquileres Realizados
- gotoxy(25,16);
- printf("ESTADO (A/D):");
- scanf("%c",&cliente_NuevosDatos.estado);
- cliente_NuevosDatos.estado=toupper(cliente_NuevosDatos.estado);
- cliente_NuevosDatos.alquileresrealizados=cliente.alquileresrealizados;
- do
- {
- gotoxy(25,20);
- printf("1-CONFIRMA ");
- gotoxy(25,21);
- printf("2-REINGRESA");
- gotoxy(25,22);
- printf("3-CANCELA ");
- conf=getch();
- if(conf<49 || conf>51) gotoxy(9,10);
- }
- while(conf<49 || conf>51);
- if (conf==49)
- { // Si de decidio grabar y nadie se llama igual...
- fseek(p1,posicion,SEEK_SET);
- if (fwrite(&cliente_NuevosDatos,sizeof(Cliente),1,p1)!=1)
- {
- gotoxy(25,24);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- else
- {
- gotoxy(25,24);
- printf("EL CLIENTE SE ACTUALIZO");
- }
- }
- do
- {
- if(conf==51)
- {
- conf=50;//sino si cancela y sigue ingresando apareceria de vuelta
- gotoxy(25,24);
- printf("INGRESO CANCELADO");
- }
- gotoxy(25,25);
- printf("INGRESAR OTRO (S/N)? ");
- otro='s';//por las dudas
- otro=getch();
- otro=toupper(otro);
- }
- while(otro!='S' && otro!='N');
- }
- while(otro=='S');
- fclose(p1);
- }
- // Agregar Cliente - INICIO
- void AgregarCliente(void)
- {
- FILE *ARCHIVO;
- int valida;
- int siguienteCodigo;
- char siguienteCodigoStr[30];
- char otro,conf;
- Cliente name,*pname,aux,*paux;
- Config config;
- paux=&aux;
- pname=&name;
- if((ARCHIVO = fopen(FILE_CLIENTES,"a+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL ABRIR ARCHIVO");
- getch();
- return;
- }
- do
- {
- do
- {
- clrscr();
- gotoxy(25,4);
- printf("Ingreso de Clientes");
- gotoxy(25,5);
- printf("====================");
- do
- {
- gotoxy(25,6);
- printf("NOMBRE: ");
- fflush(stdin);
- gets(pname->nombre);
- }
- while(strlen(pname->nombre)==0);
- do
- {
- gotoxy(25,7);
- printf("APELLIDO: ");
- fflush(stdin);
- gets(pname->apellido);
- }
- while(strlen(pname->apellido)==0);
- //VALIDA QUE EL NOMBRE NO SE REPITA
- valida=1;
- rewind(ARCHIVO);
- while(!feof(ARCHIVO))
- {
- if(!feof(ARCHIVO))
- {
- flushall();
- if(fread(&aux,sizeof(Cliente),1,ARCHIVO)!=1 && (!feof(ARCHIVO)))exit(1);
- if(strcmpi(paux->nombre,pname->nombre)==0 && strcmpi(paux->apellido,pname->apellido)==0)
- {
- valida=0;
- break;
- }
- }
- }
- if(valida==0)
- {
- gotoxy(25,17);
- printf("YA HAY UN CLIENTE CON ESE NOMBRE");
- break;
- }
- //FIN DE VALIDACION
- gotoxy(25,8);
- printf("CODIGO: ");
- config = LeerPropiedad("codclien");
- gotoxy(35,8);
- printf(config.valor);
- siguienteCodigo = atoi(config.valor);
- pname->codigo = siguienteCodigo;
- gotoxy(25,9);
- printf("DIRECCION: ");
- fflush(stdin);
- gets(pname->direccion);
- gotoxy(25,10);
- printf("TELEFONO: ");
- fflush(stdin);
- gets(pname->telefono);
- // Inicializa Estado y Alquileres Realizados
- pname->estado='A';
- pname->alquileresrealizados=0;
- do
- {
- gotoxy(25,11);
- printf("1-CONFIRMA ");
- gotoxy(25,12);
- printf("2-REINGRESA");
- gotoxy(25,13);
- printf("3-CANCELA ");
- conf=getch();
- if(conf<49 || conf>51) gotoxy(9,10);
- }
- while(conf<49 || conf>51);
- }
- while(conf==50);
- if (conf==49 && valida==1)
- { // Si de decidio grabar y nadie se llama igual...
- if (fwrite(&name,sizeof(Cliente),1,ARCHIVO)!=1)
- {
- gotoxy(25,16);
- printf("ERROR DE ESCRITURA");
- }
- else
- {
- gotoxy(25,17);
- printf("EL DATO SE ALMACENO");
- // Actualizamos el codigo de cliente en el archivo de Configuracion
- siguienteCodigo++;
- EscribirPropiedad("codclien",itoa(siguienteCodigo,siguienteCodigoStr, 10));
- }
- }
- do
- {
- if(conf==51)
- {
- conf=50;//sino si cancela y sigue ingresando apareceria de vuelta
- gotoxy(25,17);
- printf("INGRESO CANCELADO");
- }
- gotoxy(25,18);
- printf("INGRESAR OTRO (S/N)? ");
- otro='s';//por las dudas
- otro=getch();
- otro=toupper(otro);
- }
- while(otro!='S' && otro!='N');
- }
- while(otro=='S');
- fclose(ARCHIVO);
- }
- // Agregar Cliente - FIN
- int Menu_Busqueda_NomApeCodigo(void)
- {
- int op;
- do
- {
- clrscr();
- gotoxy(38,5);
- printf("MENU TIPO DE BUSQUEDA");
- gotoxy(24,8);
- printf("1- Buscar por Apellido y Apellido");
- gotoxy(24,9);
- printf("2- Buscar por Codigo");
- gotoxy(24,10);
- printf("3- Salir");
- gotoxy(24,12);
- printf("Ingrese una opcion: ");
- scanf("%d",&op);
- }while(op<1||op>3);
- return op;
- }
- void ConsultarCliente(void)
- {
- int op,codigo;
- Cliente buscadoCliente;
- op=Menu_Busqueda_NomApeCodigo();
- clrscr();
- switch(op)
- {
- case 1:
- gotoxy(24,8);
- printf("Ingrese el Nombre y Apellido de Cliente a Buscar");
- gotoxy(24,9);
- printf("Apellido: ");
- fflush(stdin);
- gets(buscadoCliente.apellido);
- gotoxy(24,10);
- printf("Nombre: ");
- fflush(stdin);
- gets(buscadoCliente.nombre);
- buscadoCliente= ObtenerCliente(buscadoCliente,2);
- break;
- case 2:
- gotoxy(24,8);
- printf("Ingrese el Codigo del Cliente a Buscar: ");
- scanf("%d",&codigo);
- buscadoCliente.codigo = codigo;
- buscadoCliente=ObtenerCliente(buscadoCliente,1);
- break;
- case 3:
- return;
- }
- if(buscadoCliente.codigo != -1)
- {
- clrscr();
- gotoxy(24,8);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(24,9);
- printf("Codigo : %d ",buscadoCliente.codigo);
- gotoxy(24,10);
- printf("Nombre : %s ",buscadoCliente.nombre);
- gotoxy(24,11);
- printf("Apellido : %s ",buscadoCliente.apellido);
- gotoxy(24,12);
- printf("Direccion : %s ",buscadoCliente.direccion);
- gotoxy(24,13);
- printf("Telefono : %s ",buscadoCliente.telefono);
- gotoxy(24,14);
- printf("Alquileres : %d ",buscadoCliente.alquileresrealizados);
- gotoxy(24,15);
- printf("Estado : %c ",buscadoCliente.estado);
- getch();
- }
- else
- {
- gotoxy(24,18);
- printf("CLIENTE NO ENCONTRADO");
- getch();
- }
- }
- long ObtenerPosicionCliente(int codigo)
- {
- Cliente reg;
- FILE *p1;
- int ss;
- long ret;
- p1=fopen(FILE_CLIENTES,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- flushall();
- ret = ftell(p1);
- if(fread(®,sizeof(Cliente),1,p1)!=1 && (!feof(p1)))exit(1);
- ss=0;
- if(codigo == reg.codigo)
- {
- ss=1;
- break;
- }
- }
- fclose(p1);
- if(ss==0)
- {
- // Cliente No Encontrado
- ret = -1;
- }
- return ret;
- }
- Cliente ObtenerCliente(Cliente cliente, int formaBusqueda)
- {
- Cliente reg;
- FILE *p1;
- int ss;
- p1=fopen(FILE_CLIENTES,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- flushall();
- if(fread(®,sizeof(Cliente),1,p1)!=1 && (!feof(p1)))exit(1);
- ss=0;
- switch(formaBusqueda)
- {
- case 1:
- if(cliente.codigo == reg.codigo)
- ss=1;
- break;
- case 2:
- if((strcmp(strlwr(cliente.apellido),strlwr(reg.apellido))==0)
- &&
- (strcmp(strlwr(cliente.nombre),strlwr(reg.nombre))==0))
- ss=1;
- break;
- }
- if (ss==1) break;
- }
- fclose(p1);
- if(ss==0)
- {
- // Cliente No Encontrado
- reg.codigo = -1;
- }
- return reg;
- }
- void Listar_EncaClientes(void)
- {
- clrscr();
- gotoxy(25,3);
- printf("LISTADO DE CONTACTOS");
- gotoxy(5,5);
- printf("COD EST CLIENTE DIRECCION TELEFONO ");
- gotoxy(5,6);
- printf("=======================================================================");
- }
- Config LeerPropiedad(char* propiedad)
- {
- Config reg;
- FILE *p1;
- int s = 0;
- p1=fopen(FILE_CONFIG,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- if(!feof(p1))
- {
- flushall();
- if(fread(®,sizeof(Config),1,p1)!=1 && (!feof(p1)))exit(1);
- if (strcmp(strlwr(propiedad),strlwr(reg.propiedad))==0)
- {
- s = 1;
- break;
- }
- }
- }
- fclose(p1);
- if(s==0)
- {
- strcpy(reg.valor,"");
- strcpy(reg.propiedad,"");
- }
- return reg;
- }
- void EscribirPropiedad(char* propiedad, char* valor)
- {
- FILE *p1;
- int existe,cant;
- long posicion;
- Config aux,*paux;
- paux=&aux;
- // Si el archivo no existe, lo crea
- if (ExisteArchivo(FILE_CONFIG)==0)
- {
- if((p1 = fopen(FILE_CONFIG,"w+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL GENERAR ARCHIVO DE CONFIGURACION");
- getch();
- return;
- }
- else
- {
- fclose(p1);
- }
- }
- // Lo abro en modo lectura primero para buscar si la propiedad existe.
- if((p1 = fopen(FILE_CONFIG,"r+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL ABRIR ARCHIVO");
- getch();
- return;
- }
- for( ; ; )
- {
- posicion = ftell(p1);
- cant = fread(&aux,sizeof(Config),1,p1);
- if(cant!=1)
- {
- if(feof(p1)!=0)
- {
- existe = 0;
- break;
- }
- else
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- }
- else
- {
- if(strcmpi(paux->propiedad,propiedad)==0)
- {
- existe = 1;
- break;
- }
- }
- }
- strcpy(paux->propiedad,propiedad);
- strcpy(paux->valor,valor);
- // Se posiciona el puntero o abre el archivo en modo Append segun corresponda
- if (existe == 1)
- {
- fseek(p1,posicion,SEEK_SET);
- }
- else
- {
- fclose(p1);
- if((p1 = fopen(FILE_CONFIG,"a+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL ABRIR ARCHIVO");
- getch();
- return;
- }
- }
- // Escribimos el dato
- if (fwrite(&aux,sizeof(Config),1,p1)!=1)
- {
- gotoxy(25,16);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- fclose(p1);
- }
- int ExisteArchivo(char* FileName)
- {
- FILE *EXISTEARCHIVO;
- if ((EXISTEARCHIVO=fopen(FileName,"rb"))==NULL)
- {
- return 0;
- }
- else
- {
- fclose(EXISTEARCHIVO);
- return 1;
- }
- }
- void AgregarPelicula(void)
- {
- long cantreg;
- char titulo[30];
- int cantidad;
- int codigo=0;
- char codigoStr[30];
- char descripcion[100];
- Config config;
- FILE *p1,*p2;
- //Creo las estructuras auxiliares
- Pelicula reg;
- Indice reg1;
- p1=fopen(FILE_PELICULAS,"a+b");
- p2=fopen(FILE_PELICULAS_INDICE,"a+b");
- //los archivos donde se van a alnmacenar?
- //Obtengo la cantidad de registros de "Datos"
- fseek(p1,0l,SEEK_END);
- cantreg=ftell(p1)/sizeof(reg);
- clrscr();
- //CARGA DE DATOS
- //Hay que crear una función para obtener el código automáticamente
- //codigo_pelicula(codigo);
- printf("\n\n- CARGA DE DATOS DE PELICULAS -");
- config = LeerPropiedad("codpeli");
- printf("Codigo: ");
- printf("%s",config.valor);
- codigo = atoi(config.valor);
- reg.codigo=codigo;
- printf("\nTitulo: ");
- fflush(stdin);
- gets(titulo);
- strcpy(reg.titulo,titulo);
- printf("Cantidad de copias: ");
- scanf("%d",&cantidad);
- reg.copias=cantidad;
- printf("Descripcion: ");
- fflush(stdin);
- gets(descripcion);
- strcpy(reg.descripcion,descripcion);
- printf("Automáticamente se asignará estado ACTIVO a la nueva pelicula"); //AGREGAR
- reg.estado='A'; //AGREGAR
- reg.vecesalquilada=0;
- reg.alquiladas=0;
- // Informacion para archivo "Indice"
- reg1.codigo=reg.codigo;
- reg1.posicion=cantreg;
- //Grabamos archivo "Registro" e "Indice"
- if (fwrite(®,sizeof(reg),1,p1) != 1 || fwrite(®1,sizeof(reg1),1,p2)!=1)
- {
- printf("ERROR DE ESCRITURA");
- getch();
- }
- else
- {
- // Grabo bien y actualizamos el Codigo de Pelicula
- codigo++;
- EscribirPropiedad("codpeli",itoa(codigo,codigoStr, 10));
- }
- fclose(p1);
- fclose(p2);
- }
- void ListarPeliculas(void)//Funcion beta de listar peliculas
- {
- FILE *p1,*p2;
- Pelicula reg;
- Indice reg1;
- p1=fopen(FILE_PELICULAS,"rb");
- p2=fopen(FILE_PELICULAS_INDICE,"rb");
- clrscr();
- printf("Contenido:\n");
- fread(®,sizeof(reg),1,p1);
- while(!feof(p1))
- {
- printf("%-10d %-30s %d %s\n",reg.codigo,reg.titulo,reg.copias,reg.descripcion);
- fread(®,sizeof(reg),1,p1);
- }
- printf("\nContenido de indice\n");
- fread(®1,sizeof(reg1),1,p2);
- while(!feof(p2))
- {
- printf("%-10d %10d\n",reg1.codigo,reg1.posicion);
- fread(®1,sizeof(reg1),1,p2);
- }
- getch();
- fclose(p2);
- fclose(p1);
- }
- //////////////////////////////////////////////////////////////
- void EliminarCliente(void)
- {
- int codigo;
- char otro,conf;
- Cliente cliente;
- long posicion;
- FILE *p1;
- do
- {
- clrscr();
- gotoxy(24,1);
- printf("Ingrese el Codigo del Cliente a Eliminar: ");
- scanf("%d",&codigo);
- posicion=ObtenerPosicionCliente(codigo);
- clrscr();
- if(posicion == -1)
- {
- gotoxy(25,20);
- printf("CLIENTE NO ENCONTRADO");
- getch();
- return;
- }
- // Cargamos el cliente para mostrarlo antes de pedir los nuevos datos
- if((p1 = fopen(FILE_CLIENTES,"r+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL GENERAR ARCHIVO DE CLIENTE %s",FILE_CLIENTES);
- getch();
- return;
- }
- fseek(p1,posicion,SEEK_SET);
- if (fread(&cliente,sizeof(Cliente),1,p1) != 1)
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- clrscr();
- gotoxy(25,2);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(25,3);
- printf("Codigo : %d ",cliente.codigo);
- gotoxy(25,4);
- printf("Nombre : %s ",cliente.nombre);
- gotoxy(25,5);
- printf("Apellido : %s ",cliente.apellido);
- gotoxy(25,6);
- printf("Direccion : %s ",cliente.direccion);
- gotoxy(25,7);
- printf("Telefono : %s ",cliente.telefono);
- gotoxy(25,8);
- printf("Alquileres : %d ",cliente.alquileresrealizados);
- gotoxy(25,9);
- printf("Estado : %c ",cliente.estado);
- getch();
- //////////////////////////////////////////////////////////
- // En Base al Estado se fija si es baja fisica o logica //
- //////////////////////////////////////////////////////////
- if(cliente.estado=='A')
- {
- gotoxy(25,10);
- printf("BAJA LOGICA");
- cliente.estado='D';
- do
- {
- gotoxy(25,20);
- printf("1-CONFIRMA ");
- gotoxy(25,21);
- printf("2-CANCELA ");
- conf=getch();
- if(conf<49 || conf>50) gotoxy(9,10);
- }
- while(conf<49 || conf>50);
- if (conf==49)
- { // Si de decidio grabar y nadie se llama igual...
- fseek(p1,posicion,SEEK_SET);
- if (fwrite(&cliente,sizeof(Cliente),1,p1)!=1)
- {
- gotoxy(25,22);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- else
- {
- gotoxy(25,22);
- printf("EL CLIENTE SE DESACTIVO");
- }
- }
- do
- {
- if(conf==50)
- {
- gotoxy(25,22);
- printf("INGRESO CANCELADO");
- }
- gotoxy(25,23);
- printf("INGRESAR OTRO (S/N)? ");
- otro='s';//por las dudas
- otro=getch();
- otro=toupper(otro);
- }
- while(otro!='S' && otro!='N');
- }
- else
- {
- gotoxy(25,10);
- printf("BAJA FISICA");
- fcloseall();
- EliminarFisicaCliente(cliente.codigo);
- getch();
- }
- }
- while (otro=='S');
- fclose(p1);
- }
- void EliminarFisicaCliente(int codClien)
- {
- FILE *ARCHIVO,*COPIA;
- Cliente dato,*pdato;
- long cant;
- char cop[]={"CLIENTES.TMP"};
- pdato=&dato;
- fcloseall();
- if((ARCHIVO=fopen(FILE_CLIENTES,"rb"))==NULL)
- {
- gotoxy(25,14);
- printf("ERROR EN APERTURA ARCHIVO");
- printf("EL DATO NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- if((COPIA=fopen(cop,"wb"))==NULL)
- {
- gotoxy(25,14);
- printf("ERROR EN APERTURA ARCHIVO");
- gotoxy(25,15);
- printf("EL DATO NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- //COMIENZA LA TRANSFERENCIA
- rewind(ARCHIVO);
- rewind(COPIA);
- for( ; ; )
- {
- cant = fread(&dato,sizeof(Cliente),1,ARCHIVO);
- if(cant!=1)
- {
- if(feof(ARCHIVO)!=0)
- {
- break;
- }
- else
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- }
- else
- {
- if(codClien != pdato->codigo)//VERIFICO QUE NO SEA EL QUE QUIERO ELIMINAR
- {
- if(fwrite(&dato,sizeof(Cliente),1,COPIA)!=1)
- {
- gotoxy(25,14);
- printf("ERROR AL ESCRIBIR");
- getch();
- return;
- };
- }
- }
- }
- //FIN DE LA COPIA
- fcloseall();
- if(remove(FILE_CLIENTES)!=0)
- {
- gotoxy(25,13);
- printf("EL ARCHIVO ORIGINAL NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- else
- {
- if(rename(cop,FILE_CLIENTES)!=0)
- {
- gotoxy(25,13);
- printf("EL ARCHIVO COP NO SE PUDO RENOMBRAR");
- getch();
- return;
- }
- else
- {
- gotoxy(25,15);
- printf("BAJA REALIZADA");
- }
- }
- fcloseall();
- }
- ////////////////////////////////////////////////////////////////////////////////
- // REGISTRACION DE ALQUILER //
- ////////////////////////////////////////////////////////////////////////////////
- void RegistrarAlquiler(void)
- {
- int cliente, pelicula;
- int flagCliente=0,posPelicula=1, posCliente=1;//flagCliente y c son bandera!
- Cliente regCliente;
- Pelicula regPelicula;
- Alquiler regAlquiler;
- FILE *fileAlquiler, *fileCliente, *filePelicula;
- fileAlquiler=fopen(FILE_ALQUILER,"a+b");
- if(fileAlquiler==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- fileCliente=fopen(FILE_CLIENTES,"r+b");
- if(fileCliente==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- filePelicula=fopen(FILE_PELICULAS,"r+b");
- if(filePelicula==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- clrscr();
- gotoxy(1,3);
- printf("Codigo Pelicula:");
- gotoxy(1,4);
- printf("Codigo Cliente:");
- gotoxy(18,3);
- scanf("%d",&pelicula);
- gotoxy(17,4);
- scanf("%d",&cliente);
- //VERIFICA VALIDEZ DE CLIENTE!!
- fread(®Cliente,sizeof(regCliente),1,fileCliente);
- while(!feof(fileCliente))
- {
- if(regCliente.codigo==cliente) {flagCliente=1;break;}
- posCliente++;
- fread(®Cliente,sizeof(regCliente),1,fileCliente);
- }
- if(flagCliente==1)
- {
- fread(®Pelicula,sizeof(regPelicula),1,filePelicula);
- while(!feof(filePelicula))
- {
- if(regPelicula.codigo==pelicula)
- {
- if((regPelicula.copias)>(regPelicula.alquiladas))
- {
- regPelicula.alquiladas++;
- regPelicula.vecesalquilada++;
- regCliente.alquileresrealizados++;
- flagCliente=2;
- break;
- }
- else {
- printf("\nNo hay stock disponible para alquiler..");
- getch();
- break;
- }
- }
- fread(®Pelicula,sizeof(regPelicula),1,filePelicula);
- posPelicula++;
- }
- }
- else {printf("El codigo de cliente ingresado es inexistente");
- getch();
- }
- if (flagCliente==2)
- {
- // Actualizamos stock de peliculas alquiladas
- fseek(filePelicula,(posPelicula-1)*sizeof(regPelicula),SEEK_SET);
- // Escribimos el dato
- if (fwrite(®Pelicula,sizeof(regPelicula),1,filePelicula)!=1)
- {
- gotoxy(25,16);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- // Actualizamos stock de peliculas alquiladas
- fseek(fileCliente,(posCliente-1)*sizeof(regCliente),SEEK_SET);
- // Escribimos el dato
- if (fwrite(®Cliente,sizeof(regCliente),1,fileCliente)!=1)
- {
- gotoxy(25,16);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- regAlquiler.codpel=pelicula;
- regAlquiler.codcli=cliente;
- getdate(®Alquiler.fecha);
- regAlquiler.estado='A';
- fwrite(®Alquiler,sizeof(regAlquiler),1,fileAlquiler);
- printf("\n\nAlquiler registrado..");
- getch();
- }
- fclose(fileAlquiler);
- fclose(fileCliente);
- fclose(filePelicula);
- }
- ////////////////////////////////////////////////////////////////////////////
- void ListarAlquileres(void)//Funcion beta de listar peliculas
- {
- FILE *p1;
- dia f;
- Alquiler reg;
- p1=fopen(FILE_ALQUILER,"rb");
- clrscr();
- printf("Peliculas Actualmente Alquiladas:\n");
- printf("Cliente Pelicula Fecha\n");
- fread(®,sizeof(reg),1,p1);
- while(!feof(p1))
- {
- f=reg.fecha;
- printf(" %d %d %d/%d/%d\n",reg.codcli,reg.codpel,f.da_day,f.da_mon,f.da_year);
- fread(®,sizeof(reg),1,p1);
- }
- getch();
- }
- void PeliculasAlquiladas(void)//Funcion reporte peliculas alquiladas
- {
- FILE *p1;
- Alquiler reg;
- p1=fopen(FILE_ALQUILER,"rb");
- clrscr();
- printf("Peliculas Actualmente Alquiladas:\n");
- printf("Cliente Pelicula Fecha\n");
- fread(®,sizeof(reg),1,p1);
- while(!feof(p1))
- {
- if(reg.estado=='A')
- {
- printf(" %d %d %d/%d/%d\n",reg.codcli,reg.codpel,reg.fecha.da_day,reg.fecha.da_mon,reg.fecha.da_year);
- }
- fread(®,sizeof(reg),1,p1);
- }
- getch();
- }
- void EliminarPelicula(void)
- {
- /*
- Para recordar la estructura de la struct pelicula
- typedef struct {
- int codigo;
- char titulo[30];
- int copias;
- int alquiladas;
- char descripcion[100];
- int vecesalquilada;
- int estado; /////// <----- AGREGAR !! MUY IMPORTANTE !!
- } Pelicula;
- */
- int codigo;
- char otro,conf;
- Pelicula peli_aux;
- long posicion;
- FILE *p1;
- do
- {
- clrscr();
- gotoxy(24,1);
- printf("Ingrese el Codigo de la Pelicula a Eliminar: ");
- scanf("%d",&codigo);
- posicion=ObtenerPosicionPelicula(codigo); // Modificar el codigo de esta funcion para que sirva a peliculas
- clrscr();
- if(posicion == -1)
- {
- gotoxy(25,20);
- printf("PELICULA NO ENCONTRADA");
- getch();
- return;
- }
- // Carga previa de la pelicula antes de pedir los nuevos datos
- if((p1 = fopen(FILE_PELICULAS,"r+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL GENERAR ARCHIVO DE PELICULAS %s",FILE_CLIENTES);
- getch();
- return;
- }
- fseek(p1,posicion,SEEK_SET); //Cambiar nombre de funcion y modificar codigo
- if (fread(&peli_aux,sizeof(Pelicula),1,p1) != 1)
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- clrscr();
- gotoxy(25,2);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(25,3);
- printf("Codigo : %d ",peli_aux.codigo);
- gotoxy(25,4);
- printf("Titulo : %s ",peli_aux.titulo);
- gotoxy(25,5);
- printf("Cantidad de copias : %d ",peli_aux.copias);
- gotoxy(25,6);
- printf("Copias alquiladas : %d ",peli_aux.alquiladas);
- gotoxy(25,7);
- printf("Descripcion : %s ",peli_aux.descripcion);
- gotoxy(25,8);
- printf("Veces alquilada : %d ",peli_aux.vecesalquilada);
- gotoxy(25,9);
- printf("Estado : %c ",peli_aux.estado);
- getch();
- //////////////////////////////////////////////////////////
- // En Base al Estado se fija si es baja fisica o logica //
- //////////////////////////////////////////////////////////
- if(peli_aux.estado=='A')
- {
- gotoxy(25,10);
- printf("BAJA LOGICA");
- peli_aux.estado='D';
- do
- {
- gotoxy(25,20);
- printf("1-CONFIRMA ");
- gotoxy(25,21);
- printf("2-CANCELA ");
- conf=getch();
- if(conf<49 || conf>50) gotoxy(9,10);
- }
- while(conf<49 || conf>50);
- if (conf==49)
- { // Si se decidio grabar y nadie se llama igual...
- fseek(p1,posicion,SEEK_SET);
- if (fwrite(&peli_aux,sizeof(Pelicula),1,p1)!=1)
- {
- gotoxy(25,22);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- else
- {
- gotoxy(25,22);
- printf("PELICULA DESACTIVADA");
- }
- }
- do
- {
- if(conf==50)
- {
- gotoxy(25,22);
- printf("INGRESO CANCELADO");
- }
- gotoxy(25,23);
- printf("INGRESAR OTRO (S/N)? ");
- otro='s';//por las dudas
- otro=getch();
- otro=toupper(otro);
- }
- while(otro!='S' && otro!='N');
- }
- else
- {
- gotoxy(25,10);
- printf("BAJA FISICA");
- fcloseall();
- EliminarFisicaPelicula(peli_aux.codigo); //Codigo de EliminarFisicaCliente adaptado para dar de baja fisica a las peliculas.
- getch();
- }
- }
- while (otro=='S');
- fclose(p1);
- }
- void cargar (void)
- {
- comen reg;
- FILE *pf;
- pf = fopen("c:\\comentario.txt","wb");
- if (pf==NULL)
- {
- clrscr();
- printf("error de apertura");
- getch();
- exit(1);
- }
- //carga del archivo
- clrscr();
- printf("Codigo de Pelicula <0 para salir>: ");
- scanf("%d", ®.codigo_pel);
- while (reg.codigo_pel)
- {
- printf("Comentario: ");
- fflush(stdin);
- gets(reg.comentario);
- fwrite(®,sizeof(reg),1,pf);
- clrscr();
- printf("Codigo de Pelicula <0 para salir>: ");
- scanf("%d", ®.codigo_pel);
- }
- fclose(pf);
- }
- void mostrar(void)
- {
- comen reg;
- FILE *pf;
- pf = fopen("c:\\comentarios.txt","rb");
- if (pf==NULL)
- {
- clrscr();
- printf("error de apertura");
- getch();
- exit(1);
- }
- printf("Los comentarios son: \n\n");
- fread(®,sizeof(reg),1,pf);
- printf("Codigo Comentarios: \n");
- while(!feof(pf))
- {
- printf(" %-10d %-20s \n", reg.codigo_pel, reg.comentario);
- fread(®,sizeof(reg),1,pf);
- }
- fclose(pf);
- getch();
- }
- long ObtenerPosicionPelicula(int codigo) //Por ahora utilizo esta funcion para probar. Luego reciclaré el codigo del indice de peliculas.
- {
- Pelicula reg;
- FILE *p1;
- int ss;
- long ret;
- p1=fopen(FILE_PELICULAS,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- flushall();
- ret = ftell(p1);
- if(fread(®,sizeof(Pelicula),1,p1)!=1 && (!feof(p1)))exit(1);
- ss=0;
- if(codigo == reg.codigo)
- {
- ss=1;
- break;
- }
- }
- fclose(p1);
- if(ss==0)
- {
- // Pelicula No Encontrada
- ret = -1;
- }
- return ret;
- }
- void EliminarFisicaPelicula(int codPeli)
- {
- FILE *ARCHIVO,*COPIA, *log_bajas;
- Pelicula dato,*pdato;
- long cant;
- char cop[]={"PELICULAS.TMP"};
- pdato=&dato;
- fcloseall();
- if((ARCHIVO=fopen(FILE_PELICULAS,"rb"))==NULL)
- {
- gotoxy(25,14);
- printf("ERROR EN APERTURA ARCHIVO");
- printf("EL DATO NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- if((COPIA=fopen(cop,"wb"))==NULL)
- {
- gotoxy(25,14);
- printf("ERROR EN APERTURA ARCHIVO");
- gotoxy(25,15);
- printf("EL DATO NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- //COMIENZA LA TRANSFERENCIA
- rewind(ARCHIVO);
- rewind(COPIA);
- for( ; ; )
- {
- cant = fread(&dato,sizeof(Pelicula),1,ARCHIVO);
- if(cant!=1)
- {
- if(feof(ARCHIVO)!=0)
- {
- break;
- }
- else
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- }
- else
- {
- if(codPeli != pdato->codigo)//VERIFICO QUE NO SEA EL QUE QUIERO ELIMINAR
- {
- if(fwrite(&dato,sizeof(Pelicula),1,COPIA)!=1)
- {
- gotoxy(25,14);
- printf("ERROR AL ESCRIBIR");
- getch();
- return;
- };
- }
- }
- }
- //FIN DE LA COPIA
- fcloseall();
- if(remove(FILE_PELICULAS)!=0)
- {
- gotoxy(25,13);
- printf("EL ARCHIVO ORIGINAL NO SE PUEDE ELIMINAR");
- getch();
- return;
- }
- else
- {
- if(rename(cop,FILE_PELICULAS)!=0)
- {
- gotoxy(25,13);
- printf("EL ARCHIVO COP NO SE PUDO RENOMBRAR");
- getch();
- return;
- }
- else
- {
- gotoxy(25,15);
- printf("BAJA REALIZADA");
- registrar_bajas(codPeli);
- }
- }
- fcloseall();
- }
- void ListarBajas(void)//Muestra Peliculas dadas de baja
- {
- FILE *p1;
- dia fecha;
- peli_baja reg;
- p1=fopen(FILE_LOG_BAJAS,"r");
- clrscr();
- printf("- Peliculas Dadas de Baja: -\n");
- fread(®,sizeof(reg),1,p1);
- while(!feof(p1))
- {
- fecha=reg.fecha;
- printf("Codigo de Pelicula: %d Dada de baja en la fecha: %d/%d/%d\n",reg.codpel,fecha.da_day,fecha.da_mon,fecha.da_year);
- fread(®,sizeof(reg),1,p1);
- }
- getch();
- }
- /*FUNCION MODIFICAR PELICULAS
- Necesito sólamente el código de la película que quiero modificar.
- Recorro el archivo de la misma manera que lo hago para hacer la baja lógica. De hecho, puedo copiar la misma función...
- Solicito los nuevos datos, los cuales serán almacenados un una estructura auxiliar del tipo Pelicula.
- Grabo encima de la posición que me arrojó la función escrita por Adrián, verifico que la grabación haya sido exitosa (cosa que seguramente será asi)
- y listo.
- */
- void ModificarPelicula(void) //AGREGAR
- {
- int codigo;
- char continua,otro,conf,codSN;
- Pelicula pelicula,pelicula_NuevosDatos,aux;
- long posicion;
- FILE *p1;
- clrscr();
- gotoxy(24,1);
- printf("Ingrese el Codigo de la Pelicula a Buscar: ");
- scanf("%d",&codigo);
- posicion=ObtenerPosicionPelicula(codigo);
- clrscr();
- if(posicion == -1)
- {
- gotoxy(25,20);
- printf("PELICULA NO ENCONTRADA");
- getch();
- return;
- }
- // Cargo los datos de la pelicula encontrada para mostrarlos antes de la modificación
- if((p1 = fopen(FILE_PELICULAS,"r+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL GENERAR ARCHIVO DE PELICULAS %s",FILE_PELICULAS);
- getch();
- return;
- }
- fseek(p1,posicion,SEEK_SET);
- if (fread(&pelicula,sizeof(Pelicula),1,p1) != 1)
- {
- clreol();
- printf("\aERROR AL LEER ARCHIVO!!!!");
- getch();
- return;
- }
- clrscr();
- gotoxy(25,2);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(25,3);
- printf("Codigo : %d ",pelicula.codigo);
- gotoxy(25,4);
- printf("Titulo : %s ",pelicula.titulo);
- gotoxy(25,5);
- printf("Copias : %d ",pelicula.copias);
- gotoxy(25,6);
- printf("Alquiladas : %d ",pelicula.alquiladas);
- gotoxy(25,7);
- printf("Descripcion : %s ",pelicula.descripcion);
- gotoxy(25,8);
- printf("Veces alquilada : %d ",pelicula.vecesalquilada);
- gotoxy(25,9);
- printf("Estado : %c ",pelicula.estado); //EL ESTADO ES UN INT???
- getch();
- /////////////////////////////////////////////
- // Pido los nuevos datos de la pelicula
- /////////////////////////////////////////////
- do
- {
- gotoxy(25,10);
- printf("====================");
- /*int codigo;
- char titulo[30];
- int copias;
- int alquiladas;
- char estado;*/
- do
- {
- gotoxy(25,11);
- printf("TITULO: ");
- fflush(stdin);
- gets(pelicula_NuevosDatos.titulo);
- }
- while(strlen(pelicula_NuevosDatos.titulo)==0);
- //VALIDA QUE EL NOMBRE NO SE REPITA
- aux = ObtenerPelicula(pelicula_NuevosDatos,2);
- if(aux.codigo!=-1)
- {
- gotoxy(25,20);
- printf("YA HAY UNA PELICULA CON ESE TITULO");
- gotoxy(25,21);
- printf("DESEA CONTINUAR? (S/N)");
- continua = getch();
- continua = toupper(continua);
- if (continua!='S')return;
- }
- //FIN DE VALIDACION
- gotoxy(25,12);
- printf("CODIGO: %d",&pelicula.codigo);
- gotoxy(25,13);
- printf("INGRESARA UN NUEVO CODIGO? (S/N)");
- scanf("%d",&codSN);
- while(codSN=='S' || codSN=='N')
- {
- if(codSN=='S')
- {
- printf("\nIngrese nuevo codigo: ");
- scanf("%d",&pelicula_NuevosDatos.codigo);
- getch();
- }
- if(codSN=='N')
- {
- pelicula_NuevosDatos.codigo=pelicula.codigo;
- }
- }
- gotoxy(25,14);
- printf("COPIAS: ");
- fflush(stdin);
- scanf("%d",&pelicula_NuevosDatos.copias);
- gotoxy(25,15);
- printf("ALQUILADAS: ");
- fflush(stdin);
- scanf("%d",&pelicula_NuevosDatos.alquiladas);
- gotoxy(25,16);
- printf("DESCRIPCION: ");
- fflush(stdin);
- gets(pelicula_NuevosDatos.descripcion);
- gotoxy(25,17);
- printf("VECES ALQUILADA: ");
- scanf("%d",&pelicula_NuevosDatos.vecesalquilada);
- // Inicializa Estado y Alquileres Realizados
- gotoxy(25,18);
- printf("ESTADO (A/D):");
- scanf("%c",&pelicula_NuevosDatos.estado);
- pelicula_NuevosDatos.estado=toupper(pelicula_NuevosDatos.estado);
- do
- {
- gotoxy(25,20);
- printf("1-CONFIRMA ");
- gotoxy(25,21);
- printf("2-REINGRESA");
- gotoxy(25,22);
- printf("3-CANCELA ");
- conf=getch();
- if(conf<49 || conf>51) gotoxy(9,10);
- }
- while(conf<49 || conf>51);
- if (conf==49)
- { // Si de decidio grabar y no se repetia el titulo de la pelicula...
- fseek(p1,posicion,SEEK_SET);
- if (fwrite(&pelicula_NuevosDatos,sizeof(Pelicula),1,p1)!=1)
- {
- gotoxy(25,24);
- printf("ERROR DE ESCRITURA");
- getch();
- return;
- }
- else
- {
- gotoxy(25,24);
- printf("MODIFICACION REALIZADA SATISFACTORIAMENTE");
- }
- }
- do
- {
- if(conf==51)
- {
- conf=50;//sino si cancela y sigue ingresando apareceria de vuelta
- gotoxy(25,24);
- printf("INGRESO CANCELADO");
- }
- gotoxy(25,25);
- printf("INGRESAR OTRO (S/N)? ");
- otro='s';//por las dudas
- otro=getch();
- otro=toupper(otro);
- }
- while(otro!='S' && otro!='N');
- }
- while(otro=='S');
- fclose(p1);
- }
- /* FUNCION QUE ARROJE DATOS ESTADISTICOS
- De acuerdo al "año" solicitado, la función recorrerá el archivo de alquileres en busca de los registros de "mes" en que son efectuados los alquileres y,
- según el mes encontrado, se irá incrementando un contador para el mes correspondiente.
- Esto lo voy a lograr obteniendo el mes actual con getdate, con un "case" de acuerdo al número de mes y con un contador en cada opción. Los contadores
- podrían ser parte de un vector de 12 posiciones. (LISTO)
- Finalmente, tengo que efectuar una función que recorra el vector en busca del mes con mayor número de alquileres (un max) y con un for tengo que hacer
- otra función que compare cada mes con el mes anterior y arroje un porcentaje de variación.
- Muestro la información cuantitativa y porcentual y c´est fini. */
- void Crear_Estad_Alq (void) //AGREGAR
- {
- clrscr();
- FILE *log_estadist;
- Estadistica estadistica;
- int mes, anio, i;
- float max;
- float contmeses[12]={0}; //contador de alquileres encontrados en cada mes
- float porcmeses[12]={0}; //almacena los porcentajes de variación de alquileres mes a mes
- fflush(stdin);
- printf("\Ingrese el anio del que se desea obtener informacion estadistica sobre los alquileres efectuados: ");
- scanf("%d",&anio);
- //Comienzo a recorrer el archivo de alquileres en busca de alquileres efectuados en el anio.
- FILE *p1;
- dia fecha;
- Alquiler reg;
- p1=fopen(FILE_ALQUILER,"r");
- clrscr();
- fread(®,sizeof(Alquiler),1,p1);
- while(!feof(p1))
- {
- if(reg.fecha.da_year==anio)
- {
- mes=reg.fecha.da_mon;
- switch(mes) //de acuerdo al mes en que se realizó el alquiler, incremento un contador.
- {
- case 1:
- contmeses[0]++;
- break;
- case 2:
- contmeses[1]++;
- break;
- case 3:
- contmeses[2]++;
- break;
- case 4:
- contmeses[3]++;
- break;
- case 5:
- contmeses[4]++;
- break;
- case 6:
- contmeses[5]++;
- break;
- case 7:
- contmeses[6]++;
- break;
- case 8:
- contmeses[7]++;
- break;
- case 9:
- contmeses[8]++;
- break;
- case 10:
- contmeses[9]++;
- break;
- case 11:
- contmeses[10]++;
- break;
- case 12:
- contmeses[11]++;
- break;
- }
- //Calculo la variación porcentual mes a mes. Enero vale 100% porque comienza el año a evaluar.
- }
- fread(®,sizeof(reg),1,p1);
- }
- for(i=0;i<12;i++)
- {
- if(i==0)
- {
- porcmeses[0]=100;
- max=contmeses[0]; //inicio max en la primer posicion.
- }
- else
- {
- if (contmeses[i-1] == 0)
- porcmeses[i] = 0;
- else
- porcmeses[i]=((contmeses[i]*100)/contmeses[i-1]);
- if(contmeses[i]>max)
- max=contmeses[i];
- }
- }
- if((log_estadist = fopen(FILE_ESTADISTICA,"a+b")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL ABRIR ARCHIVO");
- getch();
- return;
- }
- else
- {
- estadistica.anio=anio;
- for(i=0;i<12;i++)
- {
- estadistica.contmeses[i]=contmeses[i];
- estadistica.porcmeses[i]=porcmeses[i];
- }
- estadistica.max=max;
- fwrite(&estadistica,sizeof(Estadistica),1,log_estadist);
- }
- fcloseall();
- }
- Pelicula ObtenerPelicula(Pelicula pelicula, int formaBusqueda) //AGREGAR
- {
- Pelicula reg;
- FILE *p1;
- int ss;
- p1=fopen(FILE_PELICULAS,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- flushall();
- if(fread(®,sizeof(Pelicula),1,p1)!=1 && (!feof(p1)))exit(1);
- ss=0;
- switch(formaBusqueda)
- {
- case 1:
- if(pelicula.codigo == reg.codigo)
- ss=1;
- break;
- case 2:
- if((strcmp(strlwr(pelicula.titulo),strlwr(reg.titulo))==0)
- &&
- (strcmp(strlwr(pelicula.descripcion),strlwr(reg.descripcion))==0))
- ss=1;
- break;
- }
- if (ss==1) break;
- }
- fclose(p1);
- if(ss==0)
- {
- // pelicula No Encontrada
- reg.codigo = -1;
- }
- return reg;
- }
- void ListarEstadistica(void)//Lista el contenido del archivo de información estadistica //AGREGAR
- {
- FILE *p1;
- int i=0;
- static char meses[12][4]={"ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SEP","OCT","NOV","DIC"};
- Estadistica reg;
- p1=fopen(FILE_ESTADISTICA,"r");
- clrscr();
- printf("- INFORMACION ESTADISTICA ALQUILERES -\n");
- fread(®,sizeof(Estadistica),1,p1);
- while(!feof(p1))
- {
- printf("\nAño: %d",reg.anio);
- for(i=0;i<12;i++)
- {
- printf("\n%s Cant Alquileres: %.f Diferencia Porcentual Respecto Mes Anterior: %.2f",meses[i],reg.contmeses[i],reg.porcmeses[i]);
- }
- fread(®,sizeof(Estadistica),1,p1);
- }
- getch();
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Funcion de busqueda de cliente utilizando memeoria dinamica
- ///////////////////////////////////////////////////////////////////////////////
- Cliente* ObtenerCliente_MemoriaDinamica(Cliente cliente, int formaBusqueda)
- {
- Cliente reg, *ptr;
- FILE *p1;
- int ss;
- p1=fopen(FILE_CLIENTES,"rb");
- if(p1==NULL)
- {
- clrscr();
- gotoxy(22,10);
- printf("ERROR EN APERTURA DE ARCHIVO");
- getch();
- exit(1);
- }
- rewind(p1);
- while(!feof(p1))
- {
- flushall();
- if(fread(®,sizeof(Cliente),1,p1)!=1 && (!feof(p1)))exit(1);
- ss=0;
- switch(formaBusqueda)
- {
- case 1:
- if(cliente.codigo == reg.codigo)
- ss=1;
- break;
- case 2:
- if((strcmp(strlwr(cliente.apellido),strlwr(reg.apellido))==0)
- &&
- (strcmp(strlwr(cliente.nombre),strlwr(reg.nombre))==0))
- ss=1;
- break;
- }
- if (ss==1) break;
- }
- fclose(p1);
- if(ss==0)
- {
- // Cliente No Encontrado
- reg.codigo = -1;
- }
- // Lo asignamos dinamicamente para devolverlo
- ptr=(Cliente*)malloc(sizeof(reg));
- if (ptr==NULL)
- {
- clrscr();
- printf("Error en asignacion dinamica");
- getch();
- exit(1);
- }
- *ptr=reg;
- return ptr;
- }
- ////////////////////////////////////////////////////////////////////////////////
- void ConsultarCliente_MemoriaDinamica(void)
- {
- int op,codigo;
- Cliente cliente, *buscadoCliente;
- op=Menu_Busqueda_NomApeCodigo();
- clrscr();
- switch(op)
- {
- case 1:
- gotoxy(24,8);
- printf("Ingrese el Nombre y Apellido de Cliente a Buscar");
- gotoxy(24,9);
- printf("Apellido: ");
- fflush(stdin);
- gets(cliente.apellido);
- gotoxy(24,10);
- printf("Nombre: ");
- fflush(stdin);
- gets(cliente.nombre);
- buscadoCliente= ObtenerCliente_MemoriaDinamica(cliente,2);
- break;
- case 2:
- gotoxy(24,8);
- printf("Ingrese el Codigo del Cliente a Buscar: ");
- scanf("%d",&codigo);
- cliente.codigo = codigo;
- buscadoCliente=ObtenerCliente_MemoriaDinamica(cliente,1);
- break;
- case 3:
- return;
- }
- if(buscadoCliente->codigo != -1)
- {
- clrscr();
- gotoxy(24,8);
- printf("DATOS DEL REGISTRO ENCONTRADO");
- gotoxy(24,9);
- printf("Codigo : %d ",buscadoCliente->codigo);
- gotoxy(24,10);
- printf("Nombre : %s ",buscadoCliente->nombre);
- gotoxy(24,11);
- printf("Apellido : %s ",buscadoCliente->apellido);
- gotoxy(24,12);
- printf("Direccion : %s ",buscadoCliente->direccion);
- gotoxy(24,13);
- printf("Telefono : %s ",buscadoCliente->telefono);
- gotoxy(24,14);
- printf("Alquileres : %d ",buscadoCliente->alquileresrealizados);
- gotoxy(24,15);
- printf("Estado : %c ",buscadoCliente->estado);
- getch();
- }
- else
- {
- gotoxy(24,18);
- printf("CLIENTE NO ENCONTRADO");
- getch();
- }
- }
- /////////////////////////////////////////////////////////////////////////////////
- void ListarClientes(void)
- {
- FILE *ARCHIVO;
- int i=7, posicion = 0;
- long ultimo;
- char letra;
- Cliente aux,*paux,*v[500];
- paux=&aux;
- // Apertura del archivo
- if((ARCHIVO=fopen(FILE_CLIENTES,"rb"))==NULL)
- {
- gotoxy(25,13);
- printf("EL ARCHIVO NO SE ENCUENTRA");
- gotoxy(25,14);
- printf("DEBE CREARLO ENTRANDO A LA OPCION 1");
- getch();
- return;
- }
- // Se fija que el archivo tenga algo
- fseek(ARCHIVO,0,SEEK_END);
- ultimo=ftell(ARCHIVO);
- if (ultimo==0)
- {
- gotoxy(25,20);
- printf("EL ARCHIVO ESTA VACIO");
- getch();
- return;
- }
- gotoxy(20,14);
- printf("CON \"*\" LISTA COMPLETA");
- gotoxy(20,15);
- printf("INGRESA LA LETRA POR LA QUE QUERES LISTAR: ");
- letra=getch();
- rewind(ARCHIVO);
- Listar_EncaClientes();
- do
- {
- if((fread(&aux,sizeof(Cliente),1,ARCHIVO)!=1))
- {
- if(!feof(ARCHIVO))
- {
- gotoxy(3,23);
- printf("ERROR LISTANDO");
- getch();
- return;
- }
- }
- else
- {
- paux=(Cliente*)malloc(sizeof(aux));
- if (paux==NULL)
- {
- clrscr();
- printf("Error en asignacion dinamica");
- getch();
- exit(1);
- }
- *paux=aux;
- v[posicion] = paux;
- posicion++;
- }
- }
- while(!feof(ARCHIVO));
- fclose(ARCHIVO);
- // Terminamos de leer el archivo
- // Mostramos el vector
- for (int posActual = 0; posActual < posicion ; posActual++)
- {
- paux = v[posActual];
- if(letra!='*') // Lista por letra
- {
- if((toupper(letra)==aux.apellido[0]) || (tolower(letra)==aux.apellido[0]))
- {
- i++;
- gotoxy(5,i);
- printf("%d",paux->codigo);
- gotoxy(9,i);
- printf("%c",paux->estado);
- gotoxy(13,i);
- printf("%s",paux->apellido);
- gotoxy(32,i);
- printf("%s",paux->direccion);
- gotoxy(57,i);
- printf("%s",paux->telefono);
- if(i==18)
- {
- gotoxy(3,23);
- printf("PRESIONE UNA TECLA PARA CONTINUAR");
- getch();
- clrscr();
- Listar_EncaClientes();
- i=7;
- }
- }
- }
- else // Lista todo
- {
- i++;
- gotoxy(5,i);
- printf("%d",paux->codigo);
- gotoxy(10,i);
- printf("%c",paux->estado);
- gotoxy(13,i);
- printf("%s",paux->apellido);
- gotoxy(32,i);
- printf("%s",paux->direccion);
- gotoxy(57,i);
- printf("%s",paux->telefono);
- if(i==18)
- {
- if(feof(ARCHIVO))
- {
- gotoxy(3,23);
- printf("LISTA COMPLETA");
- getch();
- return;
- }
- gotoxy(3,23);
- printf("PRESIONE UNA TECLA PARA CONTINUAR");
- getch();
- clrscr();
- Listar_EncaClientes();
- i=7;
- }
- }
- }
- gotoxy(3,23);
- printf("LISTA COMPLETA");
- getch();
- return;
- }
- void registrar_bajas (int codigo) //AGREGAR
- {
- //Creo una estructura auxiliar
- peli_baja reg;
- FILE *log_p_baja;
- if((log_p_baja = fopen(FILE_LOG_BAJAS,"a")) == NULL)
- {
- gotoxy(25,13);
- printf("ERROR AL ABRIR ARCHIVO");
- getch();
- return;
- }
- else
- {
- reg.codpel=codigo;
- getdate(®.fecha);
- fwrite(®,sizeof(reg),1,log_p_baja);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement