In another file:public class CreditCardNumber { private String issuerID = "000000"; private String accountNum = "999999999"; private int checkDigit; 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) { char digit; for(int i = 0; i < temp.length(); i++){ 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); } } 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 = (num % 10) + 1; temp.setCharAt(k, (char)num); sum += num; } return sum; } public String toString() { String str = issuerID + accountNum + checkDigit; return str; } }
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.nextLine(); System.out.printf("Please enter account number: "); accountNum = scanner.nextLine(); 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# (6 digits): "); 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[] objArray){ for(int i = 0; i < objArray.length; i++){ displayCred("Credit Card # " + i + ":" + '\n', objArray[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; } }
Desired output:
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: 1
Enter an issuer ID# (6 digits): 789789
Credit Card # 0:
7897 8931 4062 1219
However when I run my code I have two problems.
1. For the complete number from input part, I my card number looks good except for the very last digit(its always a 0).
I've tried editing multiple things to fix this but have had no luck.
2. For my array printing credit card number, I just get a giant mess:
Credit Card # 0:
9879 878. 0511 2471 8666 2488 .625 2980 5398 0805 8.93 4724 9421 1246 52.1 0708 2984 6066 88.7 3952 9333 1195 260. 8686 9691 2926 3099 5.31 4072 8376 3121 .706 0624 4425 2281 48.3 2209 1522 6689 480
-pretty much until the console runs out of room.
Please help me out! I'm so close to finishing, and feel like this is probably something trivial that I am just not seeing.
Thank you for your time and please let me know if you need additional details.