UPCField.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. //***************************************************************************
  5. // This is the text field were the user enters the UPC-A code.
  6. //
  7. // Author: Ali Badereddin
  8. // Date: September 09, 2005
  9. //
  10. //***************************************************************************
  11. public class UPCField extends MNDigitField
  12. {
  13. //---------------------------------------------------------------------
  14. // Create a field were user enters UPC code.
  15. //---------------------------------------------------------------------
  16. public UPCField (JButton MButton, JButton NButton)
  17. {
  18. // Create a 12 digit field having width of 8 cols
  19. // M = 11, N = 12, MComponent = MButton, NComponent = NButton
  20. // cols = 8.
  21. super (11, 12, MButton, NButton, 8);
  22. }
  23. //---------------------------------------------------------------------
  24. // Generates check digit and appends it to the upc field.
  25. //---------------------------------------------------------------------
  26. public void generateCheckDigit ()
  27. {
  28. // Generate check digit only if there are 11 characters
  29. if (this.howManyDigits () == 11)
  30. {
  31. String upc = this.getText ();
  32. int checksum = 0;
  33. checksum = generateCheckDigit (upc);
  34. // Append the check digit to the end of the UPC
  35. this.setText (upc + checksum);
  36. }
  37. }
  38. //---------------------------------------------------------------------
  39. // Generates check digit.
  40. //---------------------------------------------------------------------
  41. public int generateCheckDigit (String upc)
  42. {
  43. int checksum = 0;
  44. for (int i = 1; i <= upc.length (); i++)
  45. {
  46. // even
  47. if (i % 2 == 0)
  48. checksum += charToInteger (upc.charAt (i - 1)) * 1;
  49. // odd
  50. else
  51. checksum += charToInteger (upc.charAt (i - 1)) * 3;
  52. }
  53. return ( 10 - ( checksum % 10 ) ) % 10;
  54. }
  55. //---------------------------------------------------------------------
  56. // Makes sure that the check digit is valid.
  57. //---------------------------------------------------------------------
  58. public boolean isCheckDigitValid ()
  59. {
  60. if (howManyDigits () == 12)
  61. {
  62. String upc = this.getText ();
  63. // The check digit entered by the user
  64. // character 12 (start at 0)
  65. int checkDigitEntered = charToInteger (upc.charAt (11));
  66. // the generate (valid) check digit
  67. int validCheckDigit = this.generateCheckDigit (upc.substring (0, 11));
  68. return checkDigitEntered == validCheckDigit;
  69. }
  70. return false;
  71. }
  72. //---------------------------------------------------------------------
  73. // Helper function to convert a character to an integer.
  74. //---------------------------------------------------------------------
  75. public int charToInteger (char c)
  76. {
  77. return c - 48;
  78. }
  79. //---------------------------------------------------------------------
  80. // Set the UPC.
  81. //---------------------------------------------------------------------
  82. public void setUPC (String upc)
  83. {
  84. setText (upc);
  85. }
  86. //---------------------------------------------------------------------
  87. // Get the UPC.
  88. //---------------------------------------------------------------------
  89. public String getUPC ()
  90. {
  91. return getText ();
  92. }
  93. //---------------------------------------------------------------------
  94. // Checks out my work...
  95. //---------------------------------------------------------------------
  96. public static void main (String[] args)
  97. {
  98. JFrame frame = new JFrame ("UPC-A Field");
  99. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  100. // create buttons
  101. JButton button1 = new JButton ("Generate Check Digit");
  102. JButton button2 = new JButton ("Generate Bar Code");
  103. // Set buttons to be initially disabled
  104. button1.setEnabled (false);
  105. button2.setEnabled (false);
  106. // Set background to white and layout to flow layout
  107. frame.setBackground (Color.white);
  108. frame.setLayout (new FlowLayout ());
  109. // add buttons and text field
  110. frame.getContentPane().add (new UPCField (button1, button2));
  111. frame.getContentPane().add (button1);
  112. frame.getContentPane().add (button2);
  113. // Pack and show the window (frame)
  114. frame.pack();
  115. frame.setVisible (true);
  116. }
  117. }