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: 3821657 • 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.

SAMPLE OUTPUT FILE

Explanation / Answer

public class Card
{
   long num;
   String type;
   String validity;
}

---------------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class ValidCards
{
  
   static JFileChooser fileDialog;
   static ArrayList<Card> cards=new ArrayList<Card>();
  
  
  
   public static void main(String args[])
   {
  
   readFile();
       generateValidity();
       writeFile();
      
   }
  
  
   public static void readFile() {
       if (fileDialog == null) // (fileDialog is an instance variable)
       fileDialog = new JFileChooser();
       fileDialog.setDialogTitle("Select File for Reading");
       fileDialog.setSelectedFile(null); // No file is initially selected.
       int option = fileDialog.showOpenDialog(fileDialog);
       // (Using "this" as a parameter to showOpenDialog() assumes that the
       // readFile() method is an instance method in a GUI component class.)
       if (option != JFileChooser.APPROVE_OPTION)
       return; // User canceled or clicked the dialog's close box.
       File selectedFile = fileDialog.getSelectedFile();
       Scanner in; // (or use some other wrapper class)
       try {
       FileReader stream = new FileReader(selectedFile); // (or a FileInputStream)
       in = new Scanner( stream );
       }
       catch (Exception e) {
       JOptionPane.showMessageDialog(fileDialog,
       "Sorry, but an error occurred while trying to open the file: " + e);
       return;
       }
       try {
         
           while(in.hasNext())
           {
               Card c=new Card();
               c.num=in.nextLong();
               c.type=in.next();
               in.nextLine();//reads end of line character
                 
               cards.add(c);
                 
           }
             
       }
       catch (Exception e) {
       JOptionPane.showMessageDialog(fileDialog,
       "Sorry, but an error occurred while trying to read the data: " + e);
       }
       finally {
       in.close();
       }
       }
  
   public static void writeFile()
   {
       PrintWriter out = null;
       try {
           out = new PrintWriter(new File("out.txt"));
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
      
       for(int i=0;i<cards.size();i++)
       {
           out.println(String.format("%d %s %s",cards.get(i).num,cards.get(i).type,cards.get(i).validity));
       }
      
      
   }
  
  
   public static void generateValidity()
   {
       for(int i=0;i<cards.size();i++)
       {
           cards.get(i).validity=isValid(cards.get(i));
       }
      
      
   }
  
  
   public static String isValid(Card c)
   {
       int[] digits=integerToDigits(c.num);
      
       //now implement your logic of checking
      
   }
  
   public static int[] integerToDigits(long number)
   {
   // = and int
       LinkedList<Integer> stack = new LinkedList<Integer>();
       while (number > 0) {
       stack.push( (int) (number % 10) );
       number = number / 10;
       }

       int a[]=new int[stack.size()];
       int i=0;
       while (!stack.isEmpty()) {
       a[i]=stack.pop();
       i++;
       }
      
   return a;
   }
  
}