Validate Credit Card Number in Java

The challenge

Let’s implement the Luhn Algorithm, which is used to help validate credit card numbers.

Given a positive integer of up to 16 digits, return true if it is a valid credit card number, and false if it is not.

Here is the algorithm:

  • Double every other digit, scanning from right to left, starting from the second digit (from the right).Another way to think about it is: if there are an even number of digits, double every other digit starting with the first; if there are an odd number of digits, double every other digit starting with the second:1714 ==> [1*, 7, 1*, 4] ==> [2, 7, 2, 4] 12345 ==> [1, 2*, 3, 4*, 5] ==> [1, 4, 3, 8, 5] 891 ==> [8, 9*, 1] ==> [8, 18, 1]
  • If a resulting number is greater than 9, replace it with the sum of its own digits (which is the same as subtracting 9 from it):[8, 18*, 1] ==> [8, (1+8), 1] ==> [8, 9, 1] or: [8, 18*, 1] ==> [8, (18-9), 1] ==> [8, 9, 1]
  • Sum all of the final digits:[8, 9, 1] ==> 8 + 9 + 1 = 18
  • Finally, take that sum and divide it by 10. If the remainder equals zero, the original credit card number is valid. 18 (modulus) 10 ==> 8 , which is not equal to 0, so this is not a valid credit card number

The solution in Java code

Option 1:

public class Validate {
    public static boolean validate(String n) {
        final boolean[] flag = {(n.length() & 1) == 1};
        return Arrays.stream(
                n.split(""))
                .map(Integer::parseInt)
                .mapToInt(value -> value)
                .map(integer -> ((flag[0] ^= true) ? (integer * 2 - 1) % 9 + 1 : integer))
                .sum() % 10 == 0;
    }
}

Option 2:

public class Validate {
    public static boolean validate(final String n){
        if (n == null || n.isEmpty()) return false;
        boolean x = true;
        int sum = 0;
        int temp = 0;
             
        for (int i = n.length() - 1; i >= 0; i--) {
            temp = n.charAt(i) - '0';
            sum += (x = !x) ? temp > 4 ? temp * 2 - 9 : temp * 2 : temp;
        }
        
        return sum % 10 == 0;
    }
}

Option 3:

public class Validate{
  public static boolean validate(String n){
        boolean[] flag = { (n.length() & 1) == 1 };
        return n.chars()
                .map(c -> c - '0')
                .map(num -> (flag[0] ^= true) ? (num * 2 - 1) % 9 + 1 : num)
                .sum() % 10 == 0;
  }
}

Test cases to validate our solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CreditcardValidationTest{
  @Test
  public void test891(){
    assertEquals(false, Validate.validate("891"));
  }
  @Test
  public void test123(){
    assertEquals(false, Validate.validate("123"));
  }
  @Test
  public void test1(){
    assertEquals(false, Validate.validate("1"));
  }
  @Test
  public void test2121(){
    assertEquals(true, Validate.validate("2121"));
  }
  @Test
  public void test1230(){
    assertEquals(true, Validate.validate("1230"));
  }
  @Test
  public void test8675309(){
    assertEquals(false, Validate.validate("8675309"));
  }
  @Test
  public void test4111111111111111(){
    assertEquals(true, Validate.validate("4111111111111111"));
  }
  @Test
  public void test26(){
    assertEquals(true, Validate.validate("26"));
  }
  @Test
  public void test2626262626262626(){
    assertEquals(true, Validate.validate("2626262626262626"));
  }
  @Test
  public void test91(){
    assertEquals(true, Validate.validate("91"));
  }
  @Test
  public void test92(){
    assertEquals(false, Validate.validate("92"));
  }
  @Test
  public void test912030(){
    assertEquals(true, Validate.validate("912030"));
  }
  @Test
  public void test922030(){
    assertEquals(false, Validate.validate("922030"));
  }
}