I'm trying to develop a Card Validation program in Java. The isCardValid
method basically checks if a given Card number is valid or not. I have specified a condition there that should throw an IllegalArgumentException
, but it doesn't & the program successfully executes with exit code 0
Following is my Java file: -
public class CardValidation {
public static String getCardBrand(final String cardIsin){
String brand = null;
if (cardIsin == null || cardIsin.equals("")){
log.warn("Given card BIN is null!! So returning null");
return brand;
}
if (cardIsin.substring(0,2).matches("51|52|53|54|55"))
brand = "MasterCard".toUpperCase();
else if (cardIsin.startsWith("4"))
brand = "Visa".toUpperCase();
else if (cardIsin.startsWith("34") || cardIsin.startsWith("37"))
brand = "Amex".toUpperCase();
else if (Pattern.matches(maestroPattern,cardIsin))
brand = "Maestro".toUpperCase();
else if (Pattern.matches(diners, cardIsin))
brand = "Diners".toUpperCase();
else if (Pattern.matches(discover,cardIsin))
brand = "Discover".toUpperCase();
else if (doesFallInRupayRange(cardIsin))
brand = "Rupay".toUpperCase();
else if (cardIsin.startsWith("35"))
brand = "JCB";
return brand;
}
public static boolean isCardValid(final String cardNumber){
if(cardNumber.matches("[^0-9]"))
throw new IllegalArgumentException("INVALID ARGUMENT!!!! Card number should contain ONLY digits.");
String cardBrand = getCardBrand(cardNumber);
if (cardBrand == null)
return false;
if(cardBrand.equalsIgnoreCase("VISA") || cardBrand.equalsIgnoreCase("MasterCard") ||
cardBrand.equalsIgnoreCase("DISCOVER") || cardBrand.equalsIgnoreCase("RUPAY")) {
return cardNumber.length() == 16;
}
else if(cardBrand.equalsIgnoreCase("AMEX")) {
return cardNumber.length() == 15;
}
else if(cardBrand.equalsIgnoreCase("MAESTRO")) {
return cardNumber.length() == 16 || cardNumber.length() == 19;
}
else if(cardBrand.equalsIgnoreCase("DINERS")) {
return cardNumber.length() == 14;
}
log.info("Unknown card number brand: " + cardNumber);
return cardNumber.length() >= 12;
}
public static boolean doesFallInRupayRange(final String isin){
long intIsin = Long.parseLong(isin.replaceAll("[^\\d]", "").substring(0,6));
for (int row = 0; row < CardValidation.rupayRange.length; row++)
{
try {
if (intIsin >= CardValidation.rupayRange[row][0] && intIsin <= CardValidation.rupayRange[row][1])
return true;
}
catch (Exception e) {
log.error("Error while checking for RuPay: " + isin, e);
}
}
return false;
}
public static void main (String... args){
CardValidation.isCardValid("42424242hghghghgh");
}
}
Now the following code snippet should throw the mentioned Exception
, shouldn't it, since I'm deliberately supplying an invalid argument to the method from my main?
if(cardNumber.matches("[^0-9]"))
throw new IllegalArgumentException("INVALID ARGUMENT!!!! Card number should contain ONLY digits.");
The following is the output I get in my console
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Process finished with exit code 0