forked from menault/2026_TD2_DEV51_Qualite_Algo
Téléverser les fichiers vers "/"
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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 */
|
||||
public int importer(List<String> lignes) {
|
||||
int importes = 0;
|
||||
for (String ligne : lignes) {
|
||||
if (ligne.isBlank() || ligne.startsWith("#"))
|
||||
continue;
|
||||
String[] champs = ligne.split(";");
|
||||
if (champs.length != 4) {
|
||||
System.err.println("Ligne ignorée : " + ligne);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
int annee = Integer.parseInt(champs[3].trim());
|
||||
if (annee > 0 && annee <= 2026) {
|
||||
livres.add(new Livre(champs[0], champs[1], champs[2], annee));
|
||||
importes++;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.err.println("Année invalide : " + champs[3]);
|
||||
}
|
||||
}
|
||||
return importes;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user