Problème de gestion tableau d'objet
bonjour, je suis en stage et je dois développer une application qui permet de saisir des informations(mise dans un tableau), puis de les enregistrées dans un fichier csv.
Le problème est le suivant lorsque je valide la création d'un objet, il me le crée et lorsque je re valide il me le crée une deuxième fois alors qu'il y a une fonction qui doit vérifier si cela n'existe pas déjà (compare deux champs du tableau avec les valeurs selectionnés dans deux combos box)
voici mon code
la classe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Cfiche {
public String categorie,souscategorie, txtprincipal,liensprincipaux,txtsecondaire,lienssecondaires;
public Cfiche(){}
public Cfiche(String cat,String souscat,String txtp,String lp, String txts, String ls){
categorie = cat;
souscategorie = souscat;
txtprincipal =txtp;
liensprincipaux = lp;//composer de different liens concatener separé par des :
txtsecondaire = txts;
lienssecondaires = ls;//composer de different liens concatener separé par des :
}
} |
Code:
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
|
public class Fenetre extends javax.swing.JFrame {
PrintWriter sortie;
BufferedReader entree;
Cfiche[] tabfiche = new Cfiche[40];
String fichier = "basededonnees.csv"; //nom du fichier généré
String chemin_pdf;
File pdf;
int idx=-1; //index du tableau
File f = new File(fichier);
public Fenetre() throws IOException {
setTitle("Mise à jour des informations");
initComponents();
//idx = -1;
chemin_pdf = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toString().substring(6)).toString();
chemin_pdf = chemin_pdf + "\\PDF";//definit le chemin du dossier des pdf
pdf = new File(chemin_pdf);
if ( f.exists() ){ //test l'existance d'un fichier de données
lire();
}
else{
idx = 0;
}
affichage();
} |
Code:
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
|
public void lire() throws FileNotFoundException, IOException{
String ligne;// une ligne du fichier lu recuperer par le buffer
String[] decoupe; // tableau contenant les parties de la ligne lu
entree = new BufferedReader (new FileReader(fichier));
do{
Cfiche elt = null;
ligne = entree.readLine();// lit une ligne du fichier
if(ligne != null){
decoupe = ligne.split(";");//decoupe chaque cellule du fichier
elt = new Cfiche(decoupe[0],decoupe[1],decoupe[2],decoupe[3],decoupe[4],decoupe[5]);
idx= idx + 1;
tabfiche[idx] = elt;
}
}
while(ligne != null);
entree.close();
}
public void ecrire(String texte){
try
{
FileWriter fw = new FileWriter(fichier, true);
// le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
BufferedWriter output = new BufferedWriter(fw);
//on marque dans le fichier ou plutot dans le BufferedWriter qui sert comme un tampon(stream)
output.write(texte+"\r\n");
//on peut utiliser plusieurs fois methode write
output.flush();
//ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter
output.close();
//et on le ferme
System.out.println("ligne créé");
}
catch(IOException ioe){
System.out.print("Erreur : ");
ioe.printStackTrace();
}
} |
Code:
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
|
private void creationfiche(){
String [] liensp,lienss;
String llistep= " ", llistes=" ";
Cfiche elt;
int i = index();
if(i == -1){
DefaultListModel p = new DefaultListModel();
listelienp.setModel(p);
if(p.isEmpty()==false){
liensp = (String[]) listelienp.getSelectedValues();
llistep = concatenationliste(liensp);
}
DefaultListModel s = new DefaultListModel();
listeliens.setModel(s);
if(s.isEmpty()==false){
lienss = (String[]) listeliens.getSelectedValues();
llistes = concatenationliste(lienss);
}
elt = new Cfiche(combocategorie.getSelectedItem().toString(),combosouscategorie.getSelectedItem().toString(),txtaprincipal.getText(),llistep,txtasecondaire.getText(),llistes);
idx = idx + 1;
tabfiche[idx] = elt;
System.out.println("creation de la fiche en index : "+idx);
System.out.println(tabfiche[idx].categorie + " " +tabfiche[idx].souscategorie);
}else{
System.out.println("fiche déjà existante à l'index : " +i);
}
} |
Code:
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
|
private void btvaliderActionPerformed(java.awt.event.ActionEvent evt) {
int i;
String [] liensp,lienss;
i = index();
if(i == -1){
creationfiche();
}else{
DefaultListModel p = new DefaultListModel();
listelienp.setModel(p);
System.out.println(p.isEmpty());
if(p.isEmpty()){
tabfiche[i].liensprincipaux =" ";
}else{
liensp = (String[]) listelienp.getSelectedValues();
tabfiche[i].liensprincipaux = concatenationliste(liensp);
}
DefaultListModel s = new DefaultListModel();
listelienp.setModel(s);
if(s.isEmpty()== false){
lienss = (String[]) listeliens.getSelectedValues();
tabfiche[i].lienssecondaires = concatenationliste(lienss);
}else{
tabfiche[i].lienssecondaires = " ";
}
}
affichage();
} |
Code:
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
|
private void affichage(){
int i;
i= index();
System.out.print("Index affiché : "+ i);
if(i != -1){
txtaprincipal.setText( tabfiche[i].txtprincipal);
txtasecondaire.setText( tabfiche[i].txtsecondaire);
listelienp.setListData(traitementliste(tabfiche[i].liensprincipaux));
listeliens.setListData(traitementliste(tabfiche[i].lienssecondaires));
}
else{
txtaprincipal.setText(" ");
txtasecondaire.setText(" ");
DefaultListModel p = new DefaultListModel();
listelienp.setModel(p);
p.removeAllElements();
listelienp.removeAll();
DefaultListModel s = new DefaultListModel();
listeliens.setModel(s);
s.removeAllElements();
listeliens.removeAll();
}
i++;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int index(){
int i=0, res=-1;
boolean test = false;
while(i < (idx) && test == false && tabfiche[i] != null){
if(tabfiche[i].categorie.equals(combocategorie.getSelectedItem().toString()) && tabfiche[i].souscategorie.equals(combosouscategorie.getSelectedItem().toString()) ){
System.out.println(tabfiche[i].categorie+" "+combocategorie.getSelectedItem().toString()+"test");
System.out.println(tabfiche[i].souscategorie+" "+combosouscategorie.getSelectedItem().toString());
test = true;
res= i;
}
i++;
}
return res;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
String concatenationliste(String[] liste){
int i;
String listeconc;
listeconc= liste[0];
i = 0;
while(liste[i] != null){
listeconc = listeconc+":" + liste[i];
i++;
}
return listeconc;
} |
Code:
1 2 3 4 5 6
|
String[] traitementliste(String chaine_lien){
String[] listelien;
listelien = chaine_lien.split(":");
return listelien;
} |
Code:
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
|
private void menusauvegarderActionPerformed(java.awt.event.ActionEvent evt) {
f.delete();
try {
System.out.println("Le Fichier existe t-il ? "+f.exists());
f.createNewFile();
} catch (IOException ex) {
Logger.getLogger(Fenetre.class.getName()).log(Level.SEVERE, null, ex);
}
int i=0;
ecrire("categorie;sous categorie;infos principales;liens principaux;infos secondaires;liens secondaires");
while(tabfiche[i] != null){
String ligne;
ligne = tabfiche[i].categorie+";";
ligne = ligne + tabfiche[i].souscategorie+";";
ligne = ligne + tabfiche[i].txtprincipal +";";
ligne = ligne + tabfiche[i].liensprincipaux +";";
ligne = ligne + tabfiche[i].txtsecondaire +";";
ligne = ligne + tabfiche[i].lienssecondaires+";";
System.out.println(ligne);
ecrire(ligne);
i++;
}
} |
merci de votre aide
remplacer par une collection
a la place du tableau
Code:
List<Cfiche> listefiche = new ArrayList <Cfiche>(40);
pour le remplir
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public void lire() throws FileNotFoundException, IOException{
String ligne;// une ligne du fichier lu recuperer par le buffer
String[] decoupe; // tableau contenant les parties de la ligne lu
Cfiche elt;
entree = new BufferedReader (new FileReader(fichier));
do{
ligne = entree.readLine();// lit une ligne du fichier
if(ligne != null){
decoupe = ligne.split(";");//decoupe chaque cellule du fichier
elt = new Cfiche(decoupe[0],decoupe[1],decoupe[2],decoupe[3],decoupe[4],decoupe[5]);
listefiche.add(elt);
idx = idx+1;
}
}
while(ligne != null);
entree.close();
} |
et pour vérifier que le champ n'existe pas avec la fonction index
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int index(){
int i=0;
int res=-1;
boolean test = false;
System.out.println(listefiche.size());
while(test == false && listefiche.size()>0 && i<listefiche.size()){
if(listefiche.get(i).getCategorie().contains(combocategorie.getSelectedItem().toString())&& listefiche.get(i).getSouscategorie().contains(combosouscategorie.getSelectedItem().toString())){
test = true;
res = i;
}
i++;
}
return res;
} |
et pour éviter les erreurs quand on parcours le tableau le tant que a comme argument :i<listefiche.size()