Learn a java program to check credit card type by credit Score of a customer. In this tutorial, we will create a Java program that determines whether a customer qualifies for a Silver, Gold, or Platinum credit card using if-else conditions.
Code Explanation (Step-by-Step)
- Create a Scanner object to take user input.
- Read the customer’s credit score.
- Check whether the score is between 400 and 850.
- If the score is outside this range, display Invalid credit score.
- If the score is between 400 and 599, issue a Silver Card.
- If the score is between 600 and 799, issue a Gold Card.
- If the score is between 800 and 850, issue a Platinum Card.
- Close the Scanner object.
import java.util.Scanner;
public class CreditCardIssue {
public static void main(String[] args) {
System.out.println("Enter the customer Credit Score :-");
Scanner sc = new Scanner(System.in);
int userScore = sc.nextInt();
System.out.println("Customer credit card type is :- ");
if (userScore < 400 || userScore > 850) {
System.out.println("Invalid credit score");
} else {
if (userScore >= 400 && userScore < 600) {
System.out.println("Silver Card");
} else if (userScore >= 600 && userScore < 800) {
System.out.println("Gold Card");
} else {
System.out.println("Platinum Card");
}
}
sc.close();
}
}
Output :-
Enter the customer Credit Score :-
550
Customer credit card type is :-
Silver Card
Java Program to Check Credit Card Type
This program identifies the credit card type based on the card number prefix and length using a separate method and startsWith().
import java.util.Scanner;
public class CreditCardTypeChecker {
public static String getCardType(String cardNumber) {
int length = cardNumber.length();
if ((length == 13 || length == 16) && cardNumber.startsWith("4")) {
return "Visa";
}
if (length == 16) {
String prefix = cardNumber.substring(0, 2);
int value = Integer.parseInt(prefix);
if (value >= 51 && value <= 55) {
return "MasterCard";
}
}
if (length == 15 &&
(cardNumber.startsWith("34") || cardNumber.startsWith("37"))) {
return "American Express";
}
if (length == 16 && cardNumber.startsWith("6011")) {
return "Discover";
}
return "Unknown Card Type";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Credit Card Number: ");
String cardNumber = sc.nextLine();
System.out.println("Card Type: " + getCardType(cardNumber));
sc.close();
}
}
Output :-
Enter Credit Card Number: 4111111111111123
Card Type: Visa