5 Replies - 364 Views - Last Post: 03 December 2012 - 12:25 AM Rate Topic: -----

#1 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

String Credit Card Program

Posted 29 November 2012 - 05:04 PM

My code:
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.

Is This A Good Question/Topic? 0
  • +

Replies To: String Credit Card Program

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: String Credit Card Program

Posted 29 November 2012 - 07:22 PM

We get really, really, really nervous about brand new members looking for help with things like credit card processing and banking programs.

What exactly is the goal of this program?
Was This Post Helpful? 0
  • +
  • -

#3 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

Re: String Credit Card Program

Posted 29 November 2012 - 07:44 PM

View PosttlhIn`toq, on 29 November 2012 - 07:22 PM, said:

We get really, really, really nervous about brand new members looking for help with things like credit card processing and banking programs.

What exactly is the goal of this program?


Why would people get nervous?
It's just a homework assignment testing my knowledge on strings and implements the Luhn algorithm... It puts together a credit card number from the ID and the account number that's entirely made up. I don't mean to give anyone a scare.
Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: String Credit Card Program

Posted 29 November 2012 - 08:13 PM

Sorry, tlhIn`toq is a paranoiac member of our Forum Admin
Once in a while, honestly he should stay in his C++ forum, but he shows up here in a while and mixed everybody

Just ignore his complaints, we will try to help you despite his problems
In theJava forum, we are more friendly than in the C/C++ forum
we understand that you may do not post with the correct format :code: the first time
we understand a lot of things... we are a lot more zen than other forum admin :)
Was This Post Helpful? 0
  • +
  • -

#5 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

Re: String Credit Card Program

Posted 01 December 2012 - 05:16 PM

View Postpbl, on 29 November 2012 - 08:13 PM, said:

Sorry, tlhIn`toq is a paranoiac member of our Forum Admin
Once in a while, honestly he should stay in his C++ forum, but he shows up here in a while and mixed everybody

Just ignore his complaints, we will try to help you despite his problems
In theJava forum, we are more friendly than in the C/C++ forum
we understand that you may do not post with the correct format :code:/> the first time
we understand a lot of things... we are a lot more zen than other forum admin :)/>


Ah okay. Yeah I doubt that any who might be trying to sabotage a bank or something would come here looking for help with their code. Haha
But now that we got that out of the way, anyone have any idea what my problem might be? Its weird because in the first part, the card number prints out entirely correctly except for the very last digit... For some reason, its the default 0 and not a 9 like it should be.

And then lastly, for the array printing that creates a random card number from the user provided issuer ID, I have a long and weird card number with random '.' in the middle. Do I have some sort of logical error in my code or something?

Thank you for your time! And once again, this isn't any kind of credit card scam I assure you. Just a homework assignment to better my learning process of Java.
Was This Post Helpful? 0
  • +
  • -

#6 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

Re: String Credit Card Program

Posted 03 December 2012 - 12:25 AM

So does anyone have any ideas? I played around with my code a bit more today and still haven't gotten any better results.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1