第六章第三十一题(金融应用:信用卡号的合法性…

2020-05-20 16:00:58来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

第六章第三十一题(金融应用:信用卡号的合法性验证)(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

下面是参考答案代码:

// https://cn.fankuiba.com
import java.util.Scanner;

public class Ans6_31_page203 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a credit card number as a long integer:");
        long number = input.nextLong();
        if (isVa1id(number) && divide(number))
            System.out.println(number + " is valid");
        else
            System.out.println(number + " is invalid");
        }

    // Step 0
    public static boolean isVa1id(long number) {
        if (prefixMatched(number,4) || prefixMatched(number,5) ||
                prefixMatched(number,37) || prefixMatched(number,6)) {
            return (number + "").length() >= 13 && (number + "").length() <= 16;
        }
        else
            return false;
    }

    // Get the result from Step 2
    public static int getEven(long number) {
        int getEven = 0;
        for (int i = 0; i < (number+"").length(); i+=2) {
            char n = (number + "").charAt(i);
            int num = Integer.parseInt(Character.toString(n));
            if (num * 2 > 9)
                num = (num * 2 / 10) + (num * 2 % 10);
            else if (num * 2 < 10)
                num = num * 2;
            getEven += num;
        }
        return getEven;
    }

    // Get the result from Step 3
    public static int getOdd(long number) {
        int getOdd = 0;
        for (int i = 1; i < (number+"").length(); i+=2) {
            char n = (number + "").charAt(i);
            int num = Integer.parseInt(Character.toString(n));
            getOdd += num;
        }
        return getOdd;
    }

    // Return sum
    public static int sumOfOddAndEven(long number) {
        return getEven(number) + getOdd(number);
    }

    // Return true if the digit d is a prefix for number
    public static boolean prefixMatched(long number, int d) {
        String strNumber = number + "";
        int charNumber0 = Integer.parseInt(strNumber.substring(0,(d+"").length()));
        return charNumber0 == d;
    }

    public static boolean divide(long d) {
        return sumOfOddAndEven(d) % 10 == 0;
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)更多内容


原文链接:https://www.cnblogs.com/in2013/p/12925322.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:入职四个月后,我当了一回面试官,面试了一名二本院校学生

下一篇:Spring 依赖注入(DI)详解 [Spring][依赖注入的 6 种实现方式][