-2
在這裏,我做了用戶輸入測試的信用卡號碼,但現在我想用JFileChooser來做這件事情,讓用戶選擇輸入文件的信用卡號碼和結果顯示在輸出文件。我得到作爲輸出文件與卡號是否有效,但如果我想打印與公司名稱卡號。我可以做到這一點?JFileChooser允許用戶選擇信用卡號碼的輸入文件
這裏是我的代碼:
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner user = new Scanner(System.in);
JFileChooser jfc = new JFileChooser(".");
int jfcUserOption = jfc.showOpenDialog(null);
if (jfcUserOption == JFileChooser.APPROVE_OPTION){
File chosenFile = jfc.getSelectedFile();
Scanner scan = new Scanner(chosenFile);
System.out.print("Output File Name: ");
String outputFileName = user.nextLine();
PrintWriter output = new PrintWriter(outputFileName);
while(scan.hasNextLine()){
long number = scan.nextLong();
while(number != 0){
if(isValid(number)){
output.println(number + " is valid number");
}
else{
output.println(number + " is invalid number");
}
output.close();
}
}
}
}
public static boolean isValid(long number) {
// TODO Auto-generated method stub
int total = sumOfEvenPlace(number) + sumOfoddPlace(number);
if ((total % 10 == 0) && (preMatched(number, 1) == true)) {
return true;
} else {
return false;
}
}
private static int sumOfoddPlace(long number) {
// TODO Auto-generated method stub
int n = getSize(number);
int sum = 0;
for(int i = 1; i <= n; i += 2){
sum += getDigitIndex(number, i);
}
return sum;
}
private static int sumOfEvenPlace(long number) {
// TODO Auto-generated method stub
int sum = 0;
int n = getSize(number);
for (int i = 2; i <= n; i+= 2){
sum += getDigit(2 * getDigitIndex(number, i));
}
return sum;
}
private static int getDigit(int number) {
// TODO Auto-generated method stub
int digit1 = number/10;
int digit2 = number % 10;
if(digit1 == 0){
return number;
}
else{
return digit1 + digit2;
}
}
private static int getDigitIndex(long number, int index) {
// TODO Auto-generated method stub
int digit;
digit = (int)(((number/Math.pow(10, index - 1))) % 10);
return digit;
}
private static int getSize(long digit) {
// TODO Auto-generated method stub
int digitNumber = 1;
while((digit = digit/10) != 0){
digitNumber++;
}
return digitNumber;
}
private static int getPre(long number, int p) {
// TODO Auto-generated method stub
int digitnumber = getSize(number);
return (int) (number/(long) (Math.pow(10.0, (double)(digitnumber - p))));
}
private static boolean preMatched(long number, int digit){
if ((getPre(number, digit) == 3) /*|| (getPre(number, digit) == 37) */ || (getPre(number, digit) == 4) || (getPre(number, digit) == 5) || (getPre(number, digit) == 6)) {
//PrintWriter pw = null;
if (getPre(number, digit) == 3 /* && getPre(number, digit) == 37 */) {
//output.println(" American Express Card");
} else if (getPre(number, digit) == 4) {
//pw.println(" Visa Card ");
} else if (getPre(number, digit) == 5) {
//pw.println("Master Card");
} else if (getPre(number, digit) == 6) {
//pw.println("Discover Card");
}
return true;
} else {
return false;
}
}
}
我的輸入文件是:test.txt的
5535085286738004
5443144794186700
6011436897231072
011600388914427
我的輸出應該是:testOutput.txt
5535085286738004 Master Card Valid
5443144794186700 Master Card Valid
6011436897231072 Discover Card Valid
011600388914427 None Invalid