Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For a project i have to make a java application to validate credit card numbers

ID: 3695836 • Letter: F

Question

For a project i have to make a java application to validate credit card numbers it should work like this:

(I understand the concept of what i need to do and i know ill need at least 3 classes. One to get the user imput, one to run the algorithm, and another to run the valid credit card numbers and get their credit card type. My main issue is knowing how i should read the file into the program, I know about basic file imput and output but I know the values after being read in from the file will have to be passed to the other classes. I am not sure how to achive this best, using an arraylist, or creating an object from the file, etc.)

Your task is to:

– Read the data listed below “Data to Evaluate” from a file.

– Evaluate each number to see if they are representing a possible credit card number.

– Validate each credit card number to see if they are a valid number.

– Store the valid numbers and invalid numbers in a separate array.

– Write the contents of validated credit card number array into a file called “valid_cards.txt”.

– Write the invalid credit card number array to a file called “invalid_numbers.txt”. – Make sure to include the issuer for each group of credit card numbers identified.

– Your application should work with any number of credit card entries.

I am required to

Get input from file.
Ask user at least a single question and read at least one answer.
Use at least a single method from Math class.
Use at least one overloaded constructor.
Use at least one if-else-if, while, for, and enhanced for loops.
Use methods to pass and return identifiers and objects.
Pass arrays to/from methods.
Use at least a method to sort the array in the project.
Use inheritance and polymorphism.
Use exception handling.
Use input validation.
Pass the input and output files as commandline arguments.

Here is the formula for the credit card validation

The Luhn Formula:

• Drop the last digit from the number. The last digit is what we want to check against

• Reverse the numbers

• Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9

• Add all the numbers together • The check digit (the last number of the card) is the amount that you would need to add to get a multiple of 10 (Modulo 10)

I understand the concept of what i need to do and i know ill need at least 3 classes. One to get the user imput, one to run the algorithm, and another to run the valid credit card numbers and get their credit card type. My main issue is knowing how i should read the file into the program, I know about basic file imput and output but I know the values after being read in from the file will have to be passed to the other classes. I am not sure how to achive this best, using an arraylist, or creating an object from the file, etc.

Here is the data to evalueate from a txt file, in the correct fromat it is just a straight down list

Data to Evaluate

3158539628375348 3337465828248404 3112804683572030 3112804683572033 5435528978467581 6706465468107999 6304362971054667 6706169762827894 6706169762827892 4844104341377697 4913405490886727 4844885754351829 4844885754351822 6371473570875275 6381475006869978 6389057917814806 347100734345549 347100734345543 6011162533805000 6011621663574413 6011824617460743 6011824617460745 6389057917814802 4539318768050385 36880982786892 36845793345501 36661154562232 36661154562234 5893329089846994 6761680165952016 6763100400984029 6763100400984022 5127043299122389 5330838017737340 5429168755154025 5429168755154023 375354034606481 379570632133224 4485521241443135 4532916206508075 4532916206508076 5590976687287124 5540641137519895 5540641137519892 30522070708059 30066552673241 30365487186802 30365487186801

Explanation / Answer

Hello there ,

I have a proposed solution below. Input files and 2 output files too. and console o/p is also shown.

Kindly let me know if you have any quereis. I have checked for 4 types of credit cards. You can add more cases if you want in getCardID.

Thanks.

=======

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Credit card validation App.
*
* @author dipal.prajapati
*
*/
public class CreditCardValidation {
   public static final int INVALID = -1;
   public static final int VISA = 0;
   public static final int MASTERCARD = 1;
   public static final int AMERICAN_EXPRESS = 2;
   public static final int EN_ROUTE = 4;
   public static final int DINERS_CLUB = 5;
   public static final int DISCOVER = 3;

   private static final String[] cardNames = { "Visa", "Mastercard", "American Express", "Discover" };

   /**
   * Valid a Credit Card number
   */
   public static boolean validCC(String number) throws Exception {
       int CardID;
       if ((CardID = getCardID(number)) != -1)
           return validCCNumber(number);
       return false;
   }

