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

Input The application\'s input will be stored in a file called creditcardnumbers

ID: 3792113 • Letter: I

Question

Input

The application's input will be stored in a file called creditcardnumbers.txt. This file will be read from the application's current directory. When the data file does not exist in the application's current directory, a message is displayed on the screen and the program will end. When the data file exists but does not contain any data, a message is displayed on the screen and the program will end.

For output specifications, reference the Output section of this document.

Input File Structure

The records within the data file contain one field (credit card number). Each line in the file contains a single record.

Example:

Processing

For each record in the data file validate the credit card number.

When an accepted credit card number is valid, print a message to the screen indicating the number is valid.

When an accepted credit card number is invalid (see Data Validation), print an invalid message to the screen. The invalid message will include a description of the validation error. Only one invalid message is display per credit card number.

Note: Considering an accepted credit card number could have an incorrect number of digits and incorrect check digit, the program should only print a single invalid message.

When a credit card number is not an accepted credit card number, it is considered rejected. No message is printed for rejected credit card numbers.

When the file has been completely processed, print a report of processing statistics.

Processing statistics include:

Total records in the file

Count of valid accepted credit card numbers

Count of invalid accepted credit card numbers

Count of rejected credit card numbers

For output specifications, reference the Output section of this document.

Data Validation

The following table outlines accepted credit card characteristics:

Credit card numbers are validated based on the following rules (in order):

Number starts with the digits of an accepted card type

Number is the correct number of digits for the card type

Check digit is correct

Credit card numbers that do not start with the digits of an accepted card type are considered rejected.

Check Digit Algorithm

An accepted credit card with the proper amount of digits must end with a specific digit called the check digit. The algorithm for verifying the check digit is as follows:

Drop the last digit from the card number. The last digit is the check digit.

Reverse the digits.

Multiply the digits in odd positions (1, 3, 5, etc.) by 2.

Subtract 9 from any result higher than 9.

Sum all the digits.

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)

Example:

Output

Data file does not exists:

Format:

Example:

Data file has no data:

Format:

Example:

Valid Credit Card Number

Format:

Sample:

Invalid Credit Card Number

Format:

Samples:

Note: Each valid/invalid message is printed on its own line with no blank lines between them.

Processing Statistics

Format:

Note: numbers are right aligned.

Sample:

Type Number of digits Starts with Mastercard 16 - 19 51 - 55 Visa 13 - 16 4 American Express 15 34, 37

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class VerifyCards {


   public static void main(String[] arg) {

       String filepath = "C:\Users\Desktop\ABC.txt"; //Please Provide file path for the File in given format
       String line;
       int count=0;
       int countVisaValid = 0;
       int countMasterValid = 0;
       int countAmexValid = 0;
       int countVisaInvalid = 0;
       int countMasterInvalid = 0;
       int countAmexInvalid = 0;
       boolean emptyFile = false;

       try {
           FileReader file = new FileReader(filepath);
           BufferedReader br = new BufferedReader(file);

           while ((line = br.readLine()) != null) {
               count++;
               emptyFile = true;
                  
               switch (checkCardTypeDigit(line)) {
               case "Mastercard":
                   if (checkDigit(line))
                       countMasterValid++;
                   else
                       countMasterInvalid++;
                   break;
               case "Visa":
                   if (checkDigit(line))
                       countVisaValid++;
                   else
                       countVisaInvalid++;
                   break;
               case "American Express":
                   if (checkDigit(line))
                       countAmexValid++;
                   else
                       countAmexInvalid++;
                   break;
               default:
                   break;
               }

           }
           if(!emptyFile)
               throw new Exception();
           br.close();
       } catch (FileNotFoundException e) {
          
           System.out.println(filepath.substring(filepath.lastIndexOf("\") + 1, filepath.length()) + " does not exist.");
      
       } catch (IOException e) {
          
           System.out.println("File IO Exceptioin.");
      
       } catch (Exception e) {
          
           System.out.println(filepath.substring(filepath.lastIndexOf("\") + 1, filepath.length()) + " is empty.");
      
       }
      
       System.out.println("Records processed: " + (count));
       System.out.println("=======================");
       System.out.println("Mastercard: "+countMasterValid+" ("+countMasterInvalid+")");
       System.out.println("Visa: "+countVisaValid+" ("+countVisaInvalid+")");
       System.out.println("American Express: "+countAmexValid+" ("+countAmexInvalid+")");
       System.out.println("Rejected: "+((count)-(countMasterValid+countMasterInvalid+countVisaValid+countVisaInvalid+countAmexValid+countAmexInvalid)));
   }

   public static boolean checkDigit(String ccNumber) {

       int[] digits = new int[ccNumber.length()];
       int reserve = Integer.parseInt(ccNumber.charAt(ccNumber.length()-1)+"");
       int index=0;
       for (int k = ccNumber.length()-2; k >= 0;k--){
           digits[index++]=Integer.parseInt(ccNumber.charAt(k)+"");
       }
      
       int sum = 0;

       for (int i = 0; i < digits.length; i += 2) {

           digits[i] *= 2;
           if (digits[i] > 9) {
               digits[i] -= 9;
           }
           sum += digits[i];
       }

       sum += reserve;

       if (sum % 10 == 0)
           return true;

       return false;
   }

   public static String checkCardTypeDigit(String card) {
       String cardType = "";

       if (card.matches("51.*|52.*|53.*|54.*|55") && card.length() <= 19 && card.length() >= 16) {
           cardType = "Mastercard";
       } else if (card.startsWith("4") && card.length() <= 16 && card.length() >= 13) {
           cardType = "Visa";
       } else if (card.matches("34.*|37") && card.length() == 15) {
           cardType = "American Express";

       }
       return cardType;
   }

}