ChriskOlson
ChriskOlson

Reputation: 513

Credit Card Verification Problems with char parsing

I was working on a group project that asks us to determine if a credit card is legitimate or not. I was able to find some links where I got an algorithm. To sum that up, if the final sum is divisible by 10, the credit card number is valid. If it's not divisible by 10, the number is invalid or fake! I have one main problem.

EDIT: the setSize() method doesn't work properly. It won't accept the dimensions I need to feed it

public class Main extends JFrame implements ActionListener {

    private Dimension d = new Dimension(500, 300);
    private JPanel panel1, panel2;
    private JButton test, reset;
    private JLabel instructions;
    private JTextField text;


    public Main(){
        setTitle("****Credit Card Test****");
        setSize(d);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();
        panel1.setSize(this.getWidth() , this.getHeight());
        panel1.setLayout(new GridBagLayout()); 

        instructions = new JLabel("Enter a Creit Card number-------");
        test = new JButton("Test");


        reset = new JButton("Reset");
        text = new JTextField();

        addItem(panel1, instructions, 0, 0, 2, 1, GridBagConstraints.CENTER);
        addItem(panel1,test, 0, 2, 1, 1, GridBagConstraints.CENTER);
        addItem(panel1, reset, 2, 2, 1, 1, GridBagConstraints.CENTER);
        addItem(panel1, text, 0, 1, 3, 1, GridBagConstraints.CENTER);


        add(panel1);
        pack();
        setVisible(true);
    }

    //Good usage of static here because there is only 1 algorithm.
    public static boolean luhmAlgorithm(String num) {
        boolean bool;       
        for(int i = 0; i <= num.length(); i++){
            num.toCharArray();
            num [i] = Integer.parseInt(num[i]);
        }       
        return bool;
    }

    public void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = x;
        constraints.gridy = y;
        constraints.gridwidth = width;
        constraints.gridheight = height;

        //Sets amount of space for padding (top, left, bottom, right)
        constraints.insets = new Insets(5, 5, 5, 5);
        ////////////////////////////////////////////////////////////
        constraints.anchor = align;//(CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST)
        //Stretch components to fill space (NONE, HORIZONTAL, VERTICAL, BOTH)
        constraints.fill = GridBagConstraints.NONE;
    }

    public void actionPerformed(ActionEvent event) {
        switch (event.getActionCommand()){
            case "Reset":
                text.setText(null);
            break;
            //Remember use colons : instead of semi-colons ;    
            case "Test":
                if(luhmAlgorithm(text.getText())){
                    JOptionPane.showMessageDialog(null, "This credit card is valid.");
                } else {
                    JOptionPane.showMessageDialog(null, "INVALID CREDIT CARD!");
                    break;
                }
            default:
                break;
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}       

Upvotes: 0

Views: 98

Answers (1)

MrMike
MrMike

Reputation: 1540

Your question pertains to obtaining the input from the user, so I am going to answer this question based on the luhmAlgorithm you provided (sum of card #s / 10):

public static boolean luhmAlgorith(String num)
{
    boolean bool;
    char[] numAsCharArray = num.toCharArray();
    int cardSum = 0;
    for (int i = 0; i <= num.length(); i++)
    {
        cardSum += Integer.parseInt(String.valueOf(numAsCharArray[i]));
    }
    if (cardSum % 10 == 0)
    {
        bool = true;
    }
    else
    {
        bool = false;
    }
    return bool;
}

In this code block, we are converting the string to a char array before looping (why create a char array every time we loop? It's overkill). Then, we will loop through each index, and add that value to our sum called cardSum. cardSum will keep track of the running total, hence why it is outside of the loop. Then, we check the modulus of the sum. If cardSum % 10 ==0 (sum is divisible by 10), then we set bool to true. Otherwise, we set bool to false. Then we return bool.

However, we can refactor this code to make it cleaner. Since we're returning a boolean, why not get rid of the if/else block? We can use this instead of the block to make things cleaner:

return cardsum % 10 == 0

If cardsum % 10 equals 0, the condition will be true, and the method will return true. This one line can get rid of 9 lines of code! -- Just something to think about :)

And another note: You may want to catch a NumberFormatException if the user provides a non-numerical input. If this is not a requirement for your project, I would not be overly concerned. However, it is a good practice to get into! (thanks for the recommendation Michel Michael Meyer)

Upvotes: 2

Related Questions