获取信用卡号并输出公司名称

时间:2014-10-28 00:45:46

标签: java

需要获得发卡机构的号码,只输出“未知”。 (底部只是测试人员代码)。 我试图测试错误的变量,我错误地使用indexOf()?请,任何帮助将不胜感激

public class CreditCard 
{
    private String card_number;
    private boolean is_number;
    private String number_string = "";
    public String issuer_name = "";

    public CreditCard(String card_number)
    {
        this.card_number = card_number;
    }

    public String toString()
    {
        for (int x = 0; x < card_number.length(); x++)
        {
            char y = card_number.charAt(x);
            is_number = Character.isDigit(y);
            if (is_number)
            {
                number_string += y;
            }

        }

        String s = number_string + " was issued by " + getIssuer();
        return s;
    }

    public void setIssuer(String issuer)
    {
        issuer_name = issuer;
    }

    public String getIssuer()
    {
        String issuer_Name;
        if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16)
        {
            issuer_Name = "VISA";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14)
        {
            issuer_Name = "Diner's Club";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14)
        {
            issuer_Name = "Diner's Club";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14)
        {
            issuer_Name = "Diner's Club";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('8') == 1 && card_number.length() == 14)
        {
            issuer_Name = "Diner's Club";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('4') == 1 && card_number.length() == 15)
        {
            issuer_Name = "American Express";
        }
        if (card_number.indexOf('3') == 0 && card_number.indexOf('7') == 1 && card_number.length() == 15)
        {
            issuer_Name = "American Express";
        }
        if (card_number.indexOf('5') == 0 && card_number.indexOf('1') == 1 && card_number.length() == 16)
        {
            issuer_Name = "MasterCard";
        }
        if (card_number.indexOf('5') == 0 && card_number.indexOf('5') == 1 && card_number.length() == 16)
        {
            issuer_Name = "MasterCard";
        }
        if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
        {
            issuer_Name = "Discover";
        }
        else
        {
            issuer_Name = "Unknown";
        }
        return issuer_Name;
    }

    public static void main(String[] args)
    {       
        System.out.println(new CreditCard("42225-22222222"));
        System.out.println(new CreditCard("76009644571"));
        System.out.println(new CreditCard("50197170-10103742"));
        System.out.println(new CreditCard("6331101899890016"));
    }


}

2 个答案:

答案 0 :(得分:1)

if else子句将启动类似

的内容
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16) {
    issuer_Name = "VISA";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14) {
    issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14) {
    issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14) {
    issuer_Name = "Diner's Club";
} else {
    issuer_Name = "Other";
}

注意我没有看到算法的正确性,但你问的是如何做if / else

答案 1 :(得分:1)

    if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
    {
        issuer_Name = "Discover";
    }
    else
    {
        issuer_Name = "Unknown";
    }

检查此代码。 如果发卡行不是发现,输出总是“未知”

if (true) {
  n=1; 
}
if (true) { 
  n=2; 
}
if (true) { 
  n=3;
}
else {
 n=0;
}

n为3或0,不能为1或2。 因为最后一个if-else会覆盖n

您可以通过将“if”更改为“else if”

来解决此问题