Credit card type and validation

Viewed 18118

I would like to run a program that can determine the validation and type of credit card number based of number entered. Compiler shows notification that there is an error in my coding but I cannot detect where is it. The program is also cannot be run. Below is the coding,

    import java.util.*;

    public class CreditCard {

        public static void main(String args[]) {
                String CType;(String number) {
                    if (number.startsWith("4"))
                        return "Visa";
                    else if (number.startsWith("5"))
                        return "MasterCard";
                    else if (number.startsWith("6"))
                        return "Discover";
                    else if (number.startsWith("37"))
                        return "American Express";
                    else
                        return "Unknown type";
                };
            Scanner input = new Scanner(System.in);

            System.out.println("Enter a credit card number: ");
            long number = input.nextLong();

            long total = sumOfEvenPlaces(number) + (sumOfOddPlaces(number)*2);

            if (isValid(total)) {
                System.out.println("The "+CType+" card number is valid");
            } else {
                System.out.println("The "+CType+" card number is invalid.");
            }
        }



        public static boolean isValid(long total) {
            if (total % 10 != 0) {
            } else {
                        return true;
                }
            return false;
        }

        public static int sumOfEvenPlaces(long number) {
            int sum = 0;
            int remainder;
            while (number % 10 != 0 || number / 10 != 0) {
                remainder = (int) (number % 10);

                sum = sum + getDigit(remainder * 2);
                number /= 100;
            }
            return sum;
        }

        public static int getDigit(int number) {
            if (number > 9) {
                return (number % 10 + number / 10);
            } 
            return number;
        }

        public static int sumOfOddPlaces(long number) {
            int sum = 0;
            int remainder;
            number /= 10;
            while (number % 10 != 0 || number / 10 != 0) {
                remainder = (int) (number % 10);
                sum = sum + getDigit(remainder * 2);
                number /= 100;
            }
            return sum;
        }
    }
2 Answers

This may be more along the lines of what you're trying to do:

    public static void main(final String args[])
    {
        String cType = null;

        System.out.println("Enter a credit card number: ");
        final Scanner input = new Scanner(System.in);
        final String cardNumber = input.next();

        if (cardNumber.startsWith("4"))
        {
            cType = "Visa";
        }
        else if (cardNumber.startsWith("5"))
        {
            cType =  "MasterCard";
        }
        else if (cardNumber.startsWith("6"))
        {
            cType =  "Discover";
        }
        else if (cardNumber.startsWith("37"))
        {
            cType =  "American Express";
        }
        else
        {
            cType =  "Unknown type";
        }

        final long total = sumOfEvenPlaces(Long.valueOf(cardNumber)) + (sumOfOddPlaces(Long.valueOf(cardNumber)) * 2);

        if (isValid(total))
        {
            System.out.println("The " + cType + " card number is valid");
        }
        else
        {
            System.out.println("The " + cType + " card number is invalid.");
        }
    }

On a stylistic note, CType should start with a lower case letter (e.g. cType). You'll have to experiment with the use of Scanner as well as I'm not sure my implementation will do what you're looking for.

Related