Validate credit card number using luhn algorithm

42.5k views Asked by At

I have a question regarding the following programming assignment.

Credit card numbers follow certain patterns. A credit card must have between 13 and 16 digits. It must start with:

• 4 for Visa cards

• 5 for Master cards

• 37 for American Express cards

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly know as the Luhn check or the Modulus 10 check, which can be described as follows. For illustration, consider the card number 4388576018402625.

  1. Double every second digit from right to left. If doubling of a digit results in a 2-digit number, add up the two digits to get a single-digit number.

2 x 2 = 4

2 x 2 = 4

4 x 2 = 8

1 x 2 = 2

6 x 2 = 12 (1+2= 3)

5 x 2 = 10 (1+0= 1)

8 x 2 = 16 (1+6= 7)

4 x 2 = 8

  1. Add all the single digit numbers from step 1 4 + 4 +8 + 2 +3 + 1 + 7 + 8 = 37

  2. Add all digits in the odd places from right to left in the card number

5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37

  1. Sum the results from step 2 and step 3 37 + 37 = 74

  2. If the result from step is divisible by 10, the card number is valid; otherwise, it’s invalid. For example, the number 4388576018402625 is invalid, but the number 4388576018410707 is a valid Visa card; the number 6011000593748745 is invalid, but the number 6011000593748746 is a valid Discover card.

I tried to solve it as shown in the following code:

import java.util.Scanner;

public class CreditCardValidation {

    public static boolean isValid(long number) {

        int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);


        if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 ) && (getSize(number)<=16 )) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDigit(int number) {

        if (number <= 9) {
            return number;
        } else {
            int firstDigit = number % 10;
            int secondDigit = (int) (number / 10);

            return firstDigit + secondDigit;
        }
    }
    public static int sumOfOddPlace(long number) {
        int result = 0;

        while (number > 0) {
            result += (int) (number % 10);
            number = number / 100;
        }

        return result;
    }

    public static int sumOfDoubleEvenPlace(long number) {

        int result = 0;
        long temp = 0;

        while (number > 0) {
            temp = number % 100;
            result += getDigit((int) (temp / 10) * 2);
            number = number / 100;
        }

        return result;
    }

    public static boolean prefixMatched(long number, int d) {

        if ((getPrefix(number, d) == 4)
                || (getPrefix(number, d) == 5)
                || (getPrefix(number, d) == 3)) {

            if (getPrefix(number, d) == 3) {
                System.out.println("\nVisa Card ");
            } else if (getPrefix(number, d) == 5) {
                System.out.println("\nMaster Card ");
            } else if (getPrefix(number, d) == 3) {
                System.out.println("\nAmerican Express Card ");
            }

            return true;

        } else {

            return false;

        }
    }

    public static int getSize(long d) {

        int count = 0;

        while (d > 0) {
            d = d / 10;

            count++;
        }

        return count;

    }

    public static long getPrefix(long number, int k) {

        if (getSize(number) < k) {
            return number;
        } else {

            int size = (int) getSize(number);

            for (int i = 0; i < (size - k); i++) {
                number = number / 10;
            }

            return number;

        }

    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a credit card number as a long integer: ");

        long input = sc.nextLong();


        if (isValid(input) == true) {
            System.out.println("\n" + input + " is Valid. ");
        } else {
            System.out.println("\n" + input + " is Invalid. ");
        }

    }
} 

My question is how can I use an array to store the credit card number instead of using a long number.

0

There are 0 answers