import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import java.util.SortedMap; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; // application utilisant des Collections de Java public class MiniGuichetEtudiant extends JPanel{ private JTextField inputCours, inputEtudiant; private JTextArea trace; private JButton nouveauCoursB, listeCoursB, listeEtudiantsB, listeTrieeParDateB,inscriptionCoursB; SortedMap> lesCours; MiniGuichetEtudiant(){ creerObjets(); placerObjets(); ecouterObjets(); lesCours = new TreeMap>(); } private void creerObjets(){ inputCours = new JTextField(40); inputEtudiant = new JTextField(40); trace = new JTextArea(20,80); nouveauCoursB = new JButton("nouveau cours"); listeCoursB = new JButton("liste cours"); listeEtudiantsB = new JButton("liste etudiants"); listeTrieeParDateB = new JButton("tri par date"); inscriptionCoursB = new JButton("inscription"); } private void placerObjets(){ setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); JPanel pCours = new JPanel(); pCours.add(new JLabel("nom du cours")); pCours.add(inputCours); add(pCours); JPanel pEtudiant = new JPanel(); pEtudiant.add(new JLabel("Prenom et nom")); pEtudiant.add(inputEtudiant); add(pEtudiant); JPanel controle = new JPanel(); controle.add(nouveauCoursB); controle.add(listeCoursB); controle.add(listeEtudiantsB); controle.add(listeTrieeParDateB); controle.add(inscriptionCoursB); add(controle); add(new JScrollPane(trace)); } private void ecouterObjets(){ nouveauCoursB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ nouveauCours(inputCours.getText()); } }); listeCoursB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ listeCours(); } }); listeEtudiantsB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ listeEtudiants(inputCours.getText(), false); } }); listeTrieeParDateB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ listeEtudiants(inputCours.getText(), true); } }); inscriptionCoursB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ StringTokenizer st = new StringTokenizer(inputEtudiant.getText()); inscriptionCours(inputCours.getText(), st.hasMoreElements()?st.nextToken():"", st.hasMoreElements()?st.nextToken():""); } }); } private void nouveauCours(String nomTitre){ nomTitre = nomTitre.trim(); if(nomTitre.length()==0){ trace.append("nom de cours vide\n"); return; } int i = nomTitre.indexOf(' '); Cours c; if (i==-1) // pas de titre c = new Cours(nomTitre); else // sépare le nom du titre c = new Cours(nomTitre.substring(0,i), nomTitre.substring(i).trim()); // ajoute un cours au début de la liste des cours if (!lesCours.containsKey(c)){ lesCours.put(c,new TreeSet()); trace.append("Ajoute "+ c.getNom()+":"+ c.getTitre()+"\n"); } else trace.append("Le cours "+ c.getNom() + " existe deja"+"\n"); } private void listeCours(){ // imprime la liste de tous les cours trace.append("Liste des cours"+"\n"); for(Cours c : lesCours.keySet()) trace.append(c.getNom()+":"+c.getTitre()+"\n"); } private void listeEtudiants(String nomCours, boolean parDate){ // imprime la liste triée de tous les inscrits à un cours trace.append("Liste des étudiants du cours "+ nomCours+"\n"); if (parDate) trace.append("par date"+"\n"); SortedSet etuds = lesCours.get(new Cours(nomCours)); if (etuds==null) trace.append("Le cours "+ nomCours + " n'existe pas"+"\n"); else { if(parDate){ SortedSet ss = new TreeSet(Etudiant.parDateInscription); ss.addAll(etuds); etuds = ss; } trace.append(""+etuds+"\n"); } } private void inscriptionCours(String nomCours, String prenom, String nom){ // inscrit un nouvel étudiant à un cours if (prenom.length()==0 && nom.length()==0) trace.append("Prenom et nom vides"+"\n"); else { trace.append("Inscription de "+ prenom + " "+ nom + " au cours "+ nomCours+"\n"); SortedSet etuds = lesCours.get(new Cours(nomCours)); if (etuds == null) trace.append("Le cours "+ nomCours + " n'existe pas"+"\n"); else { Etudiant e = new Etudiant(prenom,nom, new Date()); if (!etuds.add(e)) trace.append(prenom + " " + nom + " est deja inscrit au cours " + nomCours+"\n"); } } } public static void main(String[] args){ JFrame f = new JFrame(); f.getContentPane().add(new MiniGuichetEtudiant()); f.setSize(700,350); f.setLocation(100,100); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }