Correction exercice 5

Voici le nouveau diagramme de classes avec les classes AbstractSection, NoAcknowledgementsSection et Acknowledgements

import java.util.LinkedList;

/**
 * Classe abstraite ! Une section dispose d'un titre qui sera défini par ses
 * sous-classes et est composé d'un ensemble de paragraphes. Elle offre l'accès
 * à son titre, l'ajout d'un paragraphe et son affichage
 */
public abstract class AbstractSection {

    private LinkedList<Paragraph> paragraphs;

    public AbstractSection() {
        this.paragraphs = new LinkedList<Paragraph>();
    }

    /**
     * ajoute le paragraphe en paramètre aux paragraphes de la section
     */
    public void addParagraph(Paragraph paragraph) {
        this.paragraphs.add(paragraph);
    }

    /**
     * méthode abstraite à redéfinir dans les sous-classes
     */
    public abstract String getTitle();

    /**
     * rend une chaîne de caractères qui décrit la section (son titre suivi des
     * paragraphes)
     */
    public String toString() {
        String res = this.getTitle() + "\n";
        for (Paragraph p : this.paragraphs) {
            res += "\t" + p + "\n";
        }
        return res;
    }
}
public class Acknowledgements extends AbstractSection {

    /** Le titre de la section est prédéfini ! */

    public String getTitle() {
        return("Remerciements");
    }

    public static void main(String[] arg) {
        AbstractSection sec = new Acknowledgements();

        sec.addParagraph(new Paragraph("le texte1"));
        sec.addParagraph(new Paragraph("le texte2"));
        System.out.println(sec.toString());
    }
}
/**
 * Une section dispose d'un numéro et d'un titre et est composé d'un ensemble de
 * paragraphes. Elle offre l'accès à son titre, l'ajout d'un paragraphe et son
 * affichage
 */
public class NoAcknowledgementsSection extends AbstractSection {

    private String title;

    /**
     * un constructeur prenant comme paramètre le titre de la section
     *
     * @param title le titre de la section
     */
    public NoAcknowledgementsSection(String theTitle) {
        super();
        this.title = theTitle;
    }

    public String getTitle() {
        return this.title;
    }

    public static void main(String[] arg) {
        AbstractSection sec1 = new NoAcknowledgementsSection("sec1");
        AbstractSection sec2 = new NoAcknowledgementsSection("sec2");
        AbstractSection sec3 = new NoAcknowledgementsSection("sec3");

        sec1.addParagraph(new Paragraph("le texte1"));
        System.out.println("Section 1 : " + sec1.toString());
        sec1.addParagraph(new Paragraph("le texte2"));
        System.out.println("Section 1 : " + sec1.toString());
        sec1.addParagraph(new Paragraph("le texte3"));
        System.out.println("Section 1 : " + sec1.toString());
        sec2.addParagraph(new Paragraph("le texte4"));
        System.out.println("Section 2 : " + sec2.toString());
        sec3.addParagraph(new Paragraph("le texte5"));
        System.out.println("Section 3 : " + sec3.toString());
        sec2.addParagraph(new Paragraph("le texte 4 bis"));
        System.out.println("Section 2 : " + sec2.toString());
    }
}
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 DocumentV2 {

    /**
     * Le titre du document
     */
    private String title;
    /**
     * Les sections contenues dans le document.
     */
    private LinkedList<AbstractSection> sections;
    /**
     * L'index de la section courante.
     */
    private int currentPosition;

    /**
     * constructeur prenant comme paramètre le titre du document
     *
     * @param theTitle titre du document
     */
    public DocumentV2(String theTitle) {
        this.title = theTitle;
        this.sections = new LinkedList<AbstractSection>();
        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++;
        }
    //    System.out.println("Current position is " + this.currentPosition + "/" + this.sections.size());
    }

    /**
     * modifie la section courante en passant à la section pécédente (s'il y a
     * d'autres sections)
     */
    public void goToPreviousSection() {
        if (!this.sections.isEmpty() && this.currentPosition > 0) {
            this.currentPosition--;
        }
    //    System.out.println("Current position is " + this.currentPosition + "/" + this.sections.size());
    }

    /**
     * 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(AbstractSection 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 AbstractSection 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 (AbstractSection s : this.sections) {
            body += counter + " " + s;
            counter++;
        }
        return this.title + "\n" + body;
    }

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

        AbstractSection sec1 = new SectionSaufRemerciements("sec1");
        sec1.addParagraph(new Paragraph("le texte1"));
        sec1.addParagraph(new Paragraph("le texte2"));
        sec1.addParagraph(new Paragraph("le texte3"));

        AbstractSection sec2 = new SectionSaufRemerciements("sec2");
        sec2.addParagraph(new Paragraph("le texte4"));

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

        doc.goToNextSection();
       

        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 SectionSaufRemerciements("sec1Bis"));
        doc.goToNextSection();
        doc.goToNextSection();
        doc.insertSection(new SectionSaufRemerciements("sec2Bis"));
        doc.goToNextSection();
        doc.goToNextSection();
        doc.goToNextSection();
        doc.insertSection(new SectionSaufRemerciements("sec3Bis"));

        AbstractSection sec4 = new Remerciements();
        sec4.addParagraph(new Paragraph("Merci mes amis"));
        sec4.addParagraph(new Paragraph("pour votre soutien indéfectible !"));
        doc.goToNextSection();
        doc.insertSection(sec4);

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