سؤال

  • I have a program that validates credit card numbers using the Luhn Algorithm and throws user-defined exceptions.
  • I have cc_expired_db.xml containg a list of Expired credit card numbers.
  • I have cc_stolen_db.xml containing a list of Stolen credit card numbers.

Please view the pseudo code of my intended output.

Upon individual testing, I have managed to get the Luhn Algorithm to work and it throws the invalid exception upon FAILED.

Here is my code:

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;
}

I have successfully written and read XML using a tutorial.

However, I am not sure how to put the conditions for expired and stolen exceptions in the if-statements as intended in my pseudo-code.

هل كانت مفيدة؟

المحلول

For both if statements, define a method for each that resembles isExpired(String strCardNumber) or isStolen(String strCardNumber), which return booleans. These methods can iterate through the entries in their respective files, and if a match is found, return true. Your code then becomes:

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

I'd also recommend, if there are no other values for the value luhnStatus using a boolean value which could be more appropriately be called isValidNumber or something to that effect. If there aren't more than the two values PASSED and FAILED, the the payItem block will never be reached. To fix this, you could put the payItem block after the two if statements, as it will be skipped if an exception occurs.

Sample pseudo-code ifExpired method:

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.
}

نصائح أخرى

While reading the XML files, you have to store the entries for each type of credit card (stolen or expired) inside a List.

The following is part-pseudocode, part-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);
}

After populating these lists, you can call your code like this:

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;
}

I left a code snippet for you on Gist

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top