From f97924fad22f94244acc594323916a34ffd67478 Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Fri, 15 Mar 2024 20:36:12 -0300 Subject: [PATCH] Testing java version --- .../java/io/github/gabriel_logan/Main.java | 74 ++++++++++++++++--- .../gabriel_logan/types/ValidationResult.java | 11 +++ 2 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 packages/java/src/main/java/io/github/gabriel_logan/types/ValidationResult.java diff --git a/packages/java/src/main/java/io/github/gabriel_logan/Main.java b/packages/java/src/main/java/io/github/gabriel_logan/Main.java index af866aa..0a610ce 100644 --- a/packages/java/src/main/java/io/github/gabriel_logan/Main.java +++ b/packages/java/src/main/java/io/github/gabriel_logan/Main.java @@ -1,17 +1,71 @@ package io.github.gabriel_logan; +import io.github.gabriel_logan.types.ValidationResult; + //TIP To Run code, press or // click the icon in the gutter. public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + private String getErrorMessage(int code) { + // Implemente esta função para retornar a mensagem de erro apropriada + return ""; + } + + /** + * Checks if a string is empty or contains only whitespace. + * + * @param value the string to check + * @return true if the string is empty or contains only whitespace, false otherwise + * @throws IllegalArgumentException if the input value is not a string + */ + public static boolean isEmpty(String value) throws IllegalArgumentException { + if (value == null) { + throw new IllegalArgumentException("Input value must be a string."); + } + + return value.trim().length() == 0; + } + + public ValidationResult cpfIsValid(String cpf) { + try { + int numeroBase = 10; + int numeroBase2 = 11; + int somaTotal = 0; + int somaTotal2 = 0; + + if (cpf.length() != 11) { + return new ValidationResult(false, getErrorMessage(2)); + } + + int primeiroVerificador = 0; + int segundoVerificador = 0; + + for (int repetidor = 0; repetidor < 11; repetidor++) { + int multiplicador = Character.getNumericValue(cpf.charAt(repetidor)) * numeroBase; + numeroBase--; + somaTotal += multiplicador; + + int multiplicador2 = Character.getNumericValue(cpf.charAt(repetidor)) * numeroBase2; + numeroBase2--; + somaTotal2 += multiplicador2; + + int valorDeVerificacao = somaTotal - Character.getNumericValue(cpf.charAt(9)); + int valorDeVerificacao2 = somaTotal2 - Character.getNumericValue(cpf.charAt(10)); + + primeiroVerificador = 11 - (valorDeVerificacao % 11); + segundoVerificador = 11 - (valorDeVerificacao2 % 11); + } + + if (primeiroVerificador > 9) primeiroVerificador = 0; + if (segundoVerificador > 9) segundoVerificador = 0; + + if (primeiroVerificador == Character.getNumericValue(cpf.charAt(9)) + && segundoVerificador == Character.getNumericValue(cpf.charAt(10))) { + return new ValidationResult(true, null); + } + + return new ValidationResult(false, getErrorMessage(2)); + } catch (Exception e) { + return new ValidationResult(false, getErrorMessage(3)); } + } } \ No newline at end of file diff --git a/packages/java/src/main/java/io/github/gabriel_logan/types/ValidationResult.java b/packages/java/src/main/java/io/github/gabriel_logan/types/ValidationResult.java new file mode 100644 index 0000000..ad6213f --- /dev/null +++ b/packages/java/src/main/java/io/github/gabriel_logan/types/ValidationResult.java @@ -0,0 +1,11 @@ +package io.github.gabriel_logan; + +public class ValidationResult { + public boolean isValid; + public String errorMsg; + + public ValidationResult(boolean isValid, String errorMsg) { + this.isValid = isValid; + this.errorMsg = errorMsg; + } +} \ No newline at end of file