/* * [CreditCard.java] * * Summary: Validates and classifies credit card numbers. * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1999-08-17 * 1.1 1999-08-17 * 1.2 1999-08-17 not enough and too few digits messages. * catch NumberFormatException when too big to fit in long.\ * 1.3 1999-08-19 ignore dashes in numbers * 1.4 2005-06-17 stomped BAT files, added about box, cleaner GridBag * 1.5 2005-12-16 add Javadoc * 1.6 2006-03-04 reformat with IntelliJ, add Javadoc * 1.7 2007-05-17 add pad and icon. * 1.8 2008-03-06 convert to jdk 1.5. add build number to title. simplify with enum. */ package com.mindprod.creditcard; import com.mindprod.common18.Build; import com.mindprod.common18.CMPAboutJBox; import com.mindprod.common18.Common18; import com.mindprod.common18.FontFactory; import com.mindprod.common18.HybridJ; import com.mindprod.common18.JEButton; import com.mindprod.common18.VersionCheck; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Validates and classifies credit card numbers. *

* The CreditCardValidate Amanuensis will help you tell Validates Credit Card numbers, and determines which credit card * company they belong to. It is a simple Applet to demonstrate the use of the CreditCard class. *

* 1. if a credit card number is valid, 2. which credit card vendor handles that number. *

* It validates the prefix and the checkdigit. It does *not* contact the credit card company to ensure that number has * actually been issued and that the account is in good standing. *

* It will also tell you which of the following credit card companies issued the card: Amex, Diners Club, Carte Blanche, * Discover, enRoute, JCB, MasterCard or Visa. *

