Saturday, 20 August 2016

Chapter 6 Exercise 31, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**6.31 (Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:
■ 4 for Visa cards
■ 5 for Master cards
■ 37 for American Express cards
■ 6 for Discover cards

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
1. Double every second digit from right to left. If doubling of a digit results in a
two-digit number, add up the two digits to get a single-digit number.
2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
4. Sum the results from Step 2 and Step 3.
37 + 38 = 75
5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
it is invalid.


For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid. Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods:
/** Return true if the card number is valid */
public static boolean isValid(long number)
/** Get the result from Step 2 */
public static int sumOfDoubleEvenPlace(long number)
/** Return this number if it is a single digit, otherwise,
* return the sum of the two digits */
public static int getDigit(int number)
/** Return sum of odd-place digits in number */
public static int sumOfOddPlace(long number)
/** Return true if the digit d is a prefix for number */
public static boolean prefixMatched(long number, int d)
/** Return the number of digits in d */
public static int getSize(long d)
/** Return the first k number of digits from number. If the
* number of digits in number is less than k, return number. */
public static long getPrefix(long number, int k)

Here are sample runs of the program: (You may also implement this program by reading the input as a string and processing the string to validate the credit card.)
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
Enter a credit card number as a long integer: 4388576018402626
4388576018402626 is invalid

import java.util.Scanner;
 
public class ProgrammingExercise6_31 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
  long number;
 
  do {
   System.out
     .print("Enter a credit card number as a long integer (enter 0 to stop program):");
 
   number = input.nextLong();
 
   if (number != 0) {
    if (isValid(number)) {
     System.out.println(number + " is valid.");
    } else {
     System.out.println(number + " is invalid.");
    }
   }
 
  } while (number != 0);
 
 }
 
 /** Return true if the card number is valid */
 public static boolean isValid(long number) {
  // check 1st prefix
  int firstPrefix = (int) getPrefix(number, 1);
  if (firstPrefix != 4 && firstPrefix != 5 && firstPrefix != 3
    && firstPrefix != 6) {
   return false;
  }
  // check 2nd prefix
  if (firstPrefix == 3) {
   int secondPrefix = (int) getPrefix(number, 2);
   if (secondPrefix != 37) {
    return false;
   }
  }
 
  // check sum
 
  if ((sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 != 0) {
   return false;
  }
 
  return true;
 
 }
 
 /** Get the result from Step 2 */
 public static int sumOfDoubleEvenPlace(long number) {
  int n = getSize(number);
  int sum = 0;
 
  for (int i = 2; i <= n; i += 2) {
   sum += getDigit(2 * getDigitFromIndex(number, i));
  }
 
  return sum;
 
 }
 
 // Get digit from specific place. Index start from 1 and from the right to
 // left.
 
 public static int getDigitFromIndex(long number, int index) {
  int digit;
  digit = (int) (((number / Math.pow(10, index - 1))) % 10);
  return digit;
 }
 
 /**
  * Return this number if it is a single digit, otherwise, return the sum of
  * the two digits
  */
 public static int getDigit(int number) {
 
  int firstDigit = number / 10;
  int secondDigit = number % 10;
 
  if (firstDigit == 0) {
   return number;
  } else {
   return firstDigit + secondDigit;
  }
 
 }
 
 /** Return sum of odd-place digits in number */
 public static int sumOfOddPlace(long number) {
  int n = getSize(number);
  int sum = 0;
 
  for (int i = 1; i <= n; i += 2) {
   sum += getDigitFromIndex(number, i);
  }
 
  return sum;
 
 }
 
 /** Return true if the digit d is a prefix for number */
 public static boolean prefixMatched(long number, int d) {
 
  if (getPrefix(number, getSize(d)) == d) {
   return true;
  }
  return false;
 
 }
 
 /** Return the number of digits in d */
 public static int getSize(long d) {
 
  int numberOfDigit = 1;
  while ((d = d / 10) != 0) {
   numberOfDigit++;
  }
  return numberOfDigit;
 
 }
 
 /**
  * Return the first k number of digits from number. If the number of digits
  * in number is less than k, return number.
  */
 public static long getPrefix(long number, int k) {
 
  int numberOfDigit = getSize(number);
  return number / (long) (Math.pow(10.0, (double) (numberOfDigit - k)));
 
 }
 
}

No comments :

Post a Comment