From 4a3a790dc0210a5744ee6f4c8f3f9987d6685856 Mon Sep 17 00:00:00 2001 From: saaya-code Date: Sun, 24 Mar 2024 03:33:06 +0100 Subject: [PATCH] tested EtudiantDao and added events listenner --- src/ExercicesTP/CRUD/EtudiantDAO.java | 3 ++ src/ExercicesTP/IHM/IHMAjoutEtudiant.java | 38 +++++++++++++++++++++-- src/ExercicesTP/IHM/Principale.java | 6 +++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/ExercicesTP/CRUD/EtudiantDAO.java b/src/ExercicesTP/CRUD/EtudiantDAO.java index a4c663e..6805069 100644 --- a/src/ExercicesTP/CRUD/EtudiantDAO.java +++ b/src/ExercicesTP/CRUD/EtudiantDAO.java @@ -6,8 +6,11 @@ import java.sql.*; + +// TODO: Test this class public class EtudiantDAO implements EtudiantDaoCRUD{ + Connection con = null; Statement st = null; public EtudiantDAO() { diff --git a/src/ExercicesTP/IHM/IHMAjoutEtudiant.java b/src/ExercicesTP/IHM/IHMAjoutEtudiant.java index bd627c8..2833ddd 100644 --- a/src/ExercicesTP/IHM/IHMAjoutEtudiant.java +++ b/src/ExercicesTP/IHM/IHMAjoutEtudiant.java @@ -1,4 +1,7 @@ package ExercicesTP.IHM; +import ExercicesTP.CRUD.EtudiantDAO; +import ExercicesTP.Etudiant; + import javax.swing.*; import java.awt.*; @@ -7,15 +10,16 @@ public class IHMAjoutEtudiant extends JInternalFrame{ public JTextField nomField, prenomField, numEtdField; public JComboBox filiereComboBox; public JComboBox niveauComboBox, groupeComboBox; - + public EtudiantDAO dao; public JButton addButton, cancelButton; - public IHMAjoutEtudiant(){ + public IHMAjoutEtudiant(EtudiantDAO dao){ setTitle("Ajout d'un étudiant"); setSize(400, 400); initializeComponents(); createLayout(); - //addEventListeners(); + addEventListeners(); setVisible(true); + this.dao = dao; } private void addComponent(JPanel panel, JComponent component, GridBagConstraints c, int x, int y, int width, int height) { c.gridx = x; @@ -68,5 +72,33 @@ public void createLayout() { addComponent(panel, cancelButton, c, 1, 6, 1, 1); add(panel); } + public void emptyForm(){ + nomField.setText(""); + prenomField.setText(""); + numEtdField.setText(""); + filiereComboBox.setSelectedIndex(0); + niveauComboBox.setSelectedIndex(0); + groupeComboBox.setSelectedIndex(0); + } + public void addEventListeners(){ + addButton.addActionListener((e)->{ + // Get the values from the form + String nom = nomField.getText(); + String prenom = prenomField.getText(); + int numEtd = Integer.parseInt(numEtdField.getText()); + String filiere = (String) filiereComboBox.getSelectedItem(); + int niveau = (int) niveauComboBox.getSelectedItem(); + int groupe = (int) groupeComboBox.getSelectedItem(); + Etudiant etudiant = new Etudiant(numEtd, nom, prenom, filiere, niveau, groupe); + dao.addEtudiant(etudiant); + JOptionPane.showMessageDialog(this, "Etudiant ajouté avec succès"); + emptyForm(); + }); + + cancelButton.addActionListener((e)->{ + emptyForm(); + dispose(); + }); + } } \ No newline at end of file diff --git a/src/ExercicesTP/IHM/Principale.java b/src/ExercicesTP/IHM/Principale.java index fa6eb85..36047bc 100644 --- a/src/ExercicesTP/IHM/Principale.java +++ b/src/ExercicesTP/IHM/Principale.java @@ -1,6 +1,8 @@ package ExercicesTP.IHM; +import ExercicesTP.CRUD.EtudiantDAO; import ExercicesTP.CRUD.FormationDAO; +import ExercicesTP.Etudiant; import TP_Base.Config; import TP_Base.MyConnexion; @@ -29,6 +31,7 @@ public class Principale extends JFrame { JMenuItem menuItemAfficherFormation; JDesktopPane desktop; FormationDAO formationDao; + EtudiantDAO etudiantDAO; Principale() { menuBar = new JMenuBar(); this.setTitle("Gestion des formation"); @@ -73,6 +76,7 @@ public class Principale extends JFrame { this.setJMenuBar(menuBar); this.setVisible(true); formationDao = new FormationDAO(); + etudiantDAO = new EtudiantDAO(); // listenners menuItemAjouteurFormation.addActionListener(new ActionListener() { @Override @@ -100,7 +104,7 @@ public void actionPerformed(ActionEvent e) { menuItemAjouteurEtudiant.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - IHMAjoutEtudiant ihmAjoutEtud = new IHMAjoutEtudiant(); + IHMAjoutEtudiant ihmAjoutEtud = new IHMAjoutEtudiant(etudiantDAO); desktop.add(ihmAjoutEtud); } });