gpt4 book ai didi

java - 从 XML 读取信用卡号列表

转载 作者:行者123 更新时间:2023-12-02 05:57:21 25 4
gpt4 key购买 nike

  • 我有一个程序,可以使用 Luhn 算法验证信用卡号并引发用户定义的异常。
  • 我的 cc_expired_db.xml 包含过期信用卡号列表。
  • 我有 cc_stolen_db.xml,其中包含被盗信用卡号码的列表。

请查看pseudo code我的预期输出。

经过单独测试,我成功地让 Luhn 算法发挥作用,并且在失败时抛出无效异常。

这是我的代码:

public boolean checkCreditCard(String strCardNumber, double amount, String luhnStatus)
throws ExpiredCreditCardException, InvalidCreditCardException,
StolenCreditCardException {

if(luhnStatus.equals("PASSED")) {
if() { // missing condition for expired
throw new ExpiredCreditCardException();

} else if() { // missing condition for stolen
throw new StolenCreditCardException();
}

} else if(luhnStatus.equals("FAILED")) { // invalid
throw new InvalidCreditCardException();

} else {
payItem(amount);
System.out.print("Thank you for shopping with us!");
}

return true;
}

我已经成功writtenread XML 使用教程。

但是,我不确定如何按照伪代码中的预期将过期和被盗异常的条件放入 if 语句中。

最佳答案

对于两个 if 语句,为每个语句定义一个类似于 isExpired(String strCardNumber)isStolen(String strCardNumber) 的方法,该方法返回 boolean 值。这些方法可以迭代各自文件中的条目,如果找到匹配项,则返回 true。然后您的代码将变为:

if (isExpired(strCardNumber)) {
throw new ExpiredCreditCardException();
} else if (isStolen(strCardNumber)) {
throw new StolenCreditCardException();
}

如果 luhnStatus 值没有其他值,我还建议使用 boolean 值,该值更适合称为 isValidNumber 或类似的名称。如果 PASSEDFAILED 这两个值不超过,则永远不会到达 payItem block 。要解决此问题,您可以将 payItem block 放在两个 if 语句之后,因为如果发生异常,它将被跳过。

伪代码示例 ifExpired 方法:

private boolean isExpired(String strCardNumber) {
for each card number in expired card numbers:
if strCardNumber.equals(current number of iteration):
return true;

after iteration, return false as the value was not contained.
}

关于java - 从 XML 读取信用卡号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22961482/

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