Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Credit card companies typically assign a special digit, called a check digit, to

ID: 3831190 • Letter: C

Question

Credit card companies typically assign a special digit, called a check digit, to the end of each customer’s credit card number. Many methods for creating the check digit have been developed. One simple method is shown in the figure below. Allow the user to enter a five-digit credit card number, with the fifth digit being the check digit. Verify that the credit card number is valid. Display the appropriate messages indicating whether the credit card number is valid or invalid. Your program should have the user enter a five-digit credit card number. (This should be entered as a string). First your program should ensure that the input has exactly 5 digits and that all those digits are numbers between 0 and 9. It should then duplicate the algorithm shown in the image below to generate the fifth validation digit. Finally, your program should generate a message indicating whether the credit card number was valid or invalid. Name your JAVA class CreditCard.java. Typical output is also shown below for three separate trials.

Explanation / Answer

Hi,

Please see below the class:

Please comment for any queries.

Thanks.

CreditCard.java

import java.util.Scanner;


public class CreditCard {

   public static void main(String [] args){
       Scanner scan = new Scanner(System.in);
       boolean isValid =false;
       System.out.println("Please enter the Credit Crad number: ");
       String creditCardNum = scan.nextLine();
       //validating the characters
       isValid = validateCreditCardNum(creditCardNum);

       if(isValid){
           if(validateFifthCharacter(creditCardNum)){
               System.out.println("Valid Credit Card !");
           }
           else{
               System.out.println("Invalid Credit Card !");
           }
       }
       else{
           System.out.println("Invalid Credit Card !");
       }
   }


   public static boolean validateCreditCardNum(String creditCardNum ){
       boolean retVal = true;
       //Check 5 characters are present
       if(creditCardNum.length()!=5){
           System.out.println("Invalid Credit Card !");
           retVal= false;
       }
       else{
           //Check all are valid digits
           for(int i=0;i<creditCardNum.length();i++){
               if(creditCardNum.charAt(i) == '0'
                       || creditCardNum.charAt(i) == '1'
                       || creditCardNum.charAt(i) == '2'
                       || creditCardNum.charAt(i) == '3'
                       || creditCardNum.charAt(i) == '4'
                       || creditCardNum.charAt(i) == '5'
                       || creditCardNum.charAt(i) == '6'
                       || creditCardNum.charAt(i) == '7'
                       || creditCardNum.charAt(i) == '8'
                       || creditCardNum.charAt(i) == '9'){
                   continue;
               }
               else{
                   retVal = false;
                   System.out.println("Invalid Credit Card !");
                   break;
               }
           }

       }

       return retVal;
   }

   public static boolean validateFifthCharacter(String creditCardNum){
       Integer digit1 = Integer.valueOf(String.valueOf(creditCardNum.charAt(0)));
       Integer digit2 = Integer.valueOf(String.valueOf(creditCardNum.charAt(1)));
       Integer digit3 = Integer.valueOf(String.valueOf(creditCardNum.charAt(2)));
       Integer digit4 = Integer.valueOf(String.valueOf(creditCardNum.charAt(3)));
       Integer digit5 = Integer.valueOf(String.valueOf(creditCardNum.charAt(4)));

       //Step1. Multiply second and fourth digit by 2
       int secondM = digit2 *2;
       int fourthM = digit4 *2;

       //Step 2. Add the numbers togther
       Integer result = digit1+secondM+digit3+fourthM;
       int lastDigit = 0;
       String lastChar ="";
       String resString = result.toString();
  
       //Step 3. Get the last digit from step to and append it to the First four digits of the given credit card
       if(resString.length()==1){
           lastChar = String.valueOf(resString.charAt(0));
       }
       else{
           lastChar = String.valueOf(resString.charAt(1));
       }

       String actualCreditCardNum = digit1.toString() + digit2.toString()+
               digit3.toString()+digit4.toString()+lastChar;

       if(creditCardNum.equalsIgnoreCase(actualCreditCardNum)){
           return true;
       }
       else{
           return false;
       }
   }
  
}

SAmple output:

Please enter the Credit Crad number:
13576
Valid Credit Card !

Please enter the Credit Crad number:
56786
Invalid Credit Card !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote