Advertisement
jmccasusi

creditcard

Apr 23rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. private static boolean creditCardInput(String cardInput)
  2.     {
  3.         //credit card processing logic
  4.         int s1 = 0, s2 = 0;
  5.         String reverse = new StringBuffer(cardInput).reverse().toString();
  6.         for(int i = 0 ;i < reverse.length();i++){
  7.             int digit = Character.digit(reverse.charAt(i), 10);
  8.             if(i % 2 == 0){//this is for odd digits, they are 1-indexed in the algorithm
  9.                 s1 += digit;
  10.             }else{//add 2 * digit for 0-4, add 2 * digit - 9 for 5-9
  11.                 s2 += 2 * digit;
  12.                 if(digit >= 5){
  13.                     s2 -= 9;
  14.                 }
  15.             }
  16.         }
  17.         return((s1 + s2) % 10 == 0);
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement