Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 25.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4.  
  5. #define SIZE_FULL_PATH_FILE 1024
  6. //---------------------------------------------------------------------------
  7. //Variables globales
  8. typedef enum { false, true } bool;
  9.  
  10. typedef struct _Ventas {
  11.     char nombre[10];
  12.     int contadorventas;
  13.     int ID;
  14. }Ventas;
  15.  
  16. typedef struct _Nodo {
  17.    Ventas Vendedor;
  18.    struct _Nodo *sig;
  19. }Nodo;
  20.  
  21. typedef struct _Lista{
  22.    Nodo *primero;
  23.    Nodo *ultimo;
  24.    int numElementos; // Para simplificar operaciones sobre la lista
  25. }Lista;
  26.  
  27. typedef struct _Equipos{
  28.     char marca[10];
  29.     char color[10];
  30.     char procesador[10];
  31.     char ram[5];
  32.     char hdd[8];
  33.     int cantidad;
  34.     float preciocompra;
  35.     float precioventa;
  36. }Equipos;
  37. //---------------------------------------------------------------------------
  38. //Prototipos de funciones
  39. int menu(void);
  40. void AddPCS(void);
  41. void AddSeller(void);
  42. int ListSellers(bool);
  43. void AddSale(void);
  44. int ListStock(bool);
  45. void ListSales(void);
  46. void ListFilteredSalesBySeller(void);
  47. bool ShowSalesByID(int ID,char *path);
  48. void ShowRankingBestSeller(void);
  49. void ShowSeller(Ventas Vendedor);
  50. int ObtainSellerByPath(char *path,Ventas *Vendedor);
  51. int clean_stdin(void);
  52. bool input_string(char *buffer,int size);
  53. void pause(void);
  54.  
  55. //Prototipos de funciones de manejo de la lista
  56. Lista InicializarLista(void);
  57. void ImprimirLista(Lista* lista);
  58. Nodo* NuevoNodo(Lista* lista);
  59. void LimpiarLista(Lista* lista);
  60. void OrdenarPorVentasMayor(Lista* lista);
  61. int InsertarVendedorEnLista(Lista *lista, char *namefile);
  62. //---------------------------------------------------------------------------
  63.  
  64. // Devuelve una lista inicializada.
  65. Lista InicializarLista()
  66. {
  67.    Lista toReturn;
  68.    toReturn.primero = NULL;
  69.    toReturn.ultimo = NULL;
  70.    toReturn.numElementos = 0;
  71.  
  72.   return toReturn;
  73. }
  74. //---------------------------------------------------------------------------
  75.  
  76. void ImprimirLista(Lista* lista)
  77. {
  78.    Nodo *actual;
  79.  
  80.    //Muestro la lista ordenada
  81.    actual=lista->primero;
  82.    
  83.    if(actual != NULL){
  84.       printf("------------------------------------------------\n");
  85.       while(actual != NULL){
  86.          printf("Datos del vendedor:\n");
  87.          printf("ID: %d\n",actual->Vendedor.ID);
  88.          printf("Nombre: %s\n",actual->Vendedor.nombre);
  89.          printf("Ventas realizadas: %d\n",actual->Vendedor.contadorventas);
  90.          printf("------------------------------------------------\n");
  91.          actual=actual->sig;
  92.       }
  93.    }
  94. }
  95. //---------------------------------------------------------------------------
  96.  
  97. // Añade un nuevo nodo a la lista
  98. Nodo* NuevoNodo(Lista* lista)
  99. {
  100.    Nodo *nuevo;
  101.  
  102.    nuevo = (Nodo*)malloc(sizeof(Nodo));
  103.    if(nuevo != NULL){
  104.       if(nuevo != NULL){
  105.          nuevo->sig = NULL;
  106.          if( lista->primero== 0 ){
  107.             lista->primero = nuevo;
  108.             lista->ultimo = nuevo;
  109.          }else{
  110.             lista->ultimo->sig = nuevo;
  111.             lista->ultimo = lista->ultimo->sig;
  112.          }
  113.          lista->numElementos++;
  114.       }
  115.    }
  116.    return nuevo;
  117. }
  118. //---------------------------------------------------------------------------
  119.  
  120. // Elimina todos los nodos de la lista
  121. void LimpiarLista(Lista* lista)
  122. {
  123.    Nodo* nodo = lista->primero;
  124.  
  125.    while( nodo )
  126.    {
  127.       Nodo* temp = nodo->sig;
  128.       free(nodo);
  129.       nodo = temp;
  130.    }
  131.  
  132.    lista->primero = 0;
  133.    lista->ultimo = 0;
  134.    lista->numElementos = 0;
  135. }
  136. //---------------------------------------------------------------------------
  137.  
  138. void OrdenarPorVentasMayor(Lista* lista)
  139. {
  140.    int i,j;
  141.    // Estructura auxiliar para ordenar
  142.    // El último nodo será 0 para simplificar el proceso
  143.    Nodo** nodos = (Nodo**)calloc(lista->numElementos+1,sizeof(Nodo*));
  144.  
  145.    // Se copian los nodos de la lista a nuestro arreglo
  146.    Nodo* nodo = lista->primero;
  147.    int index = 0;
  148.    while(nodo)
  149.    {
  150.       nodos[index] = nodo;
  151.       index++;
  152.       nodo = nodo->sig;
  153.    }
  154.  
  155.    // Se ordenan los nodos
  156.    for (i = 0 ; i < lista->numElementos-1; i++)
  157.    {
  158.       for (j = 0 ; j < lista->numElementos - i - 1; j++)
  159.       {
  160.          if ( nodos[j]->Vendedor.contadorventas < nodos[j+1]->Vendedor.contadorventas)
  161.          {
  162.             Nodo* swap  = nodos[j];
  163.             nodos[j]  = nodos[j+1];
  164.             nodos[j+1] = swap;
  165.          }
  166.       }
  167.    }
  168.  
  169.    // Se reconstruye la lista
  170.    lista->primero = nodos[0];
  171.  
  172.    for( i=0; i<lista->numElementos; i++)
  173.    {
  174.       // Como el último nodo es NULL, el último nodo real de la lista acabará apuntando a NULL automáticamente
  175.       nodos[i]->sig = nodos[i+1];
  176.    }
  177.    lista->ultimo = nodos[i-1];
  178.  
  179.    free(nodos);
  180. }
  181. //---------------------------------------------------------------------------
  182.  
  183. void pause()
  184. {
  185.    printf("\nPulse intro para continuar");
  186.    getchar();
  187. }
  188. //---------------------------------------------------------------------------
  189.  
  190. int clean_stdin()
  191. {
  192.    char c;
  193.    int contador=0;
  194.  
  195.    do{
  196.       c=getchar();
  197.       contador++;
  198.    }while(c != '\n');
  199.    return contador;
  200. }
  201. //---------------------------------------------------------------------------
  202.  
  203. bool input_string(char *buffer,int size)
  204. {
  205.    int len;
  206.    bool overflow=false;
  207.  
  208.    memset(buffer,'\0',size);
  209.    fgets(buffer,size,stdin);
  210.    if(buffer[size-1] == '\0'){
  211.       len=strlen(buffer);
  212.       if(buffer[len-1] != '\n'){
  213.          if(clean_stdin() > 1)
  214.             overflow=true;
  215.       }else
  216.          buffer[len-1] = '\0';
  217.    }
  218.    return overflow;
  219. }
  220. //---------------------------------------------------------------------------
  221.  
  222. int menu(){
  223.    int op;
  224.    system("CLS");
  225.    printf("\t\t\t***HYPER STORE MULTIMEDIA***\n\n");
  226.    printf("1.Realizar venta\n");
  227.    printf("2.Listar stock de PC's\n");
  228.    printf("3.Listar ventas\n");
  229.    printf("4.Listar ventas de un vendedor\n");
  230.    printf("5.Mostrar Racking de ventas\n");
  231.    printf("6.Incorporar PC's al stock\n");
  232.    printf("7.Incorporar vendedor\n");
  233.    printf("8.Salir\n\n");
  234.    printf("Elije una opcion: ");
  235.    if(scanf("%d",&op) == 0)
  236.       clean_stdin();
  237.    return op;
  238. }
  239. //---------------------------------------------------------------------------
  240.  
  241. void AddSeller(){
  242.    FILE *archivoVendedor;
  243.    Ventas Vendedor;
  244.    bool salir=false;
  245.    char pathFile[SIZE_FULL_PATH_FILE];
  246.  
  247.    printf("\n\t\t\tIngresa los datos del vendedor\n");
  248.    printf("Nombre: ");
  249.    input_string(Vendedor.nombre,sizeof(Vendedor.nombre));
  250.    Vendedor.contadorventas=0;
  251.  
  252.    do{
  253.       printf("ID: ");
  254.       if(scanf("%d",&Vendedor.ID) == 0)
  255.          clean_stdin();
  256.       sprintf(pathFile,"Vendedores\\%d.bin",Vendedor.ID);
  257.       archivoVendedor=fopen(pathFile,"rb");
  258.      
  259.       if(archivoVendedor==NULL){ //Todo correcto
  260.          if((archivoVendedor=fopen(pathFile,"wb")) != NULL){
  261.             if(fwrite(&Vendedor,sizeof(Ventas),1,archivoVendedor)==1){
  262.                printf("\nVendedor creado satisfactoriamente\n");
  263.                fclose(archivoVendedor);
  264.                salir=true;
  265.             }else{
  266.                printf("\nHuvo un error al guardar los datos del vendedor\n");
  267.                fclose(archivoVendedor);
  268.                remove(pathFile);
  269.                salir=true;
  270.             }
  271.          }else{
  272.             printf("\nHubo algun error al crear el archivo para este vendedor\n");
  273.             salir=true;
  274.          }
  275.       }else{
  276.          fclose(archivoVendedor);
  277.          printf("\nEl ID indicado ya existe. Vuelve a intentarlo\n");
  278.       }
  279.    }while(salir==false);
  280.    clean_stdin();
  281. }
  282. //---------------------------------------------------------------------------
  283.  
  284. void AddPCS()
  285. {
  286.    FILE *archivoStock;
  287.    Equipos PC;
  288.  
  289.    printf("\n\t\t\tIngresa los datos del equipo\n");
  290.    printf("Marca: ");
  291.    input_string(PC.marca,sizeof(PC.marca));
  292.    printf("\nColor: ");
  293.    input_string(PC.color,sizeof(PC.color));
  294.    printf("\nProcesador: ");
  295.    input_string(PC.procesador,sizeof(PC.procesador));
  296.    printf("\nMemoria ram: ");
  297.    input_string(PC.ram,sizeof(PC.ram));
  298.    printf("\nDisco duro: ");
  299.    input_string(PC.hdd,sizeof(PC.hdd));
  300.    printf("\nPrecio de compra: ");
  301.    if(scanf("%f",&PC.preciocompra) == 0)
  302.       clean_stdin();
  303.    printf("\nPrecio de venta: ");
  304.    if(scanf("%f",&PC.precioventa) == 0)
  305.       clean_stdin();
  306.    printf("\nCantidad en stock: ");
  307.    if(scanf("%d",&PC.cantidad) == 0)
  308.       clean_stdin();
  309.  
  310.    if((archivoStock=fopen("Stock.bin","ab+")) != NULL){
  311.       if(fwrite(&PC,sizeof(Equipos),1,archivoStock)==1)
  312.          printf("\nArticulo añadido satisfactoriamente\n");
  313.       else
  314.          printf("\nHuvo un error al añadir el articulo al stock\n");
  315.       fclose(archivoStock);
  316.    }else{
  317.       printf("\nHubo algun error al acceder al archivo del stock\n");
  318.    }
  319.    clean_stdin();
  320. }
  321. //---------------------------------------------------------------------------
  322.  
  323. int ListSellers(bool mostrar)
  324. {
  325.    DIR *id_dir;
  326.    Ventas Vendedor;
  327.    struct dirent *filedir;
  328.    char pathFile[SIZE_FULL_PATH_FILE];
  329.    int retval=0,leidos=0;
  330.  
  331.    id_dir=opendir(".\\Vendedores\\");
  332.  
  333.    while((filedir = readdir(id_dir)) != NULL && retval == 0){
  334.       if(strcmp(filedir->d_name,".") != 0 && strcmp(filedir->d_name,"..") != 0){
  335.          sprintf(pathFile,"Vendedores\\%s",filedir->d_name);
  336.          retval = ObtainSellerByPath(pathFile, &Vendedor);
  337.          if(retval == 0){
  338.             leidos++;
  339.             if(mostrar == true)
  340.                ShowSeller(Vendedor);
  341.          }
  342.       }
  343.    }
  344.    if(leidos==0)
  345.       printf("\nNo hay vendedores en este momento\n");
  346.    closedir(id_dir);
  347.    return leidos;
  348. }
  349. //---------------------------------------------------------------------------
  350.  
  351. bool AddSaleToFile(Ventas Vendedor,Equipos PC)
  352. {
  353.    char pathVentas[SIZE_FULL_PATH_FILE];
  354.    FILE *archivoVentas;
  355.    bool realizada = false;
  356.  
  357.    sprintf(pathVentas,"Ventas\\%s_%d.bin",Vendedor.nombre,Vendedor.ID);
  358.    if((archivoVentas=fopen(pathVentas,"ab+")) != NULL){
  359.       if(fwrite(&PC,sizeof(Equipos),1,archivoVentas) != 1){
  360.          printf("\nNo se pudo agregar la venta. No se pudo escribir en el archivo\n");
  361.       }else{
  362.          realizada=true;
  363.       }
  364.       fclose(archivoVentas);
  365.    }
  366.    return realizada;
  367. }
  368. //---------------------------------------------------------------------------
  369.  
  370. bool CrearVendedorActualizado(Ventas Vendedor)
  371. {
  372.    FILE *archivoVendedor,*archivoVendedorAux;
  373.    bool creado=false;
  374.    int escritos;
  375.    char pathVendedor[SIZE_FULL_PATH_FILE],pathVendedorAux[SIZE_FULL_PATH_FILE];
  376.  
  377.    sprintf(pathVendedor,"Vendedores\\%d.bin",Vendedor.ID);
  378.    if((archivoVendedor=fopen(pathVendedor,"rb"))==NULL){
  379.       printf("\nNo se pudo abrir el archivo del vendedor\n");
  380.       return creado;
  381.    }
  382.  
  383.    sprintf(pathVendedorAux,"Vendedores\\%d_aux.bin",Vendedor.ID);
  384.    if((archivoVendedorAux=fopen(pathVendedorAux,"wb"))==NULL){
  385.       printf("\nNo se pudo crear el archivo actualizado del vendedor\n");
  386.       return creado;
  387.    }
  388.  
  389.    Vendedor.contadorventas++;
  390.    escritos = fwrite(&Vendedor,sizeof(Vendedor),1,archivoVendedorAux);
  391.    fclose(archivoVendedor);
  392.    fclose(archivoVendedorAux);
  393.  
  394.    if(escritos == 1){
  395.       creado=true;
  396.    }else{
  397.       printf("\nNo se pudo escribir en el archivo actualizado del vendedor\n");
  398.       remove("Stock_aux.bin");
  399.    }
  400.  
  401.    return creado;
  402. }
  403. //---------------------------------------------------------------------------
  404.  
  405. bool CrearStockActualizado(Ventas Vendedor, int indexPC, int nVenta, int *nRegistros)
  406. {
  407.    char pathVendedor[SIZE_FULL_PATH_FILE];
  408.    FILE *archivoStock,*archivoStockAux=NULL,*archivoVentas;
  409.    Equipos PC;
  410.    bool creado = true;
  411.  
  412.    if(nVenta <= 0){
  413.       printf("Cantidad incorrecta\n");
  414.       creado = false;
  415.       return creado;
  416.    }
  417.  
  418.    if(PC.cantidad < nVenta){
  419.       printf("\nLa cantidad a vender sobrepasa a la de stock\n");
  420.       creado = false;
  421.       return creado;
  422.    }
  423.  
  424.    if((archivoStock=fopen("Stock.bin","rb"))==NULL){
  425.       printf("Hubo un error al abrir el archivo de stock\n");
  426.       creado = false;
  427.       return creado;
  428.    }
  429.  
  430.    if((archivoStockAux=fopen("Stock_aux.bin","wb"))==NULL){
  431.       printf("Hubo un error al crear el archivo de stock auxiliar\n");
  432.       fclose(archivoStock);
  433.       creado = false;
  434.       return creado;
  435.    }
  436.  
  437.    *nRegistros=0;
  438.  
  439.    while(fread(&PC,sizeof(Equipos),1,archivoStock)==1 && !feof(archivoStock)){
  440.       if(ftell(archivoStock)!=((indexPC*sizeof(Equipos)) + sizeof(Equipos))){ //Si es el articulo elegido..
  441.          if(fwrite(&PC,sizeof(Equipos),1,archivoStockAux) != 1){
  442.             printf("\nNo se pudo escribir en el archivo auxiliar de stock\n");
  443.             creado=false;
  444.             break;
  445.          }
  446.       }else{
  447.          if(PC.cantidad == nVenta)
  448.             PC.cantidad = 0;
  449.          else
  450.             PC.cantidad -= nVenta;
  451.  
  452.          if(fwrite(&PC,sizeof(Equipos),1,archivoStockAux) != 1){
  453.             printf("\nNo se pudo escribir en el archivo auxiliar de stock\n");
  454.             creado=false;
  455.             break;
  456.          }
  457.          (*nRegistros)++;
  458.       }
  459.    }
  460.    fclose(archivoStock);
  461.    fclose(archivoStockAux);
  462.  
  463.    if(creado == false && archivoStockAux != NULL)
  464.       remove("Stock_aux.bin");
  465.      
  466.    return creado;
  467. }
  468. //---------------------------------------------------------------------------
  469.  
  470. int SolicitarIDValido(Ventas *Vendedor)
  471. {
  472.    bool salir = false;
  473.    int id;
  474.    char pathVendedor[SIZE_FULL_PATH_FILE];
  475.  
  476.    do{
  477.       printf("\nSeleccione el ID del vendedor que realizara la venta: ");
  478.       if(scanf("%d",&id) == 0)
  479.          clean_stdin();
  480.       sprintf(pathVendedor,"Vendedores\\%d.bin",id);
  481.  
  482.       if(ObtainSellerByPath(pathVendedor,Vendedor) == 0)
  483.          salir=true;
  484.       else
  485.          printf("\nSolo IDs validos.");
  486.    }while(salir == false);
  487.    
  488.    return id;
  489. }
  490. //---------------------------------------------------------------------------
  491.  
  492. int SolicitarIndiceProductoValido(int imax)
  493. {
  494.    bool salir = false;
  495.    int iEquipo;
  496.  
  497.    do{
  498.       printf("\n\nIntroduce el indice del equipo que desea vender: ");
  499.       if(scanf("%d",&iEquipo) == 0)
  500.          clean_stdin();
  501.       if(iEquipo > imax || iEquipo < 0)
  502.          printf("Introduce un indice valido\n");
  503.       else
  504.          salir = true;
  505.    }while(salir == false);
  506.  
  507.    return iEquipo;
  508. }
  509. //---------------------------------------------------------------------------
  510.  
  511. bool ObtainStockByIndex(Equipos *PC, int index)
  512. {
  513.    FILE *archivoStock;
  514.    bool retval=false;
  515.  
  516.    //Aqui voy a la posicion del archivo de stock indicada por el indice
  517.    if((archivoStock=fopen("Stock.bin","rb"))==NULL){
  518.       printf("Hubo un error al abrir el archivo de stock\n");
  519.    }else{
  520.       fseek(archivoStock,index*sizeof(Equipos),SEEK_SET);
  521.       if(fread(&(*PC),sizeof(Equipos),1,archivoStock) == 1)
  522.          retval = true;
  523.       else
  524.          printf("No se pudo leer el articulo del archivo de stock\n");
  525.  
  526.       fclose(archivoStock);
  527.    }
  528.    return retval;
  529. }
  530. //---------------------------------------------------------------------------
  531.  
  532. int ObtenerCantidadEquiposValida(Equipos PC)
  533. {
  534.    bool salir = false;
  535.    int nEquipos;
  536.  
  537.    do{
  538.       printf("\nIntroduce la cantidad a vender: ");
  539.       if(scanf("%d",&nEquipos) == 0)
  540.          clean_stdin();
  541.  
  542.       if(nEquipos > PC.cantidad || nEquipos == 0)
  543.          printf("Cantidad no correcta. Vuelve a intentarlo.\n");
  544.       else
  545.          salir = true;
  546.    }while(salir == false);
  547.  
  548.    return nEquipos;
  549. }
  550. //---------------------------------------------------------------------------
  551.  
  552. void GuardarDatosDeVenta(Ventas Vendedor, Equipos PC, int nRegistros)
  553. {
  554.    char pathVendedor[SIZE_FULL_PATH_FILE],pathVendedorAux[SIZE_FULL_PATH_FILE];
  555.  
  556.    if(AddSaleToFile(Vendedor,PC) == true){
  557.       sprintf(pathVendedor,"%d.bin",Vendedor.ID);
  558.       sprintf(pathVendedorAux,"%d_aux.bin",Vendedor.ID);
  559.       remove(pathVendedor);
  560.       rename(pathVendedorAux,pathVendedor);
  561.       if(nRegistros > 0){
  562.          remove("Stock.bin");
  563.          rename("Stock_aux.bin","Stock.bin");
  564.       }else{
  565.          remove("Stock.bin");
  566.          remove("Stock_aux.bin");
  567.       }
  568.       if(ObtainSellerByPath(pathVendedor,&Vendedor) == 0){
  569.          printf("\n\t\t\tVendedor:\nID: %d\nNombre: %s\nVentas realizadas: %d\n",Vendedor.ID,Vendedor.nombre,Vendedor.contadorventas);
  570.          printf("\nEquipo vendido\n");
  571.       }
  572.    }else{
  573.       remove(pathVendedorAux);
  574.       remove("Stock_aux.bin");
  575.    }
  576. }
  577. //---------------------------------------------------------------------------
  578.  
  579. void AddSale()
  580. {
  581.    int id,iEquipo,nEquipos,nRegistrosStock;
  582.    Ventas Vendedor;
  583.    Equipos PC;
  584.  
  585.    if(ListSellers(false) == 0)
  586.       return;
  587.  
  588.    nRegistrosStock=ListStock(false);
  589.    if(nRegistrosStock <= 0)
  590.       return;
  591.  
  592.    if(ListSellers(true) == 0)
  593.       return;
  594.      
  595.    id = SolicitarIDValido(&Vendedor);
  596.    printf("\n");
  597.  
  598.    nRegistrosStock=ListStock(true);
  599.    if(nRegistrosStock <= 0)
  600.       return;
  601.    
  602.    iEquipo = SolicitarIndiceProductoValido(nRegistrosStock-1);
  603.  
  604.    if(ObtainStockByIndex(&PC,iEquipo) == true){
  605.       nEquipos = ObtenerCantidadEquiposValida(PC);
  606.  
  607.       if(CrearStockActualizado(Vendedor,iEquipo,nEquipos,&nRegistrosStock)==true && CrearVendedorActualizado(Vendedor) == true){
  608.          PC.cantidad=nEquipos;
  609.          GuardarDatosDeVenta(Vendedor,PC,nRegistrosStock);
  610.       }
  611.    }
  612.    clean_stdin();
  613. }
  614. //---------------------------------------------------------------------------
  615.  
  616. int ListStock(bool mostrar)
  617. {
  618.    FILE *archivoStock;
  619.    int count=0;
  620.    Equipos PC;
  621.  
  622.    if((archivoStock=fopen("Stock.bin","rb")) != NULL){
  623.       fseek(archivoStock,0,SEEK_END);
  624.       if(ftell(archivoStock) != 0){
  625.          fseek(archivoStock,0,SEEK_SET);
  626.          if(mostrar==true)
  627.             printf("------------------------------------------------\n");
  628.          while(fread(&PC,sizeof(Equipos),1,archivoStock)==1 && !feof(archivoStock)){
  629.             if(mostrar==true){
  630.                   printf("\n\t\t\tDatos del equipo [%d]\n",count);
  631.                   printf("\nMarca: %s\n",PC.marca);
  632.                   printf("\nColor: %s\n",PC.color);
  633.                   printf("\nProcesador: %s\n",PC.procesador);
  634.                   printf("\nMemoria ram: %s\n",PC.ram);
  635.                   printf("\nDisco duro: %s\n",PC.hdd);
  636.                   printf("\nPrecio de compra: %.2f\n",PC.preciocompra);
  637.                   printf("\nPrecio de venta: %.2f\n",PC.precioventa);
  638.                   printf("\nCantidad en stock: %d\n",PC.cantidad);
  639.                   printf("\n------------------------------------------------\n");
  640.             }
  641.             count++;
  642.          }
  643.       }else{
  644.          printf("\nNo hay productos en stock\n");
  645.       }
  646.       fclose(archivoStock);
  647.    }else{
  648.       printf("\nNo se pudo abrir el archivo de stock\n");
  649.       count=-1;
  650.    }
  651.    return count;
  652. }
  653. //---------------------------------------------------------------------------
  654.  
  655. bool ShowSalesByID(int ID,char *path)
  656. {
  657.    FILE *archivoVentas;
  658.    Equipos PC;
  659.    int count=0;
  660.    bool retval=false;
  661.  
  662.    if((archivoVentas=fopen(path,"rb")) != NULL){
  663.       while(fread(&PC,sizeof(Equipos),1,archivoVentas)==1 && !feof(archivoVentas)){
  664.          printf("\n\t\t\tDatos de la venta [%d]\n",count++);
  665.          printf("\nMarca: %s\n",PC.marca);
  666.          printf("\nColor: %s\n",PC.color);
  667.          printf("\nProcesador: %s\n",PC.procesador);
  668.          printf("\nMemoria ram: %s\n",PC.ram);
  669.          printf("\nDisco duro: %s\n",PC.hdd);
  670.          printf("\nPrecio de compra: %.2f\n",PC.preciocompra);
  671.          printf("\nPrecio de venta: %.2f\n",PC.precioventa);
  672.          printf("\nCantidad vendida: %d\n",PC.cantidad);
  673.          printf("\n------------------------------------------------\n");
  674.       }
  675.       fclose(archivoVentas);
  676.       retval=true;
  677.    }
  678.    return retval;
  679. }
  680. //---------------------------------------------------------------------------
  681.  
  682. int ObtainSellerByPath(char *path,Ventas *Vendedor)
  683. {
  684.    FILE *archivoVendedor;
  685.    int retval=0;
  686.    
  687.    archivoVendedor=fopen(path,"rb");
  688.    if(archivoVendedor!=NULL){
  689.       if(fread(Vendedor,sizeof(Ventas),1,archivoVendedor) != 1){
  690.          printf("\nHuvo algun error al leer el vendedor\n");
  691.          retval=-1;
  692.       }
  693.       fclose(archivoVendedor);
  694.    }else{
  695.       printf("\nHubo algun error al abrir el archivo del vendedor\n");
  696.       retval=-1;
  697.    }
  698.    return retval;
  699. }
  700. //---------------------------------------------------------------------------
  701.  
  702. void ShowSeller(Ventas Vendedor)
  703. {
  704.    printf("Datos del vendedor:\n");
  705.    printf("ID: %d\n",Vendedor.ID);
  706.    printf("Nombre: %s\n",Vendedor.nombre);
  707.    printf("Ventas realizadas: %d\n",Vendedor.contadorventas);
  708.    printf("------------------------------------------------\n");
  709. }
  710. //---------------------------------------------------------------------------
  711.  
  712. void ListSales()
  713. {
  714.    DIR *id_dir;
  715.    Ventas Vendedor;
  716.    struct dirent *filedir;
  717.    char pathFile[SIZE_FULL_PATH_FILE],stringID[SIZE_FULL_PATH_FILE];
  718.    int x,id,contador=0;
  719.    int retval=0;
  720.  
  721.    id_dir=opendir(".\\Vendedores\\");
  722.  
  723.    while((filedir = readdir(id_dir)) != NULL && retval == 0){
  724.       if(strcmp(filedir->d_name,".") != 0 && strcmp(filedir->d_name,"..") != 0){
  725.          sprintf(pathFile,"Vendedores\\%s",filedir->d_name);
  726.          retval = ObtainSellerByPath(pathFile, &Vendedor);
  727.          if(retval == 0){
  728.             ShowSeller(Vendedor);
  729.             sprintf(pathFile,"Ventas\\%s_%d.bin",Vendedor.nombre,Vendedor.ID);
  730.             ShowSalesByID(Vendedor.ID,pathFile);
  731.             contador++;
  732.          }
  733.       }
  734.    }
  735.    closedir(id_dir);
  736.    if(contador == 0)
  737.       printf("\nNo hay vendedores en este momento\n");
  738. }
  739. //---------------------------------------------------------------------------
  740.  
  741. void ListFilteredSalesBySeller()
  742. {
  743.    int id;
  744.    bool i;
  745.    char pathFile[SIZE_FULL_PATH_FILE];
  746.    Ventas Vendedor;
  747.    FILE *archivoVendedor;
  748.  
  749.    if(ListSellers(false) > 0){
  750.       ListSellers(true);
  751.       do{
  752.          printf("\nIntroduce el ID de un vendedor: ");
  753.          if(scanf("%d",&id) == 0)
  754.             clean_stdin();
  755.          sprintf(pathFile,"Vendedores\\%d.bin",id);
  756.  
  757.          if(ObtainSellerByPath(pathFile,&Vendedor) == 0){
  758.             system("CLS");
  759.             ShowSeller(Vendedor);
  760.             sprintf(pathFile,"Ventas\\%s_%d.bin",Vendedor.nombre,Vendedor.ID);
  761.            
  762.             if((ShowSalesByID(id,pathFile))==false)
  763.                printf("\nNo hay ventas para este vendedor\n");
  764.          }else{
  765.             printf("\nID incorrecto. Introduce un ID de la lista\n");
  766.          }
  767.       }while(archivoVendedor == NULL);
  768.    }
  769.    clean_stdin();
  770. }
  771. //---------------------------------------------------------------------------
  772.  
  773. //Leo los datos del vendedor desde un archivo y lo añado en la lista
  774. int InsertarVendedorEnLista(Lista *lista, char *namefile)
  775. {
  776.    char pathFile[SIZE_FULL_PATH_FILE];
  777.    Ventas Vendedor;
  778.    int retval=0;
  779.    Nodo *nuevo_elemento;
  780.  
  781.    if(strcmp(namefile,".") != 0 && strcmp(namefile,"..") != 0){
  782.       //Abro cada archivo del directorio de vendedores
  783.       sprintf(pathFile,"Vendedores\\%s",namefile);
  784.       if(ObtainSellerByPath(pathFile,&Vendedor)==0){
  785.          //Creo una lista enlazada simple con todos los vendedores
  786.          nuevo_elemento = NuevoNodo(lista);
  787.          if(nuevo_elemento == NULL){
  788.             printf("\nHuvo un error al solicitar memoria para crear el ranking\n");
  789.             retval = -1;
  790.          }else{
  791.             memcpy(&nuevo_elemento->Vendedor,&Vendedor,sizeof(Ventas));
  792.          }
  793.       }else{
  794.          printf("\nHubo algun error al leer los vendedores\n");
  795.          retval = -1;
  796.       }
  797.    }
  798.    return retval;
  799. }
  800. //---------------------------------------------------------------------------
  801.  
  802. void ShowRankingBestSeller()
  803. {
  804.    struct dirent *filedir;
  805.    DIR *id_dir;
  806.    Lista Ranking;
  807.    Ventas aux;
  808.    int retval=0;
  809.  
  810.    if((id_dir=opendir(".\\Vendedores\\"))==NULL){
  811.       printf("\nNo se pudo abrir el directorio de vendedores\n");
  812.    }else{
  813.       Ranking = InicializarLista();
  814.       //Recorro todos los archivos del directorio de vendedores
  815.       while((filedir = readdir(id_dir)) != NULL && retval == 0){
  816.          retval = InsertarVendedorEnLista(&Ranking,filedir->d_name);
  817.       }
  818.       closedir(id_dir);
  819.    }
  820.  
  821.    //Ordeno por ventas de mayor a menor
  822.    OrdenarPorVentasMayor(&Ranking);
  823.  
  824.    if(Ranking.numElementos == 0)
  825.       printf("\nNo hay vendedores en este momento\n");
  826.    else{
  827.       //Muestro la lista ordenada
  828.       ImprimirLista(&Ranking);
  829.    }
  830.  
  831.    //Libero la memoria de la lista enlazada de los vendedores
  832.    LimpiarLista(&Ranking);
  833. }
  834. //---------------------------------------------------------------------------
  835.  
  836. int main () {
  837.    int opcion,i;
  838.    bool salir=false;
  839.     FILE *fichero_equipos;
  840.    
  841.    do{
  842.       opcion=menu();
  843.      
  844.       switch(opcion){
  845.          case 1:
  846.             system("CLS");
  847.             AddSale();
  848.             pause();
  849.             break;
  850.  
  851.          case 2:
  852.             system("CLS");
  853.             ListStock(true);
  854.             pause();
  855.             break;
  856.  
  857.          case 3:
  858.             system("CLS");
  859.             ListSales();
  860.             pause();
  861.             break;
  862.  
  863.          case 4:
  864.             system("CLS");
  865.             ListFilteredSalesBySeller();
  866.             pause();
  867.             break;
  868.  
  869.          case 5:
  870.             system("CLS");
  871.             ShowRankingBestSeller();
  872.             pause();
  873.             break;
  874.  
  875.          case 6:
  876.             system("CLS");
  877.             AddPCS();
  878.             pause();
  879.             break;
  880.  
  881.          case 7:
  882.             system("CLS");
  883.             AddSeller();
  884.             pause();
  885.             break;
  886.  
  887.          case 8:
  888.             salir=1;
  889.             break;
  890.  
  891.          default:
  892.             printf("\nOpcion no valida.");
  893.             clean_stdin();
  894.             pause();
  895.             system("CLS");
  896.             break;
  897.       }
  898.    }while(!salir);
  899.    return 0;
  900. }
  901. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement