我在java中使用此代码来验证银行帐号,我正在尝试将其转换为objective-c但我认为我的代码无法正常工作,这里是代码:
JAVA 1:
public boolean isValid(final String accountCnumber,
final ConstraintValidatorContext constraintValidatorContext) {
if ((accountCnumber == null) && optional) {
return true;
}
else {
if (accountCnumber == null) {
return false;
}
else {
boolean result;
if (accountCnumber.length() == 12) {
final String auxAccountNumber = accountCnumber.substring(3,5)+ accountCnumber.substring(0, 3) + accountCnumber.substring(5, 12);
Integer principalCounter = 0;
Integer counter = 0;
for (int i = 0; i < auxAccountNumber.length() - 1; i++) {
principalCounter = checkData(auxAccountNumber,
principalCounter, i);
}
final Integer cont = (principalCounter + 10) * 10;
counter = cont - principalCounter;
final String lastAux = counter.toString().substring(counter.toString().length() - 1);
final String last = accountCnumber.substring(11);
result = lastAux.equals(last);
}
else {
result = false;
}
return result;
}
}
}
我的目标-c尝试1 :(我认为错误是从.subString传递到NSRange)
-(BOOL)accountValidation: (NSString *)accountCnumber
{
if ([accountCnumber isEqualToString:@""] || accountCnumber == nil) {
return false;
}else{
Boolean result;
if (accountCnumber.length == 12) {
/*
000200041901
*/
NSString *auxAccountNumber = @"";
NSRange range1 = NSMakeRange(3, 2); //20
NSRange range2 = NSMakeRange(0, 3); //000
NSRange range3 = NSMakeRange(5, 7); //0041901
NSString *subString1 = [accountCnumber substringWithRange:range1];
NSString *subString2 = [accountCnumber substringWithRange:range2];
NSString *subString3 = [accountCnumber substringWithRange:range3];
NSLog(@"subString1 20=%@",subString1);
NSLog(@"subString2 000=%@",subString2);
NSLog(@"subString3 0041901=%@",subString3);
auxAccountNumber = [NSString stringWithFormat:@"%@%@%@",subString1,subString2,subString3];
NSLog(@"auxAccountNumber = %@",auxAccountNumber);
int principalCounter = 0;
int counter = 0;
for (int i = 0; i < auxAccountNumber.length - 1; i++) {
//principalCounter = checkData(auxAccountNumber,principalCounter, i);
principalCounter = [self checkData:auxAccountNumber p:principalCounter counter:i];
}
int cont = (principalCounter + 10) * 10;
counter = cont - principalCounter;
NSString *strFromInt = [NSString stringWithFormat:@"%d",counter];
NSString *lastAux = [strFromInt substringFromIndex:[strFromInt length]-1];
NSString *last = [accountCnumber substringFromIndex:11];
//result = lastAux.equals(last);
if ([lastAux isEqualToString:last]) {
result = true;
}else{
result = false;
}
}
else {
result = false;
}
return result;
}
}
JAVA 2:
private Integer checkData(final String auxAccountNumber, final Integer pCounter, final int i) {
Integer principalCounter = pCounter;
Integer counter = 0;
Integer aux;
if (i % 2 == 0) {
counter = Integer.parseInt(Character.toString(auxAccountNumber.charAt(i))) * 2;
if (counter >= 10) {
counter = counter - 9;
}
principalCounter = principalCounter + counter;
}
else {
aux = Integer.parseInt(Character.toString(auxAccountNumber.charAt(i)));
principalCounter = principalCounter + aux;
}
return principalCounter;
}
我的目标-c尝试:
-(int)checkData:(NSString *)auxAccountNumber p:(int)pCounter counter:(int)i
{
int principalCounter = pCounter;
int counter = 0;
int aux=0;
if (i % 2 == 0) {
NSString *s = auxAccountNumber;
const char *c = [s UTF8String];
counter = c[i] * 2;
if (counter >= 10) {
counter = counter - 9;
}
principalCounter = principalCounter + counter;
}else {
NSString *s = auxAccountNumber;
const char *c = [s UTF8String];
counter = c[i] * 2;
principalCounter = principalCounter + aux;
}
return principalCounter;
}
很抱歉很长的帖子,但我真的需要这个:)
编辑我有这些有效的银行帐号用于测试@“000200041901”和@“001200144174”方法“accountValidation”的结果必须返回true,但问题是它总是返回false。
答案 0 :(得分:1)
以下是重写的第二种方法的示例,其中更好地命名和消除了冗余:
- (int)checkDataAuxAccountNumber:(NSString *)auxAccountNumber pCounter:(int)pCounter digitIndex:(int)digitIndex {
NSString *digitString = [auxAccountNumber substringWithRange:NSMakeRange(digitIndex, 1)];
int digit = [digitString intValue];
if (digitIndex % 2 == 0) {
digit *= 2;
if (digit >= 10) {
digit -= 9;
}
}
return pCounter + digit;
}
以类似的方式转换第一种方法 然后为Java和Objective-C代码创建一些测试代码。