From 00792b24e4d7cb70b7a7cc75480c0d08b2edc8ef Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Mon, 14 Nov 2022 21:14:43 -0600 Subject: [PATCH] Work on MP4 --- .../mp4_calebfontenot/MP4_CalebFontenot.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java b/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java index cbc0f9e..fbaa279 100644 --- a/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java +++ b/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java @@ -24,12 +24,11 @@ public class MP4_CalebFontenot { int sumResult = MP4_CalebFontenot.sum(MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(CreditCard), MP4_CalebFontenot.addOddNumbersFromRightToLeft(CreditCard)); int divisionResult = sumResult % 10; if (debug) { - System.out.println(sumResult + ", " + divisionResult); + System.out.println(sumResult + ", " + divisionResult); } if (divisionResult == 0) { - return true; - } - else { + return true; + } else { return false; } } @@ -57,19 +56,22 @@ public class MP4_CalebFontenot { if (debug) { System.out.println("Now executing addOddNumbersFromRightToLeft()"); } - // Iterate through string + // 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) { + for (int i = 0; i <= (creditCardLength - 1); i++) { 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); + if (currentDigit % 2 != 0) { // Is this number an odd number? + sum += currentDigit; + if (debug) { + System.out.println(currentDigit); + System.out.println("sum: " + sum); + } } } + return sum; } @@ -89,8 +91,12 @@ public class MP4_CalebFontenot { int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0; //System.out.println(creditCard.length()); - for (int i = (creditCardLength -1); i >= 0; i = i - 2) { + for (int i = 0; i <= (creditCardLength - 1); i++){ currentDigit = Character.getNumericValue(creditCard.charAt(i)); // Parses current digit as an integer so we can do math on it + if (debug) { + System.out.println("current digit: " + currentDigit); + } + if (currentDigit % 2 == 0) { //Is this number an even number? 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. String workingString = Integer.toString(digitProduct); @@ -103,9 +109,11 @@ public class MP4_CalebFontenot { digitProduct = (tensDigit + onesDigit); } sum += digitProduct; - if (debug) { - System.out.println(digitProduct); - } + + } + if (debug) { + System.out.println("The sum is: " + sum); + } } return sum; } @@ -136,9 +144,9 @@ public class MP4_CalebFontenot { } else { validityString = "invalid"; } - + System.out.println("The credit card " + sampleCreditCard + " is " + validityString); - + /* String userInput = menu();