Advertisement
Guest User

Create check digit for 11 string upc -> 12 string upc

a guest
Feb 28th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Benjamin Ehlers on 12/18/2018 for java.
  3.  * Ported by Larry Stanfield on 2/28/2020 for cfscript.
  4.  * This class evaulates the upc, and in the case of 11 digits it will generate a check digit for it.
  5.  * Also corrected the way it was calculated on the check digit at the end.
  6.  */
  7.  
  8. component displayname='CheckDigits' output=true hint='A collection of methods used to match items in eclipse.'{
  9.  
  10.     //Strings
  11.     this.code = true;
  12.  
  13.     public void function init(required string code) {
  14.  
  15.         this.code = code;
  16.  
  17.     }
  18.  
  19.     public boolean function evaluateValidity( string code ) {
  20.        
  21.         //boolean
  22.         var valid = false;
  23.  
  24.         //numerics
  25.         var i           = 1;
  26.         var j           = 2;
  27.         var sumOne      = 0;
  28.         var stepTwo     = 0;
  29.         var sumTwo      = 0;
  30.         var stepFour    = 0;
  31.         var checkDigit  = 0;
  32.  
  33.         //String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  34.         var currentChar         = '';
  35.         variables.code          = arguments.code?:this.code;
  36.  
  37.         local.code          = listToArray( duplicate( variables.code ), "" );
  38.  
  39.         for( i; i < 12; i++ ) {
  40.  
  41.            if( i == 1 || i % 2 ){
  42.  
  43.                sumOne          +=   javaCast( 'int', local.code[i] );
  44.  
  45.            }
  46.  
  47.        }
  48.  
  49.        stepTwo = sumOne * 3;
  50.        sumTwo = 0;
  51.  
  52.        for( j; j < 12; j++ ) {
  53.  
  54.            if( j % 2 == 0 ){
  55.  
  56.                sumTwo          += javaCast( 'int', local.code[j] );
  57.  
  58.            }
  59.  
  60.        }
  61.  
  62.            stepTwo += sumTwo;
  63.            stepFour = stepTwo % 10;
  64.            checkDigit = 0;
  65.  
  66.        if( stepFour != 0 ) {
  67.  
  68.            checkDigit = 10 - stepFour;
  69.  
  70.        }
  71.  
  72.        if( arrayLen( local.code ) == 12  && checkDigit == local.code[ 12 ] ) {
  73.  
  74.            valid = true;
  75.            
  76.        }
  77.  
  78.        return valid;
  79.  
  80.    }
  81.  
  82.    public string function generateDigit( string code ) {
  83.  
  84.        //boolean
  85.        var valid = false;
  86.  
  87.        //numerics
  88.        var i           = 1;
  89.        var j           = 2;
  90.        var sumOne      = 0;
  91.        var stepTwo     = 0;
  92.        var sumTwo      = 0;
  93.        var stepFour    = 0;
  94.        var checkDigit  = 0;
  95.  
  96.        //String
  97.        var currentChar         = '';
  98.        variables.code          = arguments.code?:this.code;
  99.  
  100.        local.code          = listToArray( variables.code, "" );
  101.  
  102.        for( i; i <= 11; i++ ) {
  103.  
  104.            if( i == 1 || i % 2 ){
  105.  
  106.                sumOne          +=   javaCast( 'int', local.code[i] );
  107.                
  108.            }
  109.            
  110.        }
  111.  
  112.        stepTwo = sumOne * 3;
  113.        sumTwo = 0;
  114.  
  115.        for( j = 2; j <= 11; j++ ) {
  116.  
  117.            if( j % 2  == 0 ){
  118.                sumTwo          += javaCast( 'int', local.code[j] );
  119.            }
  120.  
  121.        }
  122.  
  123.            stepTwo += sumTwo;
  124.            stepFour = stepTwo % 10;
  125.            checkDigit = 0;
  126.  
  127.        if( stepFour != 0 ) {
  128.  
  129.            checkDigit = 10 - stepFour;
  130.  
  131.        }
  132.  
  133.        return checkDigit;
  134.  
  135.      
  136.  
  137.    }
  138.  
  139.    
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement