IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Problème de recherche des éléments dans une liste.


Sujet :

avec Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut Problème de recherche des éléments dans une liste.
    Bonjour,

    J'ai une liste d'objets de type "FichePatient" qui a plusieurs attributs "nom, prénom,etc....".

    Voici la méthode qui retourne cette liste:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     public List getAllFichesPatients(){
     
            FichePatient fichePatient1 = new FichePatient();
            fichePatient1.setNomPatient("Amine");
            fichePatient1.setPrenomPatient("Rachid");
            fichePatient1.setDatePremiereVisitePatient("01/01/2011");
            fichePatient1.setDateDerniereVisitePatient("30/01/2011");
            fichePatient1.setReferenceDossierPatient("refernec 1");
     
     
            FichePatient fichePatient2 = new FichePatient();
            fichePatient2.setNomPatient("talbi");
            fichePatient2.setPrenomPatient("mohammed");
            fichePatient2.setDatePremiereVisitePatient("02/02/2012");
            fichePatient2.setDateDerniereVisitePatient("30/02/2012");
            fichePatient2.setReferenceDossierPatient("refernec 2");
     
     
            FichePatient fichePatient3 = new FichePatient();
            fichePatient3.setNomPatient("zitouni");
            fichePatient3.setPrenomPatient("rabii");
            fichePatient3.setDatePremiereVisitePatient("01/01/2013");
            fichePatient3.setDateDerniereVisitePatient("30/01/2013");
            fichePatient3.setReferenceDossierPatient("refernec 3");
     
            FichePatient fichePatient4 = new FichePatient();
            fichePatient4.setNomPatient("charef");
            fichePatient4.setPrenomPatient("kika");
            fichePatient4.setDatePremiereVisitePatient("01/01/2014");
            fichePatient4.setDateDerniereVisitePatient("30/01/2014");
            fichePatient4.setReferenceDossierPatient("refernec 4");
     
            FichePatient fichePatient5 = new FichePatient();
            fichePatient5.setNomPatient("charef");
            fichePatient5.setPrenomPatient("mskouta");
            fichePatient5.setDatePremiereVisitePatient("01/01/2015");
            fichePatient5.setDateDerniereVisitePatient("30/01/2015");
            fichePatient5.setReferenceDossierPatient("refernec 5");
     
            FichePatient fichePatient6 = new FichePatient();
            fichePatient6.setNomPatient("do3dou3i");
            fichePatient6.setPrenomPatient("jawad");
            fichePatient6.setDatePremiereVisitePatient("01/01/2016");
            fichePatient6.setDateDerniereVisitePatient("30/01/2016");
            fichePatient6.setReferenceDossierPatient("refernec 6");
     
     
            FichePatient fichePatient7 = new FichePatient();
            fichePatient7.setNomPatient("talbi");
            fichePatient7.setPrenomPatient("abd elaziz");
            fichePatient7.setDatePremiereVisitePatient("01/01/2017");
            fichePatient7.setDateDerniereVisitePatient("30/01/2017");
            fichePatient7.setReferenceDossierPatient("refernec 7");
     
     
            FichePatient fichePatient8 = new FichePatient();
            fichePatient8.setNomPatient("Ben chekroun nizar");
            fichePatient8.setPrenomPatient("jamal");
            fichePatient8.setDatePremiereVisitePatient("01/01/2018");
            fichePatient8.setDateDerniereVisitePatient("30/01/2018");
            fichePatient8.setReferenceDossierPatient("refernec 8");
     
     
            fichePatientList.add(fichePatient1);
            fichePatientList.add(fichePatient2);
            fichePatientList.add(fichePatient3);
            fichePatientList.add(fichePatient4);
            fichePatientList.add(fichePatient5);
            fichePatientList.add(fichePatient6);
            fichePatientList.add(fichePatient7);
            fichePatientList.add(fichePatient8);
     
            return fichePatientList;
     
        }
    Je veux faire une méthode qui va me permettre de chercher les patients dont le nom commence par la chaine de caractère passé en paramètre.

    j'ai crée cette méthode :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    private List getfichesPatientByNomOrPrenom(String nomPrenom, List fiches){
            //La requete a modifier.
            List result = null;
            if(fiches != null && fiches.size()>0){
               result = new ArrayList();
               Iterator iter = fiches.iterator();
               while(iter.hasNext()){
                   FichePatient fichePatient = (FichePatient)iter.next();
                   if((fichePatient.getNomPatient().startsWith(nomPrenom) )){
                            result.add(fichePatient);
                   }
               }
            }
            return result;
        }

    ça marche bien si par exemple je cherche par "ch" ou "cha", j'obtiens bien les deux objets "charef kika" et "charef mskouta".

    Problème:

    -Le problème que j'ai c'est que lorsque je passe en paramètre "charef k" seulement, je veux récupérer l'objet "charef kika" sachant que "kika" et le prénom.

    J'ai pensé à prendre seulement les caractères qui sont avant l'espace, mais le problème se pose pour le cas de par exemple j'ai trois objet dont le nom commence par "ben" comme "ben chekroun" et "ben jelloun" et "ben taleb", donc ce cas j'aurais un problème si je tape "ben chek" car si je prends seulement l'avant espace la chaine "ben" ça va me renvoyer les trois objets.

    J'espère bien que j'ai bien expliqué et merci de me donner une solution pour ça.

    Merci d'avance.

  2. #2
    Membre éclairé

    Homme Profil pro
    Développeur mobile iOS / Android
    Inscrit en
    Décembre 2008
    Messages
    259
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur mobile iOS / Android
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 259
    Points : 690
    Points
    690
    Par défaut
    Bonjour,

    une solution c'est d'utiliser la fonction split en te basant pour séparateur l'espace. Ensuite tu travaille avec le deuxième terme après l'espace, il me semble moins problématique dans ton cas comme tu as plusieurs nom commençant par ben.

    cordialement
    « Il est assez difficile de trouver une erreur dans son code quand on la cherche. C’est encore bien plus dur quand on est convaincu que le code est juste. » - Steve McConnell

    N'oubliez pas de consulter les FAQ Swift, Android
    Tutoriel : Développer une application multilingue sous iOS

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut
    Salut,

    Merci pour votre réponse, mais je vois pas bien ce qu'il faut faire, car le vrai problème se pose au niveau suivant :

    le cas de "charef kika", si je cherche par "charef k" je vois pas comment indiquer au système que le nom est "charef" et "k" c'est le début du prénom?car si j'utilise la fonction "split" et j' oblige le système de chercher dans les prénoms par ce qui' est après l' espace , là j'aurais un autre problème, c'est que parfois on trouve "ben x" est le nom.

    Exemple :

    patient1 : nom = "charef" , prénom="kika".
    patient2: nom = "ben x", prénom="toto".

    Comment regrouper ces deux cas?

    j'espère que le problème est plus claire.

    Merci d'avance.

  4. #4
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2009
    Messages
    130
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2009
    Messages : 130
    Points : 276
    Points
    276
    Par défaut
    Moi je te conseillerai plutot de concatener nom + Prenom avant de faire ton test, dans le genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    FichePatient fichePatient = (FichePatient)iter.next();
    String nomCompletPatient = fichePatient.getNomPatient() + " " + fichePatient.getPrenomPatient() ;
    if((nomCompletPatient.startsWith(nomPrenom) )){
        result.add(fichePatient);
    }
    MigouW

    La seule bataille perdue d'avance est celle que l'on refuse de livrer.


    Pensez au tag
    Ma réponse vous a été utile, votez plus 1 sur le message.
    Ma réponse est hors sujet, votez moins 1 sur le message.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    99
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 99
    Points : 66
    Points
    66
    Par défaut
    Une autre solution serait de passer en paramètre les champs que tu veux prendre en compte et la valeur que tu cherches pour chaque champ.

    Tu peux ensuite chercher les champs concernés parmi les champs renvoyé par :

    FichePatient.class.getFields

    Puis, pour chaque fiche tester si les champs choisis ont la bonne valeur.

    Ceci te permettrait de faire une recherche avec différents critères. Pas seulement les nom et prénom

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut
    Merci d'abord pour vos réponses:

    je suis d'accord avec toi, sauf que je vois pas comment le faire, par exemple :

    nom = "charef" et prenom="kika", si je lance la recherche par la chaine "charef ki", que dois-je faire pour indiquer au programme qu'il faut chercher dans les prénoms qui commencent par "ki" sachant que le nom = "charef"? j'utilise la concaténation?.

    Merci d'avance.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    99
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 99
    Points : 66
    Points
    66
    Par défaut
    Pour ça il existe plusieurs possibilité.

    L'une d'elles serait de créer une FichePatient vierge :
    FichePatient ficheRequete = new FichePatient();

    où le constructeur FichePatient() te permet de remplir tes fiches avec une boite de dialogue.

    Il faut créer une JFrame ayant la propriété AlwaysOnTop, et 1 JTextField par champ désiré.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class FichePatient {
     
    String nom = "";
    String prenom = "";
    etc...
     
        public FichePatient() {
     
            String[] nomChamps = this.getClass().getFields();
            listeChamps = new JTextField[nomChamps.length];
            JButton OK = new JButton("OK");
     
            JFrame dialogue = new JFrame("dialogue");
            dialogue.setSize(300, 300);//valeurs au pif. A adapter
            dialogue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            dialogue.setAlwaysOnTop(true);
     
            OK.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        for(int i=0; i<n; i++) {
                                            this.getClass().getField(nomChamps[i]).
                                                 set(this,listeChamps[i].getText());
                                        }
                                        dialogue.dispose();
                                    }
            };
     
            Container conteneur = dialogue.getContentPane();
            conteneur.setLayout(new BoxLayout(conteneur, BoxLayout.Y_AXIS));
     
            for(int i=0;i<nomChamps.length;i++) {
                JTextField jtf = new JTextField();
                conteneur.add(Box.createVerticalStrut(20));
                conteneur.add(new Ligne(nomChamps[i],jtf));
            }
     
            conteneur.add(Box.createVerticalGlue());
            conteneur.add(OK);
     
            conteneur.add(Box.createVerticalStrut(20));
            dialogue.setContentPane(conteneur);
            dialogue.setVisible(true);
        }
    }
     
    class Ligne extends JPanel {
        protected Ligne(String s, JEditorPane jtf) {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            add(Box.createHorizontalStrut(30));
            add(new JLabel(s+"  :  "));
            add(jtf);
            add(Box.createHorizontalStrut(50));
        }
    }
    Ensuite, tu remplaces

    getfichesPatientByNomOrPrenom(String nomPrenom, List fiches)

    par

    getfichesPatientByAnything(FichePatient requete, List fiches)

    Enfin, ta méthode de recherche pourrait ressembler à ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    public FichePatient recherche(List fiches) {
        FichePatient requete = new FichePatient();
        return getfichesPatientByAnything(requete, fiches);
    }
    Bon, c'est perfectible, mais c'est déjà pas mal.
    Par contre, je te conseil de créer un autre constructeur que celui que j'ai créé parce que sinon, tu va avoir des surprise quand tu voudras créer une fiche vierge ^^

  8. #8
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut
    Merci pour votre aide.
    ça marche bien.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 13/12/2010, 09h34
  2. [MySQL] Comment afficher des éléments dans une liste déroulante tout en dissimulant un ?
    Par lou87 dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 23/04/2009, 16h45
  3. Réponses: 10
    Dernier message: 11/03/2009, 17h30
  4. Réponses: 3
    Dernier message: 03/11/2008, 10h09
  5. prélevez des éléments dans une liste
    Par Ganondorf dans le forum Langage
    Réponses: 4
    Dernier message: 17/10/2007, 00h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo