Complete MP4
This commit is contained in:
parent
51cb2422fd
commit
ef1576c36b
@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package mp4_calebfontenot;
|
package mp4_calebfontenot;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author caleb
|
* @author caleb
|
||||||
@ -19,8 +21,7 @@ public class MP4_CalebFontenot {
|
|||||||
* @param CreditCard
|
* @param CreditCard
|
||||||
* @return
|
* @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 sumResult = MP4_CalebFontenot.sum(MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(CreditCard), MP4_CalebFontenot.addOddNumbersFromRightToLeft(CreditCard));
|
||||||
int divisionResult = sumResult % 10;
|
int divisionResult = sumResult % 10;
|
||||||
if (debug) {
|
if (debug) {
|
||||||
@ -40,8 +41,7 @@ public class MP4_CalebFontenot {
|
|||||||
* @param step3 result form step 3
|
* @param step3 result form step 3
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int sum(int step1andStep2, int step3)
|
public static int sum(int step1andStep2, int step3) {
|
||||||
{
|
|
||||||
return step1andStep2 + step3;
|
return step1andStep2 + step3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +51,7 @@ public class MP4_CalebFontenot {
|
|||||||
* @param -creditCard the credit card number to be processed.
|
* @param -creditCard the credit card number to be processed.
|
||||||
* @return the sum of digits at odd position from right to left.
|
* @return the sum of digits at odd position from right to left.
|
||||||
*/
|
*/
|
||||||
public static int addOddNumbersFromRightToLeft(String creditCard)
|
public static int addOddNumbersFromRightToLeft(String creditCard) {
|
||||||
{
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
System.out.println("Now executing addOddNumbersFromRightToLeft()");
|
System.out.println("Now executing addOddNumbersFromRightToLeft()");
|
||||||
}
|
}
|
||||||
@ -61,7 +60,7 @@ public class MP4_CalebFontenot {
|
|||||||
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
||||||
//System.out.println(creditCard.length());
|
//System.out.println(creditCard.length());
|
||||||
|
|
||||||
for (int i = 0; i <= (creditCardLength - 1); i++) {
|
for (int i = (creditCardLength - 1); i >= 0; i--) {
|
||||||
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
||||||
if (currentDigit % 2 != 0) { // Is this number an odd number?
|
if (currentDigit % 2 != 0) { // Is this number an odd number?
|
||||||
sum += currentDigit;
|
sum += currentDigit;
|
||||||
@ -81,8 +80,7 @@ public class MP4_CalebFontenot {
|
|||||||
* @param -creditCard the credit card number to be processed.
|
* @param -creditCard the credit card number to be processed.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int doubleDigitsAndSumSingleDigits(String creditCard)
|
public static int doubleDigitsAndSumSingleDigits(String creditCard) {
|
||||||
{
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
System.out.println("Now executing doubleDigitsAndSumSingleDigits()");
|
System.out.println("Now executing doubleDigitsAndSumSingleDigits()");
|
||||||
}
|
}
|
||||||
@ -91,12 +89,12 @@ public class MP4_CalebFontenot {
|
|||||||
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0;
|
||||||
//System.out.println(creditCard.length());
|
//System.out.println(creditCard.length());
|
||||||
|
|
||||||
for (int i = 0; i <= (creditCardLength - 1); i++){
|
for (int i = (creditCardLength - 1); i >= 0; i--) {
|
||||||
|
if (i % 2 == 0) {
|
||||||
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it
|
||||||
if (debug) {
|
if (debug) {
|
||||||
System.out.println("current digit: " + currentDigit);
|
System.out.println("current digit: " + currentDigit);
|
||||||
}
|
}
|
||||||
if (currentDigit % 2 == 0) { //Is this number an even number?
|
|
||||||
digitProduct = currentDigit * 2;
|
digitProduct = currentDigit * 2;
|
||||||
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.
|
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);
|
String workingString = Integer.toString(digitProduct);
|
||||||
@ -109,12 +107,12 @@ public class MP4_CalebFontenot {
|
|||||||
digitProduct = (tensDigit + onesDigit);
|
digitProduct = (tensDigit + onesDigit);
|
||||||
}
|
}
|
||||||
sum += digitProduct;
|
sum += digitProduct;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (debug) {
|
if (debug) {
|
||||||
System.out.println("The sum is: " + sum);
|
System.out.println("The sum is: " + sum);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,21 +121,42 @@ public class MP4_CalebFontenot {
|
|||||||
*
|
*
|
||||||
* @return a string with credit card number or 0.
|
* @return a string with credit card number or 0.
|
||||||
*/
|
*/
|
||||||
public static String menu()
|
public static String menu() {
|
||||||
|
String userInput = "";
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
|
||||||
|
while (!userInput.equals("0"))//user has not entered Q/q
|
||||||
{
|
{
|
||||||
return "creditcard number or 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();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
// DEBUG
|
|
||||||
//System.out.println(isValid("4388576018402626"));
|
|
||||||
String sampleCreditCard = "4388576018410707";
|
String sampleCreditCard = "4388576018410707";
|
||||||
boolean creditCardValidity = MP4_CalebFontenot.isValid(sampleCreditCard);
|
String userInput = menu();
|
||||||
|
if (userInput == "0") { // Stop program execution if credit card not entered
|
||||||
|
System.out.println("Credit card not entered!");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean creditCardValidity = MP4_CalebFontenot.isValid(userInput);
|
||||||
String validityString;
|
String validityString;
|
||||||
if (creditCardValidity) {
|
if (creditCardValidity) {
|
||||||
validityString = "valid";
|
validityString = "valid";
|
||||||
@ -145,19 +164,7 @@ public class MP4_CalebFontenot {
|
|||||||
validityString = "invalid";
|
validityString = "invalid";
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("The credit card " + sampleCreditCard + " is " + validityString);
|
System.out.println("The credit card " + userInput + " is " + validityString);
|
||||||
|
|
||||||
/*
|
|
||||||
String userInput = menu();
|
|
||||||
|
|
||||||
while (!userInput.equals("0") )//user has not entered Q/q
|
|
||||||
{
|
|
||||||
MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(userInput);
|
|
||||||
//complete the code
|
|
||||||
userInput = menu();
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user