Ajout des fichiers issus de la correction

This commit is contained in:
Vincent Guillet
2025-04-28 18:39:47 +02:00
parent 7334089f81
commit 31e6489e31
2 changed files with 172 additions and 0 deletions

View File

@@ -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<Livre> livres;
private Map<String, LocalDate> 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<Livre> getLivresDisponibles() {
return livres.stream()
.filter(Livre::isDisponible)
.collect(Collectors.toList());
}
public List<Livre> getLivresEmpruntes() {
return livres.stream()
.filter(l -> !l.isDisponible())
.collect(Collectors.toList());
}
}

View File

@@ -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 +
'}';
}
}