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

part 2: Re-write your program so that it reads card numbers from a file rather t

ID: 3684722 • Letter: P

Question

part 2: Re-write your program so that it reads card numbers from a file rather than asking for user input. A file is provided on Pilot for your use. Be sure to use a try..catch in case the file does not open properly. Do not assume the test file used when grading your project will be the same length as the provided file. Be sure to read and process card numbers until the end of file is reached. You may assume that each card number is on a separate line within the input file. If a card number does not contain all digits, simply output “[cardNumber] card cannot be processed.” Do not perform any additional checks. Modify your code further so that the output is displayed to a separate file. Display “Program finished” to the console at the end of your program.

here is the program that I need re-written for part 2 above.

Scanner sc = new Scanner(System.in);
String card = "";
while (true) {
System.out.println("Please enter the credit card number:");
String s = sc.nextLine();
if (is_digit(s) == true) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
card += s.charAt(i);
}
}
System.out.println("credit card number is:" + card);
break;
}
System.out.println("The credit card number must contain only the digits 0-9");
}
String temp = card.substring(0, 2);
if (card.charAt(0) == '4') {
System.out.println("The card type is : VisaCard");
if (card.length() == 13 || card.length() == 16) {
if (check_sum(card) == true) {
System.out.println("Card is Valid");
} else {
System.out.println("Card is Invalid");
}
} else {
System.out.println("Card is Invalid");
}
} else if (temp.compareTo("51") == 0 || temp.compareTo("52") == 0 || temp.compareTo("53") == 0 || temp.compareTo("54") == 0 || temp.compareTo("55") == 0) {
System.out.println("The card type is : MasterCard");
if (card.length() == 16) {
if (check_sum(card) == true) {
System.out.println("Card is Valid");
} else {
System.out.println("Card is Invalid");
}
} else {
System.out.println("Card is Invalid");
}
} else if (temp.compareTo("34") == 0 || temp.compareTo("37") == 0) {
System.out.println("The card type is : AmericanExpress");
if (card.length() == 15) {
if (check_sum(card) == true) {
System.out.println("Card is Valid");
} else {
System.out.println("Card is Invalid");
}
} else {
System.out.println("Card is Invalid");
}
} else if (card.substring(0, 4).compareTo("6011") == 0) {
System.out.println("The card type is : DiscoverCard");
if (card.length() == 16) {
if (check_sum(card) == true) {
System.out.println("Card is Valid");
} else {
System.out.println("Card is Invalid");
}
} else {
System.out.println("Card is Invalid");
}
} else {
System.out.println("The card type is : Unknown");
}
}

/**
* method that checks what type of credit card number it is.
*
* @param s
* @return returns true or false if the credit card number is known as
* discovery/visa/americanexpress/MasterCard
*
*/
public static boolean check_sum(String s) {
int sum = 0;
int j = 0;
for (int i = s.length() - 2; i >= 0; i--) {
if (j % 2 == 0) {
int k = (s.charAt(i) - '0') * 2;
if (k >= 10) {
k -= 9;
}
sum += k;
} else {
sum += (s.charAt(i) - '0');
}
j += 1;
}
for (int i = s.length() - 1; i >= 0; i--) {
sum += (s.charAt(i) - '0');
}
if (sum % 10 == 0) {
return true;
}
return false;
}

/**
* checks to see if what the user enters is a digit or not and makes the
* user continue to put in a credit card number if it doesn't contain only
* digits.
*
* @param s
* @return returns true if all that is entered is numbers, returns false and
* repeats until only numbers are entered.
*/
public static boolean is_digit(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ' || (s.charAt(i) - '0' >= 0 && s.charAt(i) - '9' <= 0)) {
continue;
}
return false;
}
return true;
}
}

the part2 has a input file that has credit card numbers on it... here are the numbers:

This part 2 is due in 10 hours so if you guys can help id really appreciate it, and this code is in java. thanks a lot!

Explanation / Answer

Test11.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Test11 {
   public static void main(String arg[]){
       try{
       String inputFileName = "D:\CardNumbers.txt";
       String outputFileName = "D:\output.txt";
       FileWriter fw = new FileWriter(outputFileName);
       File file = new File(inputFileName);
       BufferedReader br = null;
       if(file.exists()){
String card = "";
br = new BufferedReader(new FileReader(file));
       String sCurrentLine;
       while ((sCurrentLine = br.readLine()) != null) {
           card = "";
if (is_digit(sCurrentLine) == true) {
for (int i = 0; i < sCurrentLine.length(); i++) {
if (sCurrentLine.charAt(i) != ' ') {
card += sCurrentLine.charAt(i);
}
}
//System.out.println("credit card number is:" + card);
fw.append("credit card number is:" + card+" ");
}
else{
//System.out.println("credit card number is:" + sCurrentLine);
//System.out.println("The credit card number must contain only the digits 0-9");
fw.append("credit card number is:" + sCurrentLine+" ");
fw.append("The credit card number must contain only the digits 0-9"+" ");
continue;
}
  
String temp = card.substring(0, 2);
if (card.charAt(0) == '4') {
//System.out.println("The card type is : VisaCard");
fw.append("The card type is : VisaCard"+" ");
if (card.length() == 13 || card.length() == 16) {
if (check_sum(card) == true) {
//System.out.println("Card is Valid");
fw.append("Card is Valid"+" ");
} else {
//System.out.println("Card is Invalid");
fw.append("Card is InValid"+" ");
}
} else {
//System.out.println("Card is Invalid");
fw.append("Card is InValid"+" ");
}
} else if (temp.compareTo("51") == 0 || temp.compareTo("52") == 0 || temp.compareTo("53") == 0 || temp.compareTo("54") == 0 || temp.compareTo("55") == 0) {
//System.out.println("The card type is : MasterCard");
fw.append("The card type is : MasterCard"+" ");
if (card.length() == 16) {
if (check_sum(card) == true) {
//System.out.println("Card is Valid");
fw.append("Card is Valid"+" ");
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else if (temp.compareTo("34") == 0 || temp.compareTo("37") == 0) {
//System.out.println("The card type is : AmericanExpress");
fw.append("The card type is : AmericanExpress"+" ");
if (card.length() == 15) {
if (check_sum(card) == true) {
//System.out.println("Card is Valid");
fw.append("Card is Valid"+" ");
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else if (card.substring(0, 4).compareTo("6011") == 0) {
//System.out.println("The card type is : DiscoverCard");
fw.append("The card type is : DiscoverCard"+" ");
if (card.length() == 16) {
if (check_sum(card) == true) {
//System.out.println("Card is Valid");
fw.append("Card is Valid"+" ");
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else {
//System.out.println("Card is Invalid");
fw.append("Card is Invalid"+" ");
}
} else {
//System.out.println("The card type is : Unknown");
fw.append("The card type is : Unknown"+" ");
}
       }
       System.out.println("Program finished");
       fw.flush();
       fw.close();
       br.close();
       }
       else{
           //System.out.println("File does not exist");
           fw.append("File does not exist"+" ");
           fw.flush();
           fw.close();
           br.close();
       }
   }
   catch(Exception e){
       e.printStackTrace();
   }
      
}
/**
* method that checks what type of credit card number it is.
*
* @param s
* @return returns true or false if the credit card number is known as
* discovery/visa/americanexpress/MasterCard
*
*/
public static boolean check_sum(String s) {
int sum = 0;
int j = 0;
for (int i = s.length() - 2; i >= 0; i--) {
if (j % 2 == 0) {
int k = (s.charAt(i) - '0') * 2;
if (k >= 10) {
k -= 9;
}
sum += k;
} else {
sum += (s.charAt(i) - '0');
}
j += 1;
}
for (int i = s.length() - 1; i >= 0; i--) {
sum += (s.charAt(i) - '0');
}
if (sum % 10 == 0) {
return true;
}
return false;
}
/**
* checks to see if what the user enters is a digit or not and makes the
* user continue to put in a credit card number if it doesn't contain only
* digits.
*
* @param s
* @return returns true if all that is entered is numbers, returns false and
* repeats until only numbers are entered.
*/
public static boolean is_digit(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ' || (s.charAt(i) - '0' >= 0 && s.charAt(i) - '9' <= 0)) {
continue;
}
return false;
}
return true;
}
   }

Output:

Program finished.

ontent in Output.txt

credit card number is:4000000000000
The card type is : VisaCard
Card is Invalid
credit card number is:6011381423629524
The card type is : DiscoverCard
Card is Invalid
credit card number is:5504811329907729a
The credit card number must contain only the digits 0-9
credit card number is:5190336472236303
The card type is : MasterCard
Card is Invalid
credit card number is:4623473920675
The card type is : VisaCard
Card is Invalid
credit card number is:5286515548704569
The card type is : MasterCard
Card is Invalid
credit card number is:5279545507721022
The card type is : MasterCard
Card is Invalid
credit card number is:5385368021084855
The card type is : MasterCard
Card is Invalid
credit card number is:5584859821449123
The card type is : MasterCard
Card is Invalid
credit card number is:5296729477934497
The card type is : MasterCard
Card is Invalid
credit card number is:5318987265021752
The card type is : MasterCard
Card is Invalid
credit card number is:5201729519773865
The card type is : MasterCard
Card is Invalid
credit card number is:5455702083843185
The card type is : MasterCard
Card is Invalid
credit card number is:6011962357136758
The card type is : DiscoverCard
Card is Invalid
credit card number is:5599211870159315
The card type is : MasterCard
Card is Invalid
credit card number is:4957844261696518
The card type is : VisaCard
Card is Invalid
credit card number is:5140617369295564
The card type is : MasterCard
Card is Invalid
credit card number is:5546848765977618
The card type is : MasterCard
Card is Invalid
credit card number is:5582082389174999
The card type is : MasterCard
Card is Valid
credit card number is:5247382621388622
The card type is : MasterCard
Card is Invalid

CardNumbers.txt:

4000000000000
6011381423629524
5504811329907729a
5190336472236303
4623473920675
5286515548704569
5279545507721022
5385368021084855
5584859821449123
5296729477934497
5318987265021752
5201729519773865
5455702083843185
6011962357136758
5599211870159315
4957844261696518
5140617369295564
5546848765977618
5582082389174999
5247382621388622