Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ValidateCpf{
- private String cpf;
- private int resultSm;
- private int resultOnDigit;
- private int resultTwoDigit;
- public ValidateCpf(String cpf){
- // Set attribute CPF
- this.cpf = cpf;
- // Action methods
- this.setResultSm();
- this.setResultOnDigit();
- this.setResultTwoDigit();
- }
- /***
- *
- * @return true or false;
- */
- public boolean isCpf(){
- int lenghtCpf = this.cpf.length();
- String digitChecker = this.cpf.substring(lenghtCpf -2, lenghtCpf);
- String digitCheck = resultOnDigit +""+resultTwoDigit;
- boolean situation = false;
- if(digitChecker.equals(digitCheck)){
- situation = true;
- }
- return situation;
- }
- /***
- * Set result calculate SM
- */
- private void setResultSm(){
- // Set attribute result SM
- this.resultSm = this.calculate(this.cpf, 9, 10);
- }
- /***
- * Set result calculate digit CPF
- */
- private void setResultOnDigit(){
- // Set Result On Digit checker CPF
- int calculateOneDigit = 11 - (this.resultSm % 11);
- this.resultOnDigit = calculateOneDigit == 11 || calculateOneDigit == 10 ? 0 : calculateOneDigit;
- }
- /**
- *
- */
- private void setResultTwoDigit(){
- int calculateTwo = this.calculate(this.cpf, 10, 11);
- int calculateTwoDigit = 11 - (calculateTwo % 11);
- this.resultTwoDigit = calculateTwoDigit == 11 || calculateTwoDigit == 10 ? 0 : calculateTwoDigit;
- }
- /***
- * Calculate - performs the multiplication of numbers
- * @param cpf
- * @param num_cpf
- * @param weight
- * @return
- */
- private int calculate(String cpf, int num_cpf, int weight){
- // Variable stores result calculate
- int resultCalculate = 0;
- for(int i=0; i < num_cpf; i++){
- // Convert char in String
- String stringCpf = Character.toString(cpf.charAt(i));
- resultCalculate += Integer.parseInt(stringCpf) * weight;
- weight--;
- }
- return resultCalculate;
- }
- /***
- * Use instruction class ValidateCpf
- *
- * // String CPF
- * String cpf = "03930735059";
- *
- * // Instance Class and get param construct
- * ValidateCpf checkCpf = new ValidateCpf(cpf);
- *
- * // Checker CPF @return true or false.
- * System.out.println(checkCpf.isCpf());
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement