Guest User

Untitled

a guest
Feb 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. //
  2. // Application Program 15 STUDENT NAME: Siddharth Upadhyay
  3. // PERIOD: Second
  4. // ** Class Design ~ Postal Bar Codes **
  5. //
  6. // For faster sorting of letters, the United States Postal Service
  7. // encourages companies that send large volumns of mail to use a bar
  8. // code denoting the ZIP code
  9. //
  10. //
  11. // eg. *************** ECRLOT ** CO57
  12. //
  13. // CODE C671RTS2
  14. // JOHN DOE CO57
  15. // 1009 FRANKLIN BLVD
  16. // SUNNYVALE CA 95014-5143
  17. //
  18. //
  19. // || | | | || || | | | | || | | || | | |
  20. // ||||||||||||||||||||||||||||||||||||||||||||||||||||
  21. //
  22. //
  23. //
  24. // The encoding sceme for a five-digit ZIP code is
  25. //
  26. //
  27. // ____________ Frame Bars ____________
  28. // / \
  29. // / \
  30. // V V
  31. // || | | | || || | | |||
  32. // |||||| ||||| ||||| ||||| ||||| ||||||
  33. // \___/ \___/ \___/ \___/ \___/ \___/
  34. // Digit Digit Digit Digit Digit Check
  35. // 1 2 3 4 5 Digit
  36. //
  37. //
  38. // The five encoded digits are followed by a correction digit, which
  39. // is computed as follows: Add up all digits and choose the correction
  40. // digit to make the sum a multiple of 10. For example, the ZIP code
  41. // 95014 has a sum of digits 19, so the correction digit is 1 to make
  42. // the sum equal to 20.
  43. //
  44. // Each digit of the ZIP code, and correction digit, is encoded
  45. // according to the following table where 0 denotes a half bar and 1
  46. // a full bar.
  47. //
  48. //
  49. // | 7 | 4 | 2 | 1 | 0
  50. // -------+-------+-------+-------+-------+-------
  51. // 1 | 0 | 0 | 0 | 1 | 1
  52. // -------+-------+-------+-------+-------+-------
  53. // 2 | 0 | 0 | 1 | 0 | 1
  54. // -------+-------+-------+-------+-------+-------
  55. // 3 | 0 | 0 | 1 | 1 | 0
  56. // -------+-------+-------+-------+-------+-------
  57. // 4 | 0 | 1 | 0 | 0 | 1
  58. // -------+-------+-------+-------+-------+-------
  59. // 5 | 0 | 1 | 0 | 1 | 0
  60. // -------+-------+-------+-------+-------+-------
  61. // 6 | 0 | 1 | 1 | 0 | 0
  62. // -------+-------+-------+-------+-------+-------
  63. // 7 | 1 | 0 | 0 | 0 | 1
  64. // -------+-------+-------+-------+-------+-------
  65. // 8 | 1 | 0 | 0 | 1 | 0
  66. // -------+-------+-------+-------+-------+-------
  67. // 9 | 1 | 0 | 1 | 0 | 0
  68. // -------+-------+-------+-------+-------+-------
  69. // 0 | 1 | 1 | 0 | 0 | 0
  70. //
  71. //
  72. // Note that they represent all combinations of two full and three
  73. // half bars. The digit can be easily computed from the bar code
  74. // using the column weights 7, 4, 2, 1, 0.
  75. //
  76. // For example,
  77. // 01100 is 0*7 + 1*4 + 1*2 + 0*1 + 0*0 = 6
  78. //
  79. // The only exception is 0, which yield 11 according to the weight
  80. // formula.
  81. //
  82. //
  83. //
  84. // Write a program that asks the user for an address, including
  85. // a five digit ZIP code. Calculate and store the ZIP bar code.
  86. // Use : for half bars, and | for full bars
  87. //
  88. // For example,
  89. // 95014 becomes
  90. // ||:|:::|:|:||::::::||:|::|:::|||
  91. //
  92. //
  93. // Add a Tracker class to your program so that you program can read
  94. // a bar code (with : denoting half bars and | denoting full bars)
  95. // and convert it to a ZIP code. Indicate an error if the bar code
  96. // is not correct.
  97. //
  98. //
  99. // ( 50) Required instance variables for Envelope
  100. // Code
  101. // Name
  102. // Address
  103. // City
  104. // State
  105. // ZIP
  106. // ZIP bar code
  107. //
  108. // ( 60) Constructor (code, name, addr, city, state, zip)
  109. //
  110. // ( 70) toString() displays the address in envelope form
  111. // ** see example above.
  112. //
  113. // ( 75) Application class that asks the user for an address,
  114. // instantiates an Envelope, and displays the address
  115. // on the evelope.
  116. //
  117. // ( 85) Program correctly prints the Zip bar code on the envelope.
  118. //
  119. // ( 90) Required instance variables for Tracker and toString
  120. // ZIP
  121. // ZIP bar code
  122. //
  123. // (100) Application class that asks the user for a ZIP bar code,
  124. // instantiates a Tracker, and displays the ZIP address.
  125. //
  126. // (105) Tracker displays an error message of the Zip bar code
  127. // is invalid
  128. //
  129. //
  130. //
  131.  
  132. package envelope;
  133.  
  134.  
  135. import static java.lang.System.*;
  136. import java.util.*;
  137.  
  138. public class App15
  139. {
  140. public static void main(String[] args)
  141. {
  142. Envelope toMe = new Envelope("C671RTS2", "JOHN DOE", "1009 FRANKLIN BLVD",
  143. "SUNNYVALE", "CA", 95014);
  144.  
  145. out.println(toMe);
  146.  
  147. Tracker toTestBarCodes = new Tracker("||:|:::|:|:||::::::||:|::|:::|||");
  148.  
  149. out.println(toTestBarCodes);
  150.  
  151. }
  152. }
  153.  
  154.  
  155. class Envelope
  156. {
  157. private String code;
  158. private String name;
  159. private String address;
  160. private String city;
  161. private String state;
  162. private int zip;
  163. private String zipBarCode;
  164.  
  165. Envelope(String code, String name, String address, String city, String state, int zip)
  166. {
  167. this.code = code.toUpperCase();
  168. this.name = name.toUpperCase();
  169. this.address = address.toUpperCase();
  170. this.city = city.toUpperCase();
  171. this.state = state.toUpperCase();
  172. this.zip = zip;
  173. zipBarCode = "";
  174.  
  175. barCode();
  176. }
  177.  
  178. private void barCode()
  179. {
  180. String rawZip = "" + zip;
  181.  
  182. zipBarCode += "|";
  183.  
  184. for(int x = 0; x < 4; x++)
  185. {
  186. int pieceOfZip = Integer.parseInt("" + rawZip.charAt(x));
  187.  
  188. switch(pieceOfZip)
  189. {
  190. case 1: zipBarCode += " ||"; break;
  191. case 2: zipBarCode += " | |"; break;
  192. case 3: zipBarCode += " || "; break;
  193. case 4: zipBarCode += " | |"; break;
  194. case 5: zipBarCode += " | | "; break;
  195. case 6: zipBarCode += " || "; break;
  196. case 7: zipBarCode += "| |"; break;
  197. case 8: zipBarCode += "| | "; break;
  198. case 9: zipBarCode += "| | "; break;
  199. case 0: zipBarCode += "|| "; break;
  200.  
  201. default: exit(0);
  202. }
  203.  
  204. }
  205.  
  206. zipBarCode += "|";
  207. }
  208.  
  209. public String toString()
  210. {
  211. String label;
  212.  
  213. label = "CODE " + code + "\n";
  214. label += name + "\n";
  215. label += address + "\n";
  216. label += city + " " + state + " " + zip + "\n";
  217. label += zipBarCode + "\n";
  218.  
  219. for(int x = 0; x < zipBarCode.length(); x++)
  220. label += "|";
  221.  
  222. label += "\n";
  223.  
  224. return label;
  225. }
  226. }
  227.  
  228.  
  229. class Tracker
  230. {
  231. private int trackerZip;
  232. private String trackerZipBarCode;
  233. private String errorFinale;
  234.  
  235. Tracker(String barCodeRaw)// A quaint little Constuctor
  236. {
  237. trackerZipBarCode = barCodeRaw;
  238. trackerZip = barCodeToZipCode(trackerZipBarCode);
  239. }
  240.  
  241. private int barCodeToZipCode(String tempRawBarCode)
  242. {
  243. String rawBarCode = tempRawBarCode;
  244. String tempZip = "";
  245. int total = 0;
  246. // v-- Due to Check Piece
  247. for(int x = 1; x <= 25; x += 5)
  248. {
  249. //Seperates Bar Code set
  250. char sev = rawBarCode.charAt(0 + x);
  251. char fou = rawBarCode.charAt(1 + x);
  252. char two = rawBarCode.charAt(2 + x);
  253. char one = rawBarCode.charAt(3 + x);
  254. char zer = rawBarCode.charAt(4 + x);
  255.  
  256. int totalPerDigit = 0; // (Re)Instantiate the current digit Total
  257.  
  258. if(sev == '|')
  259. totalPerDigit += 7;
  260. if(fou == '|')
  261. totalPerDigit += 4;
  262. if(two == '|')
  263. totalPerDigit += 2;
  264. if(one == '|')
  265. totalPerDigit += 1;
  266. if(zer == '|')
  267. totalPerDigit += 0;
  268.  
  269. if((sev == '|')&&(fou == '|'))
  270. totalPerDigit -= 11;
  271.  
  272. tempZip += "" + totalPerDigit;
  273. total += totalPerDigit;
  274. }
  275.  
  276. if(errorCheck(rawBarCode, total))
  277. return Integer.parseInt(tempZip);
  278. else
  279. return 0;
  280. }
  281.  
  282. private boolean errorCheck(String fullBarCode, int totalValue)
  283. {
  284. String barCodeToCheck = fullBarCode;
  285. int totalValueOfZip = totalValue;
  286. String checkpiece = barCodeToCheck.substring(26,31);
  287. int extra = 0;
  288.  
  289. //Seperates Bar Code set
  290. char sev = checkpiece.charAt(0);
  291. char fou = checkpiece.charAt(1);
  292. char two = checkpiece.charAt(2);
  293. char one = checkpiece.charAt(3);
  294. char zer = checkpiece.charAt(4);
  295.  
  296. if(sev == '|')
  297. extra += 7;
  298. if(fou == '|')
  299. extra += 4; // Logical Goodness
  300. if(two == '|')
  301. extra += 2;
  302. if(one == '|')
  303. extra += 1;
  304. if(zer == '|')
  305. extra += 0;
  306.  
  307. if((sev == '|')&&(fou == '|')&&(two == '|')&&(one == '|')) // Halbert Cases
  308. extra -= 14;
  309. else
  310. if((sev == '|')&&(fou == '|')&&(two == '|'))
  311. extra -= 13;
  312. else
  313. if((one == '|')&&(fou == '|')&&(two == '|'))
  314. extra -= 7;
  315. else
  316. if(((sev == '|')&&(fou == '|')))
  317. extra -= 11;
  318.  
  319. if(barCodeToCheck.length() != 32)
  320. {
  321. out.println("The Bar Code Too Long");
  322. exit(0);
  323. }
  324. else
  325. if( (totalValueOfZip + extra) % 10 != 0 )
  326. {
  327. out.println("The Bar Code is a tad wrong.");
  328. exit(0);
  329. }
  330.  
  331. return true;
  332. }
  333.  
  334. public String toString()
  335. {
  336. String tracker = "";
  337.  
  338. tracker += trackerZipBarCode + " becomes:\n";
  339. tracker += "\t" + trackerZip;
  340.  
  341. return tracker;
  342. }
  343. }
Add Comment
Please, Sign In to add comment