Posted By

DrPepper on 01/31/11


Tagged


Versions (?)

[JAVA] CH8 Zip to bar code / bar code to Zip


 / Published in: Java
 

When you execute bar code to zip, it doesn't work yet. maybe str value and bar# are different.

  1. /**
  2.   for faster sorting of letters, the united states postal service encourages companies that send large volumes of mail to use a bar code denoting the ZIP code.
  3.   The encoding scheme for a five-digit ZIP code is shoen in Figure 8. There are full-height frame bars on each side. The fice encoded digits are followed by a check digit, which is computed as follows : Add up all digits, and choose the check digit to make the sum a multip;e of 10. for example, the sum of the digits in the zip code 95014 is is 19, so the check digit is 1 to make the sum equal to 20. Each degit of the ZiP code,and the check digit , is encoded according to the following table:
  4.  
  5.   \ 7 4 2 1 0
  6.   1 0 0 0 1 1
  7.   2 0 0 1 0 1
  8.   3 0 0 1 1 0
  9.   4 0 1 0 0 1
  10.   5 0 1 0 1 0
  11.   6 0 1 1 0 0
  12.   7 1 0 0 0 1
  13.   8 1 0 0 1 0
  14.   9 1 0 1 0 0
  15.   0 1 1 0 0 0
  16.  
  17. When 0 denotes a harf bar and 1 a full bar. note that they represent all combinations of two full and three harf bars. The digit can be computed easily from the bar code using the column weights 7,4,2,1,0.for example,01100 is
  18. 0*7 + 1*4 + 1*2 + 0*1 + 0*0 = 6 digits
  19.  
  20. The only exception is 0, which would yield 11 according to the weight formula. Write a program that asks the user for a zip code and prints the bar code.
  21.  
  22. your program should also be abble to carry out the opposite conversion: translate bar into their zip code, reporting any errors in the input format or a mismatch of the digit.
  23. */
  24. import java.util.Scanner;
  25.  
  26. public class postal
  27. {
  28. /**
  29.   Here is a constructor
  30.   */
  31. public int num2; // 10000 digit
  32. public int num3; // 1000 digit
  33. public int num4; // 100 digit
  34. public int num5; // 10 digit
  35. public int num6; // 1 digit
  36. public int checkDig; // check digit
  37. public static int num;
  38. public static String temp;
  39. public static int menu;
  40. public static int zip;
  41. public static String bar0;
  42. public static String bar1;
  43. public static String bar2;
  44. public static String bar3;
  45. public static String bar4;
  46. public static String bar5;
  47. public static String bar6;
  48. public static String bar7;
  49. public static String bar8;
  50. public static String bar9;
  51. public static String str;
  52. public static int numb;
  53.  
  54. public postal()
  55. {
  56. zip = 0;
  57. /**
  58.   bar code
  59.   */
  60. bar0 = "::..."; bar1 = "...::"; bar2 = "..:.:";
  61. bar3 = "..::."; bar4 = ".:..:"; bar5 = ".:.:.";
  62. bar6 = ".::.."; bar7 = ":...:"; bar8 = ":..:.";
  63. bar9 = ":.:..";
  64.  
  65. }
  66.  
  67. public static int getZIP()
  68. {
  69. System.out.println("Enter a ZIP code : ");
  70. Scanner sc = new Scanner(System.in);
  71. zip = sc.nextInt();
  72. while((zip<01001)||(zip>99950))
  73. {
  74. System.out.println("**** ERROR ****");
  75. System.out.println("The ZIP code must be between 01001 and 99950");
  76. System.out.println("Please,enter the correct zip : ");
  77. zip = sc.nextInt();
  78. }
  79. return zip;
  80. }
  81. public static int menu()
  82. {
  83. System.out.println("**** Barcode maker / reader ****");
  84. System.out.println("Choose one option from following : ");
  85. System.out.println("1) Create ZIP barcode ");
  86. System.out.println("2) Find ZIP code from barcode ");
  87. System.out.println("3) Exit ");
  88. Scanner sc = new Scanner(System.in);
  89. return sc.nextInt();
  90. }
  91.  
  92. public void getDigit(int num)
  93. {
  94. num2 = num/10000;
  95. num3 = ((num/1000)-num2*10);
  96. num4 = (num/100 - (num2*100 + num3*10));
  97. num5 = (num/10 -(num2*1000 + num3*100 + num4*10));
  98. num6 = (num - (num2*10000 + num3*1000 + num4*100 + num5*10));
  99. // error here; need to use correct formula
  100. checkDig = 100- (num2+num3+num4+num5+num6);
  101. System.out.println(checkDig);
  102.  
  103. while(checkDig>10)
  104. { checkDig -= 10; }
  105. System.out.println("**** BARCODE ****");
  106. getBar(num2);
  107. getBar(num3);
  108. getBar(num4);
  109. getBar(num5);
  110. getBar(num6);
  111. getBar(checkDig);
  112.  
  113. }
  114. public void getBar(int x)
  115. {
  116.  
  117. switch(x)
  118. {
  119. case 0: System.out.print(bar0); break;
  120. case 1: System.out.print(bar1); break;
  121. case 2: System.out.print(bar2); break;
  122. case 3: System.out.print(bar3); break;
  123. case 4: System.out.print(bar4); break;
  124. case 5: System.out.print(bar5); break;
  125. case 6: System.out.print(bar6); break;
  126. case 7: System.out.print(bar7); break;
  127. case 8: System.out.print(bar8); break;
  128. case 9: System.out.print(bar9); break;
  129. }
  130. }
  131. public static String number()
  132. {
  133. System.out.println("Enter a barcode with using . or : ");
  134. Scanner scn = new Scanner(System.in);
  135. String bar = scn.nextLine();
  136. return bar;
  137. }
  138.  
  139. public static void getNumber(String temp)
  140. {
  141. System.out.println("*** In a getNUmber ***");
  142. System.out.println(temp);
  143. if(temp.equals(bar0))
  144. { numb = 0; }
  145. if(temp.equals(bar1))
  146. { numb = 1; }
  147. if(temp.equals(bar2))
  148. { numb = 2; }
  149. if(temp.equals(bar3))
  150. { numb = 3; }
  151. if(temp.equals(bar4))
  152. { numb = 4; }
  153. if(temp.equals(bar5))
  154. { numb = 5; }
  155. if(temp.equals(bar6))
  156. { numb = 6; }
  157. if(temp.equals(bar7))
  158. { numb = 7; }
  159. if(temp.equals(bar8))
  160. { numb = 8; }
  161. if(temp.equals(bar9))
  162. { numb = 9; }
  163. System.out.print(numb);
  164. }
  165. public static void devide(String temp)
  166. {
  167. postal po = new postal();
  168. str = temp.substring(0,4);
  169. System.out.println(str);
  170. po.getNumber(str);
  171. str = temp.substring(5,9);
  172. System.out.println(str);
  173. po.getNumber(str);
  174. str = temp.substring(10,14);
  175. System.out.println(str);
  176. po.getNumber(str);
  177. str = temp.substring(15,19);
  178. System.out.println(str);
  179. po.getNumber(str);
  180. str = temp.substring(20,24);
  181. System.out.println(str);
  182. po.getNumber(str);
  183.  
  184. }
  185.  
  186. public static void main(String[] args)
  187. {
  188. postal pos = new postal();
  189. while(menu != 3)
  190. {
  191. System.out.println();
  192. menu = pos.menu();
  193. switch (menu)
  194. {
  195. case 1:
  196. num = pos.getZIP();
  197. pos.getDigit(num);
  198. System.out.println();
  199. break;
  200. case 2:
  201. temp = pos.number();
  202. System.out.println(temp);
  203. pos.devide(temp);
  204. break;
  205. case 3:
  206. break;
  207. }
  208. }
  209. }
  210. }

Report this snippet  

You need to login to post a comment.