/home/caleb/ASDV-Java/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java |
package mp4_calebfontenot;
import java.util.Scanner;
@author
public class MP4_CalebFontenot {
static boolean debug = false;
If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.
@param CreditCard
@return
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;
}
}
Sum the results from Steps 1, 2 and Step 3.
@param step1andSep2
@param step3
@return
public static int sum(int step1andStep2, int step3) {
return step1andStep2 + step3;
}
Add all digits in the odd places from right to left in the card number.
@paramcreditCard
@return
public static int addOddNumbersFromRightToLeft(String creditCard) {
if (debug) {
System.out.println("Now executing addOddNumbersFromRightToLeft()");
}
int creditCardLength = creditCard.length();
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
for (int i = (creditCardLength - 1); i >= 0; i--) {
currentDigit = Character.getNumericValue(creditCard.charAt(i));
if (currentDigit % 2 != 0) {
sum += currentDigit;
if (debug) {
System.out.println(currentDigit);
System.out.println("sum: " + sum);
}
}
}
return sum;
}
Double every second digit from right to left.
@paramcreditCard
@return
public static int doubleDigitsAndSumSingleDigits(String creditCard) {
if (debug) {
System.out.println("Now executing doubleDigitsAndSumSingleDigits()");
}
int creditCardLength = creditCard.length();
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
for (int i = (creditCardLength - 1); i >= 0; i--) {
if (i % 2 == 0) {
currentDigit = Character.getNumericValue(creditCard.charAt(i));
if (debug) {
System.out.println("current digit: " + currentDigit);
}
digitProduct = currentDigit * 2;
if (digitProduct > 9) {
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("The sum is: " + sum);
}
return sum;
}
Displays Q/q or C/c.
@return
public static String menu() {
String userInput = "";
Scanner input = new Scanner(System.in);
while (!userInput.equals("0"))
{
System.out.println("Press Q/q to quit");
System.out.println("Press C/c to enter a credit card");
userInput = input.next();
if (userInput.equalsIgnoreCase("q")) {
userInput = "0";
}
if (userInput.equalsIgnoreCase("c")) {
System.out.print("Please enter a credit card number: ");
userInput = input.next();
try {
Long.parseLong(userInput);
} catch(Exception e) {
System.out.println("Invalid input: String doesn't look like a credit card, it failed successfully convert into an integer.");
}
if (userInput.length() != 16) {
System.out.println("Invalid input: String doesn't look like a credit card, incorrect length.");
if (debug) {
System.out.println("length: " + userInput.length());
}
} else {
break;
}
}
}
return userInput;
}
@param args
public static void main(String[] args) {
String sampleCreditCard = "4388576018410707";
String userInput = menu();
if (userInput == "0") {
System.out.println("Credit card not entered!");
return;
}
boolean creditCardValidity = MP4_CalebFontenot.isValid(userInput);
String validityString;
if (creditCardValidity) {
validityString = "valid";
} else {
validityString = "invalid";
}
System.out.println("The credit card " + userInput + " is " + validityString);
}
}