Question
I still have a liitle bit problem on my assignment. Can someone help me to fix it? Thank you very much! this is my problem
I still have a liitle bit problem on my assignment. Can someone help me to fix it? Thank you very much! this is my problem
THIS IS MY CODE:
import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class CreditCardValidate { public static void main(String args[]) {
//array lists for storing card data //using this as not sure how many entities tobe read ArrayList input_cards = new ArrayList(); ArrayList valid_cards = new ArrayList(); ArrayList invalid_cards = new ArrayList();
// reading and storing input file data into arraylist Scanner scanner; try { //reading data from input.txt file scanner = new Scanner(new File("input.txt")); while (scanner.hasNextLine()) { input_cards.add(scanner.nextLine().trim()); } } catch (FileNotFoundException e) { e.printStackTrace(); }
// validating and separating each card based on identity
for (String cardNumber : input_cards) { if (validateCreditCardNumber(cardNumber)) { valid_cards.add(cardNumber); }
else invalid_cards.add(cardNumber); }
// writing valid and invalid cards into separate files writeToFile("valid_cards.txt", valid_cards,true); //true means card type must include in file writeToFile("invalid_cards.txt", invalid_cards,false); //false means no need of card type names in file System.out.println("done..."); }
//method to get card type based on card number private static String getCreditCardType(String cardNumber) { String cType = null;
if (cardNumber.startsWith("4026") || cardNumber.startsWith("417500")|| cardNumber.startsWith("4508")|| cardNumber.startsWith("4844")|| cardNumber.startsWith("4913")|| cardNumber.startsWith("4917") && cardNumber.length()==16 ) { cType = "VISA Electron"; }
else if (cardNumber.startsWith("51") || cardNumber.startsWith("53")|| cardNumber.startsWith("52")|| cardNumber.startsWith("54")|| cardNumber.startsWith("55") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("37") || (cardNumber >= "622126" && cardNumber
else if (cardNumber.startsWith("6011")|| cardNumber.startsWith("644")|| cardNumber.startsWith("645")|| cardNumber.startsWith("646")|| cardNumber.startsWith("647")|| cardNumber.startsWith("648")|| cardNumber.startsWith("649")|| cardNumber.startsWith("65") && cardNumber.length()==16) { cType = "Discover"; }
else if (cardNumber >= "3528" && cardNumber
else if (cardNumber.startsWith("54") && cardNumber.length()==16) { cType = "Diners Club - USA & Canada "; }
else if (cardNumber.startsWith("300")|| cardNumber.startsWith("301")|| cardNumber.startsWith("302")|| cardNumber.startsWith("303")|| cardNumber.startsWith("304")|| cardNumber.startsWith("305") && cardNumber.length()==14) { cType = "Diners Club-Carte Blanche"; }
else if (cardNumber.startsWith("36") && cardNumber.length()==14) { cType = "Diners Club-International"; }
else if (cardNumber.startsWith("5018")|| cardNumber.startsWith("5020")|| cardNumber.startsWith("5038")|| cardNumber.startsWith("5893")|| cardNumber.startsWith("6304")|| cardNumber.startsWith("6759")|| cardNumber.startsWith("6761")|| cardNumber.startsWith("6762")|| cardNumber.startsWith("6763") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("6304")|| cardNumber.startsWith("6706")|| cardNumber.startsWith("6771")|| cardNumber.startsWith("6709") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("4") && (cardNumber.length()>=13 && cardNumber.length()
else if (cardNumber.startsWith("637")|| cardNumber.startsWith("638")|| cardNumber.startsWith("639") && cardNumber.length()==16) { cType = "InstaPayment"; } else { cType = "UnknownType"; }
return cType; }
// function to validate given card number is valid private static boolean validateCreditCardNumber(String str) {
int[] ints = new int[str.length()]; for (int i = 0; i = 0; i = i - 2) { int j = ints[i]; j = j * 2; if (j > 9) { j = j % 10 + 1; } ints[i] = j; } int sum = 0; for (int i = 0; i
// Write into File -> Data of string array to write static void writeToFile(String filename, ArrayList cards, boolean flag) { BufferedWriter output = null; try { File file = new File(filename); // Create file object output = new BufferedWriter(new FileWriter(file));
for (String cardNumber : cards) { // writing data); output.write(" " + cardNumber + "\t");
if (flag) //flag to check is it necessary to include card type output.write(getCreditCardType(cardNumber)+" ");
}
} catch (IOException e) {// Handling exceptions e.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("data written successfully into "+filename); } }
Class Edit Tools Optiors invalidcards.addardNmber) writing valid and invalid cards into separate files writeToFile( vslid cards.xt, valid cards, true): true mcans card type must include in file ertelotlle('1 nva i 1 d-cards. txt'. Invalld-cards, false); //false means na need of card type nanes 1n file System.out.println( "done..."): nethod to get card type based on card number private static String getCreditCardType(String cardNumber) I String cType null: if cara Nu ber startaw1th .4826") cardNumber startaw1th "41/5BB cardNumber starts?th "4es cardNumber startsWith 4844 cardNumber startsWith .4913 cardNumber atart8M1th cType - VISA Electron else if (cardNumber.startswith( "51*) II cardNumber.startswith 53)I cardNumber.startaWith 52")l cardNumber.startswith( 54 cardNumber.startswith"55") & (cardNumber.length)-16 cType "NasterCard" else if (cardNumber.startswith( "37 cType - Amer ican Express" II (cardNumber 622126& cardNumber II cardNumber.startaWith 34*) &&cardNumber.length)15) bad operand types for binary operator>' first type: java. lang. String secand type: java.lang.String else if (cardNumber.atartsNith 6811 11 cardNumber.s .) Il cardNumber. startsWith('646.)Il cardNumber .3tartsMith('647.)" cardNumber .3tarteNith('t TypeD1SCover else if (cardNumber 3528&& cardNumber3589 &cardNumber.length()--16) cType- "JCB" else if (cardNumber.startsMith"54) && cardNumber.length)--16) f cType - Diners Glub - USA &Canada"; else it (cardNumber startsNith? '30011l cardNumber.startsMith "301l cardNumber.startsMithi 302 l cardNumber.startsWith 303" cardNumber.startsWith"384 11 cardNumber.startsWithr 36 O lype hene to search Class Edit Tools Optiors invalidcards.addardNmber) writing valid and invalid cards into separate files writeToFile( vslid cards.xt, valid cards, true): true mcans card type must include in file ertelotlle('1 nva i 1 d-cards. txt'. Invalld-cards, false); //false means na need of card type nanes 1n file System.out.println( "done..."): nethod to get card type based on card number private static String getCreditCardType(String cardNumber) I String cType null: if cara Nu ber startaw1th .4826") cardNumber startaw1th "41/5BB cardNumber starts?th "4es cardNumber startsWith 4844 cardNumber startsWith .4913 cardNumber atart8M1th cType - VISA Electron else if (cardNumber.startswith( "51*) II cardNumber.startswith 53)I cardNumber.startaWith 52")l cardNumber.startswith( 54 cardNumber.startswith"55") & (cardNumber.length)-16 cType "NasterCard" else if (cardNumber.startswith( "37 cType - Amer ican Express" II (cardNumber 622126& cardNumber II cardNumber.startaWith 34*) &&cardNumber.length)15) bad operand types for binary operator>' first type: java. lang. String secand type: java.lang.String else if (cardNumber.atartsNith 6811 11 cardNumber.s .) Il cardNumber. startsWith('646.)Il cardNumber .3tartsMith('647.)" cardNumber .3tarteNith('t TypeD1SCover else if (cardNumber 3528&& cardNumber3589 &cardNumber.length()--16) cType- "JCB" else if (cardNumber.startsMith"54) && cardNumber.length)--16) f cType - Diners Glub - USA &Canada"; else it (cardNumber startsNith? '30011l cardNumber.startsMith "301l cardNumber.startsMithi 302 l cardNumber.startsWith 303" cardNumber.startsWith"384 11 cardNumber.startsWithr 36 O lype hene to searchStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started