diff --git a/panel_admin_sae/.idea/misc.xml b/panel_admin_sae/.idea/misc.xml index 69ace3f6affaf674f40a55887f3d6f3564afd626..553254dc19478c7bb1144e200305164338bc8fb3 100644 --- a/panel_admin_sae/.idea/misc.xml +++ b/panel_admin_sae/.idea/misc.xml @@ -1,5 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> + <component name="PWA"> + <option name="enabled" value="true" /> + <option name="wasEnabledAtLeastOnce" value="true" /> + </component> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/out" /> </component> diff --git a/panel_admin_sae/panel_admin_sae.iml b/panel_admin_sae/panel_admin_sae.iml index e9d852cf27d6a6d2841269a168d48eaf9b2c218f..89451bf7df3d673166849cad804d8a190c89f24f 100644 --- a/panel_admin_sae/panel_admin_sae.iml +++ b/panel_admin_sae/panel_admin_sae.iml @@ -16,5 +16,31 @@ <SOURCES /> </library> </orderEntry> + <orderEntry type="module-library"> + <library name="JUnit4"> + <CLASSES> + <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> + <orderEntry type="module-library"> + <library name="JUnit5.8.1"> + <CLASSES> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> </component> </module> \ No newline at end of file diff --git a/panel_admin_sae/src/src/Panel.java b/panel_admin_sae/src/Panel.java similarity index 52% rename from panel_admin_sae/src/src/Panel.java rename to panel_admin_sae/src/Panel.java index 9fd5225b23266f956b3386283f57833a3de153b2..7895a1e4943af7e48de47c42028e879a9ff27240 100644 --- a/panel_admin_sae/src/src/Panel.java +++ b/panel_admin_sae/src/Panel.java @@ -1,5 +1,3 @@ -package src; - import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.event.ListSelectionEvent; @@ -9,6 +7,8 @@ import javax.swing.table.TableRowSorter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.WindowEvent; +import java.awt.event.WindowStateListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -18,6 +18,7 @@ import java.text.SimpleDateFormat; import java.util.Comparator; import java.util.Date; +// Classe principale Panel public class Panel implements ActionListener { private Connection dbConnection; private JFrame frame; @@ -31,77 +32,105 @@ public class Panel implements ActionListener { private JPasswordField passwordField; private boolean isLoggedIn = false; + // Constructeur de la classe Panel public Panel(Connection connection) { this.dbConnection = connection; - frame = new JFrame("Panel"); + initializeUI(); + } - // Définir la taille de la fenêtre en plein écran + private void initializeUI() { + frame = new JFrame("MatchZéro ADMIN"); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + initializeTextFields(); + setLookAndFeel(); + tabbedPane = new JTabbedPane(); + addLoginTab(); + addCompetitionsTab(); + frame.add(tabbedPane); + frame.setVisible(true); - // Initialisation des champs de texte + frame.addWindowStateListener(new WindowStateListener() { + public void windowStateChanged(WindowEvent e) { + if ((e.getNewState() & Frame.ICONIFIED) == Frame.ICONIFIED) { + } else { + frame.setSize(1280, 720); + frame.setLocationRelativeTo(null); + } + } + }); + } + + + + // Méthode pour initialiser les champs de texte + private void initializeTextFields() { lieuField = new JTextField(20); horaireField = new JTextField(20); placesField = new JTextField(20); gagnantField = new JTextField(20); scoreField = new JTextField(20); + } + // Méthode pour définir l'apparence de l'interface + private void setLookAndFeel() { try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } + } - tabbedPane = new JTabbedPane(); - + // Méthode pour ajouter l'onglet de connexion + private void addLoginTab() { JPanel loginPanel = createLoginPanel(); tabbedPane.addTab("Login", loginPanel); + } + // Méthode pour ajouter l'onglet de gestion des compétitions + private void addCompetitionsTab() { competitionsPanel = createCompetitionsPanel(); tabbedPane.addTab("Gérer Compétitions", competitionsPanel); - tabbedPane.setEnabledAt(1, false); // Désactive l'onglet au démarrage - - frame.add(tabbedPane); - frame.setVisible(true); + tabbedPane.setEnabledAt(1, false); } - + // Méthode pour créer le panneau de connexion private JPanel createLoginPanel() { JPanel panel = new JPanel(new GridBagLayout()); panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50)); - GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(15, 15, 15, 15); gbc.fill = GridBagConstraints.HORIZONTAL; + addLogoToPanel(panel, gbc); + addLoginComponents(panel, gbc); + return panel; + } - // Chargement et ajout du logo redimensionné + // Méthode pour ajouter un logo au panneau + private void addLogoToPanel(JPanel panel, GridBagConstraints gbc) { try { BufferedImage logoImage = ImageIO.read(new File("logo.png")); - - // Redimensionner le logo - int newWidth = 100; // Largeur souhaitée - int newHeight = 100; // Hauteur souhaitée - Image resizedImage = logoImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); + Image resizedImage = logoImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH); ImageIcon logoIcon = new ImageIcon(resizedImage); - JLabel logoLabel = new JLabel(logoIcon); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; panel.add(logoLabel, gbc); - gbc.gridy = 1; // Incrémentation de la position Y pour le titre + gbc.gridy = 1; } catch (IOException e) { e.printStackTrace(); } + } - // Ajout du titre + // Méthode pour ajouter les composants de connexion au panneau + private void addLoginComponents(JPanel panel, GridBagConstraints gbc) { JLabel titleLabel = new JLabel("Connexion"); titleLabel.setFont(new Font("Arial", Font.BOLD, 36)); gbc.gridx = 0; gbc.gridwidth = 2; panel.add(titleLabel, gbc); - // Ajout du label et champ pour le nom d'utilisateur JLabel usernameLabel = new JLabel("Nom d'utilisateur:"); usernameLabel.setFont(new Font("Arial", Font.PLAIN, 20)); gbc.gridy = 2; @@ -113,7 +142,6 @@ public class Panel implements ActionListener { gbc.gridx = 1; panel.add(loginField, gbc); - // Ajout du label et champ pour le mot de passe JLabel passwordLabel = new JLabel("Mot de passe:"); passwordLabel.setFont(new Font("Arial", Font.PLAIN, 20)); gbc.gridx = 0; @@ -125,7 +153,6 @@ public class Panel implements ActionListener { gbc.gridx = 1; panel.add(passwordField, gbc); - // Ajout du bouton de connexion loginButton = new JButton("Connexion"); loginButton.setFont(new Font("Arial", Font.BOLD, 20)); loginButton.addActionListener(this); @@ -133,73 +160,90 @@ public class Panel implements ActionListener { gbc.gridy = 4; gbc.gridwidth = 2; panel.add(loginButton, gbc); + addActionToPasswordField(); + } - // Action lors de l'appui sur 'Entrée' dans le champ du mot de passe + // Méthode pour ajouter une action au champ de mot de passe + private void addActionToPasswordField() { passwordField.addActionListener(new ActionListener() { - @Override public void actionPerformed(ActionEvent e) { loginButton.doClick(); } }); - - return panel; } - - + // Méthode pour créer le panneau de gestion des compétitions private JPanel createCompetitionsPanel() { JPanel panel = new JPanel(new BorderLayout()); + initializeCompetitionsTable(); + JScrollPane scrollPane = new JScrollPane(competitionsTable); + panel.add(scrollPane, BorderLayout.CENTER); + addCompetitionTableSelectionListener(); + JPanel inputPanel = createInputPanel(); + panel.add(inputPanel, BorderLayout.SOUTH); + loadCompetitionsDataIfLoggedIn(); + return panel; + } + // Méthode pour initialiser le tableau des compétitions + private void initializeCompetitionsTable() { competitionsTableModel = new DefaultTableModel() { - @Override public boolean isCellEditable(int row, int column) { return false; } }; - competitionsTableModel.addColumn("ID"); competitionsTableModel.addColumn("Lieu"); competitionsTableModel.addColumn("Horaire"); competitionsTableModel.addColumn("Places Disponibles"); competitionsTableModel.addColumn("Gagnant"); competitionsTableModel.addColumn("Score"); - competitionsTable = new JTable(competitionsTableModel); TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(competitionsTableModel); competitionsTable.setRowSorter(sorter); - sorter.setComparator(0, Comparator.comparingInt(o -> (Integer) o)); sorter.setComparator(3, Comparator.comparingInt(o -> (Integer) o)); + } - JScrollPane scrollPane = new JScrollPane(competitionsTable); - panel.add(scrollPane, BorderLayout.CENTER); - + // Méthode pour ajouter un écouteur de sélection au tableau des compétitions + private void addCompetitionTableSelectionListener() { competitionsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { - if (!event.getValueIsAdjusting() && competitionsTable.getSelectedRow() != -1) { - int selectedRow = competitionsTable.getSelectedRow(); - if (selectedRow >= 0 && selectedRow < competitionsTableModel.getRowCount()) { - String lieu = (String) competitionsTableModel.getValueAt(selectedRow, 1); - String horaire = (String) competitionsTableModel.getValueAt(selectedRow, 2); - String places = String.valueOf(competitionsTableModel.getValueAt(selectedRow, 3)); - String gagnant = (String) competitionsTableModel.getValueAt(selectedRow, 4); - String score = (String) competitionsTableModel.getValueAt(selectedRow, 5); - - lieuField.setText(lieu); - horaireField.setText(horaire); - placesField.setText(places); - gagnantField.setText(gagnant); - scoreField.setText(score); - } - } + updateInputFieldsOnSelection(event); } }); + } - // Panel pour l'entrée des données - JPanel inputPanel = new JPanel(new GridLayout(6, 2, 10, 10)); // Mise à jour pour 6 lignes + // Méthode pour mettre à jour les champs de saisie lors de la sélection dans le tableau + private void updateInputFieldsOnSelection(ListSelectionEvent event) { + if (!event.getValueIsAdjusting() && competitionsTable.getSelectedRow() != -1) { + int selectedRow = competitionsTable.getSelectedRow(); + if (selectedRow >= 0 && selectedRow < competitionsTableModel.getRowCount()) { + String lieu = (String) competitionsTableModel.getValueAt(selectedRow, 1); + String horaire = (String) competitionsTableModel.getValueAt(selectedRow, 2); + String places = String.valueOf(competitionsTableModel.getValueAt(selectedRow, 3)); + String gagnant = (String) competitionsTableModel.getValueAt(selectedRow, 4); + String score = (String) competitionsTableModel.getValueAt(selectedRow, 5); + lieuField.setText(lieu); + horaireField.setText(horaire); + placesField.setText(places); + gagnantField.setText(gagnant); + scoreField.setText(score); + } + } + } + + // Méthode pour créer le panneau d'entrée des données + private JPanel createInputPanel() { + JPanel inputPanel = new JPanel(new GridLayout(6, 2, 10, 10)); inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + addInputFieldsToPanel(inputPanel); + addButtonsToPanel(inputPanel); + return inputPanel; + } - // Ajout des champs de texte et des étiquettes au panel + // Méthode pour ajouter des champs de saisie au panneau + private void addInputFieldsToPanel(JPanel inputPanel) { inputPanel.add(new JLabel("Lieu :")); inputPanel.add(lieuField); inputPanel.add(new JLabel("Horaire :")); @@ -210,48 +254,67 @@ public class Panel implements ActionListener { inputPanel.add(gagnantField); inputPanel.add(new JLabel("Score :")); inputPanel.add(scoreField); + } - // Boutons pour les actions + // Méthode pour ajouter des boutons au panneau + private void addButtonsToPanel(JPanel inputPanel) { addButton = new JButton("Ajouter"); addButton.addActionListener(this); deleteButton = new JButton("Supprimer"); deleteButton.addActionListener(this); updateButton = new JButton("Modifier"); updateButton.addActionListener(this); - - JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 10, 0)); // Mise à jour pour 3 boutons + JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 10, 0)); buttonPanel.add(addButton); buttonPanel.add(deleteButton); buttonPanel.add(updateButton); - inputPanel.add(buttonPanel); + } - panel.add(inputPanel, BorderLayout.SOUTH); - + // Méthode pour charger les données des compétitions si l'utilisateur est connecté + private void loadCompetitionsDataIfLoggedIn() { if (isLoggedIn) { loadCompetitionsData(); } - - return panel; } + // Méthode de validation des données de compétition + public boolean validateCompetitionData(String lieu, String horaire, String places) { + if (lieu == null || lieu.isEmpty()) return false; + if (horaire == null || horaire.isEmpty()) return false; + try { + Integer.parseInt(places); + } catch (NumberFormatException e) { + return false; + } + return true; + } + // Méthode de validation du format de la date et de l'heure + public boolean validateDateTime(String dateTime) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setLenient(false); + try { + sdf.parse(dateTime); + return true; + } catch (ParseException e) { + return false; + } + } + // Méthode de conversion d'une chaîne en entier + public int convertToInt(String numberStr) throws NumberFormatException { + return Integer.parseInt(numberStr); + } - - - - - - - private void loadCompetitionsData() { + // Méthode pour charger les données des compétitions depuis la base de données + public void loadCompetitionsData() { competitionsTableModel.setRowCount(0); try { String query = "SELECT c.CompetitionID, c.Lieu, c.Horaire, c.PlacesDisponibles, r.Gagnant, r.Score FROM competitions c LEFT JOIN resultats r ON c.CompetitionID = r.CompetitionID"; Statement stmt = dbConnection.createStatement(); ResultSet rs = stmt.executeQuery(query); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - while (rs.next()) { int competitionID = rs.getInt("CompetitionID"); String lieu = rs.getString("Lieu"); @@ -259,7 +322,6 @@ public class Panel implements ActionListener { int placesDisponibles = rs.getInt("PlacesDisponibles"); String gagnant = rs.getString("Gagnant"); String score = rs.getString("Score"); - String formattedDate = sdf.format(horaire); competitionsTableModel.addRow(new Object[]{competitionID, lieu, formattedDate, placesDisponibles, gagnant, score}); } @@ -269,49 +331,32 @@ public class Panel implements ActionListener { } } - - - @Override + // Gestionnaire d'événements pour les interactions avec les boutons public void actionPerformed(ActionEvent e) { - // Gestion de l'événement de clic sur le bouton de connexion if (e.getSource() == loginButton) { authenticateUser(); - } - - // Gestion de l'événement de clic sur le bouton "Ajouter" - else if (e.getSource() == addButton && isLoggedIn) { - showAddCompetitionDialog(); // Ouvre la boîte de dialogue pour ajouter une compétition - } - - // Gestion de l'événement de clic sur le bouton "Supprimer" - else if (e.getSource() == deleteButton && isLoggedIn) { - deleteCompetition(); // Supprime la compétition sélectionnée - } - - // Gestion de l'événement de clic sur le bouton "Modifier" - else if (e.getSource() == updateButton && isLoggedIn) { - updateCompetition(); // Met à jour la compétition sélectionnée + } else if (e.getSource() == addButton && isLoggedIn) { + showAddCompetitionDialog(); + } else if (e.getSource() == deleteButton && isLoggedIn) { + deleteCompetition(); + } else if (e.getSource() == updateButton && isLoggedIn) { + updateCompetition(); } } - + // Méthode pour authentifier l'utilisateur private void authenticateUser() { String enteredLogin = loginField.getText(); char[] enteredPasswordChars = passwordField.getPassword(); String enteredPassword = new String(enteredPasswordChars); - try { PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM admin WHERE login = ? AND password = ?"); stmt.setString(1, enteredLogin); stmt.setString(2, enteredPassword); - ResultSet rs = stmt.executeQuery(); - if (rs.next()) { isLoggedIn = true; - tabbedPane.setEnabledAt(1, true); // Active l'onglet "Gérer Compétitions" - // La ligne suivante a été supprimée car il n'y a plus de troisième onglet - // tabbedPane.setEnabledAt(2, true); + tabbedPane.setEnabledAt(1, true); tabbedPane.setSelectedComponent(competitionsPanel); loadCompetitionsData(); tabbedPane.remove(0); @@ -324,6 +369,7 @@ public class Panel implements ActionListener { } + // Méthode pour afficher le dialogue d'ajout d'une compétition private void showAddCompetitionDialog() { JDialog addDialog = new JDialog(frame, "Ajouter une Compétition", true); addDialog.setLayout(new GridLayout(0, 2)); @@ -337,68 +383,79 @@ public class Panel implements ActionListener { addTimeSpinner.setEditor(timeEditor); JTextField addPlacesField = new JTextField(); - addDialog.add(new JLabel("Lieu:")); - addDialog.add(addLieuField); - addDialog.add(new JLabel("Date (yyyy-MM-dd):")); - addDialog.add(addDateField); - addDialog.add(new JLabel("Heure (HH:mm:ss):")); - addDialog.add(addTimeSpinner); - addDialog.add(new JLabel("Places Disponibles:")); - addDialog.add(addPlacesField); + addDialogComponents(addDialog, addLieuField, addDateField, addTimeSpinner, addPlacesField); JButton submitButton = new JButton("Ajouter"); - submitButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String lieu = addLieuField.getText().trim(); - String date = addDateField.getText().trim(); - String time = timeEditor.getFormat().format(addTimeSpinner.getValue()); - String places = addPlacesField.getText().trim(); - - if (lieu.isEmpty() || places.isEmpty()) { - JOptionPane.showMessageDialog(addDialog, "Le lieu et le nombre de places disponibles ne peuvent pas être vides.", "Erreur", JOptionPane.ERROR_MESSAGE); - return; - } - - try { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String dateTime = date + " " + time; - Date parsedDate = sdf.parse(dateTime); - Timestamp horaire = new Timestamp(parsedDate.getTime()); - int placesDisponibles = Integer.parseInt(places); - - // Insertion des données dans la base de données - PreparedStatement stmt = dbConnection.prepareStatement( - "INSERT INTO competitions (Lieu, Horaire, PlacesDisponibles) VALUES (?, ?, ?)"); - stmt.setString(1, lieu); - stmt.setTimestamp(2, horaire); - stmt.setInt(3, placesDisponibles); - - int rowsAffected = stmt.executeUpdate(); - if (rowsAffected > 0) { - JOptionPane.showMessageDialog(addDialog, "Compétition ajoutée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); - loadCompetitionsData(); // Mettre à jour la liste des compétitions - } else { - JOptionPane.showMessageDialog(addDialog, "Erreur lors de l'ajout de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE); - } - - addDialog.dispose(); - } catch (NumberFormatException ex) { - JOptionPane.showMessageDialog(addDialog, "Le nombre de places doit être un nombre valide.", "Erreur", JOptionPane.ERROR_MESSAGE); - } catch (ParseException ex) { - JOptionPane.showMessageDialog(addDialog, "Format de date ou d'heure invalide.", "Erreur", JOptionPane.ERROR_MESSAGE); - } catch (SQLException ex) { - ex.printStackTrace(); - JOptionPane.showMessageDialog(addDialog, "Erreur lors de l'accès à la base de données.", "Erreur", JOptionPane.ERROR_MESSAGE); - } - } - }); + submitButton.addActionListener(e -> handleAddCompetition(addDialog, addLieuField, addDateField, timeEditor, addPlacesField)); addDialog.add(submitButton); addDialog.setVisible(true); } + // Méthode pour ajouter les composants au dialogue d'ajout + private void addDialogComponents(JDialog dialog, JTextField lieuField, JTextField dateField, JSpinner timeSpinner, JTextField placesField) { + dialog.add(new JLabel("Lieu:")); + dialog.add(lieuField); + dialog.add(new JLabel("Date (yyyy-MM-dd):")); + dialog.add(dateField); + dialog.add(new JLabel("Heure (HH:mm:ss):")); + dialog.add(timeSpinner); + dialog.add(new JLabel("Places Disponibles:")); + dialog.add(placesField); + } + + // Méthode pour gérer l'ajout d'une compétition + private void handleAddCompetition(JDialog dialog, JTextField lieuField, JTextField dateField, JSpinner.DateEditor timeEditor, JTextField placesField) { + String lieu = lieuField.getText().trim(); + String date = dateField.getText().trim(); + String time = timeEditor.getFormat().format(timeEditor.getModel().getValue()); + String places = placesField.getText().trim(); + + if (validateCompetitionDataForAdd(lieu, date, places)) { + addCompetitionToDatabase(dialog, lieu, date, time, places); + } + } + + // Méthode pour valider les données saisies pour la compétition + private boolean validateCompetitionDataForAdd(String lieu, String date, String places) { + if (lieu.isEmpty() || places.isEmpty()) { + JOptionPane.showMessageDialog(null, "Le lieu et le nombre de places disponibles ne peuvent pas être vides.", "Erreur", JOptionPane.ERROR_MESSAGE); + return false; + } + return true; + } + + // Méthode pour ajouter une compétition à la base de données + private void addCompetitionToDatabase(JDialog dialog, String lieu, String date, String time, String places) { + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateTime = date + " " + time; + Date parsedDate = sdf.parse(dateTime); + Timestamp horaire = new Timestamp(parsedDate.getTime()); + int placesDisponibles = Integer.parseInt(places); + + PreparedStatement stmt = dbConnection.prepareStatement( + "INSERT INTO competitions (Lieu, Horaire, PlacesDisponibles) VALUES (?, ?, ?)"); + stmt.setString(1, lieu); + stmt.setTimestamp(2, horaire); + stmt.setInt(3, placesDisponibles); + + int rowsAffected = stmt.executeUpdate(); + if (rowsAffected > 0) { + JOptionPane.showMessageDialog(dialog, "Compétition ajoutée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); + loadCompetitionsData(); + } else { + JOptionPane.showMessageDialog(dialog, "Erreur lors de l'ajout de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE); + } + + dialog.dispose(); + } catch (NumberFormatException | ParseException | SQLException ex) { + JOptionPane.showMessageDialog(dialog, "Erreur lors de l'ajout de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE); + } + } + + // Méthode pour afficher la boîte de dialogue de mise à jour du score private void showUpdateScoreDialog(int competitionID) { JDialog updateDialog = new JDialog(frame, "Mettre à jour le Score", true); updateDialog.setLayout(new GridLayout(0, 2)); @@ -413,44 +470,59 @@ public class Panel implements ActionListener { updateDialog.add(scoreField); JButton submitButton = new JButton("Mettre à jour"); - submitButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String gagnant = gagnantField.getText().trim(); - String score = scoreField.getText().trim(); + submitButton.addActionListener(e -> handleUpdateScore(updateDialog, competitionID, gagnantField, scoreField)); - if (gagnant.isEmpty() || score.isEmpty()) { - JOptionPane.showMessageDialog(updateDialog, "Le gagnant et le score ne peuvent pas être vides.", "Erreur", JOptionPane.ERROR_MESSAGE); - return; - } + updateDialog.add(submitButton); + updateDialog.setVisible(true); + } - try { - PreparedStatement stmt = dbConnection.prepareStatement("UPDATE resultats SET Gagnant = ?, Score = ? WHERE CompetitionID = ?"); - stmt.setString(1, gagnant); - stmt.setString(2, score); - stmt.setInt(3, competitionID); - - int rowsAffected = stmt.executeUpdate(); - if (rowsAffected > 0) { - JOptionPane.showMessageDialog(updateDialog, "Score mis à jour avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); - loadCompetitionsData(); - } else { - JOptionPane.showMessageDialog(updateDialog, "Erreur lors de la mise à jour du score.", "Erreur", JOptionPane.ERROR_MESSAGE); - } - - updateDialog.dispose(); - } catch (SQLException ex) { - ex.printStackTrace(); - JOptionPane.showMessageDialog(updateDialog, "Erreur lors de l'accès à la base de données.", "Erreur", JOptionPane.ERROR_MESSAGE); - } + // Méthode pour gérer la mise à jour du score + private void handleUpdateScore(JDialog dialog, int competitionID, JTextField gagnantField, JTextField scoreField) { + String gagnant = gagnantField.getText().trim(); + String score = scoreField.getText().trim(); + + if (validateScoreData(gagnant, score)) { + JFrame messageFrame = new JFrame("Message"); + updateScoreInDatabase(messageFrame, competitionID, gagnant, score); // Utilisation d'une nouvelle fenêtre 'messageFrame' + } + } + + + // Méthode pour valider les données du score + private boolean validateScoreData(String gagnant, String score) { + if (gagnant.isEmpty() || score.isEmpty()) { + JOptionPane.showMessageDialog(null, "Le gagnant et le score ne peuvent pas être vides.", "Erreur", JOptionPane.ERROR_MESSAGE); + return false; + } + return true; + } + + // Méthode pour mettre à jour le score dans la base de données + private void updateScoreInDatabase(JFrame dialog, int competitionID, String gagnant, String score) { + try { + PreparedStatement stmt = dbConnection.prepareStatement("UPDATE resultats SET Gagnant = ?, Score = ? WHERE CompetitionID = ?"); + stmt.setString(1, gagnant); + stmt.setString(2, score); + stmt.setInt(3, competitionID); + + // Exécution de la mise à jour + int rowsAffected = stmt.executeUpdate(); + if (rowsAffected > 0) { + JOptionPane.showMessageDialog(dialog, "Score mis à jour avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); + loadCompetitionsData(); + } else { + JOptionPane.showMessageDialog(dialog, "Erreur lors de la mise à jour du score.", "Erreur", JOptionPane.ERROR_MESSAGE); } - }); - updateDialog.add(submitButton); - updateDialog.setVisible(true); + dialog.dispose(); + } catch (SQLException ex) { + JOptionPane.showMessageDialog(dialog, "Erreur lors de l'accès à la base de données.", "Erreur", JOptionPane.ERROR_MESSAGE); + } } + + // Méthode pour supprimer une compétition private void deleteCompetition() { int selectedRow = competitionsTable.getSelectedRow(); if (selectedRow != -1) { @@ -461,7 +533,6 @@ public class Panel implements ActionListener { stmt.setInt(1, competitionID); int rowsAffected = stmt.executeUpdate(); - if (rowsAffected > 0) { JOptionPane.showMessageDialog(frame, "Compétition supprimée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); loadCompetitionsData(); @@ -476,35 +547,17 @@ public class Panel implements ActionListener { } } + // Méthode pour mettre à jour une compétition private void updateCompetition() { int selectedRow = competitionsTable.getSelectedRow(); + if (selectedRow != -1) { int competitionID = (int) competitionsTableModel.getValueAt(selectedRow, 0); String gagnant = gagnantField.getText(); String score = scoreField.getText(); - if (gagnant.isEmpty() || score.isEmpty()) { - JOptionPane.showMessageDialog(frame, "Le gagnant et le score ne peuvent pas être vides.", "Erreur", JOptionPane.ERROR_MESSAGE); - return; - } - - try { - // Mise à jour du gagnant et du score - PreparedStatement stmt = dbConnection.prepareStatement("UPDATE resultats SET Gagnant = ?, Score = ? WHERE CompetitionID = ?"); - stmt.setString(1, gagnant); - stmt.setString(2, score); - stmt.setInt(3, competitionID); - int rowsAffected = stmt.executeUpdate(); - - if (rowsAffected > 0) { - JOptionPane.showMessageDialog(frame, "Compétition mise à jour avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE); - loadCompetitionsData(); - } else { - JOptionPane.showMessageDialog(frame, "Erreur lors de la mise à jour de la compétition.", "Erreur", JOptionPane.ERROR_MESSAGE); - } - } catch (SQLException ex) { - ex.printStackTrace(); - JOptionPane.showMessageDialog(frame, "Erreur lors de l'accès à la base de données.", "Erreur", JOptionPane.ERROR_MESSAGE); + if (validateScoreData(gagnant, score)) { + updateScoreInDatabase(frame, competitionID, gagnant, score); } } else { JOptionPane.showMessageDialog(frame, "Veuillez sélectionner une compétition à mettre à jour.", "Sélection requise", JOptionPane.WARNING_MESSAGE); @@ -512,10 +565,6 @@ public class Panel implements ActionListener { } - - - - public static void main(String[] args) { try { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", ""); @@ -524,4 +573,4 @@ public class Panel implements ActionListener { e.printStackTrace(); } } -} +} \ No newline at end of file diff --git a/panel_admin_sae/src/PanelTest.java b/panel_admin_sae/src/PanelTest.java new file mode 100644 index 0000000000000000000000000000000000000000..82980453a0c6a85f4555dbe32c1950840361f91d --- /dev/null +++ b/panel_admin_sae/src/PanelTest.java @@ -0,0 +1,55 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PanelTest { + + + @Test + void validateCompetitionData_WithValidData_ShouldReturnTrue() { + Panel panel = new Panel(null); + + assertTrue(panel.validateCompetitionData("Paris", "2024-01-01 10:00", "100")); + } + + @Test + void validateCompetitionData_WithInvalidData_ShouldReturnFalse() { + Panel panel = new Panel(null); + + assertFalse(panel.validateCompetitionData("", "2024-01-01 10:00", "100")); + assertFalse(panel.validateCompetitionData("Paris", "", "100")); + assertFalse(panel.validateCompetitionData("Paris", "2024-01-01 10:00", "abc")); // non-numérique + } + + @Test + void validateDateTime_WithValidDateTime_ShouldReturnTrue() { + Panel panel = new Panel(null); + + assertTrue(panel.validateDateTime("2024-01-01 12:00:00")); + } + + @Test + void validateDateTime_WithInvalidDateTime_ShouldReturnFalse() { + Panel panel = new Panel(null); + + assertFalse(panel.validateDateTime("2024-01-01 25:00:00")); // Heure invalide + assertFalse(panel.validateDateTime("2024-13-01 12:00:00")); // Mois invalide + assertFalse(panel.validateDateTime("2024-02-30 12:00:00")); // Jour invalide + } + + @Test + void convertToInt_WithValidNumber_ShouldReturnInt() { + Panel panel = new Panel(null); + + assertEquals(100, panel.convertToInt("100")); + } + + @Test + void convertToInt_WithInvalidNumber_ShouldThrowException() { + Panel panel = new Panel(null); + + assertThrows(NumberFormatException.class, () -> { + panel.convertToInt("abc"); + }); + } +} diff --git a/panel_admin_sae/src/src/logo.png b/panel_admin_sae/src/src/logo.png deleted file mode 100644 index d631bc9f3c33c9b3e3cc4b8832a5fbb98904a956..0000000000000000000000000000000000000000 Binary files a/panel_admin_sae/src/src/logo.png and /dev/null differ