@Test public void isValid_with_invalid_number_returns_false() { String number = "123456789"; boolean valid = Luhn.isValid(number); assertFalse(valid); }
public static boolean isValid(String number) { checkLongerThan("The number must have at least 2 characters.", number, 2); checkIsDigitsOnly("Only digits are allowed", number); int length = number.length(); char[] chars = number.substring(0, length - 1).toCharArray(); int sum = Character.getNumericValue(number.charAt(length - 1)); for (int i = 0; i < length - 1; i++) { int value = Character.getNumericValue(chars[length - i - 2]); value *= (i % 2 == 1) ? 1 : 2; sum += value < 10 ? value : value - 9; } return sum % 10 == 0; }
Luhn { public static boolean isValid(String number) { checkLongerThan("The number must have at least 2 characters.", number, 2); checkIsDigitsOnly("Only digits are allowed", number); int length = number.length(); char[] chars = number.substring(0, length - 1).toCharArray(); int sum = Character.getNumericValue(number.charAt(length - 1)); for (int i = 0; i < length - 1; i++) { int value = Character.getNumericValue(chars[length - i - 2]); value *= (i % 2 == 1) ? 1 : 2; sum += value < 10 ? value : value - 9; } return sum % 10 == 0; } }
Luhn { public static boolean isValid(String number) { checkLongerThan("The number must have at least 2 characters.", number, 2); checkIsDigitsOnly("Only digits are allowed", number); int length = number.length(); char[] chars = number.substring(0, length - 1).toCharArray(); int sum = Character.getNumericValue(number.charAt(length - 1)); for (int i = 0; i < length - 1; i++) { int value = Character.getNumericValue(chars[length - i - 2]); value *= (i % 2 == 1) ? 1 : 2; sum += value < 10 ? value : value - 9; } return sum % 10 == 0; }  }
Luhn { public static boolean isValid(String number) { checkLongerThan("The number must have at least 2 characters.", number, 2); checkIsDigitsOnly("Only digits are allowed", number); int length = number.length(); char[] chars = number.substring(0, length - 1).toCharArray(); int sum = Character.getNumericValue(number.charAt(length - 1)); for (int i = 0; i < length - 1; i++) { int value = Character.getNumericValue(chars[length - i - 2]); value *= (i % 2 == 1) ? 1 : 2; sum += value < 10 ? value : value - 9; } return sum % 10 == 0; }  static boolean isValid(String number); static String checkValidity(String number); }
Luhn { public static boolean isValid(String number) { checkLongerThan("The number must have at least 2 characters.", number, 2); checkIsDigitsOnly("Only digits are allowed", number); int length = number.length(); char[] chars = number.substring(0, length - 1).toCharArray(); int sum = Character.getNumericValue(number.charAt(length - 1)); for (int i = 0; i < length - 1; i++) { int value = Character.getNumericValue(chars[length - i - 2]); value *= (i % 2 == 1) ? 1 : 2; sum += value < 10 ? value : value - 9; } return sum % 10 == 0; }  static boolean isValid(String number); static String checkValidity(String number);  }