algorytm.org

Implementacja w Java



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Standard Code 25 - Implementacja w Java
Ocena użytkownikóww: *****  / 2
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 30 marca 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

BarCode.java:
/**
 * @author Tomasz Lubinski
 * (c)2006 algorytm.org
 *
 *  Standard 2 of 5 Bar Code
 */
public class BarCode {
	
	  private static final char codeS25sign[] = {
		        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

	  private static final char codeS25bars[][] = {
		     {1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
		     {1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0},
		     {1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0},
		     {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0},
		     {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0},
		     {1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0},
		     {1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0},
		     {1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0},
		     {1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
		     {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0}
	  };

	  private static final char start[] = { 
	        1, 1, 0, 1, 1, 0, 1, 0
      };
	  
	  private static final char stop[] = { 
		  1, 1, 0, 1, 0, 1, 1, 0
	  };	  

	  public boolean check(String txt) {
	      for (int i=0; i<txt.length(); i++) {
              if (checkLetter(txt.charAt(i)) == false)
              		return false;
          }
		  return true;
	  }

	  private boolean checkLetter(char ch) {
	        for (int i=0; i<codeS25sign.length; i++) {
                if (codeS25sign[i] == ch)
                	return true;
	        }
	        return false;
	  }
	  
	  public char[] getBars(char ch) {
	        for (int i=0; i<codeS25sign.length; i++) {
                if (codeS25sign[i] == ch)
                	return codeS25bars[i];
	        }		  
		  return null;
	  }
	  
	  //returns letter value for check digit
	  private int getLetterValue(char ch) {
	          for (int i=0; i<codeS25sign.length; i++)
	                  if (codeS25sign[i] == ch)
	                          return i;

	          return 0;
	  }
	  
	  // return check digit
	  private int checkDigit(String txt) {
	        int sum = 0;
	        int w1 = 0;
	        int w2 = 0;        

	        for (int i=0; i<txt.length(); i++)
	        {
	                if (i % 2 == 0) 
	                {
	                    w1 += getLetterValue(txt.charAt(i));
	                }
	                else
	                {
	                    w2 += getLetterValue(txt.charAt(i));                
	                }
	        }
	        
	        if (txt.length() % 2 == 0)
	        {
	                w2 *= 3;
	        }
	        else
	        {
	                w1 *= 3;        
	        }
	        
	        sum = w1+ w2;
	        sum %= 10;
	        sum = 10 - sum;
	        sum %= 10;

	        return sum;
	  }	  
	  
	  public char[] getCheckDigitBars(String txt) {
           	return codeS25bars[checkDigit(txt)];
	  }
	  
	  public char[] getStartBars() {
		  return start;
	  }
	  
	  public char[] getStopBars() {
		  return stop;
	  }	  
}
Dodaj komentarz