TD2 terminé

This commit is contained in:
rabot
2026-10-14 12:43:59 +02:00
parent cecf7379f6
commit 826cde779c
3 changed files with 105 additions and 17 deletions
+29 -17
View File
@@ -108,30 +108,42 @@ public class Bibliotheque {
return resultats;
}
/* Exercice J6 */
/* 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 (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]);
}
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",