* * @author Roedy Green, Canadian Mind Products * @version 1.8 2008-03-06 convert to jdk 1.5. add build number to title. simplify with enum. * @noinspection FieldCanBeLocal * @since 1999-08-17 */ public final class CreditCard extends JApplet { /** * true if want extra debugging output * * @noinspection UnusedDeclaration */ private static final boolean DEBUGGING = false; /** * height of Applet box in pixels. Does not include surrounding frame. */ private static final int APPLET_HEIGHT = 228; /** * Width of Applet box in pixels. */ private static final int APPLET_WIDTH = 568; private static final int FIRST_COPYRIGHT_YEAR = 1999; /** * undisplayed copyright notice * * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2008-03-06"; private static final String TITLE_STRING = "CMP Credit Card Validation"; private static final String VERSION_STRING = "1.8"; private static final Color BLACK = Color.BLACK; private static final Color DARK_GREEN = new Color( 0x008000 ); private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 ); /** * for titles */ private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c ); /** * @noinspection ConstantNamingConvention */ private static final Color RED = Color.RED; private static final Color WHITE = Color.WHITE; /** * for for titles and About buttons */ private static final Font FONT_FOR_TITLE = FontFactory.build( "Dialog", Font.BOLD, 16 ); /** * for for title second line */ private static final Font FONT_FOR_TITLE2 = FontFactory.build( "Dialog", Font.PLAIN, 14 ); /** * About box */ private JButton about; /** * button to trigger validation */ private JButton validateButton; /** * dummy to help alignment */ private JLabel dummy; /** * instructions on use */ private JLabel instructions; /** * where we display whether number is valid or invalid */ private JLabel isValid; /** * title */ private JLabel title; /** * title, second line */ private JLabel title2; /** * company who issued the credit card */ private JLabel vendorName; /** * credit card number to validate */ private JTextField creditCardText; /** * allocate the components */ private void buildComponents() { title = new JLabel( TITLE_STRING + " " + VERSION_STRING ); title.setFont( FONT_FOR_TITLE ); title.setForeground( FOREGROUND_FOR_TITLE ); title2 = new JLabel( "released:" + RELEASE_DATE + " build:" + Build.BUILD_NUMBER ); title2.setFont( FONT_FOR_TITLE2 ); title2.setForeground( FOREGROUND_FOR_TITLE ); about = new JEButton( "about" ); about.setFont( FONT_FOR_TITLE ); about.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // open aboutbox frame new CMPAboutJBox( TITLE_STRING, VERSION_STRING, "Validate credit card numbers:", "Amex, Diners Club, Discover, JCD, MasterCard and Visa", "freeware", RELEASE_DATE, FIRST_COPYRIGHT_YEAR, "Roedy Green", "CREDITCARD", "1.8" ); } } ); creditCardText = new JTextField( 16 ); creditCardText.setEditable( true ); creditCardText.setFont( FontFactory.build( "Dialog", Font.PLAIN, 15 ) ); creditCardText.setForeground( BLACK ); creditCardText.setBackground( WHITE ); isValid = new JLabel( "", JLabel.CENTER ); isValid.setFont( FontFactory.build( "Dialog", Font.BOLD, 20 ) ); isValid.setForeground( DARK_GREEN ); isValid.setBackground( WHITE ); // kludge to make title expand to right. dummy = new JLabel(); vendorName = new JLabel( "", JLabel.CENTER ); vendorName.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) ); vendorName.setForeground( FOREGROUND_FOR_LABEL ); vendorName.setBackground( WHITE ); validateButton = new JEButton( "Validate" ); // leave colours the default validateButton.setFont( FontFactory.build( "Dialog", Font.BOLD, 16 ) ); instructions = new JLabel( "", JLabel.CENTER ); instructions.setText( "Enter Credit Card number to be validated, then click Validate." ); instructions.setBackground( WHITE ); instructions.setForeground( DARK_GREEN ); } /** * layout the components * * @param contentPane Where to place the components */ private void layoutGridBag( Container contentPane ) { // Applet GridBag: // 0 ________1____________ 2 // --------title-------- about 0 // -------title2---------about 1 // vendor creditCard#--------- 2 // good? dummy Validate! ------3 dummy kludge to make GridBag behave. // -----instructions---------- 4 // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( title, new GridBagConstraints( 0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 10, 10, 0, 5 ), 0, 0 ) ); contentPane.add( title2, new GridBagConstraints( 0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 10, 10, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( about, new GridBagConstraints( 2, 0, 1, 2, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 10, 5, 10, 10 ), 10, 2 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( creditCardText, new GridBagConstraints( 1, 2, 2, 1, 10.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 10 ), 0, 20 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( vendorName, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 5 ), 0, 20 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( isValid, new GridBagConstraints( 0, 3, 1, 1, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( dummy, new GridBagConstraints( 1, 3, 1, 1, 5.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( validateButton, new GridBagConstraints( 2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 10 ), 20, 5 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( instructions, new GridBagConstraints( 0, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 10, 5 ), 0, 5 ) ); } /** * action when Validate button clicked * * @param event not used * * @noinspection UnusedParameters */ private void validateButton_ActionPerformed( ActionEvent event ) { final long creditCardNumber; try { // Get the creditCardNumber. creditCardNumber = ValidateCreditCard .parseDirtyLong( creditCardText.getText() ); } catch ( NumberFormatException e ) { isValid.setText( "BAD" ); isValid.setForeground( RED ); vendorName.setText( Vendor.TOO_MANY_DIGITS.getName() ); vendorName.invalidate(); vendorName.validate(); // don't echo back the bad tidied number! return; } // display whether it is bad or good final boolean isGood = ValidateCreditCard.isValid( creditCardNumber ); isValid.setText( isGood ? "VALID" : "BAD" ); isValid.setForeground( isGood ? DARK_GREEN : RED ); // find and display the corresponding Vendor final Vendor vendor = ValidateCreditCard.matchVendor( creditCardNumber ); vendorName.setText( vendor.getName() ); vendorName.invalidate(); // Tidy the creditCardNumber creditCardText.setText( ValidateCreditCard .toPrettyString( creditCardNumber ) ); // force boxes to resize to fit new text. this.validate(); this.setVisible( true ); } // end validateButton_ActionPerformed /** * Allow this Applet to run as as application as well. * * @param args command line arguments ignored. */ public static void main( String args[] ) { HybridJ.fireup( new CreditCard(), TITLE_STRING + " " + VERSION_STRING, APPLET_WIDTH, APPLET_HEIGHT ); } // end main /** * Called by the browser or Applet viewer to inform * this Applet that it is being reclaimed and that it should destroy * any resources that it has allocated. */ @Override public void destroy() { about = null; creditCardText = null; dummy = null; instructions = null; isValid = null; title = null; title2 = null; validateButton = null; vendorName = null; } /** * Called by the browser or Applet viewer to inform * this Applet that it has been loaded into the system. */ @Override public void init() { if ( !VersionCheck.isJavaVersionOK( 1, 8, 0, this ) ) { return; } Common18.setLaf(); Container contentPane = this.getContentPane(); contentPane.setBackground( WHITE ); contentPane.setLayout( new GridBagLayout() ); buildComponents(); // add components layoutGridBag( contentPane ); // REGISTER LISTENER TheListener theListener = new TheListener(); validateButton.addActionListener( theListener ); validate(); setVisible( true ); } // end init /** * class to handle button events */ private final class TheListener implements ActionListener { /** * user validate calc * * @param e button push event */ public void actionPerformed( ActionEvent e ) { final Object object = e.getSource(); if ( object == validateButton ) { validateButton_ActionPerformed( e ); } } } // end TheListener }