Skip to content

Instantly share code, notes, and snippets.

@GravenilvecTV
Created March 28, 2020 16:00
Show Gist options
  • Save GravenilvecTV/e173f00a29b848aa13b4fa09ce35ee7e to your computer and use it in GitHub Desktop.
Save GravenilvecTV/e173f00a29b848aa13b4fa09ce35ee7e to your computer and use it in GitHub Desktop.
Correction TP 9/30 - Exo Java 2 - Rédacteur de livre
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Livre {
// attributs
// nom du livre
// liste de pages
private String nomLivre;
private List<Page> pages;
// constructeur
public Livre(String nomLivre) {
this.nomLivre = nomLivre;
this.pages = new ArrayList<>();
}
// methodes
public void ajouterPage(List<String> lignes) {
// créer la page
pages.add(new Page(lignes));
}
public void ajouterPage(String contenu) {
// créer la page
List<String> lignes = Arrays.asList(contenu.split("\n"));
pages.add(new Page(lignes));
}
public List<String> getPage(int numeroPage) {
return pages.get(numeroPage).getLignes();
}
public int getNombrePages(){
return this.pages.size();
}
public String getNom(){
return nomLivre;
}
}
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
// variable livre
Livre livreGraven = new Livre("Les Bases de la programmation");
// créer une instance de PdfReader
try{
PdfReader pdf = new PdfReader("livre.pdf");
for(int i = 1; i < pdf.getNumberOfPages(); i++){
livreGraven.ajouterPage(PdfTextExtractor.getTextFromPage(pdf, i));
}
} catch(Exception e){
e.printStackTrace();
}
// proposer à l'utilisateur d'entrer un mot
Scanner scan = new Scanner(System.in);
String mot = scan.next();
AtomicInteger compteurMots = new AtomicInteger();
// afficher la page 1
livreGraven.getPage(1)
.stream()
.filter(ligne -> ligne.contains(mot))
.forEach(ligneavecmot -> {
compteurMots.getAndIncrement();
});
// Afficher le nombre de mots
System.out.println("Nombre de fois : " + mot + " -> " + compteurMots + " fois");
}
}
import java.util.List;
public class Page {
// attribut
private List<String> lignes;
// constructeur
public Page(List<String> lignes){
this.lignes = lignes;
}
// methode
public List<String> getLignes() {
return this.lignes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment