Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. public class EANValidator {
  2.  
  3. private static int factor = 3;
  4.  
  5. public static boolean validate(final String eanCode) {
  6.  
  7. int checksum = eanCode
  8. .chars()
  9. .limit(eanCode.length() - 1)
  10. .map(i -> Character.getNumericValue((char) i))
  11. .reduce(0, (sum, i) -> {
  12. factor = factor == 1 ? 3 : 1;
  13. return sum + i * factor;
  14. });
  15. checksum = (checksum % 10 == 0 ? 0 : 10 - (checksum % 10));
  16. return checksum == Character.getNumericValue(eanCode.charAt(eanCode.length() - 1));
  17. }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement