Created
November 30, 2017 18:02
-
-
Save toriannnn/2ff2642179e7ee44bfb1096643d28e87 to your computer and use it in GitHub Desktop.
Check if a Credit Card is Valid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************************************************* | |
//Assignment: Assignment 4 | |
//Account: TAM154 | |
// | |
//Author: Tori Murray | |
// | |
// | |
//Honor Code: I pledge that this program represents my own program code. | |
// | |
//******************************************************************* | |
package validate; | |
//Import a scanner | |
import java.util.Scanner; | |
/** | |
* | |
* @author a_nog | |
*/ | |
public class Validate { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
//Add a scanner | |
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("\n" + input + " is Valid. "); | |
} else { | |
System.out.println("\n" + input + " is Invalid. "); | |
} | |
} | |
/** Return true if the card number is valid */ | |
//if the number in step 4 is divisible by 10, it is valid | |
//Sum of the results of step 2 and 3 divisible by 10=valid | |
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; | |
} | |
} | |
/** Return this number if it is a single digit, otherwise, return the sum of the two digits */ | |
public static int getDigit(int number) { | |
if (number <= 9) { | |
return number; | |
} else { | |
int firstDigit = number % 10; | |
int secondDigit = (int) (number / 10); | |
return firstDigit + secondDigit; | |
} | |
} | |
/** Get the result from Step 2 */ | |
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; | |
} | |
/** Return sum of odd-place digits in number */ | |
public static int sumOfOddPlace(long number) { | |
int result = 0; | |
while (number > 0) { | |
result += (int) (number % 10); | |
number = number / 100; | |
} | |
return result; | |
} | |
/** Return true if the digit d is a prefix for number */ | |
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("\nVisa Card "); | |
} else if (getPrefix(number, d) == 5) { | |
System.out.println("\nMaster Card "); | |
System.out.println("\nAmerican Express Card "); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** Return the number of digits in d */ | |
public static int getSize(long d) { | |
int count = 0; | |
while (d > 0) { | |
d = d / 10; | |
count++; | |
} | |
return count; | |
} | |
/** 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) { | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment