Advertisement
andresnogales

Helper.java

Nov 10th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Helper {
  8.    
  9.     public static Scanner scanner = new Scanner(System.in);
  10.     public static Random random = new Random();
  11.    
  12.     public static Integer getPositiveInt(String message) {
  13.         while(true){
  14.             try {
  15.                 System.out.println(message);
  16.                 int num = Integer.parseInt(scanner.nextLine());
  17.                 if(num > 0) return num;
  18.                 System.err.println("\nError: no ingresó un número entero positivo");
  19.             } catch (NumberFormatException e) {
  20.                 System.err.println("\nError: ingresó un caracter inválido");
  21.            }
  22.         }
  23.     }
  24.    
  25.     public static Long getPositiveLong(String message) {
  26.         while(true){
  27.             try {
  28.                 System.out.println(message);
  29.                 Long num = Long.parseLong(scanner.nextLine());
  30.                 if(num > 0) return num;
  31.                 System.err.println("\nError: no ingresó un número entero positivo");
  32.             } catch (NumberFormatException e) {
  33.                 System.err.println("\nError: ingresó un caracter inválido");
  34.            }
  35.         }
  36.     }
  37.    
  38.     private static Integer getInt(String inputMessage, String errorMessage) {
  39.         while(true){
  40.             try {
  41.                 System.out.println(inputMessage);
  42.                 return Integer.parseInt(scanner.nextLine());
  43.             } catch (NumberFormatException e) {
  44.                 System.err.println(errorMessage);
  45.            }
  46.         }
  47.     }
  48.    
  49.     public static Integer getInt(String inputMessage) {
  50.         return getInt(inputMessage, "\nError: el valor ingresado no es un número entero");
  51.     }
  52.  
  53.     public static char yesOrNo(String question){
  54.         char resp;
  55.         do {
  56.             System.out.println("\n" + question + "\nPresione 'S' (SI) o 'N' (NO)");
  57.             resp = Character.toUpperCase(scanner.nextLine().charAt(0));
  58.             if(resp == 'N' || resp == 'S')return resp;
  59.             System.err.println("\nError: lo ingresado no es una opción correcta");
  60.         } while (true);
  61.     }
  62.    
  63.     private static Double getDouble(String inputMessage, String errorMessage) {
  64.         while(true){
  65.             try {
  66.                 System.out.println(inputMessage);
  67.                 return Double.parseDouble(scanner.nextLine());
  68.             } catch (NumberFormatException e) {
  69.                 System.err.println(errorMessage);
  70.             }
  71.         }
  72.     }
  73.  
  74.     public static Double getDouble(String inputMessage){
  75.         return getDouble(inputMessage, "\nError: el valor ingresado no es un número");
  76.     }
  77.    
  78.     private static char getCharOp(String inputMessage, String errorMessage){
  79.        
  80.         while(true) {
  81.             try {
  82.                 System.out.println(inputMessage);
  83.                 char caracter = scanner.nextLine().charAt(0);
  84.                 int valorASCII = (int)caracter;
  85.                
  86.                 if (valorASCII == 42 || valorASCII == 43 || valorASCII == 45 || valorASCII == 47) {            
  87.                     return caracter;
  88.                 }
  89.                 else {
  90.                     throw new Exception(errorMessage);
  91.                 }
  92.             }
  93.             catch (Exception e) {
  94.                 System.err.println("\n" + e.getMessage());
  95.             }
  96.         }
  97.     }
  98.    
  99.     public static char getCharOp(String inputMessage){
  100.         return getCharOp(inputMessage, "\nError: ingresó un caracter de operación inválido");
  101.     }
  102.    
  103.     public static double randomDouble(int max, int min) {
  104.         double number =  random.nextDouble() * (max - min) + min;
  105.         return (double)Math.round(number * 100d)/100;
  106.     }
  107.    
  108.     public static int randomInt(int min,int max) {
  109.         return random.nextInt(max - min + 1) + min;
  110.     }    
  111.    
  112.     public static void pressEnterKeyToContinue()
  113.     {
  114.         System.out.println("\nPulse una tecla para continuar...");
  115.         scanner.nextLine();
  116.     }
  117.    
  118.     public static String getString(String message) {
  119.         System.out.println(message);
  120.         return scanner.nextLine();
  121.     }
  122.    
  123.     public static boolean isValidadWord(String nombre) {
  124.         return nombre.matches("[A-Za-zÁÉÍÓÚáéíóúñÑ]+");
  125.     }
  126.    
  127.     public static String getValidWord(String inputMessage, String errorMessage) {
  128.         String palabra;
  129.         while(true) {
  130.             System.out.println(inputMessage);
  131.             palabra= scanner.nextLine();
  132.             if (isValidadWord(palabra)) {
  133.                 return palabra;
  134.             } else {
  135.                 System.err.println(errorMessage);
  136.             }
  137.         }
  138.     }
  139.    
  140.    public static String getValidWord(String inputMessage) {
  141.         return getValidWord(inputMessage, "\nERROR: Solo se admiten letras, sin espacios");
  142.     }
  143.    
  144.    public static String getStringWithLettersAndSpaces(String message) {
  145.        Pattern pattern = Pattern.compile(new String ("^[a-zA-Z\\s]*$"));
  146.         while(true){
  147.             System.out.println(message);
  148.             String str = scanner.nextLine();
  149.             Matcher matcher = pattern.matcher(str);
  150.             if(matcher.matches()) {
  151.                 return str;
  152.             }else {
  153.                 System.err.println("\nError: Solo se admiten letras y espacios");
  154.             }
  155.         }
  156.     }
  157.    
  158.    public static String getStringAlphaNum(String message) {
  159.         while(true){
  160.             System.out.println(message);
  161.             String str = scanner.nextLine();
  162.             if(str.matches("[A-Za-z0-9]+")) {
  163.                 return str;
  164.             }else {
  165.                 System.err.println("\nError: Solo se admiten letras, números");
  166.             }
  167.         }
  168.     }
  169.    
  170.    public static String getPhone(String message) {     
  171.         while(true) {
  172.             String phone = "" + Helper.getPositiveLong(message);
  173.             if(phone.length() <= 12 && phone.length() >= 9) return phone;
  174.             else {
  175.                 System.err.println("\nError: Número de celular inválido" +
  176.                         "\nTip: el número no debe exceder los 12 caracteres");
  177.             }
  178.         }      
  179.     }
  180.    
  181.    
  182.     public static String getEmail(String message) {
  183.         Pattern pattern = Pattern.compile("^(.+)@(.+)$");
  184.        
  185.         while(true) {
  186.             String email = Helper.getStringAlphaNum(message);
  187.             Matcher matcher = pattern.matcher(email);
  188.             if(matcher.matches()) return email;
  189.             else {
  190.                 System.err.println("\nError: Email inválido" +
  191.                         "\nTip: un email válido debe ser por ej.: usuario@gmail.com");
  192.             }
  193.         }
  194.     }
  195.    
  196.     public static Integer getYear(String message) {
  197.         while(true) {
  198.                 Integer year = getPositiveInt(message);
  199.             if(year >= 1900 && year <= LocalDate.now().getYear()) {
  200.                 return year;
  201.             }
  202.             else {
  203.                 System.err.println("\nError: Año inválido");
  204.             }
  205.         }
  206.     }
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement