package homework4;
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(Digits(TempissuerID) && Digits(TempaccountNum))
{
issuerID = TempissuerID;
accountNum = TempaccountNum;
calcCheckDigits();
}
}
public boolean Digits(String temp1)
{
String temp = "0123456789";
int count = 0;
for(int i = 0; i < temp1.length();i++)
for(int j = 0; j < temp.length();j++)
if(temp1.charAt(i) != temp.charAt(j))
{
count++;
}
if(count == temp1.length()){
return true;
}
return false;
}
public CreditCardNumber(){}
public String getID()
{
return issuerID;
}
public String getAccNum()
{
return accountNum;
}
public int getDigits()
{
return checkDigit;
}
private void calcCheckDigits()
{
int sum;
sum = checkSum();
if((sum + checkDigit) % 10 != 0)
{
checkDigit = sum - (sum % 10);
}
}
public void CreateCred(String TempissuerID)
{
if(TempissuerID != null && TempissuerID.length() ==6 && Digits(TempissuerID))
{
issuerID = TempissuerID;
}
else
{
issuerID = "000000";
}
StringBuilder TempString = new StringBuilder();
for(int i = 0; i < 9 ; i++)
{
TempString = TempString.append((Math.random()*(9-0+1)+0));
System.out.printf("%d",TempString);
}
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.append(issuerID.length());
for(int j = 0 ; j < accountNum.length(); j++)
temp.append(accountNum.length());
}
for(int k = 0 ; k < temp.length(); k +=2)
{
num = temp.charAt(k) - '0';
num *=2;
if(num > 9)
num = 1 + (num % 10);
temp.setCharAt(k, (char) num);
}
for(int v = 0 ; v < temp.length(); v++)
{
sum += temp.charAt(v) - '0';
}
return sum;
}
public String toString()
{
return issuerID + accountNum + checkDigit;
}
}
// in another file
package homework4;
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args)
{ CreditCardNumber[] cred1;
CreditCardNumber cred2 = getInput();
Display(cred2);
cred1 = getInputArray();
}
public static CreditCardNumber getInput() {
String ID;
String accNum;
CreditCardNumber tempCred;
Scanner scanner = new Scanner(System.in);
System.out.printf("Please enter issuer ID:");
ID = scanner.next();
System.out.printf("Please enter account number:");
accNum = scanner.next();
tempCred = new CreditCardNumber(ID, accNum);
return tempCred;
}
public static void Display(CreditCardNumber cred2)
{
System.out.printf("The complete number from your input:");
System.out.println(cred2);
return;
}
public static CreditCardNumber[] getInputArray()
{
CreditCardNumber[] tempArray;
String tempID;
int size;
Scanner scanner = new Scanner(System.in);
System.out.printf("Please enter size of the aray:");
size = scanner.nextInt();
if(size < 1)
{
size = 1;
}
tempArray = new CreditCardNumber[size];
System.out.printf("Please enter issuer ID:");
tempID = scanner.next();
for(int i = 0; i < size; i++)
{
}
return tempArray;
}
}
hi i have a question on the getInput method in main, when i compile the code and run it the out put is
enter issuer ID:321321
Please enter account number:654654654
The complete number from your input:0000009999999990
which is the default value for issuerId and acountNum from the CreditCardNumber class
but what i want is this output
Enter a credit card issuer number: 321321
Enter an account number: 654654654
The complete number from your input:
3213 2165 4654 6549
with 4 space between each 4 character
why does my code output the default value instead of the user input value ?
can anyone show me whats wrong with the code or getinput() function?
thank in advance
i just debugged your code, your digit method is returning false for this particular input , which will fail the if condition in the constructor, thus it would still hold the default values.
just use regex to check if the string passed is numeric.
change your Digits method like below.
change yout toString method like this:
it gave me the output: