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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| package xxx;
import java.util.*;
public class xxx {
//______________________________________________________________________________________________
public static void main(String[] args ) {
Scanner keby = new Scanner(System.in);
System.out.println("Veuillez saisir le nombre détudiants :");
// Liste des étudiants
int n = keby.nextInt();
String[][] tabEtud = new String[n][4]; // Une structure ou class aurait été préférable
System.out.println("Veuillez saisir le nombre pair détudiant incompatible :");
// Liste de paires étudiants incompatible dans le même dortoir
int n1 = keby.nextInt();
String [][] tabEtudInc = new String[n1][2];
System.out.println("Veuillez saisir le nombre détudiants à sélectionner par pair :");
// 2 étudiants par chambre soit 2*nombres de chambres (mais c ressaisi plus loin !)
int m = keby.nextInt();
String [][] tabEtudSelect = new String[m][2];
Integer Indice = 0;
String clobalId = "001"; // globalId ? C'est un nombre pourquoi String ?
String choix; // Pourquoi utiliser String ?
do {
System.out.println("------Menu principal ---------");
System.out.println(" 1. Liste détudiants ");
System.out.println(" 2.Liste détudiants incompatibles ");
System.out.println(" 3. Sélection détudiants ");
System.out.println(" 4.Quitter ");
System.out.println(" Taper votre choix : ");
choix = keby.nextLine();
switch(choix) {
case "1": saisieEtudiant (tabEtud, keby);
break;
case "2": saisieIncompatibilite (tabEtudInc, keby);
break;
case "3": Selection(tabEtudSelect, tabEtud , tabEtudInc , keby, clobalId, Indice);
break;
default : System.out.println("Veuillez respecter le menu!");
}
} while(choix != "4");
}
//______________________________________________________________________________________________
public static void saisieEtudiant(String [][] tabEtud, Scanner keby) {
int n = tabEtud.length;
int i = 0;
String idEtud;
String nom;
String prenom;
String adresse;
do { // C'est un do while qui mime exactement un for
System.out.println("Veuillez saisir lId de létudiants :");
idEtud = keby.nextLine();
tabEtud[i][0] = idEtud;
System.out.println("Veuillez saisir le nom de létudiants :");
nom = keby.nextLine();
tabEtud[i][1] = nom;
System.out.println("Veuillez saisir le prénom de létudiants :");
prenom = keby.nextLine();
tabEtud[i][2] = prenom;
System.out.println("Veuillez saisir ladresse de létudiants :");
adresse = keby.nextLine(); // Pourquoi pas tabEtud[i][3] = keby.nextLine(); ?
tabEtud[i][3] = adresse;
i = i + 1;
} while (i < n);
}
//______________________________________________________________________________________________
public static void saisieIncompatibilite(String [][] tabEtudInc, Scanner keby) {
int n = tabEtudInc.length;
String idEtud1;
String idEtud2;
int i = 0;
do {
System.out.println("Veuillez saisir lId de létudiant1 incompatible:");
idEtud1 = keby.nextLine();
tabEtudInc[i][0] = idEtud1;
System.out.println("Veuillez saisir lId de létudiant2 incompatible:");
idEtud2 = keby.nextLine();
tabEtudInc[i][1] = idEtud2;
i = i + 1;
} while(i < n); // Pourquoi pas n² ou mieux n*(n-1)/2 ?
}
// Sélection par tirage au sort ________________________________________________________________
public static void Selection(String [][] tabEtudSelect, String [][]tabEtud,
String [][]tabEtudInc, Scanner keby,String clobalId, Integer Indice) {
int n = tabEtud.length;
Boolean Ajouter = false; // Si Ajouter = vrai sortir du while
String[][] tabTirage = new String[1][2]; // 2x tirés, rangés et remplacés au tirage suivant
// Mets la liste étudiant dans ArrayList pour faciliter le traitement
ArrayList<String> tabEtudCopy = new ArrayList< String > ();
// Récupère les selections et à la sortie recopierra dans tabEtudSelect
ArrayList<String> tabEtudSelectCopy = new ArrayList< String > ();
for(int i = 0; i < n; i++) {
tabEtudCopy.add(tabEtud[i][0]); // Transfert de la liste étudiant dans la copie
}
System.out.println("Veuillez saisir le nombre de chambres:");
// Permet de saisir le nombres de chambres
int c = keby.nextInt(); // Nombre de chambres, devrait être = m / 2
String id1;
String id2;
String idRe;
for(int i = 0; i < c; i++) {
Ajouter = false; // Chaque ajout egal vrai pour sortir du do while
do {
Random random = new Random(); // Tirage au sort ou nombre aleatoire
int nb;
n = tabEtudCopy.size(); // Determine le nombre elements dans etudiant
nb = random.nextInt(n); // Choix dans l'intervale 0 et n
id1= tabEtudCopy.get(nb); // Si id1 retenu, l'etudiant sera selectionné
clobalId = id1;
idRe = recherche(tabEtudInc, clobalId, Indice); // id1 dans liste d'incompatibilié ?
if(idRe == "-1") {
// Sinon alors il est ajouté
tabTirage[0][0] = id1; // Il est rangé dans le tirage
tabEtudCopy.remove(nb); // Oté du tableau pour ne pas retomber sur le même
n = tabEtudCopy.size();
nb = random.nextInt(n);
id2 = tabEtudCopy.get(nb);
tabTirage[0][1] = id2; // Son binomme est aussi tiré
tabEtudCopy.remove(nb);
Ajouter = true; // Pour sortir du while, aller chambre suivante
}
else {
// Sinon alors on cherche le second et on vérifie s'il sont incompatibles
tabEtudCopy.remove(nb); // Evidemment il faut supprimer id1 avant
n = tabEtudCopy.size();
nb = random.nextInt(n);
id2 = tabEtudCopy.get(nb);
if(((id1 == tabEtudInc[Indice][0]) && (id2 != tabEtudInc[Indice][1]))
|| ((id1 == tabEtudInc[Indice][1]) && (id2 != tabEtudInc[Indice][0]))) {
// Si le couplage est possible alors on les tire tous les deux
tabTirage[0][0] = id1;
tabTirage[0][1] = id2;
tabEtudCopy.remove(nb);
Ajouter = true;
}
else tabEtudCopy.add(id1);
/* Si le duo n'est pas possible, on remet dans la table tabEtudCopy l'id1 qui a
été supprimé pour recommencer sans changer de chambre. Ajouter reste false */
}
} while(Ajouter == false);
// Les deux tirés sont rangés dans la selection
n = tabEtudSelectCopy.size();
tabEtudSelectCopy.add(tabTirage[0][0]);
tabEtudSelectCopy.add(tabTirage[0][1]);
}
/* récupére la selection en tableau static si le travail est fini */
int j = 0;
for(String elem:tabEtudSelectCopy) { // Tiens on repasse de c à m !
tabEtudSelect[j][0] = elem; // Récupération
j++;
}
}
//______________________________________________________________________________________________
public static String recherche(String [][]tabEtudInc, String clobalId, Integer Indice) {
int n = tabEtudInc.length;
String sortietest="-1"; // cherche les incompatibilités, sort avec "-1" si id n'existe pas
for(int i = 0; i < n ; i++) {
for(int j = 0; j < 2; j++ ) {
if(clobalId.equals(tabEtudInc[i][j])) {
Indice = i;
sortietest = tabEtudInc[i][j];
}
}
}
return sortietest ;
}
} |