1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Portoirs {
public static void ecrire(String[][] tableau, String valeur, String coordonnees) {
char cabscisse = coordonnees.toUpperCase().charAt(0); // premier caractère de la chaîne coordonnees (on le transforme en majuscule pour éviter tout problème à ce sujet)
String sordonnee = coordonnees.substring(1); // le reste de la chaîne après le premier caractère de la chaîne coordonnees
int abscisse = cabscisse - 'A'; // abscisse numérique correspondant à la lettre (A=0, B=1...)
int ordonnee = Integer.parseInt(sordonnee)-1; // abscisse numérique correspondant au chiffre
tableau[abscisse][ordonnee]=valeur; // écriture de la valeur dans le tableau aux coordonnées voulues
}
public static String convertir(int abscisse, int ordonnee) {
return (char)('A'+abscisse) + String.valueOf(ordonnee+1);
}
public static void main(String[] args) {
Map<String, String[][]> tableaux = new HashMap<>();
// remplisage aléatoire de 100 tableaux
Random random = new Random();
for(int t=0; t<100; t++) {
String[][] tableau = new String[10][10];
for(int i=0; i<tableau.length; i++) {
for( int j=0; j<tableau.length; j++) {
ecrire(tableau, String.format("%10d",random.nextInt(99999999)).replace(' ', '0'), convertir(i, j));
}
}
tableaux.put(String.valueOf(t+1), tableau);
}
try {
stocker("truc.txt", tableaux);
} catch (IOException e) {
e.printStackTrace();
}
try {
tableaux = lire("truc.txt");
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> index = createIndex(tableaux);
// vérification
String[][] tableau = tableaux.get("1");
String code = tableau[3][3];
System.out.println(trouver(index, code)); // affiche bien "ce tube se trouve sur le portoir : 1 en position D4"
}
public static void stocker(String fichier, Map<String, String[][]> tableaux) throws IOException {
try(BufferedWriter writer = Files.newBufferedWriter(Paths.get(fichier))) { // ouvre le fichier
for(Map.Entry<String, String[][]> entry : tableaux.entrySet()) { // on parcourt tout ce qui se trouve dans la map
writer.write(entry.getKey()); // on écrit l'id de tableau
writer.newLine(); // on passe à la ligne
for(String[] ligne : entry.getValue()) { // on parcourt les lignes du tableau
writer.write(String.join(",", ligne)); // on écrit la ligne entière, les éléments séparés par des virgules (,)
writer.newLine(); // on passe à la ligne
}
}
}
}
public static Map<String,String[][]> lire(String fichier) throws IOException {
Map<String, String[][]> tableaux = new HashMap<>();
Path path = Paths.get(fichier);
if ( Files.exists(path) ) {
try(BufferedReader reader = Files.newBufferedReader(path)) { // ouvre le fichier
int i=0; // va nous permettre de savoir sur quelle ligne on se trouve par rapport au tableau
String id=null; // l'id de tableau
String[][] tableau = null; // pour construire le tableau
for(String ligne = reader.readLine(); ligne!=null; ligne=reader.readLine()) { // on lit ligne par ligne
if ( ligne.trim().isEmpty() ) continue; // on ignore les lignes vides (normalement, il n'y en a pas, mais si on ajoute pour faciliter la lecture du fichier, on aura pas de probleme avec
switch(i) { // en fonction de i
case 0: // valeur 0 : l'id de tableau
id=ligne;
if ( id.contains(",") ) { // un petit contrôle d'erreur en passant
throw new IOException("Fichier incorrect : il manque l'identifiant de tableau");
}
i++; // on incrémente i;
tableau=new String[10][10]; //on crée un nouveau tableau
break;
default: // dans tous les autres cas
tableau[i-1] = ligne.split(","); // on découpe la ligne en utilisant le séparateur ,
if ( tableau[i-1].length!=10 ) { // un petit contrôle d'erreur en passant
throw new IOException("Ligne ne contenant pas 10 valeurs");
}
i++; // on passe à ligne suivante
break;
}
if ( i==11 ) { // on a lu tout un tableau
if ( tableaux.containsKey(id) ) {
throw new IOException("Deux tableaux avec le même identiant : "+id);
}
tableaux.put(id, tableau); // on le stocke
i=0; // on recommence pour le tableau suivant
}
}
if( i!=0 ) {
// le dernier tableau était incomplet
throw new IOException("Fichier incorrect (incomplet)");
}
}
}
return tableaux;
}
public static Map<String, String> createIndex(Map<String, String[][]> tableaux) {
Map<String, String> index = new HashMap<>();
for(Map.Entry<String, String[][]> entry : tableaux.entrySet()) {
String id = entry.getKey();
String[][] tableau = entry.getValue();
for(int i=0; i<tableau.length; i++) {
for(int j=0; j<tableau[i].length; j++) {
index.put(tableau[i][j], String.join(",", id, convertir(i, j))); // on concatène les 2 informations en les séparant par une virgule
}
}
}
return index;
}
public static String trouver(Map<String, String> index, String code) {
if ( index.containsKey(code) ) {
String position = index.get(code);
return String.format("ce tube se trouve sur le portoir : %s en position %s", (Object[])position.split(",")); // on créé le message complet qu'on aura plus qu'a afficher (en coupant l'information trouvée par la virgule)
}
else {
return "Non trouvé !";
}
}
} |
Partager