RAUL-SUAREZ

Helper

Sep 13th, 2022
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Helper {
  4.    
  5.     static Scanner entrada = new Scanner(System.in);
  6.     public static int forcePositiveIntEnter(String msg) {
  7.         int size = 0;
  8.         while(true){
  9.             System.out.println(msg);
  10.             try{
  11.                 size = Integer.parseInt(entrada.nextLine());
  12.                 if (size > 0) break;
  13.                 else System.out.println("Validos numeros mayores que 0");
  14.             }
  15.             catch(NumberFormatException e){
  16.                 System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS POSITIVOS. "+e);
  17.             }
  18.         }
  19.         return size;
  20.     }
  21.    
  22.     public static int forceIntegerWithoutZ(String msg){
  23.         int n = 0;
  24.         while(true){
  25.             System.out.println(msg);
  26.             try{
  27.                 n = Integer.parseInt(entrada.nextLine());
  28.                 if (n != 0) break;
  29.                 else System.out.println("Ingrese un numero mayor a 0");
  30.             }
  31.             catch(NumberFormatException e){
  32.                 System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
  33.             }
  34.         }
  35.         return n;
  36.     }
  37.  
  38.     public static int forceInteger(String msg){
  39.         int n = 0;
  40.         while(true){
  41.             System.out.println(msg);
  42.             try{
  43.                 n = Integer.parseInt(entrada.nextLine());
  44.                 break;
  45.             }
  46.             catch(NumberFormatException e){
  47.                 System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
  48.             }
  49.         }
  50.         return n;
  51.     }
  52.  
  53.     public static int forcePositiveIntegerWithZ(String msg){
  54.         int n = 0;
  55.         while(true){
  56.             System.out.println(msg);
  57.             try{
  58.                 n = Integer.parseInt(entrada.nextLine());
  59.                 if (n >=0) break;
  60.                 else System.out.println("Ingrese un numero mayor o igual a 0");
  61.             }
  62.             catch(NumberFormatException e){
  63.                 System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
  64.             }
  65.         }
  66.         return n;
  67.     }
  68.  
  69.     public static void showFinalResults(String msg) {
  70.         System.out.println("************FIN DE EJECUCION************");
  71.         System.out.println("Se mostraran resultados finales: ");
  72.         System.out.println(msg);
  73.         System.out.println("******************FIN******************");
  74.     }
  75.  
  76.     public static int menuManualRandom() {
  77.         int option = 0;
  78.         while (true){
  79.             try{
  80.                 //mostrar e ingresar valores para el menu
  81.                 System.out.println("---------------MENU---------------");
  82.                 System.out.println("1- Ingresar valores manuales");
  83.                 System.out.println("2- Ingresar valores aleatorios");
  84.                 option = Integer.parseInt(entrada.nextLine());
  85.                 //corroborar si es correcta la opcion elegida
  86.                 if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
  87.                 else return option;
  88.             }
  89.             catch (NumberFormatException e){
  90.                 System.out.println("Ingrese valor NUMERICO valido" + e);
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static int randomIntGenerator(int min, int max) {
  96.         int x = (int) ((int)(Math.random()*((max-min)+1))+min);
  97.     return x;
  98.     }
  99.    
  100.     public static boolean continueProgram(){
  101.         while (true){
  102.             System.out.println("Desea continuar con la ejecucion del programa? Opciones validas: (S/s || N/n)");
  103.             String resp = entrada.nextLine();
  104.             if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
  105.             else  System.out.println("Opcion no valida");
  106.         }
  107.     }
  108.     public static int menuProductoSuma() {
  109.         int option = 0;
  110.         while (true){
  111.             try{
  112.                 //mostrar e ingresar valores para el menu
  113.                 System.out.println("---------------MENU---------------");
  114.                 System.out.println("1- Producto de una Fila");
  115.                 System.out.println("2- Suma de una Columna");
  116.                 option = Integer.parseInt(entrada.nextLine());
  117.                 //corroborar si es correcta la opcion elegida
  118.                 if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
  119.                 else return option;
  120.             }
  121.             catch (NumberFormatException e){
  122.                 System.out.println("Ingrese valor NUMERICO valido" + e);
  123.             }
  124.         }
  125.     }
  126.     public static int menuOriginalTranspuesta() {
  127.         int option = 0;
  128.         while (true){
  129.             try{
  130.                 //mostrar e ingresar valores para el menu
  131.                 System.out.println("---------------MENU---------------");
  132.                 System.out.println("1- Matriz Original");
  133.                 System.out.println("2- Matriz Transpuesta");
  134.                 option = Integer.parseInt(entrada.nextLine());
  135.                 //corroborar si es correcta la opcion elegida
  136.                 if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
  137.                 else return option;
  138.             }
  139.             catch (NumberFormatException e){
  140.                 System.out.println("Ingrese valor NUMERICO valido" + e);
  141.             }
  142.         }
  143.     }
  144.     public static boolean continueCargaFibonacci(){
  145.         while (true){
  146.             System.out.println("Desea seguir Cargando datos de Fibonacci?  (S/s=si || N/n=no)");
  147.             String resp = entrada.nextLine();
  148.             if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
  149.             else  System.out.println("Opcion no valida");
  150.         }
  151.     }
  152.     public static boolean continueCargaLibro(){
  153.         while (true){
  154.             System.out.println("Desea seguir Cargando Libros?  (S/s=si || N/n=no)");
  155.             String resp = entrada.nextLine();
  156.             if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
  157.             else  System.out.println("Opcion no valida");
  158.         }
  159.     }
  160.     public static Float getFloat(Scanner entrada, String inputMessage, String errorMessage) {
  161.         Float floatValuer = 0f;
  162.         while (true) {
  163.             try {
  164.                 System.out.print(inputMessage);
  165.                 floatValuer = Float.parseFloat(entrada.nextLine());
  166.                 // return BigDecimal.valueOf(d).setScale(decimalPlace,BigDecimal.ROUND_HALF_UP).floatValue();
  167.                 return ((int) ((floatValuer + 0.005f) * 100)) / 100f;
  168.             }
  169.             catch (Exception exception) {
  170.                 System.out.println(errorMessage);
  171.             }
  172.         }
  173.     }
  174.     public static Float getFloat(Scanner entrada, String inputMessage) {
  175.         return getFloat(entrada, inputMessage, "Ingrese un número válido\n");
  176.     }
  177.     public static Float getFloat(String inputMessage, String errorMessage) {
  178.         return getFloat(Helper.entrada, inputMessage, errorMessage);
  179.     }
  180.     public static Float getFloat(String inputMessage) {
  181.         return getFloat(Helper.entrada, inputMessage, "Ingrese un número válido\n");
  182.     }
  183.    
  184.     public static int menuAutorEditorial() {
  185.         int option = 0;
  186.         while (true){
  187.             try{
  188.                 //mostrar e ingresar valores para el menu
  189.                 System.out.println("---------------MENU---------------");
  190.                 System.out.println("1- Buscar Libro por Autor");
  191.                 System.out.println("2- Buscar Libro por Editorial");
  192.                 option = Integer.parseInt(entrada.nextLine());
  193.                 //corroborar si es correcta la opcion elegida
  194.                 if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
  195.                 else return option;
  196.             }
  197.             catch (NumberFormatException e){
  198.                 System.out.println("Ingrese valor NUMERICO valido" + e);
  199.             }
  200.         }
  201.     }
  202.     public static String getValidsString(String msg){
  203.         while(true){
  204.             try{
  205.                 System.out.println(msg);
  206.                 String rtnString = entrada.nextLine();
  207.                 if (rtnString.isEmpty() || rtnString.charAt(0) == ' ') Integer.parseInt("s");
  208.                 return rtnString;
  209.             }
  210.             catch(Exception e){
  211.                 System.out.println("Ingrese una cadena valida. Sin espacios al comienzo.");
  212.             }
  213.         }
  214.     }
  215.     public static boolean continueCargaAutores(){
  216.         while (true){
  217.             System.out.println("Desea seguir Cargando Autores?  (S/s=si || N/n=no)");
  218.             String resp = entrada.nextLine();
  219.             if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
  220.             else  System.out.println("Opcion no valida");
  221.         }
  222.     }
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment