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

Lab Exercise 08.2 Credit card validation USE JAVA Credit card numbers follow cer

ID: 3906360 • Letter: L

Question

Lab Exercise 08.2

Credit card validation

USE JAVA

Credit card numbers follow certain patterns, which is why many web sites can determine what type of card you have as soon as you enter the card number. A credit card number must have between 13 and 16 digits and it must start with:

4 for Visa cards

5 for Master cards

37 for American Express cards

6 for Discover cards

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

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

8?8*2=16 (1+6=7)

Now add all of the single-digit numbers from step 1.

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

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

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

Add the results from step 2 and step 3.

37 + 38 = 75

If the result from step 4 is evenly divisible by 10 (is a multiple of 10), the card number is valid. Otherwise it is invalid. In the example given above, 75 is not a multiple of 10 so the number is invalid.

This exercise involves writing a program that prompts the user to enter a credit card number as a String of digits. As the digits are entered, the program will display asterisks in place of the digits. In order to do this, your program will use the JPasswordField class for input.

Display whether the card is valid or invalid and the name of the organization that issued the card.

Your validation code will be contained in a class named CcValidate and will use the following methods:

Return TRUE if the card number is valid

public static boolean isValid(String number)

Get the result from step 2

public static int sumOfEvenPlace(String number)

Return this number if it is a single digit, otherwise

return the sum of the two digits

public static int getDigit(int number)

Return the sum of the odd-place digits in the number.

public static int sumOfOddPlace(String number)

Validate first digit of card number

public static Boolean isValidIssuer(String number)

Validate length of card number

public static Boolean isValidLength(String number)

Explanation / Answer

import java.io.*;
import java.util.*;

public class CC{


   public static boolean isValidLength(String a){
      if (a.length() < 13 || a.length() > 16)
         return false;
      else
         return true;
   }

   public static boolean isValidIssuer(String a){
    
      if (a.charAt(0) != '4' && a.charAt(0) != '5' && a.charAt(0) != '6' && !a.substring(0,1).equals("37"))
         return false;
      else
         return true;
   }
   public static int getDigit(int num){
         if (num < 10)
            return num;
         else
            return num/10 + num % 10;
   }
   public static int sumOfOddPlaces(String a){
      int sum = 0;
      for (int i = 0; i<a.length(); i++){
          if (i % 2 != 0){
             int b = Character.getNumericValue(a.charAt(i));
             b = 2 * b;
             sum = sum + getDigit(b);
          }
      }
      return sum;
   }
   public static int sumOfEvenPlaces(String a){
      int sum = 0;
      for (int i = 0; i<a.length(); i++){
          if (i % 2 == 0){
             int b = Character.getNumericValue(a.charAt(i));
             sum = sum + b;
          }
      }
      return sum;
   }
   public static boolean isValid(String a){
        if (!isValidIssuer(a) || !isValidLength(a))
           return false;

       int oddsum = sumOfOddPlaces(a);
       int evensum = sumOfEvenPlaces(a);
       int sum = oddsum + evensum;
       if (sum % 10 == 0)
          return true;
       else
         return false;
   }

   public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the credit card number:");
        String cc = sc.nextLine();
        if (isValid(cc))
           System.out.println("It is a valid number");
        else
           System.out.println("It is not a valid number");
   }
}