×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Samuel Palacios
Added: May 26, 2019 9:33 PM
Views: 3917
Tags: no tags
  1. package practica;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.InputMismatchException;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  *
  10.  * @author sdpb0
  11.  */
  12. public class Practica {
  13.  
  14.     static String respuesta,
  15.             nombre = "",
  16.             nombrePersistencia = "",
  17.             // pathFicheros = "C:\\Users\\sdpb0\\Desktop\\Proyecto Fundamentos\\Practica\\src\\archivos",
  18.             pathPersistencia = "./src/persistencia/",
  19.             // pathPersistencia = "C:\\Users\\sdpb0\\Desktop\\Proyecto Fundamentos\\Practica\\src\\persistencia",
  20.             pathFicheros = "./src/ficheros/",
  21.             despedida = "\nGracias, hasta luego.";
  22.     static int opcion, datoEvaluador;
  23.     static Dato dato = new Dato();
  24.     static float comparador;
  25.     static ArrayList<Dato> lista, listaFiltrada;
  26.     static Scanner in;
  27.     static DataFrame dataFrame;
  28.  
  29.     public static void main(String[] args) throws FileNotFoundException {
  30.         run();
  31.     }
  32.  
  33.     private static void run() {
  34.         try {
  35.             in = new Scanner(System.in);
  36.             dataFrame = new DataFrame();
  37.             ciudad();
  38.             lista = dataFrame.leerFichero(nombre, pathFicheros);
  39.             menu();
  40.         } catch (FileNotFoundException e) {
  41.             System.out.println(e.getMessage());
  42.         }
  43.  
  44.     }
  45.  
  46.     private static void ciudad() {
  47.         System.out.println("\n==================");
  48.         System.out.println("     Ciudades  ");
  49.         System.out.println("==================");
  50.         System.out.println("1) Medellín");
  51.         System.out.println("2) Bogotá");
  52.         System.out.println("3) Lyon");
  53.         System.out.println("4) Roma");
  54.         System.out.println("5) prueba");
  55.         System.out.println("6) salir");
  56.         try {
  57.             do {
  58.                 System.out.print("\nIngresa una opción: ");
  59.                 opcion = in.nextInt();
  60.             } while ((opcion > 6) || (opcion <= 0));
  61.  
  62.             opcionCiudad(opcion);
  63.         } catch (InputMismatchException e) {
  64.             System.out.println("\nSolo numeros de 1 a 6");
  65.             run();
  66.         }
  67.  
  68.     }
  69.  
  70.     private static void opcionCiudad(int opcion) {
  71.         switch (opcion) {
  72.             case 1:
  73.                 nombre = "medellin.csv";
  74.  
  75.                 break;
  76.  
  77.             case 2:
  78.                 nombre = "bogota.csv";
  79.  
  80.                 break;
  81.  
  82.             case 3:
  83.                 nombre = "lyon.csv";
  84.  
  85.                 break;
  86.  
  87.             case 4:
  88.                 nombre = "roma.csv";
  89.  
  90.                 break;
  91.  
  92.             case 5:
  93.                 nombre = "asd.csv";
  94.  
  95.                 break;
  96.  
  97.             case 6:
  98.                 System.out.println(despedida);
  99.  
  100.                 break;
  101.  
  102.             default:
  103.                 throw new AssertionError();
  104.         }
  105.     }
  106.  
  107.     private static void menu() throws FileNotFoundException {
  108.         System.out.println("\n==================");
  109.         System.out.println("  Menú Principal");
  110.         System.out.println("==================");
  111.         System.out.println("1) Estadisticas");
  112.         System.out.println("2) Filtros");
  113.         System.out.println("3) Visualización");
  114.         System.out.println("4) Ver todos los datos en consola");
  115.         System.out.println("5) Resumen ciudad");
  116.         System.out.println("6) Escoger otra ciudad");
  117.         System.out.println("7) Salir");
  118.         try {
  119.             do {
  120.                 System.out.print("\nIngresa una opción: ");
  121.                 opcion = in.nextInt();
  122.             } while ((opcion > 7) || (opcion <= 0));
  123.  
  124.             opcionMenu(opcion);
  125.         } catch (InputMismatchException e) {
  126.             System.out.println("\nSolo numeros de 1 a 7");
  127.  
  128.         }
  129.  
  130.     }
  131.  
  132.     private static void opcionMenu(int opcion) {
  133.         try {
  134.             switch (opcion) {
  135.                 case 1:
  136.                     estadisticas();
  137.  
  138.                     break;
  139.  
  140.                 case 2:
  141.                     filtros();
  142.  
  143.                     break;
  144.  
  145.                 case 3:
  146.                     visualizacion();
  147.  
  148.                     break;
  149.  
  150.                 case 4:
  151.                     dataFrame.escribirFichero();
  152.                     opciones();
  153.                 case 5:
  154.                     System.out.println("\n" + nombre.substring(0, nombre.length() - 4).toUpperCase());
  155.                     System.out.println("-------------------------------------");
  156.                     Estadistica.resumenCuidad(lista);
  157.                     opciones();
  158.                     break;
  159.  
  160.                 case 6:
  161.                     run();
  162.  
  163.                     break;
  164.  
  165.                 case 7:
  166.                     System.out.println("\nGracias, Hasta Luego.");
  167.  
  168.                     break;
  169.  
  170.                 default:
  171.                     System.out.println("Solo numeros de 1-7");
  172.                     throw new AssertionError();
  173.  
  174.             }
  175.         } catch (FileNotFoundException e) {
  176.             System.out.println(e.getMessage());
  177.         }
  178.  
  179.     }
  180.  
  181.     private static void estadisticas() throws FileNotFoundException {
  182.         System.out.println("\n==================");
  183.         System.out.println("   Estadisticas   ");
  184.         System.out.println("==================");
  185.         System.out.println("1) Promedio");
  186.         System.out.println("2) Moda");
  187.         System.out.println("3) Minimo");
  188.         System.out.println("4) Máximo");
  189.         System.out.println("5) Desviación Estandard");
  190.         System.out.println("6) Conteo");
  191.         System.out.println("7) Menu principal");
  192.         System.out.println("8) Salir");
  193.         try {
  194.             do {
  195.                 System.out.print("\nIngresa una opción: ");
  196.                 opcion = in.nextInt();
  197.             } while ((opcion > 8) || (opcion <= 0));
  198.             datos();
  199.             opcionEstadisticas(opcion, datoEvaluador);
  200.         } catch (InputMismatchException e) {
  201.             System.out.println("\nSolo numeros de 1 a 8");
  202.         }
  203.  
  204.     }
  205.  
  206.     private static void opcionEstadisticas(int opcion, int datoEvaluador) {
  207.         try {
  208.             switch (opcion) {
  209.                 case 1:
  210.                     System.out.println("\nPromedio: " + Estadistica.promedio(lista, datoEvaluador));
  211.                     opciones();
  212.                     break;
  213.  
  214.                 case 2:
  215.                     String moda = (Estadistica.moda(lista, datoEvaluador) == -1) ? "\nNo hay moda, todos los datos son diferentes." : "\nModa: " + Estadistica.moda(lista, datoEvaluador);
  216.                     System.out.println(moda);
  217.                     opciones();
  218.                     break;
  219.  
  220.                 case 3:
  221.                     System.out.println("\nMinimo: " + Estadistica.minimo(lista, datoEvaluador));
  222.                     opciones();
  223.                     break;
  224.  
  225.                 case 4:
  226.                     System.out.println("\nMáximo" + Estadistica.maximo(lista, datoEvaluador));
  227.                     opciones();
  228.                     break;
  229.                 case 5:
  230.                     System.out.println("\nDesviación estandard" + Estadistica.desvEstandard(lista, datoEvaluador));
  231.                     opciones();
  232.                     break;
  233.  
  234.                 case 6:
  235.                     System.out.println("\nConteo" + Estadistica.conteo(lista, datoEvaluador));
  236.                     opciones();
  237.                     break;
  238.  
  239.                 case 7:
  240.                     menu();
  241.  
  242.                     break;
  243.  
  244.                 case 8:
  245.                     System.out.println(despedida);
  246.  
  247.                     break;
  248.  
  249.                 default:
  250.                     throw new AssertionError();
  251.             }
  252.         } catch (FileNotFoundException | ArithmeticException e) {
  253.             System.out.println(e.getMessage());
  254.         }
  255.  
  256.     }
  257.  
  258.     private static void filtros() throws FileNotFoundException {
  259.         System.out.println("\n==================");
  260.         System.out.println("      Filtros     ");
  261.         System.out.println("==================");
  262.         System.out.println("1) Mayor que...");
  263.         System.out.println("2) Menor que...");
  264.         System.out.println("3) Diferente De...");
  265.         System.out.println("4) Igual A...");
  266.         System.out.println("5) Numero de sensor");
  267.         System.out.println("6) Menu principal");
  268.         System.out.println("7) Salir");
  269.         try {
  270.             do {
  271.                 System.out.print("\nIngresa una opción: ");
  272.                 opcion = in.nextInt();
  273.             } while ((opcion > 7) || (opcion <= 0));
  274.  
  275.             datos();
  276.             System.out.print("\nAhora ingresa la pauta comparadora: ");
  277.             comparador = in.nextFloat();
  278.             opcionFiltros(opcion, datoEvaluador, comparador);
  279.         } catch (InputMismatchException e) {
  280.             System.out.println("\nSolo numeros de 1 a 7");
  281.         }
  282.  
  283.     }
  284.  
  285.     private static void opcionFiltros(int opcion, int datoEvaluador, float comparador) {
  286.         try {
  287.             switch (opcion) {
  288.                 case 1:
  289.                     listaFiltrada = Filtros.mayorQue(lista, datoEvaluador, comparador);
  290.                     guardarDatos();
  291.                     break;
  292.  
  293.                 case 2:
  294.                     listaFiltrada = Filtros.menorQue(lista, datoEvaluador, comparador);
  295.                     guardarDatos();
  296.                     break;
  297.  
  298.                 case 3:
  299.                     listaFiltrada = Filtros.diferenteDe(lista, datoEvaluador, comparador);
  300.                     guardarDatos();
  301.                     break;
  302.  
  303.                 case 4:
  304.                     listaFiltrada = Filtros.igualA(lista, datoEvaluador, comparador);
  305.                     guardarDatos();
  306.                     break;
  307.  
  308.                 case 5:
  309.                     listaFiltrada = Filtros.numSensor(lista, datoEvaluador, comparador);
  310.                     guardarDatos();
  311.                     break;
  312.  
  313.                 case 6:
  314.                     menu();
  315.                     break;
  316.  
  317.                 case 7:
  318.                     System.out.println(despedida);
  319.  
  320.                     break;
  321.  
  322.                 default:
  323.                     throw new AssertionError();
  324.             }
  325.         } catch (FileNotFoundException e) {
  326.             System.out.println(e.getMessage());
  327.         }
  328.  
  329.     }
  330.  
  331.     private static void visualizacion() throws FileNotFoundException {
  332.         System.out.println("\n==================");
  333.         System.out.println("   Visualización  ");
  334.         System.out.println("==================");
  335.         System.out.println("1) Diagrama de Lineas");
  336.         System.out.println("2) Diagrama de Puntos");
  337.         System.out.println("3) Diagrama de Torta");
  338.         System.out.println("4) Diagrama de Barras");
  339.         System.out.println("5) Menu principal");
  340.         System.out.println("6) salir");
  341.         try {
  342.             do {
  343.                 System.out.print("\nIngresa una opción: ");
  344.                 opcion = in.nextInt();
  345.             } while ((opcion > 6) || (opcion <= 0));
  346.  
  347.             opcionVisualizacion(opcion);
  348.         } catch (InputMismatchException e) {
  349.             System.out.println("\nSolo numeros de 1 a 6");
  350.         }
  351.  
  352.     }
  353.  
  354.     private static void opcionVisualizacion(int opcion) {
  355.         try {
  356.             switch (opcion) {
  357.                 case 1:
  358.                     break;
  359.  
  360.                 case 2:
  361.                     break;
  362.  
  363.                 case 3:
  364.                     break;
  365.  
  366.                 case 4:
  367.                     break;
  368.  
  369.                 case 5:
  370.                     menu();
  371.                     break;
  372.  
  373.                 case 6:
  374.                     System.out.println(despedida);
  375.                     break;
  376.                 default:
  377.                     throw new AssertionError();
  378.             }
  379.         } catch (FileNotFoundException e) {
  380.         }
  381.  
  382.     }
  383.  
  384.     private static void opciones() throws FileNotFoundException {
  385.         System.out.println("\n==================");
  386.         System.out.println("      Opciones    ");
  387.         System.out.println("==================");
  388.         System.out.println("1) Elegir otra ciudad");
  389.         System.out.println("2) Volver al menu principal");
  390.         System.out.println("3) Salir");
  391.         try {
  392.             do {
  393.                 System.out.print("\nIngresa una opción: ");
  394.                 opcion = in.nextInt();
  395.             } while ((opcion > 3) || (opcion <= 0));
  396.  
  397.             opcionOpciones(opcion);
  398.         } catch (InputMismatchException e) {
  399.             System.out.println("\nSolo numeros de 1 a 3");
  400.         }
  401.  
  402.     }
  403.  
  404.     private static void opcionOpciones(int opcion) {
  405.         try {
  406.             switch (opcion) {
  407.                 case 1:
  408.                     run();
  409.  
  410.                     break;
  411.  
  412.                 case 2:
  413.                     menu();
  414.  
  415.                     break;
  416.  
  417.                 case 3:
  418.                     System.out.println("\nGracias, hasta luego.");
  419.  
  420.                     break;
  421.  
  422.                 default:
  423.                     throw new AssertionError();
  424.             }
  425.         } catch (FileNotFoundException e) {
  426.             System.out.println(e.getMessage());
  427.         }
  428.  
  429.     }
  430.  
  431.     private static void datos() throws FileNotFoundException {
  432.         System.out.println("\n¿Qué dato desear evaluar?");
  433.         System.out.println("==================");
  434.         System.out.println("       Datos      ");
  435.         System.out.println("==================");
  436.         System.out.println("1) Precipitación");
  437.         System.out.println("2) Temperatura promedio");
  438.         System.out.println("3) Temperatura máxima");
  439.         System.out.println("4) Temperatura minima");
  440.         try {
  441.             do {
  442.                 System.out.print("\nIngresa una opción: ");
  443.                 datoEvaluador = in.nextInt();
  444.             } while ((datoEvaluador > 4) || (datoEvaluador <= 0));
  445.         } catch (InputMismatchException e) {
  446.             System.out.println("\nSolo numeros de 1 a 4");
  447.             menu();
  448.         }
  449.  
  450.     }
  451.  
  452.     private static void guardarDatos() {
  453.         try {
  454.             do {
  455.                 System.out.print("\n ¿Deseas guardar estos datos? (y/n) ");
  456.                 respuesta = Character.toString(in.next().charAt(0));
  457.             } while (!(respuesta.equalsIgnoreCase("y") || !(respuesta.equalsIgnoreCase("n"))));
  458.             if (respuesta.equalsIgnoreCase("y")) {
  459.                 nombrePersistencia = nombre.substring(0, nombre.length() - 4)
  460.                         .toUpperCase() + " " + dato.toString(datoEvaluador) + " " + comparador
  461.                         + ".txt";
  462.  
  463.                 dataFrame.escribirFichero(lista, pathPersistencia + nombrePersistencia);
  464.             }
  465.             opciones();
  466.         } catch (FileNotFoundException e) {
  467.             System.out.println(e.getMessage());
  468.         }
  469.  
  470.     }
  471.  
  472. }//No tocar
  473.