andresnogales

Helper.java

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