WIP: correction

utilise BigInteger, erreur sur 1er IBAN
This commit is contained in:
Meutel 2019-06-16 15:39:01 +02:00
parent eccfbfb469
commit 70190cbeb2
2 changed files with 58 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import static utils.Utils.*;
import java.math.BigInteger;
public class Iban {
@ -18,7 +19,56 @@ public class Iban {
}
public static boolean verifIban(char[] iban) {
return false;
char[] etape1 = deplacer(iban);
int[] etape2 = remplacerLettres(etape1);
BigInteger num = convertir(etape2);
BigInteger rem = num.remainder(BigInteger.valueOf(97));
return rem.equals(BigInteger.ONE);
}
public static char[] deplacer(char[] in) {
char[] out = new char[in.length];
for (int i=0;i<4;i++) {
out[out.length-4+i]=in[i];
}
for (int i=4; i<in.length;i++) {
out[i-4] = in[i];
}
return out;
}
public static int[] remplacerLettres(char[] in) {
int[] out = new int[in.length];
for (int i=0;i<in.length;i++) {
// '0' => 48, '1' => 49, ...
// 'A' => 65, 'B' => 66
char c = in[i];
int cAsInt = c;
int cc;
if (cAsInt > 64) {
cc = cAsInt - 55;
} else {
cc = cAsInt - 48;
}
out[i] = cc;
}
return out;
}
public static BigInteger convertir(int[] in) {
BigInteger sum = BigInteger.valueOf(0);
long mult = 1;
for (int i=0;i<in.length;i++) {
int val = in[i];
sum = sum.add(BigInteger.valueOf(in[i] * mult));
if (val < 10) {
mult *= 10;
} else {
mult *= 100;
}
}
return sum;
}
}

View File

@ -44,13 +44,18 @@ public class Utils {
}
}
public static final void afficher(char[] args) {
System.out.println(new String(args));
}
public static final void afficher(Object... args) {
if (args == null) {
System.out.println("null");
} else {
String str = Arrays.stream(args)
.map(String::valueOf)
.collect(Collectors.joining(" "));
.map(String::valueOf)
.collect(Collectors.joining(" "));
System.out.println(str);
}
System.out.print(' ');