/home/caleb/ASDV-Java/Assignments/MP4_CalebFontenot/src/mp4_calebfontenot/MP4_CalebFontenot.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mp4_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
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) {
        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. 37 + 38 = 75
     *
     * @param step1andSep2 result form step 1 and step 2
     * @param step3 result form step 3
     * @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. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
     *
     * @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) {
        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 = (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;
                if (debug) {
                    System.out.println(currentDigit);
                    System.out.println("sum: " + sum);
                }
            }
        }

        return sum;
    }

    /**
     * Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 Step 2 Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
     *
     * @param -creditCard the credit card number to be processed.
     * @return
     */
    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, sum = 0;
        //System.out.println(creditCard.length());

        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("current digit: " + currentDigit);
                }
                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;
            }

        }
        if (debug) {
            System.out.println("The sum is: " + sum);
        }
        return sum;
    }

    /**
     * Displays Q/q or C/c. If the user enters C, it read the credit number and returns it as String. If the user types Q/q returns 0
     *
     * @return a string with credit card 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) {

        //String sampleCreditCard = "4388576018410707";
        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";
        } else {
            validityString = "invalid";
        }

        System.out.println("The credit card " + userInput + " is " + validityString);
    }

}