Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Benjamin Ehlers on 12/18/2018 for java.
- * Ported by Larry Stanfield on 2/28/2020 for cfscript.
- * This class evaulates the upc, and in the case of 11 digits it will generate a check digit for it.
- * Also corrected the way it was calculated on the check digit at the end.
- */
- component displayname='CheckDigits' output=true hint='A collection of methods used to match items in eclipse.'{
- //Strings
- this.code = true;
- public void function init(required string code) {
- this.code = code;
- }
- public boolean function evaluateValidity( string code ) {
- //boolean
- var valid = false;
- //numerics
- var i = 1;
- var j = 2;
- var sumOne = 0;
- var stepTwo = 0;
- var sumTwo = 0;
- var stepFour = 0;
- var checkDigit = 0;
- //String
- var currentChar = '';
- variables.code = arguments.code?:this.code;
- local.code = listToArray( duplicate( variables.code ), "" );
- for( i; i < 12; i++ ) {
- if( i == 1 || i % 2 ){
- sumOne += javaCast( 'int', local.code[i] );
- }
- }
- stepTwo = sumOne * 3;
- sumTwo = 0;
- for( j; j < 12; j++ ) {
- if( j % 2 == 0 ){
- sumTwo += javaCast( 'int', local.code[j] );
- }
- }
- stepTwo += sumTwo;
- stepFour = stepTwo % 10;
- checkDigit = 0;
- if( stepFour != 0 ) {
- checkDigit = 10 - stepFour;
- }
- if( arrayLen( local.code ) == 12 && checkDigit == local.code[ 12 ] ) {
- valid = true;
- }
- return valid;
- }
- public string function generateDigit( string code ) {
- //boolean
- var valid = false;
- //numerics
- var i = 1;
- var j = 2;
- var sumOne = 0;
- var stepTwo = 0;
- var sumTwo = 0;
- var stepFour = 0;
- var checkDigit = 0;
- //String
- var currentChar = '';
- variables.code = arguments.code?:this.code;
- local.code = listToArray( variables.code, "" );
- for( i; i <= 11; i++ ) {
- if( i == 1 || i % 2 ){
- sumOne += javaCast( 'int', local.code[i] );
- }
- }
- stepTwo = sumOne * 3;
- sumTwo = 0;
- for( j = 2; j <= 11; j++ ) {
- if( j % 2 == 0 ){
- sumTwo += javaCast( 'int', local.code[j] );
- }
- }
- stepTwo += sumTwo;
- stepFour = stepTwo % 10;
- checkDigit = 0;
- if( stepFour != 0 ) {
- checkDigit = 10 - stepFour;
- }
- return checkDigit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement