Correction exercice 4

import java.util.LinkedList;
/**
 * Un document dispose d'un titre, est composé d'un ensemble de
 * section et connaît la section en cours de manipulation. Il offre
 * des services de déplacements entre section, d'ajout de section et
 * d'affichage
 */
public class Document {
    /** Le titre du document */
    private String title;
    /** Les sections contenues dans le document. */
    private LinkedList<Section> sections;
    /** L'index de la section courante (-1 s'il est avant le premier
     * élément). */
    private int currentPosition;

    /**
     * constructeur prenant comme paramètre le titre du document
     * @param theTitle titre du document
     */
    public Document(String theTitle){
        this.title = theTitle;
        this.sections = new LinkedList<Section>();
        this.currentPosition = 0;
    }
    /** modifie la section courante en passant à la section suivante
     * (s'il y a d'autres sections)
     */
    public void goToNextSection() {
        if (!this.sections.isEmpty() && this.currentPosition < sections.size() - 1) {
            this.currentPosition++;
        }
    }
    /** modifie la section courante en passant à la section précédente
     * (s'il y a d'autres sections)
     */
    public void goToPreviousSection() {
        if (!this.sections.isEmpty() && this.currentPosition > 0) {
            this.currentPosition--;
    }    

    /** ajoute après la section courante la nouvelle section passée en
     * paramètre. La section ajoutée devient la section courante
     * @param section la section à ajouter
     */
     public void insertSection(Section section) {
        if (this.sections.isEmpty()) {
            this.sections.add(section);
        } else {
            this.sections.add(this.currentPosition + 1, section);
        }
    }
    /** renvoie la section courante s'il y en a une
     */
     public Section currentSection() {
        if (!this.sections.isEmpty()) {
            return this.sections.get(this.currentPosition);
        }
        return null;
    }
    /** rend une chaîne de caractères qui décrit le document avec
     *  les sections et de leurs paragraphes associés
     * @result le contenu du document avec son titre
     */
    public String toString(){
        String body = "";
        int counter = 1;
        for(Section s : this.sections) {
            body += counter + " " + s;
            counter++;
        }
        return this.title + "\n" + body;
    }

    public static void main(String[] arg){
        Document doc = new Document("le document");

        Section sec1 = new Section("sec1");
        sec1.addParagraph(new Paragraph("le texte1"));
        sec1.addParagraph(new Paragraph("le texte2"));
        sec1.addParagraph(new Paragraph("le texte3"));
       
        Section sec2 = new Section("sec2");
        sec2.addParagraph(new Paragraph("le texte4"));

        Section sec3 = new Section("sec3");
        sec3.addParagraph(new Paragraph("le texte5"));

        doc.insertSection(sec1);
        doc.goToNextSection();
        doc.insertSection(sec2);
        doc.goToNextSection();
        doc.insertSection(sec3);
        doc.goToNextSection();

        System.out.println("Document :\n"+ doc);

        doc.goToPreviousSection();
        doc.goToPreviousSection();
        doc.goToPreviousSection();
        doc.goToPreviousSection();
        doc.insertSection(new Section("sec1Bis"));
        doc.goToNextSection();
        doc.goToNextSection();
        doc.insertSection(new Section("sec2Bis"));
        doc.goToNextSection();
        doc.goToNextSection();
        doc.goToNextSection();
        doc.insertSection(new Section("sec3Bis"));
        System.out.println("Document :\n"+ doc);
    }
}