sexta-feira, 17 de novembro de 2017

Função valida CPF e CNPJ em java

Tentei simplificar o máximo para saber o que faz cada parte

//chamada 
String Retorno =  ValidaDocumento(String documento);
if(!Retorno.equals('n'))
{
Log.i("Retorno", ""+Retorno);
}


//funcao
public String ValidaDocumento(String documento){
    String erro = "n";
    //verifica se esta preenchido    if(documento.length() == 0) {
        erro = "Preencha o campo";
        
    }else{
        //verifica se o tamanho esta correto        
if(documento.length() != 11 && documento.length() != 14 ){
            erro = "Número do documento inválido";
        }else{

            Integer n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14;
            n1 = Integer.parseInt(documento.substring(0,1));
            n2 = Integer.parseInt(documento.substring(1,2));
            n3 = Integer.parseInt(documento.substring(2,3));
            n4 = Integer.parseInt(documento.substring(3,4));
            n5 = Integer.parseInt(documento.substring(4,5));
            n6 = Integer.parseInt(documento.substring(5,6));
            n7 = Integer.parseInt(documento.substring(6,7));
            n8 = Integer.parseInt(documento.substring(7,8));
            n9 = Integer.parseInt(documento.substring(8,9));
            n10 = Integer.parseInt(documento.substring(9,10));
            n11 = Integer.parseInt(documento.substring(10,11));


            if(documento.length() == 14) {
                //trata cnpj                
                n12 = Integer.parseInt(documento.substring(11, 12));
                n13 = Integer.parseInt(documento.substring(12, 13));
                n14 = Integer.parseInt(documento.substring(13, 14));
                //valida numeros iguais                
         if((n1 == n2) && (n2 == n3) && (n3 == n4) && (n4 == n5) && (n5 == n6) &&
            (n6 == n7) && (n7 == n8) && (n8 == n9) && (n9 == n10) && (n10 == n11)
                        && (n11 == n12) && (n12 == n13) ){
                    erro = "Número de Cnpj inválido";
                }else {
                    int soma = (n1*5 + n2*4 + n3*3 + n4*2 + n5*9 + n6*8 + n7*7 + n8*6 + n9*5 + n10*4 + n11*3 + n12*2) ;
                    
                    soma = soma%11;
                    
                    soma = Math.abs((soma < 2 ? 0 : 11 - soma));
                    
                    if(n13 != soma){
                        erro = "Cnpj Inválido";
                    }else{
                        soma = (n1*6 + n2*5 + n3*4 + n4*3 + n5*2 + n6*9 + n7*8 + n8*7 + n9*6 + n10*5 + n11*4 + n12*3 + n13*2) ;
                        
                        soma = soma%11;
                        
                        soma = Math.abs((soma < 2 ? 0 : 11 - soma));
                        
                        if(n14 != soma){
                            erro = "Cnpj Inválido";
                        }
                    }
                }
            }else{
                //trata cpf                
//valida numeros iguais               
        if((n1 == n2) && (n2 == n3) && (n3 == n4) && (n4 == n5) && (n5 == n6) &&
                   (n6 == n7) && (n7 == n8) && (n8 == n9) && (n9 == n10) && (n10 == n11) ){
                    erro = "Número de cpf inválido";
                }else{


                    int soma = n1*10 + n2*9 + n3*8 + n4*7 + n5*6 + n6*5 + n7*4 + n8*3 + n9*2 ;
                    int resto = (soma *10) %11;
                    if(resto == 10)
                    {
                        resto = 0;
                    }

                    int soma2 = n1 *11 + n2 *10 + n3 *9 + n4 *8 + n5 *7 + n6 *6 + n7 *5 + n8 *4 + n9 *3 + n10 *2 ;
                    int resto2 = (soma2 *10) %11;
                    if(resto2 == 10)
                    {
                        resto2 = 0;
                    }
                    
                    if(resto == n10 && resto2 == n11)
                    {
                        erro = "n";
                    }else{
                        erro = "CPF inválido";
                    }
                }
            }
        }

    }
    
    return erro;
}