SHARE
TWEET

CPF Generator

pedrolemoz May 30th, 2020 (edited) 712 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun main() {
  2.     var generator = GeneratorAlgorithm()
  3.    
  4.     for (i in 0..100){
  5.         println(generator.generateCPF())
  6.     }
  7. }
  8.  
  9. class GeneratorAlgorithm() {
  10.     fun generateCPF() : String {
  11.         var buildCPF = ArrayList<Int>()
  12.        
  13.         for (i in 0..8) {
  14.             buildCPF.add((0..9).random())
  15.         }
  16.        
  17.         for (i in 0..1) {
  18.             buildCPF.add(digit(buildCPF))
  19.         }
  20.  
  21.         return buildString(buildCPF)
  22.     }
  23.    
  24.     private fun digit(buildCPF : ArrayList<Int>) : Int {
  25.         var sum: Int = 0
  26.         var index: Int = buildCPF.size + 1
  27.        
  28.         for (i in buildCPF) {
  29.             sum += i * index
  30.             index--
  31.         }
  32.        
  33.         return verifySum(sum)
  34.     }
  35.    
  36.     private fun verifySum(sum: Int) : Int {
  37.         if ((sum % 11) < 2) return 0 else return 11 - (sum % 11)
  38.     }
  39.    
  40.     private fun buildString(buildCPF : ArrayList<Int>) : String {
  41.         var getNumbers = ArrayList<String>()
  42.        
  43.         for (i in buildCPF) {
  44.             getNumbers.add(i.toString())
  45.         }
  46.        
  47.         var CPF: String = ""
  48.        
  49.         getNumbers.add(3, ".")
  50.         getNumbers.add(7, ".")
  51.         getNumbers.add(11, "-")
  52.        
  53.         for (i in getNumbers) {
  54.             CPF += i.toString()
  55.         }
  56.        
  57.         return CPF
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top