Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Helper {
- public static Scanner scanner = new Scanner(System.in);
- public static Integer getPositiveInt(String message) {
- while(true){
- try {
- System.out.println(message);
- int num = Integer.parseInt(scanner.nextLine());
- if(num > 0) return num;
- System.err.println("\nError: no ingresó un número entero positivo");
- } catch (NumberFormatException e) {
- System.err.println("\nError: ingresó un caracter inválido");
- }
- }
- }
- public static Long getPositiveLong(String message) {
- while(true){
- try {
- System.out.println(message);
- Long num = Long.parseLong(scanner.nextLine());
- if(num > 0) return num;
- System.err.println("\nError: no ingresó un número entero positivo");
- } catch (NumberFormatException e) {
- System.err.println("\nError: ingresó un caracter inválido");
- }
- }
- }
- public static Random random = new Random();
- private static Integer getInt(String inputMessage, String errorMessage) {
- while(true){
- try {
- System.out.println(inputMessage);
- return Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException e) {
- System.err.println(errorMessage);
- }
- }
- }
- public static Integer getInt(String inputMessage) {
- return getInt(inputMessage, "\nError: el valor ingresado no es un entero");
- }
- public static char yesOrNo(String question){
- char resp;
- do {
- System.out.println("\n" + question + "\nPresione 'S' (SI) o 'N' (NO)");
- resp = Character.toUpperCase(scanner.nextLine().charAt(0));
- if(resp == 'N' || resp == 'S')return resp;
- System.err.println("\nError: lo ingresado no es una opción correcta");
- } while (true);
- }
- private static Double getDouble(String inputMessage, String errorMessage) {
- while(true){
- try {
- System.out.println(inputMessage);
- return Double.parseDouble(scanner.nextLine());
- } catch (NumberFormatException e) {
- System.err.println(errorMessage);
- }
- }
- }
- public static Double getDouble(String inputMessage){
- return getDouble(inputMessage, "\nError: el valor ingresado no es un número");
- }
- private static char getCharOp(String inputMessage, String errorMessage){
- while(true) {
- try {
- System.out.println(inputMessage);
- char caracter = scanner.nextLine().charAt(0);
- int valorASCII = (int)caracter;
- if (valorASCII == 42 || valorASCII == 43 || valorASCII == 45 || valorASCII == 47) {
- return caracter;
- }
- else {
- throw new Exception(errorMessage);
- }
- }
- catch (Exception e) {
- System.err.println("\n" + e.getMessage());
- }
- }
- }
- public static char getCharOp(String inputMessage){
- return getCharOp(inputMessage, "\nError: ingresó un caracter de operación inválido");
- }
- public static double randomDouble(int max, int min) {
- double number = random.nextDouble() * (max - min) + min;
- return (double)Math.round(number * 100d)/100;
- }
- public static int randomInt(int min,int max) {
- return random.nextInt(max - min + 1) + min;
- }
- public static void pressEnterKeyToContinue()
- {
- System.out.println("\nPulse una tecla para continuar...");
- scanner.nextLine();
- }
- public static String getStringAlphabetic(String message) {
- while(true){
- System.out.println(message);
- String str = scanner.nextLine();
- if(str.matches("[a-zA-Z]+")) {
- return str;
- }else {
- System.err.println("\nError: Solo se admiten letras");
- }
- }
- }
- public static String getStringAlphaNum(String message) {
- while(true){
- System.out.println(message);
- String str = scanner.nextLine();
- if(str.matches("[A-Za-z0-9_@.]+")) {
- return str;
- }else {
- System.err.println("\nError: Solo se admiten letras, números y símbolos como @ y .");
- }
- }
- }
- public static LocalDate getLocalDate(String message) {
- DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
- while(true) {
- System.out.println(message);
- System.out.println("Ingrese la fecha con el formato: dd/MM/yyyy");
- String dateStr = scanner.nextLine();
- try {
- LocalDate date = LocalDate.parse(dateStr, dateFormat);
- return date;
- } catch (DateTimeParseException e) {
- System.err.println("\nError: Fecha inválida");
- }
- }
- }
- public static Integer getYear(String message) {
- while(true) {
- Integer year = getPositiveInt(message);
- if(year >= 1900 && year <= LocalDate.now().getYear()) {
- return year;
- }
- else {
- System.err.println("\nError: Año inválido");
- }
- }
- }
- public static String getCellphone(String message) {
- Pattern pattern = Pattern.compile("^\\d{10}$");
- while(true) {
- String cellphone = "" + Helper.getPositiveLong(message);
- Matcher matcher = pattern.matcher(cellphone);
- if(matcher.matches()) return "+549" + cellphone;
- else {
- System.err.println("\nError: Número de celular inválido" +
- "\nTip: si el número es +54 9 388-4123456, ingrese 3884123456");
- }
- }
- }
- public static String getEmail(String message) {
- Pattern pattern = Pattern.compile("^(.+)@(.+)$");
- while(true) {
- String email = Helper.getStringAlphaNum(message);
- Matcher matcher = pattern.matcher(email);
- if(matcher.matches()) return email;
- else {
- System.err.println("\nError: Email inválido" +
- "\nTip: un email válido debe ser por ej.: usuario@gmail.com");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment