From ef1576c36b69a332c0780184baba6076a3b4854d Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Tue, 15 Nov 2022 09:15:59 -0600 Subject: [PATCH] Complete MP4 --- .../mp4_calebfontenot/MP4_CalebFontenot.java | 101 ++++++++++-------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java b/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java index fbaa279..11ed45a 100644 --- a/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java +++ b/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java @@ -5,6 +5,8 @@ */ package mp4_calebfontenot; +import java.util.Scanner; + /** * * @author caleb @@ -19,8 +21,7 @@ public class MP4_CalebFontenot { * @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) { @@ -40,8 +41,7 @@ public class MP4_CalebFontenot { * @param step3 result form step 3 * @return */ - public static int sum(int step1andStep2, int step3) - { + public static int sum(int step1andStep2, int step3) { return step1andStep2 + step3; } @@ -51,8 +51,7 @@ 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) - { + public static int addOddNumbersFromRightToLeft(String creditCard) { if (debug) { System.out.println("Now executing addOddNumbersFromRightToLeft()"); } @@ -61,7 +60,7 @@ public class MP4_CalebFontenot { int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0; //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 if (currentDigit % 2 != 0) { // Is this number an odd number? sum += currentDigit; @@ -81,8 +80,7 @@ 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()"); } @@ -91,30 +89,30 @@ public class MP4_CalebFontenot { int currentDigit, digitProduct, tensDigit, onesDigit, sum = 0; //System.out.println(creditCard.length()); - 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); - tensDigit = Character.getNumericValue(workingString.charAt(0)); - onesDigit = Character.getNumericValue(workingString.charAt(1)); + 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 if (debug) { - System.out.println("tensDigit is " + tensDigit); - System.out.println("onesDigit is " + onesDigit); + System.out.println("current digit: " + currentDigit); } - digitProduct = (tensDigit + onesDigit); + 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); + 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; } - sum += digitProduct; - + } if (debug) { System.out.println("The sum is: " + sum); } - } return sum; } @@ -123,21 +121,42 @@ public class MP4_CalebFontenot { * * @return a string with credit card number or 0. */ - public static String menu() - { - return "creditcard number or 0"; + public static String menu() { + String userInput = ""; + Scanner input = new Scanner(System.in); + + while (!userInput.equals("0"))//user has not entered Q/q + { + 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 */ - public static void main(String[] args) - { - // DEBUG - //System.out.println(isValid("4388576018402626")); + public static void main(String[] args) { + 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; if (creditCardValidity) { validityString = "valid"; @@ -145,19 +164,7 @@ public class MP4_CalebFontenot { validityString = "invalid"; } - System.out.println("The credit card " + sampleCreditCard + " is " + validityString); - - /* - String userInput = menu(); - - while (!userInput.equals("0") )//user has not entered Q/q - { - MP4_CalebFontenot.doubleDigitsAndSumSingleDigits(userInput); - //complete the code - userInput = menu(); - - } - */ + System.out.println("The credit card " + userInput + " is " + validityString); } }