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);
}
System.out.printf("%d", checkDigit);
}
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));
}
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));
}
System.out.println(temp);
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';
}
System.out.printf("%d", sum);
return sum;
}
public String toString() {
String s = "";
String str = issuerID + accountNum;
return str;
}
}
我的校验和方法有问题,我写了一些printf语句来检查sum的值,结果是-930但是这不是值和的假设,如果只是添加没有理由因为它有一个负号。当我为issuerID输入321321而对于accountNum输入654654654时,我期望checkDigit数为9,并且check of sum中的sum值将被传递给calCheckDigits方法以获取CheckDigit的值。是9。
这种方法我做错了什么?
好的我现在只需编辑代码我得到-30的总和
我只是再次编辑我的第一个2校验和循环错误导致我的临时字符串值不正确
现在它得到-320 仍然不正确,但越来越好