Question

Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

Validating Credit Card Numbers

Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book)

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

  • 4 for Visa cards
  • 5 for Master cards
  • 6 for Discover cards
  • 37 for American Express cards

The algorithm for determining whether a card number is entered correctly as developed by Hans Luhn of IBM in 1954. The Luhn check or Mod 10 check works as follows. Consider the card number 5842016792622547.

  1. Double every second digit from right to left. If the doubling of a digit results in a two-digit number, add up the digits to get a single-digit number. The digits are shown in bold and italic here:

    5842016792622547

    This gives us (from right to left):

    • 4 * 2 = 8
    • 2 * 2 = 4
    • 6 * 2 = 12 → (1 + 2) = 3
    • 9 * 2 = 18 → (1 + 8) = 9
    • 6 * 2 = 12 → (1 + 2) = 3
    • 0 * 2 = 0
    • 4 * 2 = 8
    • 5 * 2 = 10 → (1 + 0) = 1
  2. Add up those numbers (8 + 4 + 3 + 9 + 3 + 0 + 8 + 1 = 36)
  3. Now add up all the digits in the odd positions from right to left: (7 + 5 + 2 + 2 + 7 + 1 + 2 + 8 = 34)
  4. Add those two numbers (36 + 34 = 70). If this sum is divisible by 10, the number is valid. If it is not divisible by 10, the number is not valid.

Your program will prompt the user to enter a credit card number as a long integer and display whether the number is valid or not. Design your program to use the following methods:

/** Return true if the card number is valid */
public static boolean isValid(long number)

/** Return the sum of the doubled even-place digits */
public static int sumOfDoubleEvenPlace(long number)

/** Return the given number if it is a single digit,
 *  otherwise return the sum of the two digits */
public static int getDigit(int n)

/** Return the sum of the odd-place digits */
public static int sumOfOddPlace(long number)

/** Return the number of digits in the given number */
public static int getSize(long number)

/** Return the first k number of digits from number. If the
 *  number of digits in number is less than k, return number. */
public static long getPrefix(long number, int k)

You may also implement this function if you feel it is useful for your solution:

/** Return true if d is a prefix for number */
public static boolean prefixMatched(long number, int d)

Here is the result of running the program a few times:

Enter credit card number: 412345678
That is not a valid credit card number.
Enter credit card number: 5842016792428358
That is a valid credit card number.
Enter credit card number: 5782077482835719
That is not a valid credit card number.

Extra Challenges

Challenge 1: If the card is valid, display which type of card it is. For example:

Enter credit card number: 5842016792428358
That is a valid Master card number.

Challenge 2: Repeatedly ask for credit card numbers until the user just presses ENTER. (Hint: while)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;
class Creditcard{
   static int sw;
   public static void main(String args[]){
       Scanner s=new Scanner(System.in);
       String si[]={"Visa","Master","Discover","American Express"};
       while(true){
           System.out.print("Enter a credit card number:");
           String xi;
           xi=s.nextLine();
           if(xi.isEmpty()){
               break;
           }
           else{
               long x=Long.parseLong(xi);
               if(isvalid(x)){
                   if(sw==4){
                       System.out.println("That is a valid "+si[0]+"card number.");
                   }
                   else if(sw==5){
                       System.out.println("That is a valid "+si[1]+"card number.");
                   }
                   else if(sw==6){
                       System.out.println("That is a valid "+si[2]+"card number.");
                   }
                   else{
                       System.out.println("That is a valid "+si[3]+"card number.");
                   }
               }
               else{
                   System.out.println("That is not a valid credit card number.");
               }
           }
           System.out.println();
          
       }
   }
   public static boolean isvalid(long number){
       int i=getSize(number);
       long sa,sb;
       if(i>12 && i<17){
           if(prefixMatched(number,4) || prefixMatched(number,5) || prefixMatched(number,6) || prefixMatched(number,37)){
               sa=sumOfDoubleEvenPlace(number);
               sb=sumOfOddPlace(number);
               if((sa+sb)%10==0){
                   return true;
               }
               else{
                   return false;
               }
           }
           else{
               return false;
           }          
       }
       else{
           return false;
       }
   }
   public static long sumOfDoubleEvenPlace(long number){
       int i=1;
       long r,sum=0;
       while(number>0){
           if(i%2==0){
               r=number%10;
               sum=sum+getDigit(2*r);
           }          
           number=number/10;
           i++;
       }
       return sum;
   }
   public static long getDigit(long n){
       long s=0;
       if(n<10){
           return n;
       }
       else{
           while(n>0){
               s=s+n%10;
               n=n/10;
           }
           return s;
       }
   }
   public static int getSize(long number){
       int i=0;
       while(number>0){
           number=number/10;
           i++;
       }
       return i;
   }
   public static long sumOfOddPlace(long number){
       int i=1;
       long d,sum=0;
       while(number>0){
           if(i%2!=0){
               d=number%10;
               sum=sum+d;
           }          
           number=number/10;
           i++;
       }
       return sum;      
   }
   public static long getPrefix(long number,int k){
       long s=1,i;
       for(i=1;i<getSize(number)-1;i++){
               s=s*10;
       }
       if(k==1){
           return number/(s*10);
       }
       else{
           return number/(s-1)*10;
       }
   }
   public static boolean prefixMatched(long number, int d){
       int i=(int) getPrefix(number,getSize(d));
       if(i==d){
           sw=d;
           return true;
       }
       else{
           return false;
       }
   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit 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 to determine whether a card number is entered correctly or whether a credit card...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: 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 invalid To check the validity of the...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard 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 to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card...

    The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card number: 3056 9309 0259 04\n Checksum: 50\n Card status: VALID\n Credit card number: 3852 0000 0232 37\n Checksum: 40\n Card status: VALID\n Credit card number: 6011 1111 1111 1117\n Checksum: 30\n Card status: VALID\n Credit card number: 6011 0009 9013 9424\n Checksum: 50\n Card status: VALID\n Credit card number: 3530 1113 3330 0000\n Checksum: 40\n Card status: VALID\n Credit card number: 3566 0020 2036...

  • 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...

  • Consider the following credit card numbers and tell me if they are valid or not. If...

    Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4634 5163 4122 7983 b. 5321 4142 6379 4771 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 44? Give one of the valid credit card numbers.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT