Advertisement
andresnogales

Main2.java

Sep 15th, 2021 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. public class Main2 {
  2.  
  3.     public static void main(String[] args) {
  4.         Arreglo arregloDatos = new Arreglo();
  5.  
  6.         String[] options = {
  7.                 "Ingresar datos por consola",
  8.                 "Generar datos aleatorios"
  9.                 };
  10.        
  11.         Menu menu = new Menu(options);
  12.         boolean repeat = true;
  13.        
  14.         while(repeat) {
  15.             int option = menu.getOption();
  16.             switch (option) {
  17.             case 1:
  18.                 arregloDatos.setDimension(Helper.getPositiveInt("Ingrese el tamaño del arreglo: "));
  19.                 ingresoDatosConsola(arregloDatos);
  20.                 mostrarResultados(arregloDatos);
  21.                 break;
  22.             case 2:
  23.                 arregloDatos.setDimension(Helper.getPositiveInt("Ingrese el tamaño del arreglo: "));
  24.                 generarDatosAleatorio(arregloDatos);
  25.                 mostrarResultados(arregloDatos);
  26.                 break;
  27.             case 0:
  28.                 repeat = false;
  29.                 System.out.println("Fin de programa");
  30.                 continue;
  31.             }
  32.             Helper.pressEnterKeyToContinue();
  33.         }
  34.     }
  35.  
  36.     // En este procedimiento se ingresan valores por consola y se guardan solo los
  37.     // que cumplen con la condicion
  38.     public static void ingresoDatosConsola(Arreglo arregloDatos) {
  39.         Integer[] arreglo = new Integer[arregloDatos.getDimension()];
  40.         Integer num, i = 0;
  41.         while (i < arregloDatos.getDimension()) {
  42.             num = Helper.getInt("Ingrese dato " + (i + 1) + " :");
  43.             if (validarCondicionMultiplos(num)) {
  44.                 arreglo[i] = num;// Se guarda solo los numeros que han sido verificados que cumplen con la
  45.                                     // condicion de multiplos
  46.                 i++;
  47.             } else {
  48.                 System.err.println(
  49.                         "El numero no cumple con la condicion de que sea multiplo de 5 y a la vez no sea multiplo de 3.\nIntente de nuevo.");
  50.             }
  51.         }
  52.         arregloDatos.setArreglo(arreglo);
  53.     }
  54.  
  55.     // Valida que el numero ingresado sea multiplo de 5 pero no de 3
  56.     public static boolean validarCondicionMultiplos(int num) {
  57.         return num % 5 == 0 && num % 3 != 0;
  58.     }
  59.  
  60.     // En este procedimiento se generan valores aleatorios y se guardan solo los que
  61.     // cumplen con la condicion
  62.     public static void generarDatosAleatorio(Arreglo arregloDatos) {
  63.         int n = arregloDatos.getDimension();
  64.         Integer intArray[] = new Integer[n];
  65.         for (int i = 0; i < n; i++) {
  66.             int number = Helper.randomInt(1, 100) * 5;
  67.             if (MathUtils.isMultipleOf(number, 3)) number += 5;
  68.             intArray[i] = number;
  69.         }
  70.             arregloDatos.setArreglo(intArray);
  71.     }
  72.  
  73.     public static void mostrarResultados(Arreglo arregloDatos) {
  74.         System.out.println("Datos ingresados: ");
  75.         ArrayUtils.print(arregloDatos.getArreglo());
  76.         System.out.println("Datos ingresados invertidos: ");
  77.         Object [] reverseArray = ArrayUtils.reverse(arregloDatos.getArreglo());
  78.         ArrayUtils.print(reverseArray);
  79.         arregloDatos.mostrarParImpar();
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement