Ajout des fichiers issus de la correction
This commit is contained in:
83
com/humanbooster/exercices/Bibliotheque.java
Normal file
83
com/humanbooster/exercices/Bibliotheque.java
Normal 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());
|
||||
}
|
||||
}
|
||||
89
com/humanbooster/exercices/Livre.java
Normal file
89
com/humanbooster/exercices/Livre.java
Normal 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 +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user