Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EANValidator {
- private static int factor = 3;
- public static boolean validate(final String eanCode) {
- int checksum = eanCode
- .chars()
- .limit(eanCode.length() - 1)
- .map(i -> Character.getNumericValue((char) i))
- .reduce(0, (sum, i) -> {
- factor = factor == 1 ? 3 : 1;
- return sum + i * factor;
- });
- checksum = (checksum % 10 == 0 ? 0 : 10 - (checksum % 10));
- return checksum == Character.getNumericValue(eanCode.charAt(eanCode.length() - 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement