Wednesday, February 21, 2024
HomeSoftware EngineeringCredit score Card Issuer Checking in Java

# Credit score Card Issuer Checking in Java

## The problem#

Given a bank card quantity we will decide who the issuer/vendor is with just a few primary knowns.

Full the operate `getIssuer()` that can use the values proven beneath to find out the cardboard issuer for a given card quantity. If the quantity can’t be matched then the operate ought to return the string `Unknown`.

``````| Card Sort  | Begins With          | Quantity Size |
|------------|----------------------|---------------|
| AMEX       | 34 or 37             | 15            |
| Uncover   | 6011                 | 16            |
| Mastercard | 51, 52, 53, 54 or 55 | 16            |
| VISA       | 4                    | 13 or 16      |
``````

Examples

``````getIssuer(4111111111111111) == "VISA"
getIssuer(4111111111111) == "VISA"
getIssuer(4012888888881881) == "VISA"
getIssuer(378282246310005) == "AMEX"
getIssuer(6011111111111117) == "Uncover"
getIssuer(5105105105105100) == "Mastercard"
getIssuer(5105105105105106) == "Mastercard"
getIssuer(9111111111111111) == "Unknown"
``````

## The answer in Java code#

Possibility 1:

``````public class CreditCardChecker {
public static String getIssuer(String cardNumber)  cardNumber.startsWith("37")) && cardNumber.size()==15)
return "AMEX";
if (cardNumber.startsWith("6011") && cardNumber.size()==16)
return "Uncover";
if ((cardNumber.startsWith("51")
}
``````

Possibility 2:

``````import static java.util.regex.Sample.matches;
public class CreditCardChecker {
public static String getIssuer(String cn) {
if (matches("^3[4|7].{13}\$", cn)) return "AMEX";
if (matches("^6011.{12}\$", cn)) return "Uncover";
if (matches("^5[1-5].{14}\$", cn)) return "Mastercard";
if (matches("^4.{12}(...)?\$", cn)) return "VISA";
return "Unknown";
}
}
``````

Possibility 3:

``````public class CreditCardChecker {
public static String getIssuer(String cardNumber) {
String visa = "4+(d{15}|d{12})";
String amex = "(34|37)+d{13}";
String uncover = "6011+d{12}";
String mastercard = "(51|52|53|54|55)+d{14}";
String end result = "Unknown";
if (cardNumber.matches(visa)) return "VISA";
if (cardNumber.matches(amex)) return "AMEX";
if (cardNumber.matches(uncover)) return "Uncover";
if (cardNumber.matches(mastercard)) return "Mastercard";
return end result;
}
}
``````

## Take a look at circumstances to validate our answer#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Take a look at
public void issuerTests() {
assertEquals("VISA",       CreditCardChecker.getIssuer("4111111111111111"));
assertEquals("AMEX",       CreditCardChecker.getIssuer("378282246310005"));
assertEquals("Unknown",    CreditCardChecker.getIssuer("9111111111111111"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5105105105105100"));
}
}
``````

## Extra check circumstances#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Take a look at
public void issuerTests() {
assertEquals("VISA",       CreditCardChecker.getIssuer("4111111111111111"));
assertEquals("VISA",       CreditCardChecker.getIssuer("4111111111111"));
assertEquals("VISA",       CreditCardChecker.getIssuer("4012888888881881"));
assertEquals("Unknown",    CreditCardChecker.getIssuer("41111111111111"));
assertEquals("Unknown",    CreditCardChecker.getIssuer("411111111111111"));

assertEquals("AMEX",       CreditCardChecker.getIssuer("378282246310005"));
assertEquals("AMEX",       CreditCardChecker.getIssuer("348282246310005"));

assertEquals("Uncover",   CreditCardChecker.getIssuer("6011111111111117"));

assertEquals("Mastercard", CreditCardChecker.getIssuer("5105105105105100"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5105105105105106"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5205105105105106"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5305105105105106"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5405105105105106"));
assertEquals("Mastercard", CreditCardChecker.getIssuer("5505105105105106"));

assertEquals("Unknown",    CreditCardChecker.getIssuer("9111111111111111"));
assertEquals("Unknown",    CreditCardChecker.getIssuer("5605105105105106"));
}
@Take a look at
public void randomTests() {

}
}
``````
RELATED ARTICLES