Validate International Securities Identification Number: Difference between revisions

→‎{{header|Java}}: Was marked as needing attention, so attended.
No edit summary
(→‎{{header|Java}}: Was marked as needing attention, so attended.)
Line 455:
=={{header|Java}}==
 
{{update|Java|UseThis the new test-cases, andnow considerassumes callingthat the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task insteadis ofavailable in the same duplicating(default) itpackage.}}
 
<lang java>public class ISIN {
 
public static void main(String[] args) {
String[] isins = {"US0378331005", "US0373831009", "D56000543287", "AU0000XVGZA3",
"AU0000VXGZA3US0378331005", "GB0002634946", "US0373831005"};
} else {"US0373831005",
}"U50378331005",
"US03378331005",
"AU0000XVGZA3",
"AU0000VXGZA3",
"FR0000988040",
};
for (String isin : isins)
System.out.printf("%s is %s%n\n", isin, ISINtest(isin) ? "valid" : "not valid");
}
 
Line 471 ⟶ 478:
if (!isin.matches("^[A-Z]{2}[A-Z0-9]{9}\\d$"))
return false;
 
int checkDigit = Character.digit(isin.charAt(11), 10);
 
StringBuilder sb = new StringBuilder();
for (char c : isin.substring(0, 1112).toCharArray())
sb.append(Character.digit(c, 36));
 
return checkDigit == checkDigitLuhn.luhnTest(sb.toString());
}
 
static int checkDigit(StringBuilder sb) {
int sum = 0;
int len = sb.length();
for (int i = 1; i <= len; i++) {
int ordinal = Character.digit(sb.charAt(i - 1), 10);
if ((len % 2 == 0 && i % 2 == 0) || (len % 2 == 1 && i % 2 == 1)) {
sum += (ordinal / 5) + (2 * ordinal) % 10;
} else {
sum += ordinal;
}
}
return (10 - (sum % 10)) % 10;
}
}</lang>
Anonymous user