MAIN METHOD IS ALREADY PROVIDED AND CANT BE CHANGED ANY HELP
MAIN METHOD IS ALREADY PROVIDED AND CAN\'T BE CHANGED. ANY HELP WOULD BE AWESOME, THANKS!!
P.s. I know that i\'ve posted this before but I couldn\'t edit the other post to insert better pictures
1. Description
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. 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 to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. All credit card numbers are generated following this va- lidity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
Double every second digit from right to left. If the doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
2*2= 4
2*2= 4
4*2= 8
1*2= 2
6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4*2= 8
Now add all 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 card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Sum the results from Step 2 and Step 3. 37 + 38 = 75
If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.
Finish writing the CCValidator.java code given to you on ilearn. YOU ARE NOT ALLOWED to touch the main method. In the main method are tests to determine if all the code you have written is correct. Make Sure to name your Project CCValidator to make things easier. To make this assignment easier it may to implement the functions in the following order (since the test will execute in this order):
getSize getPrefix(code given) prefixMatched
1
getDigit sumOfOddPlace sumOfDoubleEvenPlace isValid
You are also welcome to write helper functions if you feel you need them. Following Methods can be used to write your program:
Example Output:
Solution
/** * Created by RahulPatidar on 17/11/16. */ public class CCValidator { public static boolean isValid(long number){ if((sumOfDoubleEvenPlace(number)+sumOfOddPlace(number))%10 == 0)return true; return false; } public static int sumOfDoubleEvenPlace(long number){ String strNum = Long.toString(number); int n = getSize(number); int i=2; int sum = 0; while(n-i>=0){ sum += getDigit(2*Character.getNumericValue(strNum.charAt(n-i))); i+=2; } return sum; } public static int getDigit(int number){ if(number<10)return number; return (number/10) + (number%10); } public static int sumOfOddPlace(long number){ String strNum = Long.toString(number); int n = getSize(number); int i=1; int sum = 0; while(n-i>=0){ sum += Character.getNumericValue(strNum.charAt(n-i)); i+=2; } return sum; } public static boolean prefixMatched(long number, int d){ String strNum = Long.toString(number); if(Character.getNumericValue(strNum.charAt(0))==d)return true; return false; } public static int getSize(long d){ return (Long.toString(d).length()); } public static long getPrefix(long number, int k){ int size =getSize(number); if(size<=k)return number; int remove_count = size-k; while(remove_count>0){ number /= 10; remove_count--; } return number; } }
