|
| 1 | +/** |
| 2 | + * (Financial: credit card number validation) Credit card numbers follow certain |
| 3 | + * patterns. A credit card number must have between 13 and 16 digits. It must start |
| 4 | + * with |
| 5 | + * ■■ 4 for Visa cards |
| 6 | + * ■■ 5 for Master cards |
| 7 | + * ■■ 37 for American Express cards |
| 8 | + * ■■ 6 for Discover cards |
| 9 | + * In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card |
| 10 | + * numbers. The algorithm is useful to determine whether a card number is entered |
| 11 | + * correctly, or whether a credit card is scanned correctly by a scanner. Credit card |
| 12 | + * numbers are generated following this validity check, commonly known as the |
| 13 | + * Luhn check or the Mod 10 check, which can be described as follows (for illustration, |
| 14 | + * consider the card number 4388576018402626): |
| 15 | + * 1. Double every second digit from right to left. If doubling of a digit results in a |
| 16 | + * two-digit number, add up the two digits to get a single-digit number. |
| 17 | + * 2. Now add all single-digit numbers from Step 1. |
| 18 | + * 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 3 7 |
| 19 | + * 3. Add all digits in the odd places from right to left in the card number. |
| 20 | + * 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 3 8 |
| 21 | + * 4. Sum the results from Step 2 and Step 3. |
| 22 | + * 3 7 + 3 8 = 7 5 |
| 23 | + * 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, |
| 24 | + * it is invalid. For example, the number 4388576018402626 is invalid, but the |
| 25 | + * number 4388576018410707 is valid. |
| 26 | + * Write a program that prompts the user to enter a credit card number as a long |
| 27 | + * integer. Display whether the number is valid or invalid. Design your program to |
| 28 | + * use the following methods: |
| 29 | + * Here are sample runs of the program: (You may also implement this program by |
| 30 | + * reading the input as a string and processing the string to validate the credit card.) |
| 31 | + * Enter a credit card number as a long integer: |
| 32 | + * 4388576018410707 |
| 33 | + * 4388576018410707 is valid |
| 34 | + * Enter a credit card number as a long integer: |
| 35 | + * 4388576018402626 |
| 36 | + * 4388576018402626 is invalid |
| 37 | + * |
| 38 | + * Created by Sven on 07/27/19. |
| 39 | + */ |
| 40 | +package Chapter06; |
| 41 | + |
| 42 | +import java.util.Scanner; |
| 43 | + |
| 44 | +public class Exercise0631_Financial_credit_card_number_validation { |
| 45 | + public static void main(String[] args) { |
| 46 | + Scanner input = new Scanner(System.in); |
| 47 | + System.out.println("Enter a credit card number as a long integer:"); |
| 48 | + long number = input.nextLong(); |
| 49 | + System.out.println(isValid(number) ? (number + " is valid") : (number + " is invalid")); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Return true if the card number is valid |
| 54 | + */ |
| 55 | + public static boolean isValid(long number) { |
| 56 | + final int PREFIX_VISA = 4; |
| 57 | + final int PREFIX_MASTER = 5; |
| 58 | + final int PREFIX_AMERICAN_XP = 37; |
| 59 | + final int PREFIX_DISCOVERS = 6; |
| 60 | + if (prefixMatched(number, PREFIX_VISA) || |
| 61 | + prefixMatched(number, PREFIX_MASTER) || |
| 62 | + prefixMatched(number, PREFIX_AMERICAN_XP) || |
| 63 | + prefixMatched(number, PREFIX_DISCOVERS)) { |
| 64 | + if (getSize(number) >= 13 && getSize(number) <= 16) { |
| 65 | + int sum = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number); |
| 66 | + return sum % 10 == 0; |
| 67 | + } else { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } else { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the result from Step 2 |
| 77 | + */ |
| 78 | + public static int sumOfDoubleEvenPlace(long number) { |
| 79 | + int sum = 0; |
| 80 | + while (number > 0) { |
| 81 | + number /= 10; |
| 82 | + int digit = getDigit((int) (number % 10) * 2); |
| 83 | + sum += digit; |
| 84 | + number /= 10; |
| 85 | + } |
| 86 | + return sum; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Return this number if it is a single digit, otherwise, |
| 91 | + * return the sum of the two digits |
| 92 | + */ |
| 93 | + public static int getDigit(int number) { |
| 94 | + return (number > 9) ? (number % 10 + number / 10) : number; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Return sum of odd-place digits in number |
| 99 | + */ |
| 100 | + public static int sumOfOddPlace(long number) { |
| 101 | + int sum = 0; |
| 102 | + while (number > 0) { |
| 103 | + int digit = (int) (number % 10); |
| 104 | + digit = getDigit(digit); |
| 105 | + sum += digit; |
| 106 | + number /= 100; |
| 107 | + } |
| 108 | + return sum; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Return true if the number d is a prefix for number |
| 113 | + */ |
| 114 | + public static boolean prefixMatched(long number, int d) { |
| 115 | + return getPrefix(number, getSize(d)) == d; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Return the number of digits in d |
| 120 | + */ |
| 121 | + public static int getSize(long d) { |
| 122 | + int size = 0; |
| 123 | + while (d > 0) { |
| 124 | + size++; |
| 125 | + d /= 10; |
| 126 | + } |
| 127 | + return size; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Return the first k number of digits from number. If the |
| 132 | + * number of digits in number is less than k, return number. |
| 133 | + */ |
| 134 | + public static long getPrefix(long number, int k) { |
| 135 | + if (getSize(number) < k) { |
| 136 | + return number; |
| 137 | + } else { |
| 138 | + long difference = getSize(number) - k; |
| 139 | + return (int) (number / Math.pow(10, difference)); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments