Advertisement
weeez

Barcode

Nov 7th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package shoestore;
  2.  
  3. public class Barcode{
  4.     private byte[] vonalkod;
  5.    
  6.     private Barcode(byte[] tomb){
  7.         this.vonalkod = tomb;
  8.     }
  9.    
  10.     private static boolean barCode_e(byte[] tomb){
  11.         if(tomb.length == 13){ 
  12.             for(byte a:tomb){          
  13.                 if(a < 0 || a > 9){            
  14.                     return false;
  15.                 }
  16.             }
  17.             int szam =  0;
  18.             int felsoKorlat = 0;
  19.             for(int i = 0; i < 12; ++i){
  20.                 if((i+1) % 2 == 0){
  21.                     szam += tomb[i]*3;
  22.                 }
  23.                 else{
  24.                     szam += tomb[i];
  25.                 }
  26.             }          
  27.             felsoKorlat = szam + (10 -(szam % 10));
  28.             if(szam % 10 != 0){
  29.                 return felsoKorlat - szam == tomb[12];
  30.             }
  31.             return tomb[12] == 0;
  32.            
  33.         }
  34.         return false;
  35.     }
  36.     public static Barcode create(byte[] tomb){
  37.         if(barCode_e(tomb)){
  38.             return new Barcode(tomb);
  39.         }
  40.         return null;
  41.     }
  42.     public static Barcode create(String s){
  43.         if(s.length() == 13){
  44.             char[] sv = s.toCharArray();;
  45.             byte[] tomb = new byte[13];
  46.             for(int i = 0; i < 13; ++i){
  47.                 if(!Character.isLetter(sv[i])){
  48.                     tomb[i] = (byte)(Character.getNumericValue(sv[i]));        
  49.                 }
  50.                 else{
  51.                     return null;
  52.                 }
  53.             }
  54.             if(barCode_e(tomb)){
  55.                 return new Barcode(tomb);
  56.             }
  57.         }
  58.         return null;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement