In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine
Posted: Tue Jul 12, 2022 8:04 am
In 1954, Hans Luhn of IBM proposed analgorithm for validating credit card numbers. The algorithm isuseful to determine whether a card number is entered correctly orwhether a credit card is scanned correctly by a scanner. Creditcard numbers are generated following this validity check, commonlyknown as the Luhn check or the Mod 10check, which can be described as follows (forillustration, consider the card number 4388576018402626):
Credit card numbers follow certainpatterns. A credit card number must have between 13 and 16 digits.It must start with: 4 for Visa cards , 5 forMastercards , 37 for American Express cards, 6 forDiscover cards
Double every second digit from right to left. If doubling of adigit results in a two-digit number, add up the two digits to get asingle-digit number.
Now add all single-digit numbers from theprevious Step
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 Add all digits in the odd places from right to leftin the card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Sum the results from previous twoSteps.37 + 38 = 75
If the result from the previous Step is divisible by 10,the card number is valid; otherwise, it is invalid. For example,the number 4388576018402626 is invalid, but the number4388576018410707 is valid.
Your task is to write a program along with extensive JUnit tests.The program must prompt the user to enter a credit card number as along integer and display whether the number is valid orinvalid. Create a JUnit test file called“MyCreditCardTest.java” and a java file “CreditCard.java”. In yourJUnit test add the following seven stub methods. (Each one is anindividual unit test)
Write five test cases for each of theunit test methods that call the appropriate methods in the javafile. The unit tests should still fail at this point as you arewriting the unit test first. Write the seven methods inMyCreditCard.java you may only move onto the next implementationonce each unit test has been satisfied.
In the regular java file add in thefollowing stub methods
check you have completed the following.
Credit card numbers follow certainpatterns. A credit card number must have between 13 and 16 digits.It must start with: 4 for Visa cards , 5 forMastercards , 37 for American Express cards, 6 forDiscover cards
Double every second digit from right to left. If doubling of adigit results in a two-digit number, add up the two digits to get asingle-digit number.
Now add all single-digit numbers from theprevious Step
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 Add all digits in the odd places from right to leftin the card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Sum the results from previous twoSteps.37 + 38 = 75
If the result from the previous Step is divisible by 10,the card number is valid; otherwise, it is invalid. For example,the number 4388576018402626 is invalid, but the number4388576018410707 is valid.
Your task is to write a program along with extensive JUnit tests.The program must prompt the user to enter a credit card number as along integer and display whether the number is valid orinvalid. Create a JUnit test file called“MyCreditCardTest.java” and a java file “CreditCard.java”. In yourJUnit test add the following seven stub methods. (Each one is anindividual unit test)
Write five test cases for each of theunit test methods that call the appropriate methods in the javafile. The unit tests should still fail at this point as you arewriting the unit test first. Write the seven methods inMyCreditCard.java you may only move onto the next implementationonce each unit test has been satisfied.
In the regular java file add in thefollowing stub methods
check you have completed the following.