Advertisement
kinhoSilva

ValidateCpf.java

Apr 29th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. public class ValidateCpf{
  2.  
  3.     private String cpf;
  4.     private int resultSm;
  5.     private int resultOnDigit;
  6.     private int resultTwoDigit;
  7.  
  8.     public ValidateCpf(String cpf){
  9.         // Set attribute CPF
  10.         this.cpf = cpf;
  11.  
  12.         // Action methods
  13.         this.setResultSm();
  14.         this.setResultOnDigit();
  15.         this.setResultTwoDigit();
  16.     }
  17.  
  18.     /***
  19.      *
  20.      * @return true or false;
  21.      */
  22.     public boolean isCpf(){
  23.         int lenghtCpf = this.cpf.length();
  24.  
  25.         String digitChecker = this.cpf.substring(lenghtCpf -2, lenghtCpf);
  26.  
  27.         String digitCheck = resultOnDigit +""+resultTwoDigit;
  28.  
  29.         boolean situation = false;
  30.         if(digitChecker.equals(digitCheck)){
  31.             situation = true;
  32.         }
  33.  
  34.         return situation;
  35.     }
  36.     /***
  37.      *  Set result calculate SM
  38.      */
  39.     private void setResultSm(){
  40.         // Set attribute result SM
  41.         this.resultSm = this.calculate(this.cpf, 9, 10);
  42.     }
  43.  
  44.     /***
  45.      * Set result calculate digit CPF
  46.      */
  47.     private void setResultOnDigit(){
  48.         // Set Result On Digit checker CPF
  49.         int calculateOneDigit = 11 - (this.resultSm % 11);
  50.  
  51.         this.resultOnDigit = calculateOneDigit == 11 || calculateOneDigit == 10 ? 0 : calculateOneDigit;
  52.     }
  53.  
  54.     /**
  55.      *
  56.      */
  57.     private void setResultTwoDigit(){
  58.         int calculateTwo = this.calculate(this.cpf, 10, 11);
  59.  
  60.         int calculateTwoDigit = 11 - (calculateTwo % 11);
  61.  
  62.         this.resultTwoDigit = calculateTwoDigit == 11 || calculateTwoDigit == 10 ? 0 : calculateTwoDigit;
  63.     }
  64.  
  65.     /***
  66.      * Calculate - performs the multiplication of numbers
  67.      * @param cpf
  68.      * @param num_cpf
  69.      * @param weight
  70.      * @return
  71.      */
  72.     private int calculate(String cpf, int num_cpf, int weight){
  73.  
  74.         // Variable stores result calculate
  75.         int resultCalculate = 0;
  76.  
  77.         for(int i=0; i < num_cpf; i++){
  78.  
  79.             // Convert char in String
  80.             String stringCpf = Character.toString(cpf.charAt(i));
  81.  
  82.             resultCalculate += Integer.parseInt(stringCpf) * weight;
  83.  
  84.             weight--;
  85.         }
  86.  
  87.         return resultCalculate;
  88.     }
  89.  
  90.     /***
  91.      *  Use instruction class ValidateCpf
  92.      *  
  93.      *          // String CPF
  94.      *         String cpf = "03930735059";
  95.      *
  96.      *         // Instance Class and get param construct
  97.      *         ValidateCpf checkCpf = new ValidateCpf(cpf);
  98.      *
  99.      *         // Checker CPF @return true or false.
  100.      *         System.out.println(checkCpf.isCpf());
  101.      */
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement