You have been hired by a credit card company to test their software which genera
ID: 3817437 • Letter: Y
Question
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) 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. Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1+ 7 + 8 = 37 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 Sum the results from Step 2 and Step 3. 37 + 38 = 75 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.Explanation / Answer
//Jfilechooser is used to choose the file . modify the code as per your file location (output file only)
package JFileChooserDemo1;
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 java.util.Scanner;
/**
*
* @author lenovo
*/
public class JFileChooser extends javax.swing.JFrame {
private Object cc;
/**
* Creates new form java
*/
public JFileChooser() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
FileChooser = new javax.swing.JFileChooser();
jScrollPane1 = new javax.swing.JScrollPane();
textarea = new javax.swing.JTextArea();
jMenuBar2 = new javax.swing.JMenuBar();
File = new javax.swing.JMenu();
Open = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
FileChooser.setDialogTitle("This is my open dialog");
FileChooser.setFileFilter(new MyCustomFilter());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textarea.setColumns(20);
textarea.setRows(5);
jScrollPane1.setViewportView(textarea);
File.setText("File");
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
File.add(Open);
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
File.add(Exit);
jMenuBar2.add(File);
setJMenuBar(jMenuBar2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 164, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = FileChooser.showOpenDialog(this);
if (returnVal == FileChooser.APPROVE_OPTION) {
File file = FileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
BufferedReader br=new BufferedReader(new FileReader(file));
String str;
str = br.readLine();
long number = Long.parseLong(str);
System.out.println(number);
Card cc=new Card(number);
//cc.Card(number);
// long no= Long.valueOf(String number).longValue();
//textarea.read( new FileReader( file.getAbsolutePath() ), null );
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
class MyCustomFilter extends javax.swing.filechooser.FileFilter {
@Override
public boolean accept(File file) {
// Allow only directories, or files with ".txt" extension
return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
}
@Override
public String getDescription() {
// This description will be displayed in the dialog,
// hard-coded = ugly, should be done via I18N
return "Text documents (*.txt)";
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFileChooser().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenu File;
private javax.swing.JFileChooser FileChooser;
private javax.swing.JMenuItem Open;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textarea;
// End of variables declaration
}
class Card {
//public static void main(String[] args) {
long number;
public Card(long number) throws IOException{
number=number;
long total;
Scanner input = new Scanner(System.in);
//System.out.print("Please input your credit card number: ");
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
//out.write("aString this is a ttest");
//long number = input.nextLong();
total = sumOfEvenPlaces(number) + sumOfOddPlaces(number);
//System.out.print(total);
if(isValid(total)) {
// System.out.println("The length of your card number is: " + getSize(number));
System.out.println("This card is valid.");
out.write(toString(number));
cardtype(number);
}
else {
System.out.println("The length of your card number is: " + getSize(number));
System.out.println("Your card is invalid.");
}
}
public static boolean isValid(long total) {
if (total % 10 == 0) {
return true;
}
return false;
}
public static int sumOfEvenPlaces(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;
}
//System.out.println("Even= "+sum);
return sum;
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else if (number > 9)
return (number % 10 + number / 10);
return number;
}
public static int sumOfOddPlaces(long number) {
int sum = 0;
int remainder;
while(number % 10 != 0 || number / 10 != 0) {
remainder = (int)(number % 10);
sum = sum + remainder ;
number /= 100;
}
//System.out.println("odd "+sum);
return sum;
}
public static int getSize(long number) {
int len =0 ;
while (number >= 10) {
number /= 10;
len++;
}
return len+1;
}
public static void cardtype(long number) {
int f;
for(int i=0;i< (getSize(number)-1);i++){
number=number/10;
}
f = ((int)number)/1000000;
if(f==34 || f==37) {
System.out.println("American Express Card");
}
else{
int a=f/10;
switch(a){
case 4:
System.out.println("Visa Card");
break;
case 5:
System.out.println("Master Card");
break;
case 6:
System.out.println("Discover Card ");
break;
default:
System.out.println("Cannot find card type");
break;
}
}
}
Card() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private char[] toString(long number) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Output1:
creditno.txt
4388576018402626
output.txt
4388576018402626 Invalidcard
Output2:
creditno.txt
4388576018402621
output.txt
4388576018402626 validcard Visa Card
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.