Roke98

Helper

Sep 13th, 2022 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.60 KB | None | 0 0
  1.  
  2. //import java.math.BigInteger;
  3. //import java.text.DecimalFormat;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.Random;
  7.  
  8. public class Helper {
  9.  
  10.     public static Random random = new Random();
  11.     static Scanner scanner = new Scanner(System.in);
  12.  
  13.     //Character
  14.     public static Character getCharacter(Scanner scanner, String inputMessage, String errorMessage) {
  15.         Character characterValue;
  16.         while(true) {
  17.             System.out.println(inputMessage);
  18.             try {
  19.                 characterValue = scanner.nextLine().charAt(0);
  20.                 return characterValue;
  21.             }catch(Exception exception){
  22.                 System.out.println(errorMessage);
  23.                 scanner.nextLine();
  24.             }
  25.         }  
  26.     }
  27.     public static Character getCharacter(Scanner scanner, String inputMessage) {
  28.         return getCharacter(scanner, inputMessage,"Ingrese un carácter válido...");
  29.     }
  30.     public static Character getCharacter(String inputMessage, String errorMessage) {
  31.         return getCharacter(Helper.scanner, inputMessage, errorMessage);
  32.     }
  33.     public static Character getCharacter(String inputMessage) {
  34.         return getCharacter(Helper.scanner, inputMessage, "Ingrese un carácter válido...");
  35.     }
  36.  
  37.     //Integer
  38.     public static Integer getInteger(Scanner scanner, String inputMessage, String errorMessage) {
  39.         Integer integerValue = 0;
  40.         while(true) {
  41.             System.out.println(inputMessage);
  42.             try {
  43.                 integerValue = Integer.parseInt(scanner.nextLine());
  44.                 return integerValue;
  45.             }catch(Exception exception){
  46.                 System.out.println(errorMessage);
  47.             }
  48.         }
  49.     }
  50.     public static Integer getInteger(Scanner scanner, String inputMessage) {
  51.         return getInteger(scanner, inputMessage, "Ingrese un número entero ");
  52.     }
  53.     public static Integer getInteger(String inputMessage, String errorMessage) {
  54.         return getInteger(Helper.scanner, inputMessage, errorMessage);
  55.     }
  56.     public static Integer getInteger(String inputMessage) {
  57.         return getInteger(Helper.scanner, inputMessage, "Ingrese un número entero ");
  58.     }
  59.     public static Integer getPositiveInt(String inputMessage, String errorMessage) {
  60.         while(true) {
  61.                 int num = getInteger(inputMessage);
  62.                 if (num>0)return num;
  63.                 System.out.println(errorMessage);
  64.         }
  65.     }
  66.     public static Integer getPositiveInt(String inputMessage) {
  67.         return getPositiveInt(inputMessage, "\nError: Ingrese un número positivo ");
  68.     }
  69.  
  70.     //Double
  71.     public static Double getDouble(Scanner scanner, String inputMessage, String errorMessage) {
  72.         Double doubleValue = 0.0;
  73.         while(true) {
  74.             System.out.println(inputMessage);
  75.             try {
  76.                 doubleValue = Double.parseDouble(scanner.nextLine());
  77.                 return doubleValue;
  78.             }catch(Exception exception){
  79.                 System.out.println(errorMessage);
  80.             }
  81.         }
  82.     }
  83.     public static Double getDouble(Scanner scanner, String inputMessage) {
  84.         return getDouble(scanner, inputMessage, "Ingrese un número válido ");
  85.     }
  86.     public static Double getDouble(String inputMessage, String errorMessage) {
  87.         return getDouble(Helper.scanner, inputMessage, errorMessage);
  88.     }
  89.     public static Double getDouble(String inputMessage) {
  90.         return getDouble(Helper.scanner, inputMessage, "Ingrese un número válido ");
  91.     }
  92.  
  93.     //Float
  94.     public static Float getFloat(Scanner scanner, String inputMessage, String errorMessage) {
  95.         Float floatValue = 0f;
  96.         while(true) {
  97.             System.out.println(inputMessage);
  98.             try {
  99.                 floatValue = Float.parseFloat(scanner.nextLine());
  100.                 return floatValue;
  101.             }catch(Exception exception){
  102.                 System.out.println(errorMessage);
  103.             }
  104.         }
  105.     }
  106.     public static Float getFloat(Scanner scanner, String inputMessage) {
  107.         return getFloat(scanner, inputMessage, "Ingrese un número válido ");
  108.     }
  109.     public static Float getFloat(String inputMessage, String errorMessage) {
  110.         return getFloat(Helper.scanner, inputMessage, errorMessage);
  111.     }
  112.     public static Float getFloat(String inputMessage) {
  113.         return getFloat(Helper.scanner, inputMessage, "Ingrese un número válido ");
  114.     }
  115.     public static Float getPositiveFloat(String inputMessage, String errorMessage) {
  116.         while(true) {
  117.                 float num = getFloat(inputMessage);
  118.                 if (num>0)return num;
  119.                 System.out.println(errorMessage);
  120.         }
  121.     }
  122.     public static Float getPositiveFloat(String inputMessage) {
  123.         return getPositiveFloat(inputMessage, "Error: Ingrese un número positivo ");
  124.     }
  125.  
  126.     //Random
  127.     public static int randomInt(int num) {
  128.         int numero;
  129.         numero = random.nextInt(num);
  130.         return numero;
  131.     }
  132.    
  133.     public static float randomFloat(int num) {
  134.         float numero;
  135.         numero = random.nextFloat(num);
  136.         return numero;
  137.     }
  138.    
  139.     public static double randomDouble(int num) {
  140.         double numero;
  141.         numero = random.nextDouble(num);
  142.         return numero;
  143.     }
  144.     public static long randomLong(long l) {
  145.         long numero;
  146.         numero = random.nextLong(l);
  147.         return numero;
  148.     }
  149.    
  150.    
  151.     //random entre
  152.     public static int randomInt2(int num1, int num2) {
  153.         int numero;
  154.         numero =  random.nextInt(num2 - num1) + num1;
  155.         return numero;
  156.     }
  157.     public static float randomFloat2(int num1, int num2) {
  158.         float numero;
  159.         numero =  random.nextFloat(num2 - num1) + num1;
  160.         return numero;
  161.     }
  162.     public static double randomDouble2(int num1, int num2) {
  163.         Double numero;
  164.         numero =  random.nextDouble(num2 - num1) + num1;
  165.         return numero;
  166.     }
  167.     public static long randomLong2(int num1, int num2) {
  168.         Long numero;
  169.         numero =  random.nextLong(num2 - num1) + num1;
  170.         return numero;
  171.     }
  172.    
  173.     //limitar
  174.     public static double limitar2(double num){
  175.         //DecimalFormat decimalFormat = new DecimalFormat("#.00");
  176.         double numero = num;
  177.         double numeroConDosDecimales = Math.round(numero * 100.0) / 100.0;
  178.         return numeroConDosDecimales;
  179.        
  180.     }
  181.    
  182.     //Arrays
  183.  
  184.     public static void mostrarArrayUnaDimension (String inicio , int[] arrayComp , String fin){
  185.         System.out.println(inicio);
  186.         for (Object elemento : arrayComp){
  187.             System.out.print("["+ elemento.toString() +"]");
  188.         }
  189.         System.out.println(fin);
  190.     }
  191.  
  192.     public void mostrarArrayDosDimensiones (String textBefore, int[][] array, String textAfter) {
  193.         System.out.print(textBefore + "\n");
  194.  
  195.         for (int i = 0; i < array.length; i++) {
  196.             for (int j = 0; j < array[i].length; j++) {
  197.                 System.out.print("["+array[i][j]+"]");
  198.             }System.out.println();
  199.         }
  200.     }
  201.  
  202.     //utilidades
  203.     public static void espera (){
  204.         Scanner waitForKeypress = new Scanner(System.in);
  205.         System.out.print("Presiona la tecla Enter para continuar....");
  206.         waitForKeypress.nextLine();
  207.     }
  208.  
  209.     //region Enum Helpers
  210.  
  211.     // from http://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string
  212.     public static String[] getEnumNames(Class<? extends Enum<?>> e) {
  213.         return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", ");
  214.     }
  215.  
  216.     //endregion
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment