Advertisement
Mitoeap

Clase Helper

Aug 22nd, 2021
1,669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Helper {
  7.  
  8.     //region Static Objects
  9.  
  10.     public static Scanner scanner = new Scanner(System.in);
  11.     public static Random random = new Random();
  12.  
  13.     //endregion
  14.  
  15.     //region Integer Helper
  16.  
  17.     private static Integer getInt(String inputMessage, String errorMessage) {
  18.         while(true){
  19.             try {
  20.                 System.out.println(inputMessage);
  21.                 return Integer.parseInt(scanner.nextLine());
  22.             } catch (NumberFormatException e) {
  23.                 System.out.println(errorMessage);
  24.            }
  25.         }    
  26.     }
  27.  
  28.     public static Integer getInt(String inputMessage) {
  29.         return getInt(inputMessage, "\nERROR: EL VALOR INGRESADO NO CORRESPONDE A UN NUMERO ENTERO");
  30.     }
  31.  
  32.     private static Integer getPositiveInt(String inputMessage, String errorMessge) {
  33.         while (true) {
  34.             int num = getInt(inputMessage);
  35.             if(num > 0)return num;
  36.             System.out.println("\n" + errorMessge);
  37.         }
  38.     }
  39.  
  40.     public static Integer getPositiveInt(String inputMessage){
  41.         return getPositiveInt(inputMessage, "\nERROR: EL NUMERO INGRESADO NO ES POSITIVO");
  42.     }
  43.  
  44.     //endregion
  45.  
  46.     //region Double Helper
  47.  
  48.     private static Double getDouble(String inputMessage, String errorMessage) {
  49.         while(true){
  50.             try {
  51.                 System.out.println(inputMessage);
  52.                 return Double.parseDouble(scanner.nextLine());
  53.             } catch (NumberFormatException e) {
  54.                 System.out.println(errorMessage);
  55.             }
  56.         }
  57.     }
  58.  
  59.     public static Double getDouble(String inputMessage){
  60.         return getDouble(inputMessage, "\nERROR: EL VALOR INGRESADO NO CORRESPONDE A UN NUMERO");
  61.     }
  62.  
  63.     //endregion
  64.  
  65.     //region Character Helper
  66.  
  67.     private static Character getChar(String inputMessage, String errorMessage) {
  68.  
  69.         while (true) {
  70.             try {
  71.                 System.out.println("\n" + inputMessage);
  72.                 char caracter = scanner.nextLine().toUpperCase().charAt(0);
  73.         int valorASCII = (int)caracter;
  74.                 if (valorASCII == 165 || (valorASCII >= 65 && valorASCII <= 90))
  75.                     return caracter;
  76.                 else
  77.                     throw new Exception(errorMessage);
  78.             } catch (Exception e) {
  79.                 System.out.println("\n" + e.getMessage());
  80.                 scanner.nextLine();
  81.             }
  82.         }
  83.     }
  84.  
  85.     public static Character getChar(String inputMessage){
  86.         return getChar(inputMessage, "\nERROR: INGRESE UN CARACTER VALIDO");
  87.     }
  88.  
  89.     //endregion
  90.  
  91.     //region Random Double with two decimal
  92.  
  93.     public static double randomDouble(int bound) {
  94.         double num;
  95.         num = random.nextInt(bound) + random.nextDouble();
  96.         return (double)Math.round(num * 100d)/100;
  97.     }
  98.  
  99.     //endregion
  100.  
  101.     //region Question (yes or no)
  102.  
  103.     public static char yesOrNo(String question){
  104.         char resp;
  105.         do {
  106.             System.out.println("\n" + question + "\nPRESIONE 'S'-SI\nPRESIONE 'N'-NO");
  107.             resp = Character.toUpperCase(scanner.nextLine().charAt(0));
  108.             if(resp == 'N' || resp == 'S')return resp;
  109.             System.out.println("\nERROR: LA OPCION INGRESADA NO ES CORRECTA INTENTELO DE NUEVO");
  110.         } while (true);
  111.     }
  112.  
  113.     //endregion
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement