Question

In: Computer Science

Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company to contact without having to "look" at the card itself and see what the bank is on it.

Another key part of the credit card number standard is the check digit. The last digit of every credit card number is determined by the previous 15 digits through a simple mathematical formula known the Luhn Algorithm. The Lhun Algorithm can be used to check if a credit card number has been corrupted in its transmission between the vendor reading the card, and the bank which has the account. It can also be used to check to see if a credit card number is valid before transmitting it to the bank.

The Luhn Algorithm is described at the link above, but the basic idea is:

From the right-to-left, double the value of each digit that is in an even-numbered position (with the check-digit at position 1). If this doubling gives you a two-digit value for any of the numbers, then subtract 9 from the value (which is easier than adding the digits together but gets you the same result). Leave the odd-valued positions as is.
Sum together all of the values except the check digit.
Take the digit in the one's position of the sum. If the value of that digit is 0, then it stays as 0. If the value is greater than zero, subtract that value from 10. That value should be the check digit (note that the special case for 0 is required since "10" is not a single digit).
For example, suppose the card you want to validate is: 5457623898234113. In this case the check-digit is 3 and the remaining digits are the 15-digit account number. We can confirm that we likely have a good card number by validating the check digit as follows:

Position 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Original Value 5 4 5 7 6 2 3 8 9 8 2 3 4 1 1 3
Doubled value 10 10 12 6 18 4 8 2
Doubled-value adjusted 1 1 3 6 9 4 8 2
Sum of values = 67 1 4 1 7 3 2 6 8 9 8 4 3 8 1 2
Check digit is 3 (10 - 7 = 3)
You can read more about credit card numbers, as well as how the Luhn Algorithm is used in other areas of Computer Science, in this article from the Data Genetics blog (where the above example was taken from).

For this lab you will write a Java program that checks credit card strings to see if they are valid. Your program should first prompt the user to enter a string of numbers as a credit card number, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 16 characters in length. If the user enters a string that is not 16 characters in length, your program should print an error message and ask again for a valid string. Your program should use the Luhn Algorithm above to compute what the check digit should be and then compare it to the actual value in the provided string and report whether the credit card number is valid or wrong. If it is wrong, your program should report what the correct check digit should be for the input value. Your program should keep asking for new values until the user enters a blank line to quit the program.

Create a new project named LuhnAlgorithm and a new Java program in that project folder named LuhnAlgorithm.java for this project. You can find a selection of valid-but-fake credit card numbers courtesy of PayPal at this link. Change the check digit on any of them to get an invalid number (note that your code should only use the 16 digit numbers and does not have to account for any of the card numbers that have any number of digits other than 16).

NOTE: You do NOT need to use arrays to solve this problem - this problem can be solved just with nested loops. Solutions that use an array where it isn't needed will be penalized in two ways: First, you're making the problem much harder than it needs to be, and second there will be a point deduction for use of an unnecessary array in the solution.

Sample Output: This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.

Enter a credit card number (enter a blank line to quit): 5457623898234112
Check digit should be: 3
Check digit is: 2
Number is not valid.

Enter a credit card number (enter a blank line to quit): 5457623898234113
Check digit should be: 3
Check digit is: 3
Number is valid.

Enter a credit card number (enter a blank line to quit): 5555555555554
ERROR! Number MUST have exactly 16 digits.

Enter a credit card number (enter a blank line to quit): 5555555555554445
Check digit should be: 4
Check digit is: 5
Number is not valid.

Enter a credit card number (enter a blank line to quit):
Goodbye!
Note: You will need to convert characters in your string into integer values to make this project work. There are two ways to do this. The first is to use Integer.parseInt and substring to get a character value. The following lines of code would give you the integer value of the first character of the string input:

String s = input.substring(0,1);
int val = Integer.parseInt(s);
Another way to get a numeric value from a character is to use the method Character.getNumericValue. This takes a char value (not a String) and converts it to its correct integer value. The following lines of code would give the integer value of the first character of the String input:

char c = input.charAt(0);
int val = Character.getNumericValue(c);

Solutions

Expert Solution

/*****************************************LuhnAlgorithm.java*********************************/

import java.util.Scanner;


/**
* The Class LuhnAlgorithm.
*/
public class LuhnAlgorithm {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       String card;
       do {
           System.out.print("Enter a credit card number (enter a blank line to quit): ");
           card = scan.nextLine();
           if (card.isEmpty()) {
               System.out.println("Good Bye!");
               break;
           }
           while (card.length() != 16) {

               System.out.println("ERROR! Number MUST have exactly 16 digits.");
               System.out.print("\nEnter a credit card number (enter a blank line to quit): ");
               card = scan.nextLine();
           }
           int checkDigit = Integer.parseInt("" + card.charAt(15));
           System.out.println("Check digit should be: " + checkDigit);

           System.out.println("Check digit is: " + checkDigit(card));

           if (checkDigit == checkDigit(card)) {

               System.out.println("Number is valid.");
           } else {

               System.out.println("Number is not valid.");
           }
       } while (!card.equalsIgnoreCase(""));

       scan.close();
   }

   /**
   * Check digit.
   *
   * @param card the card
   * @return the int
   */
   private static int checkDigit(String card) {

       int sum = 0;
       for (int i = 0; i < card.length() - 1; i++) {

           int val = Integer.parseInt("" + card.charAt(i));
           if ((i) % 2 == 0) {

               val = 2 * val;
               if (val >= 10) {

                   sum += (val % 10 + 1);
               } else {

                   sum += val;
               }
           } else {

               sum += val;
           }
       }

       return (10 - sum % 10);

   }

}
/************************output***************************/

Enter a credit card number (enter a blank line to quit): 5457623898234112
Check digit should be: 2
Check digit is: 3
Number is not valid.
Enter a credit card number (enter a blank line to quit): 5457623898234113
Check digit should be: 3
Check digit is: 3
Number is valid.
Enter a credit card number (enter a blank line to quit): 5555555555554
ERROR! Number MUST have exactly 16 digits.

Enter a credit card number (enter a blank line to quit): 5555555555554445
ERROR! Number MUST have exactly 16 digits.

Enter a credit card number (enter a blank line to quit): 5555555555554445
Check digit should be: 5
Check digit is: 4
Number is not valid.
Enter a credit card number (enter a blank line to quit):
Good Bye!

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Credit Cards Credit card numbers follow patterns. Cards must have 13 - 16 digits (inclusive) Card...
Credit Cards Credit card numbers follow patterns. Cards must have 13 - 16 digits (inclusive) Card numbers must start with the number(s): 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards Given that a number has satisfied the above criteria A and B the following steps are taken to validate the number: Double every second digit from right to left.  If doubling the digit results in a two-digit number, add the two digits...
Discussion Topic: You've learned a bit about credit cards, but many of us learn about them...
Discussion Topic: You've learned a bit about credit cards, but many of us learn about them the hard way. While sometimes people are forced into misusing credit cards due to circumstances beyond their control , sometimes the new availability of credit causes us to be less than prudent. Without divulging any personal information about anyone, share a story you've heard, know about, or researched (a search on credit card horror stories is usually very fruitful) about someone misusing credit cards...
Presently cyber  crimes related to used of credit cards is increasing. Banks issued the credit cards without...
Presently cyber  crimes related to used of credit cards is increasing. Banks issued the credit cards without much knowledge of financial position of the user. Later such people unable to pay the bill and  default. Therefore, Credit Cards should only be restricted to people with income higher than the national average. Please discuss.
Rattapoom is a boy that loves to play with numbers. He received digit cards as a...
Rattapoom is a boy that loves to play with numbers. He received digit cards as a present on his birthday, where he joyously showed it to his friends in the neighborhood. He received N cards of number, where each card contains a single digit. He wishes to arrange all his cards in such a way that the digits form a smallest possible N-digit number, and that N-digit number must not begin with 0. Given N digit cards that Rattapoom received,...
If you have never thought about starting a business or have no desire to own one,...
If you have never thought about starting a business or have no desire to own one, tell us why. Would you rather not have the responsibility or burden? Include the advantages of not starting a business and the cons about it.
What is your personal theory of nursing? If you have never thought about it until now,...
What is your personal theory of nursing? If you have never thought about it until now, just start somewhere. It doesn't need to be more than a couple of paragraphs long and there are no right or wrong answers.
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with all three digits different? c) How many of the numbers is part b) are odd?
Your friend said to you that "I thought accounting was all about numbers and columns, but...
Your friend said to you that "I thought accounting was all about numbers and columns, but your AIS class sounds different than that." Explain to your friend why the study of accounting information systems is important for future accountants.
Your friend said to you that "I thought accounting was all about numbers and columns, but...
Your friend said to you that "I thought accounting was all about numbers and columns, but your AIS class sounds different than that." Explain to your friend why the study of accounting information systems is important for future accountants.
You draw two cards from a standard deck of 52 cards, but before you draw the...
You draw two cards from a standard deck of 52 cards, but before you draw the second card, you put the first one back and reshuffle the deck. (Round your answer to three decimal places) 1) Find P(Ace on first card and Red card on second card) 2) Find P(Ace and King in either order) 3) If you do not replace the first card before drawing the second card, Find P(Ace on first card and King on second card)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT