第六章第三十一题(金融应用:信用卡号的合法性验证)(Financial: credit card number validation)

第六章第三十一题(金融应用:信用卡号的合法性验证)(Financial: credit card number validation)

  • **6.31(金融应用:信用卡号的合法性验证)信用卡号遵循某种模式。一个信用卡号必须是13到16位的整数。它的开头必须是:
    4,指Visa卡
    5,指Master卡
    37,指American Express 卡
    6,指Discover卡
    1954年,IBM的Hans Luhn提出一种算法,用于验证信用卡号的有效性。这个算法在确定输入的卡号是否正确,或者这张信用卡是否被扫描仪正确扫描方面是非常有用的。遵循这个合法性检测可以生成所有的信用卡号,通常称之为Luhn检测或者Mod 10检测,可以如下描述(为了方便解释,假设卡号4388576018402626):
    1.从右到左对偶数位数字翻倍。如果对某个数字翻倍之后的结果是一个两位数,那么就将这两位加在一起得到一位数。
    2.现在将第一步得到的所有一位数相加。
    3.将卡号里从右到左奇数位上的所有数字相加。
    4.将第二步和第三步得到的结果相加。
    5.如果第四步得到的结果能被10整除,那么卡号是合法的;否则,卡号是不合法的。例如,号码4388576018402626是不合法的,但是号码4388576018410707是合法的。
    编写程序,提示用户输入一个long型整数的信用卡号码,显示这个数字是合法的还是非法的。使用下面的方法设计程序:
    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)
    下面是程序的运行示例:(你也可以通过将输入作为一个字符串读入,以及对字符串进行处理来验证信用卡卡号。)
    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
  • 参考代码:
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;
    }
}

  • 结果显示:
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109211409