【问题标题】:Reading a list of credit card numbers from XML从 XML 读取信用卡号列表
【发布时间】:2014-04-09 20:13:19
【问题描述】:
  • 我有一个程序使用 Luhn 算法验证信用卡号码并引发用户定义的异常。
  • 我的 cc_expired_db.xml 包含过期信用卡号列表。
  • 我有 cc_stolen_db.xml,其中包含被盗信用卡号的列表。

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

在个别测试中,我设法让 Luhn 算法工作,并在 FAILED 时抛出无效异常。

这是我的代码:

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 语句中。

【问题讨论】:

  • 您可以将日期更改为 long 并使用 - 并比较您是否需要 Java 中的 Date Util 我可以发送给您

标签: java xml validation xml-serialization javabeans


【解决方案1】:

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

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

如果值luhnStatus 没有其他值,我也建议使用布尔值,更恰当地称为isValidNumber 或类似的东西。如果不超过PASSEDFAILED 这两个值,则永远不会到达payItem 块。要解决此问题,您可以将 payItem 块放在两个 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.
}

【讨论】:

  • 您好,谢谢您的建议。我还将考虑您关于 luhnStatus 只是布尔值的提示。您能否提供isExpired(String strCardNumber) 的模板代码以及答案?非常感谢您的帮助。
  • @ohtph 当然。添加到我的答案中。
  • 嗨,我很抱歉,但这对我来说可能是一个弱点。我对 XML 很陌生。您可以为此提供实际代码吗? Here 是我使用的源代码和实际的 XML(如果有帮助的话)。再次抱歉。我真的很感激。
  • 我已经用luhnStatus 实现了你的建议,它确实更优雅(pastebin)。我暂时硬编码了 XML 中的数字,以便可以运行程序。再次感谢。我现在只缺少布尔方法。如果您愿意提供该方法的帮助,这对我学习如何处理 XML 将是一个很好的参考。您的帮助得到了很好的记录。
  • 嗨,我接受这个作为答案,因为您确实回答了“在条件中添加什么”的主要问题。我可能会在一个单独的问题中询问该方法。再次感谢。
【解决方案2】:

在读取 XML 文件时,您必须将每种信用卡类型(被盗或过期)的条目存储在一个列表中。

以下是部分伪代码,部分是java:

List<String> expiredCCList = new ArrayList<String>();
while (reading_CC_Expired.xml) {
    String expiredCC = readEntryFromExpiredXML();
    expiredCCList.add(expiredCC);
}


List<String> stolenCCList = new ArrayList<String>();
while (reading_CC_Stolen.xml) {
    String stolenCC = readEntryFromStolenXML();
    stolenCCList.add(stolenCC);
}

填充这些列表后,您可以这样调用您的代码:

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

if(luhnStatus.equals("PASSED")) {
    if(expiredCCList.contains(strCardNumber)) {
        throw new ExpiredCreditCardException();

    } else if(stolenCCList.contains(strCardNumber) {
        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;
}

我在Gist给你留了一个代码sn-p

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2013-06-28
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多