cours-info/utils/Utils.java
2019-06-01 17:36:27 +02:00

79 lines
2.0 KiB
Java

package utils;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.Scanner;
import java.util.function.BiFunction;
public class Utils {
public static String[] CESAR_DATA_CLEAR = new String[] {
"un beau texte"
};
public static int[] CESAR_DATA_KEYS = new int[] {
7
};
public static String[] CESAR_DATA_CRYPT = new String[] {
"crypt"
};
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(" "));
System.out.println(str);
}
System.out.print(' ');
}
public static final void afficherTabInt(int... args) {
if (args == null) {
System.out.println("null");
} else {
String str = Arrays.stream(args)
.boxed()
.map(i -> i+"")
.collect(Collectors.joining(" "));
System.out.println(str);
}
System.out.print(' ');
}
public static int lireEntier() {
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Saisir un entier: ");
return reader.nextInt();
}
}
public static String lireChaine() {
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Saisir une valeur et valider: ");
return reader.nextLine();
}
}
public static void testCesar(BiFunction<char[], Integer, char[]> func,
boolean chiffre) {
for (int i = 0; i<CESAR_DATA_KEYS.length; i++) {
int cle = CESAR_DATA_KEYS[i];
if (chiffre) {
afficher("Clair: " + CESAR_DATA_CLEAR[i]);
afficher("Chiffré: " + String.valueOf(func.apply(CESAR_DATA_CLEAR[i]
.toCharArray(), cle)));
afficher("Attendu: " + String.valueOf(CESAR_DATA_CRYPT[i]));
} else {
afficher("Chiffré: " + CESAR_DATA_CRYPT[i]);
afficher("Clair: " + String.valueOf(func.apply(CESAR_DATA_CRYPT[i]
.toCharArray(), cle)));
afficher("Attendu: " + String.valueOf(CESAR_DATA_CLEAR[i]));
}
}
}
}