MP4 progress
This commit is contained in:
@@ -11,15 +11,27 @@ package mp4_calebfontenot;
|
||||
*/
|
||||
public class MP4_CalebFontenot {
|
||||
|
||||
static boolean debug = true;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param CreditCard
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValid(String CreditCard) {
|
||||
|
||||
public static boolean isValid(String CreditCard)
|
||||
{
|
||||
int sumResult = MP4_CalebFontenot.sum(MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(CreditCard), MP4_CalebFontenot.addOddNumbersFromRightToLeft(CreditCard));
|
||||
int divisionResult = sumResult % 10;
|
||||
if (debug) {
|
||||
System.out.println(sumResult + ", " + divisionResult);
|
||||
}
|
||||
if (divisionResult == 0) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,8 +41,9 @@ public class MP4_CalebFontenot {
|
||||
* @param step3 result form step 3
|
||||
* @return
|
||||
*/
|
||||
public static int sum(int step1andSep2, int step3) {
|
||||
return step1andSep2 + step3;
|
||||
public static int sum(int step1andStep2, int step3)
|
||||
{
|
||||
return step1andStep2 + step3;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,8 +52,25 @@ public class MP4_CalebFontenot {
|
||||
* @param -creditCard the credit card number to be processed.
|
||||
* @return the sum of digits at odd position from right to left.
|
||||
*/
|
||||
public static int addOddNumbersFromRightToLeft(String creditCard) {
|
||||
return -1;
|
||||
public static int addOddNumbersFromRightToLeft(String creditCard)
|
||||
{
|
||||
if (debug) {
|
||||
System.out.println("Now executing addOddNumbersFromRightToLeft()");
|
||||
}
|
||||
// Iterate through string
|
||||
int creditCardLength = creditCard.length();
|
||||
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
||||
//System.out.println(creditCard.length());
|
||||
|
||||
for (int i = 0; i <= (creditCardLength -1); i = i + 2) {
|
||||
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
||||
sum += currentDigit;
|
||||
if (debug) {
|
||||
System.out.println(currentDigit);
|
||||
System.out.println("sum: " + sum);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,25 +79,35 @@ public class MP4_CalebFontenot {
|
||||
* @param -creditCard the credit card number to be processed.
|
||||
* @return
|
||||
*/
|
||||
public static int doubleDigitsAndSumSingleDigits(String creditCard) {
|
||||
public static int doubleDigitsAndSumSingleDigits(String creditCard)
|
||||
{
|
||||
if (debug) {
|
||||
System.out.println("Now executing doubleDigitsAndSumSingleDigits()");
|
||||
}
|
||||
// Iterate through string
|
||||
int creditCardLength = creditCard.length();
|
||||
int currentDigit, digitProduct, tensDigit, onesDigit;
|
||||
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
||||
//System.out.println(creditCard.length());
|
||||
|
||||
for (int i = 15; i >= 0; i = i - 2) {
|
||||
for (int i = (creditCardLength -1); i >= 0; i = i - 2) {
|
||||
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
||||
digitProduct = currentDigit * 2;
|
||||
System.out.println(digitProduct);
|
||||
if (digitProduct > 9) { // Product is larger than 9
|
||||
tensDigit = currentDigit / 10 % 10;
|
||||
onesDigit = currentDigit / 1 % 10;
|
||||
System.out.println("tensDigit is " + tensDigit);
|
||||
System.out.println("onesDigit is " + onesDigit);
|
||||
if (digitProduct > 9) { // Product is larger than 9. Typecast the integer back into a string so we can seperate the digits and add them together.
|
||||
String workingString = Integer.toString(digitProduct);
|
||||
tensDigit = Character.getNumericValue(workingString.charAt(0));
|
||||
onesDigit = Character.getNumericValue(workingString.charAt(1));
|
||||
if (debug) {
|
||||
System.out.println("tensDigit is " + tensDigit);
|
||||
System.out.println("onesDigit is " + onesDigit);
|
||||
}
|
||||
digitProduct = (tensDigit + onesDigit);
|
||||
}
|
||||
sum += digitProduct;
|
||||
if (debug) {
|
||||
System.out.println(digitProduct);
|
||||
}
|
||||
|
||||
}
|
||||
return -1;
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +115,8 @@ public class MP4_CalebFontenot {
|
||||
*
|
||||
* @return a string with credit card number or 0.
|
||||
*/
|
||||
public static String menu() {
|
||||
public static String menu()
|
||||
{
|
||||
return "creditcard number or 0";
|
||||
}
|
||||
|
||||
@@ -83,10 +124,21 @@ public class MP4_CalebFontenot {
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// DEBUG
|
||||
//System.out.println(isValid("4388576018402626"));
|
||||
MP4_CalebFontenot.doubleDigitsAndSumSingleDigits("4388576018402626");
|
||||
String sampleCreditCard = "4388576018410707";
|
||||
boolean creditCardValidity = MP4_CalebFontenot.isValid(sampleCreditCard);
|
||||
String validityString;
|
||||
if (creditCardValidity) {
|
||||
validityString = "valid";
|
||||
} else {
|
||||
validityString = "invalid";
|
||||
}
|
||||
|
||||
System.out.println("The credit card " + sampleCreditCard + " is " + validityString);
|
||||
|
||||
/*
|
||||
String userInput = menu();
|
||||
|
||||
|
Reference in New Issue
Block a user