public class CreditCardNumber {
private String issuerID = "000000";
private String accountNum = "999999999";
private int checkDigit = 0;
public CreditCardNumber(String tempIssuerID, String tempAccountNum) {
if (tempIssuerID != null && tempAccountNum != null
&& tempIssuerID.length() == 6 && tempAccountNum.length() == 9)
if (checkDigits(tempIssuerID) && checkDigits(tempAccountNum)) {
issuerID = tempIssuerID;
accountNum = tempAccountNum;
calcCheckDigits();
}
}
public CreditCardNumber() {
issuerID = "000000";
accountNum = "999999999";
checkDigit = 0;
}
public boolean checkDigits(String temp) {
for(int i = 0; i < temp.length(); i++){
char digit = temp.charAt(i);
if(digit < '0')
return false;
if (digit > '9')
return false;
}
return true;
}
public String getIssuerID() {
return issuerID;
}
public String getAccountNum() {
return accountNum;
}
public int getCheckDigits() {
return checkDigit;
}
private void calcCheckDigits() {
int sum;
sum = checkSum();
if ((sum + checkDigit) % 10 == 0) {
checkDigit = sum + (sum % 10); //or -?
}
}
public void createCard(String tempIssuerID) {
if (tempIssuerID != null && tempIssuerID.length() == 6
&& checkDigits(tempIssuerID)) {
issuerID = tempIssuerID;
} else {
issuerID = "000000";
}
StringBuilder tempString = new StringBuilder();
for (int i = 0; i < 9; i++) {
tempString = tempString.append((Math.random() * 9));
}
accountNum = tempString.toString();
calcCheckDigits();
}
private int checkSum() {
StringBuilder temp = new StringBuilder();
int num;
int sum = 0;
for(int i = 0 ; i <issuerID.length(); i++){
temp = temp.append(issuerID.charAt(i));
}
for(int j = 0 ; j < accountNum.length(); j++){
temp = temp.append(accountNum.charAt(j));
}
for (int k = 0; k < temp.length(); k += 2) {
num = temp.charAt(k);
num *= 2;
if (num >= 10)
num = 1 + (num % 10);
temp.setCharAt(k, (char)num);
}
for (int v = 0; v < temp.length(); v++) {
sum += temp.charAt(v);
}
return sum;
}
public String toString() {
String str = issuerID + accountNum + checkDigit;
return str;
}
}
In another file:
import java.util.Scanner;
public class Prog4 {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
CreditCardNumber[] objArray;
CreditCardNumber objCred = getUserInput();
displayCred("The complete number from your input:", objCred);
objArray= getInputArray();
displayArray(objArray);
tryAnother(objArray);
}
public static CreditCardNumber getUserInput() {
String ID;
String accountNum;
CreditCardNumber userNum;
System.out.printf("Please enter issuer ID: ");
ID = scanner.next();
System.out.printf("Please enter account number: ");
accountNum = scanner.next();
userNum = new CreditCardNumber(ID, accountNum);
return userNum;
}
public static void displayCred(String ch, CreditCardNumber cred){
System.out.println(ch);
System.out.println(cred.toString().replaceAll(".{4}", "$0 "));
}
public static CreditCardNumber[] getInputArray(){
int size;
CreditCardNumber[] tempAry;
String tempID;
System.out.printf("Please enter size of the array: ");
size = scanner.nextInt();
if(size < 1) {
size = 1;
}
tempAry = new CreditCardNumber[size];
System.out.printf("Please enter issuer ID: ");
tempID = scanner.next();
for(int i = 0; i < tempAry.length; i++){
tempAry[i] = new CreditCardNumber();
tempAry[i].createCard(tempID);
}
return tempAry;
}
public static void displayArray(CreditCardNumber[] cred1){
for(int i = 0; i< cred1.length; i++){
displayCred("Credit Card # " + i + ":" + '\n', cred1[i]);
}
System.out.println();
}
public static boolean tryAnother(CreditCardNumber[] cred1) {
String s;
System.out.printf("Get more credit card numbers? (n for no):");
s = scanner.next();
if(s.charAt(0) != 'N' && s.charAt(0) != 'n'){
cred1 = getInputArray();
displayArray(cred1);
if(s.charAt(0) != 'N' && s.charAt(0) != 'n')
tryAnother(cred1);
}
return false;
}
}
I've been staring at this for an hour... It must be some small error that I'm not noticing but I'm not sure. To keep it simple, the output that I'm trying to get is something like:
Enter a credit card issuer number: 321321
Enter an account number: 654654654
The complete number from your input:
3213 2165 4654 6549
Enter the number of elements in the array: 2
Enter an issuer ID# (6 digits): 789789
Credit Card # 0:
7897 8931 4062 1219
Credit Card # 1:
7897 8920 2125 3522
The horrendous output I'm getting is:
Please enter issuer ID: 321321
Please enter account number: 654654654
The complete number from your input:
3213 2165 4654 6544 10
Please enter size of the array: 2
Please enter issuer ID# (6 digits): 789789
Credit Card # 0:
7897 891. 8162 3868 0586 7254 0.81 4502 0879 4174 677. 0643 1063 5647 9191 .915 4300 8218 9641 51.1 7872 4000 2968 333. 9214 9062 2054 3012 .982 6226 7973 0559 6.41 6503 6694 4614 46.4 6801 8234 9267 6546 30
Credit Card # 1:
7897 896. 2451 4350 4889 9710 .267 1189 1912 7549 86.2 5602 4191 7657 062. 6769 5662 0097 3576 3.55 6071 4027 5117 936. 9805 2365 8710 4610 .433 5788 4216 5790 237. 0287 8153 0187 8340 .470 6143 5223 8544 10
-The trouble begins with the line that says "The complete number from your input:"... I have an extra 10 hanging around on the end, and instead of 6549 I have 6544... I'm assuming that this problem is also the source of my credit card array display mess, but I have no idea what I've done wrong.
Please let me know if you have any suggestions or tips.

New Topic/Question
Reply



MultiQuote





|