信用卡类型和验证

我想运行一个程序,可以根据输入的号码确定信用卡号码的确认和类型。 编译器显示通知我的代码有错误,但我无法检测到它在哪里。 该程序也无法运行。 下面是编码,

    import java.util.*;

    public class CreditCard {

        public static void main(String args[]) {
                String CType;(String number) {
                    if (number.startsWith("4"))
                        return "Visa";
                    else if (number.startsWith("5"))
                        return "MasterCard";
                    else if (number.startsWith("6"))
                        return "Discover";
                    else if (number.startsWith("37"))
                        return "American Express";
                    else
                        return "Unknown type";
                };
            Scanner input = new Scanner(System.in);

            System.out.println("Enter a credit card number: ");
            long number = input.nextLong();

            long total = sumOfEvenPlaces(number) + (sumOfOddPlaces(number)*2);

            if (isValid(total)) {
                System.out.println("The "+CType+" card number is valid");
            } else {
                System.out.println("The "+CType+" card number is invalid.");
            }
        }



        public static boolean isValid(long total) {
            if (total % 10 != 0) {
            } else {
                        return true;
                }
            return false;
        }

        public static int sumOfEvenPlaces(long number) {
            int sum = 0;
            int remainder;
            while (number % 10 != 0 || number / 10 != 0) {
                remainder = (int) (number % 10);

                sum = sum + getDigit(remainder * 2);
                number /= 100;
            }
            return sum;
        }

        public static int getDigit(int number) {
            if (number > 9) {
                return (number % 10 + number / 10);
            } 
            return number;
        }

        public static int sumOfOddPlaces(long number) {
            int sum = 0;
            int remainder;
            number /= 10;
            while (number % 10 != 0 || number / 10 != 0) {
                remainder = (int) (number % 10);
                sum = sum + getDigit(remainder * 2);
                number /= 100;
            }
            return sum;
        }
    }

我使用enum做卡片类型检测:

package com.gabrielbauman.gist;

import java.util.regex.Pattern;

public enum CardType {

    UNKNOWN,
    VISA("^4[0-9]{12}(?:[0-9]{3}){0,2}$"),
    MASTERCARD("^(?:5[1-5]|2(?!2([01]|20)|7(2[1-9]|3))[2-7])d{14}$"),
    AMERICAN_EXPRESS("^3[47][0-9]{13}$"),
    DINERS_CLUB("^3(?:0[0-5]d|095|6d{0,2}|[89]d{2})d{12,15}$"),
    DISCOVER("^6(?:011|[45][0-9]{2})[0-9]{12}$"),
    JCB("^(?:2131|1800|35d{3})d{11}$"),
    CHINA_UNION_PAY("^62[0-9]{14,17}$");

    private Pattern pattern;

    CardType() {
        this.pattern = null;
    }

    CardType(String pattern) {
        this.pattern = Pattern.compile(pattern);
    }

    public static CardType detect(String cardNumber) {

        for (CardType cardType : CardType.values()) {
            if (null == cardType.pattern) continue;
            if (cardType.pattern.matcher(cardNumber).matches()) return cardType;
        }

        return UNKNOWN;
    }

}

然后,您可以执行CardType.detect("cardnumbergoeshere")然后您将返回CardType.VISA等。

要点有单元测试。

为了验证,我有:

public boolean isValid(String cardNumber) {
    int sum = 0;
    boolean alternate = false;
    for (int i = cardNumber.length() - 1; i >= 0; i--) {
        int n = Integer.parseInt(cardNumber.substring(i, i + 1));
        if (alternate) {
            n *= 2;
            if (n > 9) {
                n = (n % 10) + 1;
            }
        }
        sum += n;
        alternate = !alternate;
    }
    return (sum % 10 == 0);
}

这应该做到这一点。


这可能更符合你想要做的事情:

    public static void main(final String args[])
    {
        String cType = null;

        System.out.println("Enter a credit card number: ");
        final Scanner input = new Scanner(System.in);
        final String cardNumber = input.next();

        if (cardNumber.startsWith("4"))
        {
            cType = "Visa";
        }
        else if (cardNumber.startsWith("5"))
        {
            cType =  "MasterCard";
        }
        else if (cardNumber.startsWith("6"))
        {
            cType =  "Discover";
        }
        else if (cardNumber.startsWith("37"))
        {
            cType =  "American Express";
        }
        else
        {
            cType =  "Unknown type";
        }

        final long total = sumOfEvenPlaces(Long.valueOf(cardNumber)) + (sumOfOddPlaces(Long.valueOf(cardNumber)) * 2);

        if (isValid(total))
        {
            System.out.println("The " + cType + " card number is valid");
        }
        else
        {
            System.out.println("The " + cType + " card number is invalid.");
        }
    }

在文体上, CType应该以小写字母(例如cTypecType 。 您必须尝试使用Scanner以及我不确定我的实施将如何执行您的要求。

链接地址: http://www.djcxy.com/p/70731.html

上一篇: Credit card type and validation

下一篇: Auto detect credit card type vs ask the user