commit fafa1a27c5a9796cafd26e94b094b6ee19c6dd38 Author: Awen Lelu Date: Fri Nov 14 10:13:20 2025 +0100 init diff --git a/bibliotheque.sql b/bibliotheque.sql new file mode 100644 index 0000000..b7cb3fe --- /dev/null +++ b/bibliotheque.sql @@ -0,0 +1,38 @@ +-- Création de la base +DROP DATABASE IF EXISTS bibliotheque; +CREATE DATABASE bibliotheque; +USE bibliotheque; + +-- Table des clients +CREATE TABLE Clients ( + id_client INT AUTO_INCREMENT PRIMARY KEY, + nom VARCHAR(50) NOT NULL, + prenom VARCHAR(50) NOT NULL +); + +-- Table des livres +CREATE TABLE Livres ( + id_livre INT AUTO_INCREMENT PRIMARY KEY, + titre VARCHAR(200) NOT NULL, + auteur VARCHAR(100) NOT NULL, + disponible BOOLEAN NOT NULL DEFAULT TRUE +); + +-- Table des emprunts +CREATE TABLE Emprunts ( + id_emprunt INT AUTO_INCREMENT PRIMARY KEY, + id_client INT NOT NULL, + id_livre INT NOT NULL, + date_emprunt DATE NOT NULL, + date_retour DATE DEFAULT NULL, + FOREIGN KEY (id_client) REFERENCES Clients(id_client), + FOREIGN KEY (id_livre) REFERENCES Livres(id_livre) +); + +-- Quelques données de test +INSERT INTO Clients (nom, prenom) +VALUES ('Dupont','Jean'); + +INSERT INTO Livres (titre, auteur) +VALUES ('1984','George Orwell'), + ('Le Petit Prince','Antoine de Saint-Exupéry'); diff --git a/compile.bat b/compile.bat new file mode 100755 index 0000000..f1fb708 --- /dev/null +++ b/compile.bat @@ -0,0 +1,3 @@ +call mvn clean +call mvn package -e +java -jar .\target\gestion-bibliotheque-1.0.jar \ No newline at end of file diff --git a/creation_bdd.bat b/creation_bdd.bat new file mode 100755 index 0000000..92051b0 --- /dev/null +++ b/creation_bdd.bat @@ -0,0 +1 @@ +mysql -u root -p --execute="source bibliotheque.sql" \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..9c05123 --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + com.ic-epsi + gestion-bibliotheque + 1.0 + + + + maven-jar-plugin + 3.3.0 + + + + Main + + + + + + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + Main + + + + + + + + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + junit-jupiter-api + org.junit.jupiter + + + junit-jupiter-params + org.junit.jupiter + + + junit-jupiter-engine + org.junit.jupiter + + + + + + 17 + 17 + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ab30a33 --- /dev/null +++ b/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + com.ic-epsi + gestion-bibliotheque + 1.0 + + + 17 + 17 + + + + + + com.mysql + mysql-connector-j + 8.0.33 + + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + + + Main + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + Main + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..2c34e92 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,45 @@ + +import java.sql.Connection; +import java.sql.SQLException; +import java.time.LocalDate; + +public class Main { + public static void main(String[] args) { + //bibliotheque.Livre livre = new bibliotheque.Livre("1984", "George Orwell"); + System.out.println("Bonjour !"); + try (Connection conn = bibliotheque.BDDbilbio.getConnection()) { + System.out.println("Connexion réussie à la base de données !"); + + // Ajout d'un livre + System.out.println("=====Ajout d'un livre====="); + bibliotheque.Livre livre = new bibliotheque.Livre(conn); + livre.addToBDD("Le petit prince", "Antoine de Saint Exupéry"); + + livre.afficherTous(); + + // Ajout d'un client + System.out.println("=====Ajout d'un client====="); + bibliotheque.Client client = new bibliotheque.Client(conn); + client.addToBDD("Nicolas", "Zaimeche"); + + client.afficherTous(); + + System.out.println("=====Ajout d'un emprunt====="); + bibliotheque.Emprunt emprunt = new bibliotheque.Emprunt(conn); + Integer id_livre = livre.findId("Le petit prince", "Antoine de Saint Exupéry"); + Integer id_client = client.findId("Nicolas", "Zaimeche");; + emprunt.addToBDD(id_client, id_livre, LocalDate.now(), 4); + emprunt.afficherTous(); + emprunt.deleteByLivre(id_livre); + emprunt.afficherTous(); + livre.deleteFromBDD("Le petit prince", "Antoine de Saint Exupéry"); + livre.afficherTous(); + client.deleteFromBDD("Nicolas", "Zaimeche"); + client.afficherTous(); + + } catch (SQLException e) { + System.out.println("Erreur de connexion : " + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/bibliotheque/BDDbilbio.java b/src/main/java/bibliotheque/BDDbilbio.java new file mode 100644 index 0000000..4b23cd4 --- /dev/null +++ b/src/main/java/bibliotheque/BDDbilbio.java @@ -0,0 +1,20 @@ +package bibliotheque; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class BDDbilbio { + private static final String URL = ""; + private static final String USER = ""; + private static final String PASSWORD = ""; + + public static Connection getConnection() throws SQLException { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return DriverManager.getConnection(URL, USER, PASSWORD); + } +} \ No newline at end of file diff --git a/src/main/java/bibliotheque/Client.java b/src/main/java/bibliotheque/Client.java new file mode 100644 index 0000000..6e504c3 --- /dev/null +++ b/src/main/java/bibliotheque/Client.java @@ -0,0 +1,93 @@ +package bibliotheque; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Client { + // Classe pour interagir avec la table Clients + + private Connection conn; + + public Client(Connection conn) + { + this.conn = conn; + } + + public void addToBDD(String nom, String prenom) + { + // Ajout d'un Client à la bilbiothèque + String sql = "INSERT INTO Clients(nom, prenom) VALUES (?, ?)"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + stmt.setString(1, nom); + stmt.setString(2, prenom); + stmt.executeUpdate(); + System.out.println("Client ajouté : " + nom); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void afficherTous() + { + String sql = "SELECT id_client, nom, prenom FROM Clients"; + try (Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + + System.out.println("Liste des Clients :"); + while (rs.next()) { + int id = rs.getInt("id_client"); + String nom = rs.getString("nom"); + String prenom = rs.getString("prenom"); + System.out.println(id + " | " + nom + " | " + prenom); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void deleteFromBDD(String nom, String prenom) + { + String sql = "DELETE FROM clients WHERE nom = ? AND prenom = ?"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + + stmt.setString(1, nom); + stmt.setString(2, prenom); + int rows = stmt.executeUpdate(); + + if (rows > 0) { + System.out.println("Client supprimé : " + nom + " " + prenom); + } else { + System.out.println("Aucun client trouvé avec ce nom/prenom."); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public Integer findId(String nom, String prenom) + { + String sql = "SELECT id_client FROM Clients WHERE nom = ? AND prenom = ?"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + stmt.setString(1, nom); + stmt.setString(2, prenom); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("id_client"); + } else { + System.out.println("Aucun client trouvé pour : " + prenom + " " + nom); + return null; + } + } + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } +} \ No newline at end of file diff --git a/src/main/java/bibliotheque/Emprunt.java b/src/main/java/bibliotheque/Emprunt.java new file mode 100644 index 0000000..b975062 --- /dev/null +++ b/src/main/java/bibliotheque/Emprunt.java @@ -0,0 +1,77 @@ +package bibliotheque; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Date; + +public class Emprunt { + // Classe pour interagir avec la table Emprunts + + private Connection conn; + + public Emprunt(Connection conn) + { + this.conn = conn; + } + + public void addToBDD(int idClient, int idLivre, java.time.LocalDate dateEmprunt, int dureeSemaines) + { + String addEmpruntSql = "INSERT INTO Emprunts(id_client, id_livre, date_emprunt, date_retour) VALUES (?, ?, ?, ?)"; + String updateLivreSql = "UPDATE Livres SET disponible = FALSE WHERE id_livre = ?"; + try { + PreparedStatement stmtEmprunt = conn.prepareStatement(addEmpruntSql); + stmtEmprunt.setInt(1, idClient); + stmtEmprunt.setInt(2, idLivre); + stmtEmprunt.setDate(3, Date.valueOf(dateEmprunt)); // conversion LocalDate -> SQL Date + stmtEmprunt.setDate(4, Date.valueOf(dateEmprunt.plusWeeks(dureeSemaines))); + + stmtEmprunt.executeUpdate(); + System.out.println("Nouvel emprunt ajouté : client " + idClient + ", livre " + idLivre); + + PreparedStatement stmtDisponible = conn.prepareStatement(updateLivreSql); + stmtDisponible.setInt(1, idLivre); + stmtDisponible.executeUpdate(); + + } + catch (SQLException e) { + e.printStackTrace(); + } + } + + public void afficherTous() { + String sql = "SELECT id_emprunt, id_client, id_livre, date_emprunt, date_retour FROM Emprunts"; + try (PreparedStatement stmt = conn.prepareStatement(sql); + ResultSet rs = stmt.executeQuery()) { + + System.out.println("=== Liste des emprunts ==="); + while (rs.next()) { + int idEmprunt = rs.getInt("id_emprunt"); + int idClient = rs.getInt("id_client"); + int idLivre = rs.getInt("id_livre"); + java.sql.Date dateEmprunt = rs.getDate("date_emprunt"); + java.sql.Date dateRetour = rs.getDate("date_retour"); + + System.out.println( + "Emprunt #" + idEmprunt + + " | Client: " + idClient + + " | Livre: " + idLivre + + " | Date emprunt: " + dateEmprunt + + " | Date retour: " + (dateRetour != null ? dateRetour : "Non rendu") + ); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void deleteByLivre(Integer idLivre) throws SQLException { + String sql = "DELETE FROM emprunts WHERE id_livre = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, idLivre); + ps.executeUpdate(); + } + } +} diff --git a/src/main/java/bibliotheque/Livre.java b/src/main/java/bibliotheque/Livre.java new file mode 100644 index 0000000..2a0efd7 --- /dev/null +++ b/src/main/java/bibliotheque/Livre.java @@ -0,0 +1,90 @@ +package bibliotheque; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Livre { + // Classe pour interagir avec la table Livres + + private Connection conn; + + public Livre(Connection conn) + { + this.conn = conn; + } + + public void addToBDD(String titre, String auteur) { + // Ajout d'un livre à la bilbiothèque + String sql = "INSERT INTO livres(titre, auteur) VALUES (?, ?)"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + stmt.setString(1, titre); + stmt.setString(2, auteur); + stmt.executeUpdate(); + System.out.println("Livre ajouté : " + titre); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void afficherTous() { + String sql = "SELECT id_livre, titre, auteur FROM livres"; + try (Statement stmt = this.conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + + System.out.println("Liste des livres :"); + while (rs.next()) { + int id = rs.getInt("id_livre"); + String titre = rs.getString("titre"); + String auteur = rs.getString("auteur"); + System.out.println(id + " | " + titre + " | " + auteur); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void deleteFromBDD(String titre, String auteur) { + String sql = "DELETE FROM livres WHERE titre = ? AND auteur = ?"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + + stmt.setString(1, titre); + stmt.setString(2, auteur); + int rows = stmt.executeUpdate(); + + if (rows > 0) { + System.out.println("Livre supprimé : " + titre + " de " + auteur); + } else { + System.out.println("Aucun livre trouvé avec ce titre/auteur."); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public Integer findId(String titre, String auteur) + { + String sql = "SELECT id_livre FROM Livres WHERE titre = ? AND auteur = ?"; + try (PreparedStatement stmt = this.conn.prepareStatement(sql)) { + stmt.setString(1, titre); + stmt.setString(2, auteur); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("id_livre"); + } else { + System.out.println("Aucun livre trouvé pour : " + titre + " " + auteur); + return null; + } + } + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } +} \ No newline at end of file diff --git a/src/test/java/ClientTest.java b/src/test/java/ClientTest.java new file mode 100644 index 0000000..44ea1b6 --- /dev/null +++ b/src/test/java/ClientTest.java @@ -0,0 +1,3 @@ +public class ClientTest { + +} diff --git a/src/test/java/EmpruntTest.java b/src/test/java/EmpruntTest.java new file mode 100644 index 0000000..4a0816a --- /dev/null +++ b/src/test/java/EmpruntTest.java @@ -0,0 +1,3 @@ +public class EmpruntTest { + +} diff --git a/src/test/java/LivreTest.java b/src/test/java/LivreTest.java new file mode 100644 index 0000000..b67d704 --- /dev/null +++ b/src/test/java/LivreTest.java @@ -0,0 +1,42 @@ +package bibliotheque; + +import org.junit.jupiter.api.*; +import java.sql.Connection; +import java.sql.SQLException; +import static org.junit.jupiter.api.Assertions.*; + +public class LivreTest { + + private static Connection conn; + private Livre livre; + + @BeforeAll + static void initConnection() throws SQLException { + conn = bibliotheque.BDDbilbio.getConnection(); + assertNotNull(conn, "Connexion à la BDD échouée !"); + } + + @BeforeEach + void setup() { + livre = new Livre(conn); + } + + @Test + void testAddAndFindLivre() throws SQLException { + livre.addToBDD("TestLivre", "AuteurTest"); + Integer id = livre.findId("TestLivre", "AuteurTest"); + assertNotNull(id, "Le livre n'a pas été trouvé après insertion !"); + } + + @Test + void testDeleteLivre() throws SQLException { + livre.deleteFromBDD("TestLivre", "AuteurTest"); + Integer id = livre.findId("TestLivre", "AuteurTest"); + assertNull(id, "Le livre devrait avoir été supprimé !"); + } + + @AfterAll + static void closeConn() throws SQLException { + conn.close(); + } +} \ No newline at end of file