Question

In: Computer Science

The following algorithm is widely used for checking whether a credit or debit card number has...

The following algorithm is widely used for checking whether a credit or debit card number has been entered correctly on a website. It doesn't guarantee that the credit card number belongs to a valid card, but it rules out numbers which are definitely not valid. Here are the steps:

  1. Check that the long number has exactly 16 digits. If not, the long number is not valid.
  2. If the long number has 16 digits, drop the last digit from the long number, as this is the "check digit" that we want to check against.  
  3. Multiply by 2 the value of each digit starting from index 0 and then at each even index. In each case, if the resulting value is greater than 9, subtract 9 from it. Leave the values of the digits at the odd indexes unchanged.
  4. Add all the new values derived from the even indexes to the values at the odd indexes and call this S.
  5. Find the number that you would have to add to S to round it up to the next highest multiple of 10. Call this C. If C equals the check digit, then the long number could be valid.

Example

Here is a worked example using the long number "4916592478445662".

  1. There are exactly 16 digits.
  2. So, create a new string including the first fifteen digits of the long number, i.e. "491659247844566" and note that the dropped digit, 2, is the check digit.
  3. Multiply by 2 the value of each remaining digit starting from index 0 and then each even index. In each case, if the resulting value is greater than 9, subtract 9 from it (which reduces larger values to a single digit). Leave the values of the digits at the odd indexes unchanged.
longNumber index longNumber value at index values at even indexes multiplied by 2 Adjusted to a single digit by subtracting 9
0 4 8 8
1 9 9
2 1 2 2
3 6 6
4 5 10 1
5 9 9
6 2 4 4
7 4 4
8 7 14 5
9 8 8
10 4 8 8
11 4 4
12 5 10 1
13 6 6
14 6 12 3
  1. Find S by adding all the values in the last column:

    S = 8 + 9 + 2 + 6 + 1 + 9 + 4 + 4 + 5 + 8 + 8 + 4 + 1 + 6 + 3 = 78

  2. As S = 78, the next highest multiple of 10 is 80. To get from 78 to 80 you must add 2, so C = 2. As this equals the value of the last digit in the long number, the long number could be valid.

a- Write a public method called isCorrectLength() that takes no arguments and returns the boolean value true if the length of longNumber is 16 and false otherwise.

b - Write a public method firstFifteen() that returns a String consisting of the first fifteen characters in the longNumber.

c) - write a public method calculateCheckNumber() to find S and then use the expression S/10 * 10 + 10 – S to find C. The method should first create a string omitting the last digit of the longNumber and then find S by iterating through this string. (You may choose to do a separate iteration for the odd and even indexes, or you could do both in a single loop.)

Solutions

Expert Solution

Here's the Java Code for the same:

package javaProject;

import java.util.Stack;

public class Luhn {

        public static boolean isCorrectLength(long cardNo) {
                int len = 0;
                
                // loop to find the length
                while (cardNo > 0) {
                        len++;
                        cardNo /= 10;
                }
                
                // if valid length
                if (len == 16)
                        return true;
                
                // else
                return false;
        }
        
        public static String firstFifteen(long cardNo) {
                
                // discarding the last digit
                cardNo /= 10;
                
                // the first fifteen digits string to be returned
                String firstFifteenDigits = new String("" + cardNo);
                
                return firstFifteenDigits;
        }
        
        public static int calculateCheckNumber(long cardNo) {
                
                // get the first fifteen digits string
                String firstFifteen = firstFifteen(cardNo);

                int sum = 0, dig;
                
                // iterate over the string and calculate the sum
                for (int i = 0; i < firstFifteen.length(); ++i) {
                        
                        // if current digit position is even then double the digit and if it goes above 10 then subtract 9 from it & finally add to sum
                        if (i % 2 == 0) {
                                dig = 2 * (firstFifteen.charAt(i) % 48);
                                if (dig > 9)
                                        dig -= 9;
                                sum += dig;
                        }
                        // if position is odd simply add the current rightmost digit to the sum
                        else
                                sum += firstFifteen.charAt(i) % 48;

                }

                // return the required value
                return ((sum / 10) * 10 + 10 - sum);
                
        }
        
        public static void main(String[] args) {
                
                long cardNo = 4916592478445662L;
                
                // check for correct length
                if (isCorrectLength(cardNo)) {
                        
                        // check if the last digit is equal to the check number or not and print a message accordingly
                        if (calculateCheckNumber(cardNo) == cardNo % 10)
                                System.out.println("Valid Card number!");
                        else System.out.println("Invalid Card number");
                }
                else System.out.println("Incorrect length of card number");

        }

}

Added comments in the code for explanation of each step, do refer them :). For any doubt, feel free to reach back.

Sample Output for the card number used in the code:

Hope it helps, consider hitting a like if it did :)

Cheers!


Related Solutions

Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position,...
Recording credit card and debit card sales Restaurants do a large volume of business with credit and debit cards. Suppose Summer,
Question Recording credit card and debit card sales Restaurants do a large volume of business with credit and debit cards. Suppose Summer,Sand, and Castles Resort restaurant had these transactions on January 28, 2018: National Express credit card sales $ 10,800 Value Card debit card sales 10,000. Requirements1. Suppose Summer, Sand, and Castles Resort’s processor charges a 2% fee and deposits sales net of the fee. Journalize these sales transactions for the restaurant.2. Suppose Summer, Sand, and Castles Resort’s processor charges...
Question What are two common methods used when accepting deposits for credit card and debit card transactions?
Question What are two common methods used when accepting deposits for credit card and debit card transactions?
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as error in a single digit or switching two digits. The following method is used to verify actual credit card number but, for simplicity, we will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
1. Identify whether each of the following is a debit or a credit in the country...
1. Identify whether each of the following is a debit or a credit in the country of Freedonia’s BOP and indicate where the item would be classified. a. Freedonian firms export $250 million worth of goods. b. Freedonian citizen’s purchase $50 million worth of tickets on US Airways flights. c. A Freedonian firm purchases a shoe factory in Mexico for $30 million. d. Freedonia receives $5 million in foreign aid from the United States. e. Freedonian citizens deposit $15 million...
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
should the cashless debit card (the card used as a means of distributing welfare payments) be...
should the cashless debit card (the card used as a means of distributing welfare payments) be applied or implemented to all the recipients of government payments, like students, pensioners? please explain
Define money and discuss its various functions. Is debit card money? Credit Card? Why? Why not?...
Define money and discuss its various functions. Is debit card money? Credit Card? Why? Why not? Explain.
Indicate whether Paid-in Capital in Excess of Par is a debit or credit account and whether...
Indicate whether Paid-in Capital in Excess of Par is a debit or credit account and whether it is closed or not closed each period. Select one: a. Debit, closed b. Debit, not closed c. Credit, closed d. Credit, not closed The coupon (stated) rate of interest is used to determine: Select one: a. The discount or premium recorded when the bond is issued. b. The amount of cash interest the bond issuer pays to the investor on the interest payment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT