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

Java Financial: Credit card number validation. Credit card numbers follows certa

ID: 3891783 • Letter: J

Question

Java

Financial: Credit card number validation.

Credit card numbers follows certain patterns. A credit card number must have between 13 and 16 digits. It must start with:

4 for Visa cards

5 for Master cards

37 for American Express cards

6 for Discover cards

In 1954, Habs Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or Mod 10 check, which is described as follows ( for illustration, consider the card number 4388576018402626):

Double every second digit from right to left. If double of a digit results in a two-digit number, add up the two digits to get a single digit number.

Now add all single-digit number from the step above (Step 1 ).

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

Add all digits in the odd places from right to left in the card number.

6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

Sum the results from the above two step ( Step 2 and Step 3).

37 + 38 = 75

If the result from the above step ( step 4 ) is divisible by 10, the card is valid; otherwise, the card is invalid. For example, the card number 4388576018402626 is invalid, but the card number 4388576018410707 is valid.

Design and Implement the above algorithm in Java Recursively. Your algorithm should prompt the user to enter a credit card number as a long integer. Display whether the number is a valid or invalid credit card number. Design your program to use the following methods: These methods should be done recursively… Make sure you have several runs, i.e. several testing conditions.

//Return if the card number is valid

Public static Boolean isValid(long number)

//Get the result from step 2

Public static int sumOfDoubleEvenPlace( long number )

//Return this number it is a single digit, otherwise, return the sum of the two //digits

Public static int getDigit( int number )

//Return sum of sumOfOddPlace( long number )

Public static int sumOfOddPlace( long number )

//Return the number of digits in digit

Public static int getSize( long number )

//Return the first k number of digits from the number. If the number of digits in number is less than k, return number

Public static long getPrefix( long number, int k )

Here are some runs of the program

Enter a credit card number as a long integer

4388576018410707

4388576018410707 is a valid credit card number.

Enter a credit card number as a long integer

4388576018402626

4388576018402626 is a valid credit card number.

Explanation / Answer

Solution:

########CreditCardNumber.java#############

import java.util.Scanner;

public class CreditCardValidation {

public static void main(String args[]){

Scanner scan = new Scanner(System.in);

System.out.print("Enter a credit card number as a long integer ");

long creditCardNumber = scan.nextLong();

if (isValid(creditCardNumber)){

System.out.println(creditCardNumber + " is valid");

}

else{

System.out.println(creditCardNumber + " is not valid");

}

}

//Return if the card number is valid

public static boolean isValid(long number){

boolean validPrefix = (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 37) || prefixMatched(number, 6));

boolean validSize = getSize(number) >= 13 && getSize(number) <= 16;

boolean checksumIsValid = ((sumOfOddPlaces(number) + sumOfDoubleEvenPlace(number)) % 10 == 0);

return validPrefix && validSize && checksumIsValid;

}

//Get the result from step 2

public static int sumOfDoubleEvenPlace(long number){

int sum = 0;

number /= 10;

for (int i = 2; number > 0; i+=2){

sum += getDigit((int)(2*(number % 10)));

number /= 100;

}

return sum;

}

//Return this number it is a single digit, otherwise, return the sum of the two //digits

public static int getDigit(int number){

if (number > 9){

return (number / 10) + (number % 10);

}

return number;

}

//Return sum of sumOfOddPlace( long number )

public static int sumOfOddPlaces(long number){

int sum = 0;

for (int i = 1; number > 0; i+=2){

sum += number % 10;

//getDigit((int)(number % 10));

number /= 100;

}

return sum;

}

public static boolean prefixMatched(long number, int d){

return getPrefix(number, getSize(d)) == d;

}

//Return the number of digits in digit

public static int getSize(long d){

int size = 0;

for (long i = d; i > 0; i /= 10, size++){}

return size;

}

//Return the first k number of digits from the number. If the number of digits in number is less than k, return number

public static long getPrefix(long number, int k){

if (getSize(number) < k){

return number;

}

return (long)(number / Math.pow(10, (getSize(number) - k)));

}

}

Sample Run 1:

Enter a credit card number as a long integer
4388576018410707
4388576018410707 is valid

Enter a credit card number as a long integer
4388576018402626
4388576018402626 is not valid

Note: If you have any query, do discuss below befor egiving any negative feedback. if you liked the solution then don't forget to hit thumbs up.

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