Advertisement
Mitoeap

Helper Class

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