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