Please help in java Credit card numbers follow certain patte
Please help in java:
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.
You are to write a program that tests whether a certain credit card number is valid.
Step 1. Double every second digit from RIGHT to LEFT. if doubling a digit results in a two digit number add the 2 digits to get a single digit number. Add all these single digit numbers. Write a method called sumOfDoubleEvenPlace to do this. This method takes a long as parameter and returns an int.
Step 2: Add all digits in the credit card number\'s odd places from right to left. Write a method called sumOfOddPlace to do this. This method takes a long as parameter and returns an int.
Step 3: sum these two results. If this result is divisible by 10 the card is valid.
To help you: 4388576018410707 is valid. Obviously numbers that start with 0,1,2,7,8,9,0 are invalid, and if it starts with 3 the next digit must be 7. Write a method called checkPrefix that checks this and returns either true or false.
Also write a method called getSize which checks whether the number is within the range 13-16 and returns true or false.
Solution
true
import java.util.Scanner;
public class CreditCardValidation {
public static boolean isValid(long number) {
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 ) && (getSize(number)<=16 )) {
return true;
} else {
return false;
}
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int) (number / 10);
return firstDigit + secondDigit;
}
}
public static int sumOfOddPlace(long number) {
int result = 0;
while (number > 0) {
result += (int) (number % 10);
number = number / 100;
}
return result;
}
public static int sumOfDoubleEvenPlace(long number) {
int result = 0;
long temp = 0;
while (number > 0) {
temp = number % 100;
result += getDigit((int) (temp / 10) * 2);
number = number / 100;
}
return result;
}
public static boolean prefixMatched(long number, int d) {
if ((getPrefix(number, d) == 4)
|| (getPrefix(number, d) == 5)
|| (getPrefix(number, d) == 3)) {
if (getPrefix(number, d) == 3) {
System.out.println(\"\ Visa Card \");
} else if (getPrefix(number, d) == 5) {
System.out.println(\"\ Master Card \");
} else if (getPrefix(number, d) == 3) {
System.out.println(\"\ American Express Card \");
}
return true;
} else {
return false;
}
}
public static int getSize(long d) {
int count = 0;
while (d > 0) {
d = d / 10;
count++;
}
return count;
}
public static long getPrefix(long number, int k) {
if (getSize(number) < k) {
return number;
} else {
int size = (int) getSize(number);
for (int i = 0; i < (size - k); i++) {
number = number / 10;
}
return number;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(\"Enter a credit card number as a long integer: \");
long input = sc.nextLong();
if (isValid(input) == true) {
System.out.println(\"\ \" + input + \" is Valid. \");
} else {
System.out.println(\"\ \" + input + \" is Invalid. \");
}
}
}



