From 31e6489e31459fd410e26e44a9ca02b04e3340b2 Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Mon, 28 Apr 2025 18:39:47 +0200 Subject: [PATCH] Ajout des fichiers issus de la correction --- com/humanbooster/exercices/Bibliotheque.java | 83 ++++++++++++++++++ com/humanbooster/exercices/Livre.java | 89 ++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 com/humanbooster/exercices/Bibliotheque.java create mode 100644 com/humanbooster/exercices/Livre.java diff --git a/com/humanbooster/exercices/Bibliotheque.java b/com/humanbooster/exercices/Bibliotheque.java new file mode 100644 index 0000000..e465554 --- /dev/null +++ b/com/humanbooster/exercices/Bibliotheque.java @@ -0,0 +1,83 @@ +package com.humanbooster.exercices; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Bibliotheque { + private List livres; + private Map emprunts; + + public Bibliotheque() { + this.livres = new ArrayList<>(); + this.emprunts = new HashMap<>(); + } + + public boolean ajouterLivre(Livre livre) { + if (livre == null || livres.contains(livre)) { + return false; + } + return livres.add(livre); + } + + public boolean supprimerLivre(String isbn) { + Livre livre = rechercherLivre(isbn); + if (livre == null) { + return false; + } + return livres.remove(livre); + } + + // public boolean supprimerLivre(String isbn) { + // return livres.removeIf(l -> l.getIsbn().equals(isbn)); + // } + + public Livre rechercherLivre(String isbn) { + return livres.stream() + .filter(l -> l.getIsbn().equals(isbn)) + .findFirst() + .orElse(null); + } + + public boolean emprunterLivre(String isbn) { + Livre livre = rechercherLivre(isbn); + if (livre == null || !livre.isDisponible() || emprunts.containsKey(isbn)) { + return false; + } + livre.setDisponible(false); + emprunts.put(isbn, LocalDate.now()); + return true; + } + + public boolean rendreLivre(String isbn) { + + // verifier qu'il s'agit bien d'un livre emprunté + if (!emprunts.containsKey(isbn)) { + return false; + } + + // Récupérer le livre et le retirer de la liste d'emprunts + Livre livre = rechercherLivre(isbn); + if (livre == null) { + return false; + } + livre.setDisponible(true); + emprunts.remove(isbn); + return true; + } + + public List getLivresDisponibles() { + return livres.stream() + .filter(Livre::isDisponible) + .collect(Collectors.toList()); + } + + public List getLivresEmpruntes() { + return livres.stream() + .filter(l -> !l.isDisponible()) + .collect(Collectors.toList()); + } +} diff --git a/com/humanbooster/exercices/Livre.java b/com/humanbooster/exercices/Livre.java new file mode 100644 index 0000000..49d8845 --- /dev/null +++ b/com/humanbooster/exercices/Livre.java @@ -0,0 +1,89 @@ +package com.humanbooster.exercices; + +import java.util.Objects; + +public class Livre { + private String isbn; + private String titre; + private String auteur; + private int anneePublication; + private boolean disponible; + + public Livre(String isbn, String titre, String auteur, int anneePublication, boolean disponible) { + this.isbn = isbn; + this.titre = titre; + this.auteur = auteur; + this.anneePublication = anneePublication; + this.disponible = disponible; + } + + public int getAnneePublication() { + return anneePublication; + } + + public String getAuteur() { + return auteur; + } + + public String getIsbn() { + return isbn; + } + + public String getTitre() { + return titre; + } + + public boolean isDisponible() { + return disponible; + } + + public void setAnneePublication(int anneePublication) { + this.anneePublication = anneePublication; + } + + public void setAuteur(String auteur) { + this.auteur = auteur; + } + + public void setTitre(String titre) { + this.titre = titre; + } + + public void setDisponible(boolean disponible) { + this.disponible = disponible; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + @Override + public boolean equals(Object o) { + // si mon objet courant est égal return true + if (this == o) { + return true; + } + // si mon objet reçu est null ou ne correspond pas à ma classe Livre + // je retourne false + if (o == null || getClass() != o.getClass()) { + return false; + } + // cast mon objet en livre et comparer en fonction de l'isbn + Livre livre = (Livre) o; + return Objects.equals(isbn, livre.isbn); + } + + @Override + public String toString() { + return "Livre{" + + "isbn='" + isbn + '\'' + + ", titre='" + titre + '\'' + + ", auteur='" + auteur + '\'' + + ", anneePublication=" + anneePublication + + ", disponible=" + disponible + + '}'; + } + + + +}