Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package shoestore;
- public class Barcode{
- private byte[] vonalkod;
- private Barcode(byte[] tomb){
- this.vonalkod = tomb;
- }
- private static boolean barCode_e(byte[] tomb){
- if(tomb.length == 13){
- for(byte a:tomb){
- if(a < 0 || a > 9){
- return false;
- }
- }
- int szam = 0;
- int felsoKorlat = 0;
- for(int i = 0; i < 12; ++i){
- if((i+1) % 2 == 0){
- szam += tomb[i]*3;
- }
- else{
- szam += tomb[i];
- }
- }
- felsoKorlat = szam + (10 -(szam % 10));
- if(szam % 10 != 0){
- return felsoKorlat - szam == tomb[12];
- }
- return tomb[12] == 0;
- }
- return false;
- }
- public static Barcode create(byte[] tomb){
- if(barCode_e(tomb)){
- return new Barcode(tomb);
- }
- return null;
- }
- public static Barcode create(String s){
- if(s.length() == 13){
- char[] sv = s.toCharArray();;
- byte[] tomb = new byte[13];
- for(int i = 0; i < 13; ++i){
- if(!Character.isLetter(sv[i])){
- tomb[i] = (byte)(Character.getNumericValue(sv[i]));
- }
- else{
- return null;
- }
- }
- if(barCode_e(tomb)){
- return new Barcode(tomb);
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement