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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import com.opencsv.CSVReader;
public class Main extends JFrame{
private JTextField chemin_fichierCSV;
private JButton btnParcourirCSV;
private JButton btnGoCSV;
private JFileChooser fc_parcourirCSV;
private String fichier;
private BufferedWriter bw;
private static BufferedWriter bw2;
private JFileChooser fc_saveCSV;
private String cheminCSV;
private JTextField textField;
private String Annee;
private int toInt;
private CSVReader reader,reader2;
private List<Ecole> listeCodeEcole;
private Ecole monEcole;
public Main (){
fc_parcourirCSV = new JFileChooser();
fc_saveCSV = new JFileChooser();
//------------Parametre de la fenetre ------------
this.setTitle("Extraction Eleves -> .dbf"); //le titre
this.setSize(580, 140); //les dimensions
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //ferme quand on appuie sur la croix
this.setLocationRelativeTo(null); //place au milieu de l'ecran
this.getContentPane().setLayout(null);
//------------Label "fichier .CSV :" ------------
JLabel lblFichierCSV = new JLabel("Fichier .CSV : ",JLabel.CENTER);
lblFichierCSV.setBounds(18, 11, 129, 14); //Positionnement et dimensions
getContentPane().add(lblFichierCSV);
//------------Chemin du fichier CSV ------------
chemin_fichierCSV = new JTextField();
chemin_fichierCSV.setEditable(false); //desactive
chemin_fichierCSV.setBounds(145, 8, 260, 20);
getContentPane().add(chemin_fichierCSV);
chemin_fichierCSV.setColumns(10);
//------------Bouton parcourir CSV ------------
btnParcourirCSV = new JButton("Parcourir...");
//btnParcourirCSV.setEnabled(false);
btnParcourirCSV.setBounds(425, 8, 116, 20);
btnParcourirCSV.setHorizontalTextPosition(AbstractButton.LEADING);
//btnParcourirCSV.setIcon(icon);
this.getContentPane().add(btnParcourirCSV);
//------------Bouton Enregistrer CSV------------
btnGoCSV = new JButton("Enregistrer");
btnGoCSV.setBounds(425, 55, 116, 20);
btnGoCSV.setEnabled(false); //desactive
this.getContentPane().add(btnGoCSV);
//------------Label Année------------
JLabel lblAnne = new JLabel("Ann\u00E9e : ");
lblAnne.setBounds(75, 38, 46, 14);
getContentPane().add(lblAnne);
//------------Formulaire Année------------
textField = new JTextField();
textField.setText("20**");
textField.setEditable(false);
textField.setBounds(144, 35, 59, 20);
getContentPane().add(textField);
textField.setColumns(10);
//------------Quand on clic sur le bouton parcourir------------
btnParcourirCSV.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
fc_parcourirCSV.showOpenDialog(null); //ouvre une fenetre
if (fc_parcourirCSV.getSelectedFile() != null) {
fichier = fc_parcourirCSV.getSelectedFile().toString(); //On prend le chemin
chemin_fichierCSV.setText(fichier); //On l'affiche dans le formulaire désactivé
textField.setEditable(true);
btnGoCSV.setEnabled(true); //active le bouton Enregistrer
}
}
});
//------------Quand on clic sur le bouton Enregistrer------------
btnGoCSV.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
fc_saveCSV.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //permet de ne sélectionner que les répertoires
fc_saveCSV.showSaveDialog(null); //ouvre une boite de dialogue
cheminCSV = fc_saveCSV.getSelectedFile() + "\\"; //recupere le chemin + \
try {
CaptureColonne();
} catch (IOException e) {
e.printStackTrace();
}
}
});
this.setVisible(true); //On rend visible l'interface graphique
}
/*--------------------------------FONCTION QUI AJOUTE ECOLE/NIVEAU--------------------------------*/
public static void ajouteEcoleNiveau(Map<String,Ecole> eMap,String autreEcole,String niveau){
if(eMap.get(autreEcole)== null){
//System.out.println("l'ecole n'est pas dans la map");
Ecole newEcole = new Ecole(autreEcole);
newEcole.ajoutOccurenceNiveau(niveau);
eMap.put(autreEcole,newEcole);
}
else{
//System.out.println("l'ecole est dans la map");
eMap.get(autreEcole).ajoutOccurenceNiveau(niveau);
}
}
/*--------------------------------FONCTION QUI AFFICHE LES STATS--------------------------------*/
public static void afficheStatistiqueListe(Map<String,Ecole> eMap){
Set<String> cles = eMap.keySet();
Iterator<String> it = cles.iterator();
while (it.hasNext()){ //parcour la hashMap
Object cle = it.next(); //recupere la clé
Object valeur = eMap.get(cle); //recupere la valeur
try {
bw2.write((String)cle+";");
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println((String)cle); //affiche la clé(CodeEcole)
try {
bw2.write(((Ecole) valeur).getStatistique());
} catch (IOException e) {
e.printStackTrace();
} //affiche les niveaux de l'ecole
}
}
/*-------------------------------- PROGRAMME--------------------------------*/
//----------Partie Traitement Doublon------------
public void CaptureColonne() throws IOException {
reader = new CSVReader(new FileReader(fichier), ';','$',1); //ouvre le flux de lecture
String [] nextLine; //tableau qui va contenir les colonnes d'une ligne
Annee = textField.getText(); //recupere l'année
toInt = Integer.parseInt(Annee); //transforme String en Int
List<Enregistrement> listeAvecDoublon = new ArrayList<Enregistrement>();
bw = new BufferedWriter(new FileWriter(cheminCSV+"Eleves_"+Annee+".dbf", false)); //flux d'ecriture
bw2 = new BufferedWriter(new FileWriter(cheminCSV+"Eleves_Stats_"+Annee+".dbf", true));
bw.write("Nom;Prenom;Niveau;CodeEcole;NomEcole;\n");
bw2.write("CodeEcole;TPS;PS;MS;GS;CP;CE1;CE2;CM1;CM2;TOTAL;\n");
while ((nextLine = reader.readNext()) != null){
Enregistrement Eleve = new Enregistrement(nextLine[0],nextLine[1],nextLine[2],nextLine[5],nextLine[6]);
// System.out.println(compt+"Eleve créé !");
listeAvecDoublon.add(Eleve);
}
int comptDoublon = 0;
Map<String,Map<String,List<Enregistrement>>> listeStructure = new HashMap<String,Map<String,List<Enregistrement>>>();
for(Enregistrement ligne : listeAvecDoublon){
// On prend le dossier de la famille
if(listeStructure.get(ligne.getNom())== null){
Map<String,List<Enregistrement>> prenomStructure = new HashMap<String,List<Enregistrement>>();
List<Enregistrement> listeDoublon = new ArrayList<Enregistrement>();
listeDoublon.add(ligne);
prenomStructure .put(ligne.getPrenom() ,listeDoublon);
listeStructure.put(ligne.getNom(),prenomStructure );
bw.write(ligne.getNom()+";"+ligne.getPrenom()+";"+ligne.getNiveau()+";"+ligne.getCodeEcole()+";"+ligne.getNomEcole()+";\n");
}
else {
Map<String,List<Enregistrement>> prenomStructure = listeStructure .get(ligne.getNom());
// On prend le dossier du prénom dans la famille.
if(prenomStructure.get(ligne.getPrenom()) == null){
List<Enregistrement> listeDoublon = new ArrayList<Enregistrement>();
listeDoublon.add(ligne);
prenomStructure .put(ligne.getPrenom() ,listeDoublon);
bw.write(ligne.getNom()+";"+ligne.getPrenom()+";"+ligne.getNiveau()+";"+ligne.getCodeEcole()+";"+ligne.getNomEcole()+";\n");
}
else {
prenomStructure.get(ligne.getPrenom()).add(ligne);
comptDoublon++;
//System.out.println(ligne.getNom()+" "+ligne.getPrenom()+" est en doublon");
}
}
}
JOptionPane.showMessageDialog(null, +comptDoublon+" doublon(s) supprimé(s)","Doublon(s) supprimé(s)", JOptionPane.INFORMATION_MESSAGE);
//----------------Partie Statistique----------------
bw.close();
reader.close();
reader2 = new CSVReader(new FileReader(cheminCSV+"Eleves_"+Annee+".dbf"),';','$',1);
String [] nextLine2;
HashMap<String, Ecole> mapEcoles = new HashMap<String, Ecole>(); //liste d'ecole
while ((nextLine2 = reader2.readNext()) != null){ //on ajoute la ligne dans un tableau<Ecole>
monEcole = new Ecole(nextLine2[3]);
ajouteEcoleNiveau(mapEcoles, nextLine2[3], nextLine2[2]); //ajoute les ecoles dans la map, et ajoute les niveaux
}
afficheStatistiqueListe(mapEcoles); //ecrit dans le fichier statistique
reader2.close();
bw2.close();
JOptionPane.showMessageDialog(null, "Génération terminée ! ","Terminé !", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
try { //Dès qu'il est lancé...
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
new Main(); //...On lance Main()
}
catch (Exception e) {
e.printStackTrace();
}
}
} |