I solved the "Algorithm" kata! Take the code initiation @
codewars.com/r/DE7rhA
to enlist and challenge yourself.
#codewars
public class Kata {
//enum cardType = {"Unknown", "AMEX", "Discover", "Mastercard", "VISA"};
public static String getIssuer(String cardNumber) {
// AMEX 3400~3499
// AMEX 3700~3799
// Discover 6011
// Mastercard 5100~5599
// VISA 4000~4999
int n = cardNumber.length();
if(n < 4) return "Unknown";
if(cardNumber.startsWith("34") || cardNumber.startsWith("37")) {
if(n == 15) return "AMEX";
} else if(cardNumber.startsWith("6011")) {
if(n == 16) return "Discover";
} else if(cardNumber.startsWith("51")
|| cardNumber.startsWith("52")
|| cardNumber.startsWith("53")
|| cardNumber.startsWith("54")
|| cardNumber.startsWith("55")) {
if(n == 16) return "Mastercard";
} else if(cardNumber.startsWith("4")) {
if(n == 13 || n == 16) return "VISA";
}
return "Unknown";
}
}
import static java.util.regex.Pattern.matches;
public class Kata {
public static String getIssuer(String cn) {
if (matches("^3[4|7].{13}$", cn)) return "AMEX";
if (matches("^6011.{12}$", cn)) return "Discover";
if (matches("^5[1-5].{14}$", cn)) return "Mastercard";
if (matches("^4.{12}(...)?$", cn)) return "VISA";
return "Unknown";
}
}