Created
December 4, 2015 06:11
-
-
Save yogonza524/5942ed5e46520623b496 to your computer and use it in GitHub Desktop.
Método para generar CUIT a partir de un enum de genero y el dni
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Genero { | |
MASCULINO,FEMENINO,SOCIEDAD | |
} | |
public static String generate(Genero g,int dni) throws Exception{ | |
int tipo; | |
if (g.equals(Genero.MASCULINO)) { | |
tipo = 20; | |
} | |
else{ | |
if (g.equals(Genero.FEMENINO)) { | |
tipo = 27; | |
} | |
else{ | |
tipo = 30; | |
} | |
} | |
String aux = String.valueOf(tipo) + String.valueOf(dni); | |
String[] cuitArray = aux.split(""); | |
int codigo = 0; | |
if (aux.length() != 10) { | |
throw new Exception("Sexo o DNI no valido. La longitud no corresponde"); | |
} | |
Integer[] serie = {5, 4, 3, 2, 7, 6, 5, 4, 3, 2}; | |
for (int i=0; i<10; i++){ | |
codigo += Integer.valueOf(cuitArray[i]) * serie[i]; | |
} | |
codigo = 11 - (codigo % 11); | |
//Si el resultado anterior es 11 el código es 0 | |
if (codigo == 11){ | |
codigo = 0; | |
//o si el resultado anterior es 10 el código es 9 | |
} else if (codigo == 10){ | |
codigo = 9; | |
} | |
return tipo + "-" + dni + "-" + codigo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment