Files
2026_TD2_DEV51_Qualite_Algo/Bibliotheque.java
T
2026-10-14 12:43:59 +02:00

158 lines
4.9 KiB
Java

/*
* R5.A.04 - Qualité Algorithmique
* TD Complexité cyclomatique - Partie Java
*
* Pour chaque méthode, calculez la complexité cyclomatique M.
*/
import java.util.ArrayList;
import java.util.List;
public class Bibliotheque {
static class Livre {
String titre, auteur, genre;
int annee;
boolean emprunte, reserve, abime;
Livre(String titre, String auteur, String genre, int annee) {
this.titre = titre;
this.auteur = auteur;
this.genre = genre;
this.annee = annee;
}
}
static class Adherent {
String nom;
boolean etudiant, cotisationAJour;
int nbEmprunts, joursDeRetardCumules;
Adherent(String nom, boolean etudiant) {
this.nom = nom;
this.etudiant = etudiant;
this.cotisationAJour = true;
}
}
private final List<Livre> livres = new ArrayList<>();
/* Exercice J1 */
public int nombreDeLivres() {
return livres.size();
}
/* Exercice J2 */
public boolean estDisponible(Livre livre) {
return !livre.emprunte && !livre.reserve && !livre.abime;
}
/* Exercice J3 */
public String peutEmprunter(Adherent a, Livre l) {
if (!a.cotisationAJour)
return "Cotisation à renouveler";
if (a.joursDeRetardCumules > 30)
return "Trop de retards";
int maxEmprunts = a.etudiant ? 8 : 5;
if (a.nbEmprunts >= maxEmprunts)
return "Nombre maximum d'emprunts atteint";
if (l.emprunte)
return "Livre déjà emprunté";
else if (l.reserve && !a.etudiant)
return "Livre réservé";
else if (l.abime)
return "Livre en réparation";
return "OK";
}
/* Exercice J4 */
public double calculerPenalite(int joursRetard, Adherent a, int nbLivres) {
if (joursRetard <= 0 || nbLivres <= 0)
return 0;
double tarif;
if (joursRetard <= 7)
tarif = 0.20;
else if (joursRetard <= 30)
tarif = 0.50;
else
tarif = 1.00;
double penalite = 0;
for (int i = 0; i < nbLivres; i++) {
penalite += joursRetard * tarif;
if (i >= 2)
penalite += 1.0;
}
if (a.etudiant)
penalite *= 0.5;
return penalite > 50 ? 50 : penalite;
}
/* Exercice J5 */
public List<Livre> rechercher(String motCle, String genre, int anneeMin, boolean disponiblesSeulement) {
List<Livre> resultats = new ArrayList<>();
for (Livre l : livres) {
if (genre != null && !l.genre.equals(genre))
continue;
if (l.annee < anneeMin)
continue;
if (disponiblesSeulement && !estDisponible(l))
continue;
if (motCle == null || motCle.isEmpty()) {
resultats.add(l);
} else {
String m = motCle.toLowerCase();
if (l.titre.toLowerCase().contains(m) || l.auteur.toLowerCase().contains(m))
resultats.add(l);
}
}
return resultats;
}
/* Exercice J6 - M = 3 : 1 + for + if (avant refactorisation : M = 8). */
public int importer(List<String> lignes) {
int importes = 0;
for (String ligne : lignes) {
if (importerLigne(ligne))
importes++;
}
return importes;
}
/* M = 4 : 1 + 2 if + ||. */
private boolean importerLigne(String ligne) {
if (ligne.isBlank() || ligne.startsWith("#"))
return false;
String[] champs = ligne.split(";");
if (champs.length != 4) {
System.err.println("Ligne ignorée : " + ligne);
return false;
}
return importerLivre(champs);
}
/* M = 4 : 1 + if + || + catch. */
private boolean importerLivre(String[] champs) {
try {
int annee = Integer.parseInt(champs[3].trim());
if (annee <= 0 || annee > 2026)
return false;
livres.add(new Livre(champs[0], champs[1], champs[2], annee));
return true;
} catch (NumberFormatException e) {
System.err.println("Année invalide : " + champs[3]);
return false;
}
}
public static void main(String[] args) {
Bibliotheque b = new Bibliotheque();
b.importer(List.of("# titre;auteur;genre;annee",
"Le Petit Prince;Saint-Exupéry;conte;1943",
"Dune;Herbert;SF;1965",
"Fondation;Asimov;SF;abc"));
Adherent a = new Adherent("Maxime", true);
System.out.println(b.nombreDeLivres() + " " + b.rechercher("dune", "SF", 1900, true).size()
+ " " + b.peutEmprunter(a, b.livres.get(0)) + " " + b.calculerPenalite(10, a, 3));
}
}