gpt4 book ai didi

java - 在java中使用枚举检测卡类型时遇到问题

转载 作者:行者123 更新时间:2023-12-01 11:31:29 25 4
gpt4 key购买 nike

我正在开发一个接受付款的 Android 应用程序。我在检测卡类型时遇到问题。在这里,我在 CardType.Java Enum 中遇到一个问题,仅返回 visa 它无法找到任何其他类型的卡。我正在输入600206,我的类(class)返回签证而不是MAESTRO,这是错误的,请帮助我。

CardType.Java

public enum CardType {

VISA("4"),
MCRD("5"),
MAESTRO("67", "56", "502260", "504433",
"504434", "504435", "504437", "504645", "504681",
"504753", "504775", "504809", "504817", "504834",
"504848", "504884", "504973", "504993", "508125",
"508126", "508159", "508192", "508227", "600206",
"603123", "603741", "603845", "622018"),
DINERCLUB("30", "36", "38", "39"),
JCB("35"),
AMEX("34", "37"),
DISCOVER("60", "62", "64", "65"),
UNKNOWN("0");

private final String[] pattern;

private CardType(String... pattern) {

System.out.println("pattern== "+pattern.toString());
this.pattern = pattern;
}




public static CardType typeOf(String card) {

System.out.println("card== "+card);
for (CardType type : values()) {

System.out.println("type== "+type);
return type;
}
return null;
}

public static String getScheme(CardType cardType) {
System.out.println("getScheme");
switch (cardType) {
case VISA:
return "VISA";
case MCRD:
return "MCRD";
case MAESTRO:
return "MAESTRO";
case DINERCLUB:
return "DINERCLUB";
case JCB:
return "JCB";
case AMEX:
return "AMEX";
case DISCOVER:
return "DISCOVER";
default:
return "UNKNOWN";
}
}
}

最佳答案

您的typeOf方法返回枚举的第一个元素,即VISA,将其替换为

public static CardType typeOf(String card) {
for (CardType type : values()) {
for(String numbers : type.pattern) {
if(numbers.equals(card)) {
return type;
}
}
}
throw new IllegalArgumentException("Type for card pattern " + card + " was not found.");
}

关于java - 在java中使用枚举检测卡类型时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30348757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com