Advertisement
luliu

Credit Card Number Validation

Nov 8th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 11/8/2015
  4.  * Course Number: CSC-111
  5.  * Course Name: Introduction to Java Programming
  6.  * Problem Number:Problem 6.31(Credit Card Number Validation)  
  7.  * Email: lliu0001@student.stcc.edu
  8.  * Problem Description: Credit Card Number Validation
  9.  */
  10. package chapter06;
  11.  
  12. import java.util.Scanner;
  13.  
  14. public class CreditCardValidation {
  15.     public static void main(String args[]) {
  16.  
  17.         final String TITLE = "Credit Card Validator";
  18.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  19.         System.out.println("Welcome to " + TITLE);
  20.         Scanner sc = new Scanner(System.in);
  21.         do {
  22.             System.out.println("Enter a credit card number as a long integer: ");
  23.             long ccn = sc.nextLong();
  24.             sc.nextLine();
  25.             if (isValid(ccn))
  26.                 System.out.println(ccn + " is valid");
  27.             else
  28.                 System.out.println(ccn + " is invalid");
  29.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  30.         sc.close();
  31.         System.out.println("Thank you for using " + TITLE);
  32.     }
  33.  
  34.     private static boolean doThisAgain(Scanner sc, String prompt) {
  35.         System.out.print(prompt);
  36.         String doOver = sc.nextLine();
  37.         return doOver.equalsIgnoreCase("Y");
  38.     }
  39.  
  40.     public static boolean isValid(long ccn) {
  41.         if (ccn <= 0)
  42.             return false;
  43.         int size = getSize(ccn);
  44.         if (size < 13 || size > 16)
  45.             return false;
  46.         if (!prefixMatched(ccn, 4) && !prefixMatched(ccn, 5) && !prefixMatched(ccn, 6) && !prefixMatched(ccn, 37))
  47.             return false;
  48.         return (sumOfDoubleEvenPlace(ccn) + sumOfOddPlace(ccn)) % 10 == 0;
  49.     }
  50.  
  51.     public static int getSize(long d) {
  52.         int count = 0;
  53.         while (d > 0) {
  54.             d = d / 10;
  55.             count++;
  56.         }
  57.         return count;
  58.     }
  59.  
  60.     public static int sumOfDoubleEvenPlace(long number) {
  61.         int sum = 0;
  62.         while (number > 0) {
  63.             int last2Digits = (int) (number % 100);
  64.             int evenDigit = last2Digits / 10;
  65.             sum += getDigit(2 * evenDigit);
  66.             number = number / 100;
  67.         }
  68.         return sum;
  69.     }
  70.  
  71.     public static int getDigit(int number) {
  72.         if (number <= 9)
  73.             return number;
  74.         else
  75.             return number / 10 + number % 10;
  76.     }
  77.  
  78.     public static int sumOfOddPlace(long number) {
  79.         int result = 0;
  80.         while (number > 0) {
  81.             result += (int) (number % 10);
  82.             number = number / 100;
  83.         }
  84.         return result;
  85.     }
  86.  
  87.     public static boolean prefixMatched(long number, int d) {
  88.         if ((getPrefix(number, 1) == d) || (getPrefix(number, 2) == d))
  89.             return true;
  90.         else
  91.             return false;
  92.     }
  93.  
  94.     public static long getPrefix(long number, int k) {
  95.         if (getSize(number) < k)
  96.             return number;
  97.         else {
  98.             int size = (int) getSize(number);
  99.             for (int i = 0; i < (size - k); i++) {
  100.                 number = number / 10;
  101.             }
  102.             return number;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement