Chapter 6 Problem 31 (Financial Application: Credit Card Number Validation) (Financial: credit card number validation)

Chapter 6 Problem 31 (Financial Application: Credit Card Number Validation) (Financial: credit card number validation)

  • **6.31 (Financial Application: Legality Verification of Credit Card Numbers) Credit card numbers follow a certain pattern. A credit card number must be an integer from 13 to 16 digits. It must start with:
    4, refers to Visa card
    5, refers to Master card
    37, refers to American Express card
    6, refers to Discover card
    In 1954, IBM's Hans Luhn proposed an algorithm to verify the validity of credit card numbers. This algorithm is very useful in determining whether the entered card number is correct, or whether the credit card was scanned correctly by the scanner. Following this legality test can generate all credit card numbers, usually called Luhn test or Mod 10 test, which can be described as follows (for the convenience of explanation, suppose the card number is 4388576018402626):
    1. Double the even digits from right to left. If the result of doubling a number is a two-digit number, then add these two numbers together to get a one-digit number.
    2. Now add all the one-digit numbers obtained in the first step.
    3. Add all the numbers on the odd digits from right to left in the card number.
    4. Add the results from the second and third steps.
    5. If the result obtained in step 4 can be divisible by 10, then the card number is legal; otherwise, the card number is illegal. For example, the number 4388576018402626 is illegal, but the number 4388576018410707 is legal.
    Write a program that prompts the user to enter a long integer credit card number to show whether the number is legal or illegal. Use the following method to design the program:
    public static boolean isValid(long number)
    public static int sumOfDoubleEvenPlace(long number)
    public static int getDigit(int number)
    public static int sumOfOddPlace(long number)
    public static boolean prefixMatched(long number, int d)
    public static int getSize(long d)
    public static long getPrefix(long number, int k)
    The following is the program Running example: (You can also verify the credit card number by reading the input as a string and processing the string.)
    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
    **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):
    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.
    Now add all single-digit numbers from Step 1.
    Add all digits in the odd places from right to left in the card number.
    Sum the results from Step 2 and Step 3.
    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:
    public static boolean isValid(long number)
    public static int sumOfDoubleEvenPlace(long number)
    public static int getDigit(int number)
    public static int sumOfOddPlace(long number)
    public static boolean prefixMatched(long number, int d)
    public static int getSize(long d)
    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
  • Reference Code:
package chapter06;

import java.util.Scanner;

public class Code_31 {
    
    
    public static void main(String[] args) {
    
    
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter a credit card number as a long integer: ");
        long creditCardNumber = inputScanner.nextLong();
        if(isValid(creditCardNumber))
            System.out.printf("%d is valid", creditCardNumber);
        else
            System.out.printf("%d is invalid", creditCardNumber);
    }
    public static boolean isValid(long number){
    
    
        if((13 <= getSize(number) && getSize(number) <= 16) && (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 37) || prefixMatched(number, 6)) && ((sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0))
            return true;
        else
            return false;
    }
    public static int sumOfDoubleEvenPlace(long number){
    
    
        String numberString = String.valueOf(number);
        int sum = 0;
        for(int i = getSize(number) - 2;i >= 0 ;i -= 2)
            sum += getDigit(2 * Integer.parseInt(String.valueOf(numberString.charAt(i))));
        return sum;
    }
    public static int getDigit(int number){
    
    
        if(number <= 9)
            return number;
        else
            return (number / 10 + number % 10);
    }
    public static int sumOfOddPlace(long number) {
    
    
        String numberString = String.valueOf(number);
        int sum = 0;
        for(int i = getSize(number) - 1;i >= 0 ;i -= 2)
            sum += Integer.parseInt(String.valueOf(numberString.charAt(i)));
        return sum;
    }
    public static boolean prefixMatched(long number, int d) {
    
    
        return d == getPrefix(number,getSize(d));
    }
    public static int getSize(long d) {
    
    
        String dString = String.valueOf(d);
        return dString.length();
    }
    public static long getPrefix(long number, int k) {
    
    
        if(getSize(number) < k){
    
    
            return number;
        }
        long prefixNumber = number / (long)Math.pow(10, getSize(number) - k);
        return prefixNumber;
    }
}

  • The results show that:
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
Process finished with exit code 0

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109211409