Advertisement
WallHero

TP03E06ClassHelper

Oct 16th, 2020
2,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package punto;
  2. import java.util.Scanner;
  3.  
  4. class Helper {
  5.     public static Scanner scanner = new Scanner(System.in);
  6.    
  7.     public static int forceReadPositiveInteger(String msg)
  8.     {
  9.         int ret = 0;
  10.         while(true)
  11.         {
  12.             try
  13.             {
  14.                 System.out.println(msg);
  15.                 while((ret = Integer.parseInt(scanner.next())) < 1) System.out.println("El número a cargar debe ser positivo.\n"+msg);
  16.                 return ret;
  17.             }catch (Exception e) {
  18.                 System.out.println("Error en la carga.");
  19.             }
  20.         }
  21.     }
  22.    
  23.     public static int forceReadRangeInteger(String msg, int a, int b)
  24.     {
  25.         int ret = 0;
  26.         while(true)
  27.         {
  28.             try
  29.             {
  30.                 System.out.println(msg);
  31.                 while((ret = Integer.parseInt(scanner.next())) < a || ret > b) System.out.println("Opción fuera de rango.\n"+msg);
  32.                 return ret;
  33.             }catch (Exception e) {
  34.                 System.out.println("Error en la carga.");
  35.             }
  36.         }
  37.     }
  38.    
  39.    
  40.     public static char forceReadAnLetter(String msg)
  41.     {
  42.         char ret = '\0';
  43.         while(true)
  44.         {
  45.             try
  46.             {
  47.                 String aux;
  48.                 System.out.println(msg);
  49.                 while((aux = scanner.next()).length() > 1 || !Character.isAlphabetic((ret = aux.charAt(0))) ) System.out.println("Se debe cargar una sola letra.\n" + msg);
  50.                 return ret;
  51.             }catch (Exception e) {
  52.                 System.out.println("Error en la carga.");
  53.             }
  54.         }
  55.     }
  56.    
  57.    
  58.     public static int getRandomIntBetweenRange(double min, double max){
  59.         int x = (int) ((int)(Math.random()*((max-min)+1))+min);
  60.         return x;
  61.     }
  62.     public static char generateRandomLetter()
  63.     {
  64.         return ((getRandomIntBetweenRange(0,17)% 2 == 0) ?  (char) (getRandomIntBetweenRange(0, 25)+97): (char) (getRandomIntBetweenRange(0, 25)+65));
  65.     }
  66.    
  67.    
  68.     public static boolean isVowel(char xCh)
  69.     {
  70.         xCh = Character.toLowerCase(xCh);
  71.         return xCh == 'a' || xCh == 'e' || xCh == 'i' || xCh == 'o' || xCh == 'u';
  72.     }
  73.    
  74.     public static String generateRandomString()
  75.     {
  76.         String ret = "";
  77.         for(int i = 0; i < getRandomIntBetweenRange(5, 20); i++)
  78.         {
  79.             ret+=generateRandomLetter();
  80.         }
  81.         return ret;
  82.     }
  83.     public static boolean continueInput(String msg)
  84.     {
  85.         char x = '\0';
  86.         while(true)
  87.         {
  88.             try
  89.             {
  90.                 System.out.println(msg);
  91.                 x =Character.toUpperCase(scanner.next().charAt(0));
  92.                 if(x == 'S' || x == 'N') return x == 'S';
  93.                 System.out.println("Opción inválida, reintente.");
  94.             }catch (Exception e) {
  95.                 System.out.println("Opción inválida, reintente.");
  96.             }
  97.         }
  98.     }
  99.    
  100.     public static boolean isRandom()
  101.     {
  102.         char x = '\0';
  103.         while(true)
  104.         {
  105.             try
  106.             {
  107.                 System.out.println("¿Generar valores aleatorios? (S/N)");
  108.                 x =Character.toUpperCase(scanner.next().charAt(0));
  109.                 if(x == 'S' || x == 'N') return x == 'S';
  110.                 System.out.println("Opción inválida, reintente.");
  111.             }catch (Exception e) {
  112.                 System.out.println("Opción inválida, reintente.");
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement