Check Credit Card Validity using Luhn Algorithm

前端 未结 12 1556
刺人心
刺人心 2020-12-03 04:02

I tried to check the validation of credit card using Luhn algorithm, which works as the following steps:

  1. Double every second digit from right to left. If do

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 04:32

    Here's my implementation of the Luhn Formula.

    /**
     * Runs the Luhn Equation on a user inputed CCN, which in turn
     * determines if it is a valid card number.
     * @param c A user inputed CCN.
     * @param cn The check number for the card.
     * @return If the card is valid based on the Luhn Equation.
     */
    public boolean luhn (String c, char cn)
    {
        String card = c;
        String checkString = "" + cn;
        int check = Integer.valueOf(checkString);
    
        //Drop the last digit.
        card = card.substring(0, ( card.length() - 1 ) );
    
        //Reverse the digits.
        String cardrev = new StringBuilder(card).reverse().toString();
    
        //Store it in an int array.
        char[] cardArray = cardrev.toCharArray();
        int[] cardWorking = new int[cardArray.length];
        int addedNumbers = 0;
    
        for (int i = 0; i < cardArray.length; i++)
        {
            cardWorking[i] = Character.getNumericValue( cardArray[i] );
        }
    
        //Double odd positioned digits (which are really even in our case, since index starts at 0).
    
        for (int j = 0; j < cardWorking.length; j++)
        {
            if ( (j % 2) == 0)
            {
                cardWorking[j] = cardWorking[j] * 2;
            }
        }
    
        //Subtract 9 from digits larger than 9.
    
        for (int k = 0; k < cardWorking.length; k++)
        {
            if (cardWorking[k] > 9)
            {
                cardWorking[k] = cardWorking[k] - 9;
            }
        }
    
        //Add all the numbers together.
        for (int l = 0; l < cardWorking.length; l++)
        {
            addedNumbers += cardWorking[l];
        }
    
        //Finally, check if the number we got from adding all the other numbers
        //when divided by ten has a remainder equal to the check number.
        if (addedNumbers % 10 == check)
        {
            return true;
        }
        else
        {           
            return false;
        }
    }
    

    I pass in the card as c which I get from a Scanner and store in card, and for cn I pass in checkNumber = card.charAt( (card.length() - 1) );.

提交回复
热议问题