信用卡验证char解析的问题

时间:2014-02-11 01:05:05

标签: java char jframe

我正在开展一个小组项目,要求我们确定信用卡是否合法。我能找到一些我得到算法的链接。总结一下,如果最终总和可以被10整除,则信用卡号有效。如果它不能被10整除,则该数字无效或是假的!我有一个主要问题。

编辑:setSize()方法无法正常工作。它不接受我需要的尺寸

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();
    }
}       

1 个答案:

答案 0 :(得分:2)

您的问题与获取用户的输入有关,因此我将根据您提供的luhmA算法(卡片总数#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;
}

在这个代码块中,我们在循环之前将字符串转换为char数组(为什么每次循环都会创建一个char数组?这太过分了)。然后,我们将循环遍历每个索引,并将该值添加到名为cardSum的总和中。 cardSum将跟踪运行总计,因此它在循环之外的原因。然后,我们检查总和的模数。如果cardSum % 10 ==0(总和可以被10整除),那么我们将bool设置为true。否则,我们将bool设置为false。然后我们返回bool

但是,我们可以重构此代码以使其更清晰。既然我们返回一个布尔值,为什么不摆脱if/else块呢?我们可以使用它代替块来使事情更清洁:

return cardsum % 10 == 0

如果cardsum % 10等于0,则条件为真,方法将返回true。这一行可以摆脱9行代码! - 只需考虑一下:)

另一个注意事项:如果用户提供非数字输入,您可能想要捕获NumberFormatException。如果这不是您项目的要求,我不会过分担心。但是,进入是一个很好的做法! (感谢Michel Michael Meyer的推荐)