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

I need help writing this program in JAVA, this is an introductory java course, s

ID: 3817752 • Letter: I

Question

I need help writing this program in JAVA, this is an introductory java course, so if possible, keep it as basic/simple as possible while still following the instructions. The output should look like the sample execution at the end of the problem.

You have been hired by a credit card company to test their software which generates credit card numbers for new members. The software produces a text file with a bunch of generated credit card numbers. Your task is to write a program which checks each credit card number in the file and determines whether or not the card number is valid.

Credit card numbers follow a certain pattern. The number must have between 13 and 16 digits. The number must start with:

4 for Visa Cards

5 for Master Cards

34 or 37 for American Express cards

6 for Discover Card cards

An algorithm exists to determine whether or not a card number is valid. The algorithm is as follows: (for this example we will use the card number 4388576018402626)

1. 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.
    4388576018402626
    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

2. Now add all single-digit numbers from Step 1.
    4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

3. Add all digits in the odd places from right to left in the card number.
    4388576018402626
    6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

4. Sum the results from Step 2 and Step 3.
    37 + 38 = 75

5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.
    (75 % 10 == 0) //this is false so the card is not valid.

Write a program that uses JFileChooser to allow the user to select the input file of credit card numbers. Process the file by determining which card numbers are valid and which are invalid. Display the results in an output file NOT the console. Your results should have the following format: the card number, company name (if the company can be identified), and whether or not the card was valid or invalid. The following are examples of what your .txt might look like.

To get credit for this assignment, you MUST make creative use of your own methods. Your program should be intuitively divided into an appropriate number of methods for this type of project. This program must strictly follow the ONE TASK PER METHOD rule.

Explanation / Answer

Hi,

Please see below the classes, sample inputs/outputs. Please comment for any queries/feedbacks.

Thanks,

Anita

CardChecker.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class CardChecker {

   public static void main(String [] args){
       //Reading the file
       String cardNumber;
       String cardName;
       JFrame parent = new JFrame();
       String content = "";

       JFileChooser fileChooser = new JFileChooser();
       fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
       int result = fileChooser.showOpenDialog(parent);
       if (result == JFileChooser.APPROVE_OPTION) {
           File selectedFile = fileChooser.getSelectedFile();
           System.out.println("Selected file: " + selectedFile.getAbsolutePath());
           BufferedReader br = null;
           FileReader fr = null;

           try {

               String sCurrentLine;
               br = new BufferedReader(new FileReader(selectedFile));
               while ((sCurrentLine = br.readLine()) != null) {
                   String [] input = sCurrentLine.split(" ");
                   cardName = input[0];
                   cardNumber = input[1];
                   boolean isValid = false;
                   content =content + " "+cardName+" "+cardNumber;
                   if(validateName(cardName,cardNumber) && (cardNumber.length()<=16) &&(cardNumber.length()>=13)){
                       if(validate(cardNumber)){
                           isValid = true;
                           content = content + " Valid";
                       }
                       else{
                           isValid = false;
                           content = content + " Invalid";
                       }
                   }
                   else{
                       isValid = false;
                       content = content + " Invalid";
                   }
                  
                  
               }

           } catch (IOException e) {

               e.printStackTrace();

           }
          
           //Writing results to output file
           writeToFile(content);
          
       }

      
   }

   public static boolean validate(String cardNum){
       String secondDigits = "";
       String oddDigits = "";
       int Two_x_currDigit_int = 0;
       int sumOfSecondDigits = 0;
       int sumOfOdddDigits = 0;
       int total=0;
       double result=0;
       boolean retVal =false;
       for(int i=cardNum.length()-1;i>=0;i--){
           oddDigits = oddDigits+cardNum.charAt(i);
           secondDigits = secondDigits +cardNum.charAt(i-1);
           i--;
       }

       for(int i=0;i<secondDigits.length();i++){
           Two_x_currDigit_int = 0;

           String currDigit = String.valueOf(secondDigits.charAt(i));
           String Two_x_currDigit= String.valueOf( Integer.valueOf(currDigit) *2);
           if(Two_x_currDigit.length() >1){
               for(int j=0; j<Two_x_currDigit.length();j++){
                   String currdig = String.valueOf(Two_x_currDigit.charAt(j));
                   Two_x_currDigit_int = Two_x_currDigit_int+ Integer.valueOf(currdig);
               }
           }
           else{
               Two_x_currDigit_int = Integer.valueOf(Two_x_currDigit);
           }
           sumOfSecondDigits = sumOfSecondDigits + Two_x_currDigit_int;
       }

       for(int i=0;i<oddDigits.length();i++){
           String currdig = String.valueOf(oddDigits.charAt(i));
           sumOfOdddDigits = sumOfOdddDigits + Integer.valueOf(currdig);
       }
       total = sumOfSecondDigits +sumOfOdddDigits;
       if(total % 10 ==0){
           retVal = true;
       }
       else{
           retVal = false;
       }
      
       return retVal;
   }

   //To validate the first digit of card number
   public static boolean validateName(String cardName, String cardNumber){
       boolean retVal=false;
       String firstDigit = String.valueOf(cardNumber.charAt(0));
       if("Visa".equalsIgnoreCase(cardName) && firstDigit.equalsIgnoreCase("4")){
           retVal = true;
       }
       else if("Master".equalsIgnoreCase(cardName) && firstDigit.equalsIgnoreCase("5")){
           retVal = true;
       }
       else if("Amex".equalsIgnoreCase(cardName) && firstDigit.equalsIgnoreCase("3")){
           String secDigit = String.valueOf(cardNumber.charAt(1));
           if("7".equalsIgnoreCase(secDigit) || "4".equalsIgnoreCase(secDigit)){
               retVal = true;
           }
       }
       else if("Discover".equalsIgnoreCase(cardName) && firstDigit.equalsIgnoreCase("6")){
           retVal = true;
       }
      
       return retVal;
   }
  
   public static void writeToFile(String content){
       BufferedWriter bw = null;
       FileWriter fw = null;

       try {


           fw = new FileWriter("outputCardnumber.txt");
           bw = new BufferedWriter(fw);
           bw.write(content);
           bw.flush();

           System.out.println("Done");

       } catch (IOException e) {

           e.printStackTrace();

       }
   }
}

cardnumberInput.txt

Visa 4388576018402626
Master 5388576018402626
Amex 3788576018402626
Discover 6388576018402626
Visa 8388576018402626
Master 4388576018402626
Amex 4388576018402626
Discover 4388576018402626

outputCardnumber.txt

Visa 4388576018402626 Invalid
Master 5388576018402626 Invalid
Amex 3788576018402626 Invalid
Discover 6388576018402626 Valid
Visa 8388576018402626 Invalid
Master 4388576018402626 Invalid
Amex 4388576018402626 Invalid
Discover 4388576018402626 Invalid