使用Luhn算法验证信用卡号码
我有一个关于下列程序分配的问题。
I have a question regarding the following programming assignment.
信用卡号码遵循一定的模式。信用卡必须有数字和16之间的13。它必须以:
Credit card numbers follow certain patterns. A credit card must have between 13 and 16 digits. It must start with:
•4 Visa卡
•5主卡
•37为美国前preSS卡
• 37 for American Express cards
在1954年,IBM的汉斯·卢恩提出了验证信用卡号码的算法。如果输入了卡号码是否正确,或者如果信用卡由扫描器正确扫描的算法是要确定是有用的。几乎所有的信用卡号码,产生以下这个有效性检查,通常所知的卢恩支票或模数10检查,其可以被描述如下。为了说明,考虑卡号4388576018402625。
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly know as the Luhn check or the Modulus 10 check, which can be described as follows. For illustration, consider the card number 4388576018402625.
- 从从右到左双每秒位数。如果在2位数增加一倍一个数字结果,添加了两个数字得到一个单一的数字。
2×2 = 4
2×2 = 4
4×2 = 8
1×2 = 2
6×2 = 12(1 + 2 = 3)
6 x 2 = 12 (1+2= 3)
5×2 = 10(1 + 0 = 1)
5 x 2 = 10 (1+0= 1)
8×2 = 16(1 + 6 = 7)
8 x 2 = 16 (1+6= 7)
4×2 = 8
-
第1步,添加所有的个位数
4 + 4 + +8 2 + 3 + 1 + 7 + 8 = 37
Add all the single digit numbers from step 1 4 + 4 +8 + 2 +3 + 1 + 7 + 8 = 37
在奇数的地方将所有的数字从右卡号至左
Add all digits in the odd places from right to left in the card number
5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37
5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37
-
从步骤2和步骤3求和结果
37 + 37 = 74
Sum the results from step 2 and step 3 37 + 37 = 74
如果来自步骤结果是被10整除,卡号是有效的;否则,它是无效的。例如,该数4388576018402625是无效的,但数量4388576018410707是一个有效的维萨卡;数6011000593748745是无效的,但数量6011000593748746是一个有效的发现卡。
If the result from step is divisible by 10, the card number is valid; otherwise, it’s invalid. For example, the number 4388576018402625 is invalid, but the number 4388576018410707 is a valid Visa card; the number 6011000593748745 is invalid, but the number 6011000593748746 is a valid Discover card.
我试图来解决它,如图以下code:
I tried to solve it as shown in the following code:
import java.util.Scanner;
public class CreditCardValidation {
public static boolean isValid(long number) {
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 ) && (getSize(number)<=16 )) {
return true;
} else {
return false;
}
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int) (number / 10);
return firstDigit + secondDigit;
}
}
public static int sumOfOddPlace(long number) {
int result = 0;
while (number > 0) {
result += (int) (number % 10);
number = number / 100;
}
return result;
}
public static int sumOfDoubleEvenPlace(long number) {
int result = 0;
long temp = 0;
while (number > 0) {
temp = number % 100;
result += getDigit((int) (temp / 10) * 2);
number = number / 100;
}
return result;
}
public static boolean prefixMatched(long number, int d) {
if ((getPrefix(number, d) == 4)
|| (getPrefix(number, d) == 5)
|| (getPrefix(number, d) == 3)) {
if (getPrefix(number, d) == 3) {
System.out.println("\nVisa Card ");
} else if (getPrefix(number, d) == 5) {
System.out.println("\nMaster Card ");
} else if (getPrefix(number, d) == 3) {
System.out.println("\nAmerican Express Card ");
}
return true;
} else {
return false;
}
}
public static int getSize(long d) {
int count = 0;
while (d > 0) {
d = d / 10;
count++;
}
return count;
}
public static long getPrefix(long number, int k) {
if (getSize(number) < k) {
return number;
} else {
int size = (int) getSize(number);
for (int i = 0; i < (size - k); i++) {
number = number / 10;
}
return number;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a credit card number as a long integer: ");
long input = sc.nextLong();
if (isValid(input) == true) {
System.out.println("\n" + input + " is Valid. ");
} else {
System.out.println("\n" + input + " is Invalid. ");
}
}
}
我的问题是如何使用数组来存储而不是使用长一些的信用卡号码。
My question is how can I use an array to store the credit card number instead of using a long number.
好吧,这可以用一个类型转换为字符串和一些Java 8来解决
东东。不要忘记数字和字符重新presenting数字是不一样的。 '1'!= 1
Okay, this can be solved with a type conversions to string and some Java 8 stuff. Don't forget numbers and the characters representing numbers are not the same. '1' != 1
public static int[] longToIntArray(long cardNumber){
return Long.toString(cardNumber).chars()
.map(x -> x - '0') //converts char to int
.toArray(); //converts to int array
}
您现在可以使用此方法来执行Luhn算法:
You can now use this method to perform the luhn algorithm:
public static int luhnCardValidator(int cardNumbers[]) {
int sum = 0, nxtDigit;
for (int i = 0; i<cardNumbers.length; i++) {
if (i % 2 == 0)
nxtDigit = (nxtDigit > 4) ? (nxtDigit * 2 - 10) + 1 : nxtDigit * 2;
sum += nxtDigit;
}
return (sum % 10);
}