lundi 3 juin 2019

How to replace many if's statements in java?

I am trying to make my code "cleaner". I have many functions with a lot of if. How can I replace them?

I'm using 4 differents hashtable that's why I have those ifs. The only thing changing is the hashtable name and the jList where I add the informations.

if(n.getCategorie().getNum() == Categorie.INTERNATIONAL.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListInternationales.getModel();
                    dlm.addElement(n.getTitre());
                    jListInternationales.setModel(dlm);
                    NewsInter.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.POLITIQUE.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListViePolitique.getModel();
                    dlm.addElement(n.getTitre());
                    jListViePolitique.setModel(dlm);
            NewsPolitique.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.RAGOT.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListRagotsEtPotins.getModel();
                    dlm.addElement(n.getTitre());
                    jListRagotsEtPotins.setModel(dlm);
            NewsRagot.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.SPORT.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListInfosSports.getModel();
                    dlm.addElement(n.getTitre());
                    jListInfosSports.setModel(dlm);
            NewsSport.put(""+nNews, n);
                }

Aucun commentaire:

Enregistrer un commentaire