   /**
   * Get the Card type returns the credit card type INVALID = -1; VISA = 0;
   * MASTERCARD = 1; AMERICAN_EXPRESS = 2; DISCOVER = 3
   */
   public static int getCardID(String number) {
       int valid = INVALID;

       String digit1 = number.substring(0, 1);
       String digit2 = number.substring(0, 2);
       String digit3 = number.substring(0, 3);
       String digit4 = number.substring(0, 4);

       if (isNumber(number)) {
           /*
           * ----* VISA prefix=4* ---- length=13 or 16 (can be 15 too!?!
           * maybe)
           */
           if (digit1.equals("4")) {
               if (number.length() == 13 || number.length() == 16)
                   valid = VISA;
           }
           /*
           * ----------* MASTERCARD prefix= 51 ... 55* ---------- length= 16
           */
           else if (digit2.compareTo("51") >= 0 && digit2.compareTo("55") <= 0) {
               if (number.length() == 16)
                   valid = MASTERCARD;
           }

           /*
           * ----* DISCOVER card prefix = 60* -------- lenght = 16* left as an
           * exercise ...
           */
           else if (digit4.equals("6011")) {
               if (number.length() == 16)
                   valid = DISCOVER;
           }
           /*
           * ----* AMEX prefix=34 or 37* ---- length=15
           */
           else if (digit2.equals("34") || digit2.equals("37")) {
               if (number.length() == 15)
                   valid = AMERICAN_EXPRESS;
           }

       }

       return valid;

   }

   public static boolean isNumber(String n) {
       try {
           double d = Double.valueOf(n).doubleValue();
           return true;
       } catch (NumberFormatException e) {
           e.printStackTrace();
           return false;
       }
   }

   public static String getCardName(int id) {
       return (id > -1 && id < cardNames.length ? cardNames[id] : "");
   }

   public static boolean validCCNumber(String n) {
       try {
           /*
           * * known as the LUHN Formula (mod10)
           */
           int j = n.length();

           String[] s1 = new String[j];
           for (int i = 0; i < n.length(); i++)
               s1[i] = "" + n.charAt(i);

           int checksum = 0;

           for (int i = s1.length - 1; i >= 0; i -= 2) {
               int k = 0;

               if (i > 0) {
                   k = Integer.valueOf(s1[i - 1]).intValue() * 2;
                   if (k > 9) {
                       String s = "" + k;
                       k = Integer.valueOf(s.substring(0, 1)).intValue() + Integer.valueOf(s.substring(1)).intValue();
                   }
                   checksum += Integer.valueOf(s1[i]).intValue() + k;
               } else
                   checksum += Integer.valueOf(s1[0]).intValue();
           }
           return ((checksum % 10) == 0);
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }

   /**
   * It writes valid/Invalid card numbers to the file bases on the file name
   * given.
   *
   * @param filename
   * @param cardNums
   */
   public static void write(String filename, List<String> cardNums) {
       System.out.println("Writing Numbers to " + filename + "....");
       File file = new File(filename);
       // creates the file
       try {
           file.createNewFile();

           // creates a FileWriter Object
           FileWriter writer = new FileWriter(file);
           // Writes the content to the file
           for (String number : cardNums) {
               writer.append(number + " ");
           }
           writer.flush();
           writer.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

   /**
   * Read the file with the given space separated numbers in it.
   *
   * @param input
   * filename
   */
   public static void read(String filename) {
       System.out.println("Reading " + filename + "....");

       List<String> validCards = new ArrayList<String>();
       List<String> invalidNums = new ArrayList<String>();
       try {
           BufferedReader read = new BufferedReader(new FileReader(filename));
           String line = null;
           // ArrayList to maintain list of Valid and Invalid Card numbers.

           while ((line = read.readLine()) != null) {
               String[] var = line.split(" ");
               for (String cardNum : var) {

                   System.out.println("Card number : " + cardNum);
                   int cardType = getCardID(cardNum);
                   CreditCard card = null;
                   CardFactory cardFactory = new CardFactory();
                   if ((validCC(cardNum))) {

                       card = cardFactory.getCard(cardType, cardNum);
                       System.out.println("This is a " + getCardName(cardType) + " card. " + card.toString());
                       validCards.add(card.toString());
                   }

                   else
                       System.out.println("This card is invalid or unsupported!");
                   invalidNums.add(cardNum);

               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       write("valid_cards.txt", validCards);
       write("invalid_numbers.txt", invalidNums);
       System.out.println("Finished !");
   }

   /*
   * * For testing purpose** java CCUtils [credit card number] or java CCUtils
   * *
   */
   public static void main(String args[]) throws Exception {
       read("yourfile.txt");

   }
}

=====

/**
* Factory class to generate appropriate instance of Card.
* @author dipal.prajapati
*
*/
public class CardFactory {

   public CreditCard getCard(int cardType, String aCard) {
       CreditCard card = null;
       switch (cardType) {
       case 0:
           card = new VisaCC(aCard);
           break;
       case 1:
           card = new MasterCC(aCard);
           break;
       case 2:
           card = new AmExCC(aCard);
           break;
       case 3:
           card = new DiscoverCC(aCard);
           break;
       }
       return card;
   }
}

======

/**
* Represents Visa Credit Card.
* @author dipal.prajapati
*
*/
public class VisaCC extends CreditCard {

   public VisaCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "VisaCC ["+super.number+"]";
   }

}

======

/**
* Represents Master Credit Card.
* @author dipal.prajapati
*
*/
public class MasterCC extends CreditCard {

   public MasterCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "MasterCC ["+super.number+"]";
   }

}

======

/**
* Represents AmericanExpress Credit Card.
* @author dipal.prajapati
*
*/
public class AmExCC extends CreditCard {

   public AmExCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "AmExCC ["+super.number+"]";
   }

}

=====

/**
* Represents Discover Credit Card.
* @author dipal.prajapati
*
*/
public class DiscoverCC extends CreditCard {

   public DiscoverCC(String cardNumber) {
       super.number=cardNumber;
   }

   @Override
   public String toString() {
       return "DiscoverCC ["+super.number+"]";
   }

}

========yourfile.txt===== Input file having space separated nums

3158539628375348 3337465828248404 3112804683572030 3112804683572033 5435528978467581 6706465468107999 6304362971054667 6706169762827894 6706169762827892 4844104341377697 4913405490886727 4844885754351829 4844885754351822 6371473570875275 6381475006869978 6389057917814806 347100734345549 347100734345543 6011162533805000 6011621663574413 6011824617460743 6011824617460745 6389057917814802 4539318768050385 36880982786892 36845793345501 36661154562232 36661154562234 5893329089846994 6761680165952016 6763100400984029 6763100400984022 5127043299122389 5330838017737340 5429168755154025 5429168755154023 375354034606481 379570632133224 4485521241443135 4532916206508075 4532916206508076 5590976687287124 5540641137519895 5540641137519892 30522070708059 30066552673241 30365487186802 30365487186801

======valid_cards.txt======

MasterCC [5435528978467581]
VisaCC [4844104341377697]
VisaCC [4913405490886727]
VisaCC [4844885754351829]
AmExCC [347100734345549]
DiscoverCC [6011162533805000]
DiscoverCC [6011621663574413]
DiscoverCC [6011824617460743]
VisaCC [4539318768050385]
MasterCC [5127043299122389]
MasterCC [5330838017737340]
MasterCC [5429168755154025]
AmExCC [375354034606481]
AmExCC [379570632133224]
VisaCC [4485521241443135]
VisaCC [4532916206508075]
MasterCC [5590976687287124]
MasterCC [5540641137519895]

========invalid_numbers.txt======

3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6381475006869978
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
6011824617460743
6011824617460745
6389057917814802
4539318768050385
36880982786892
36845793345501
36661154562232
36661154562234
5893329089846994
6761680165952016
6763100400984029
6763100400984022
5127043299122389
5330838017737340
5429168755154025
5429168755154023
375354034606481
379570632133224
4485521241443135
4532916206508075
4532916206508076
5590976687287124
5540641137519895
5540641137519892
30522070708059
30066552673241
30365487186802
30365487186801

========Console Output=======

Reading yourfile.txt....
Card number : 3158539628375348
This card is invalid or unsupported!
Card number : 3337465828248404
This card is invalid or unsupported!
Card number : 3112804683572030
This card is invalid or unsupported!
Card number : 3112804683572033
This card is invalid or unsupported!
Card number : 5435528978467581
This is a Mastercard card. MasterCC [5435528978467581]
Card number : 6706465468107999
This card is invalid or unsupported!
Card number : 6304362971054667
This card is invalid or unsupported!
Card number : 6706169762827894
This card is invalid or unsupported!
Card number : 6706169762827892
This card is invalid or unsupported!
Card number : 4844104341377697
This is a Visa card. VisaCC [4844104341377697]
Card number : 4913405490886727
This is a Visa card. VisaCC [4913405490886727]
Card number : 4844885754351829
This is a Visa card. VisaCC [4844885754351829]
Card number : 4844885754351822
This card is invalid or unsupported!
Card number : 6371473570875275
This card is invalid or unsupported!
Card number : 6381475006869978
This card is invalid or unsupported!
Card number : 6389057917814806
This card is invalid or unsupported!
Card number : 347100734345549
This is a American Express card. AmExCC [347100734345549]
Card number : 347100734345543
This card is invalid or unsupported!
Card number : 6011162533805000
This is a Discover card. DiscoverCC [6011162533805000]
Card number : 6011621663574413
This is a Discover card. DiscoverCC [6011621663574413]
Card number : 6011824617460743
This is a Discover card. DiscoverCC [6011824617460743]
Card number : 6011824617460745
This card is invalid or unsupported!
Card number : 6389057917814802
This card is invalid or unsupported!
Card number : 4539318768050385
This is a Visa card. VisaCC [4539318768050385]
Card number : 36880982786892
This card is invalid or unsupported!
Card number : 36845793345501
This card is invalid or unsupported!
Card number : 36661154562232
This card is invalid or unsupported!
Card number : 36661154562234
This card is invalid or unsupported!
Card number : 5893329089846994
This card is invalid or unsupported!
Card number : 6761680165952016
This card is invalid or unsupported!
Card number : 6763100400984029
This card is invalid or unsupported!
Card number : 6763100400984022
This card is invalid or unsupported!
Card number : 5127043299122389
This is a Mastercard card. MasterCC [5127043299122389]
Card number : 5330838017737340
This is a Mastercard card. MasterCC [5330838017737340]
Card number : 5429168755154025
This is a Mastercard card. MasterCC [5429168755154025]
Card number : 5429168755154023
This card is invalid or unsupported!
Card number : 375354034606481
This is a American Express card. AmExCC [375354034606481]
Card number : 379570632133224
This is a American Express card. AmExCC [379570632133224]
Card number : 4485521241443135
This is a Visa card. VisaCC [4485521241443135]
Card number : 4532916206508075
This is a Visa card. VisaCC [4532916206508075]
Card number : 4532916206508076
This card is invalid or unsupported!
Card number : 5590976687287124
This is a Mastercard card. MasterCC [5590976687287124]
Card number : 5540641137519895
This is a Mastercard card. MasterCC [5540641137519895]
Card number : 5540641137519892
This card is invalid or unsupported!
Card number : 30522070708059
This card is invalid or unsupported!
Card number : 30066552673241
This card is invalid or unsupported!
Card number : 30365487186802
This card is invalid or unsupported!
Card number : 30365487186801
This card is invalid or unsupported!
Writing Numbers to valid_cards.txt....
Writing Numbers to invalid_numbers.txt....
Finished !

Let me know if you have any quereis.

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote