diff --git a/src/uebung08/doc/Book.java b/src/uebung08/doc/Book.java index bc13d59..6b0c412 100644 --- a/src/uebung08/doc/Book.java +++ b/src/uebung08/doc/Book.java @@ -29,13 +29,20 @@ public class Book { } public String tableOfContents() { - List res = new ArrayList<>(); + List gesamt = new ArrayList<>(); + + for (int i = 0; i < content.size(); i++) { + List res = new ArrayList<>(); + res.addAll(content.get(i).accept(new TableOfContentsVisitor())); + + for (String string : res) { + string = i +"." + string; + gesamt.add(string); + } - for (TextComponent textComponent : content) { - res.addAll(textComponent.accept(new TableOfContentsVisitor())); } - return res.toString(); + return gesamt.toString(); } public int countWordsByVisitor() { diff --git a/src/uebung08/doc/Section.java b/src/uebung08/doc/Section.java index 6cf1aa2..2a5e609 100644 --- a/src/uebung08/doc/Section.java +++ b/src/uebung08/doc/Section.java @@ -24,6 +24,10 @@ public class Section implements TextComponent { return content; } + public String getHeader() { + return header; + } + @Override public T accept(Visitor visitor) { return visitor.visit(this); } } diff --git a/src/uebung08/doc/TableOfContentsVisitor.java b/src/uebung08/doc/TableOfContentsVisitor.java index 4f2e09e..f3a7e57 100644 --- a/src/uebung08/doc/TableOfContentsVisitor.java +++ b/src/uebung08/doc/TableOfContentsVisitor.java @@ -1,10 +1,34 @@ package uebung08.doc; +import java.util.ArrayList; import java.util.List; public class TableOfContentsVisitor implements Visitor>{ public List visit(Image image) { - + return new ArrayList<>(); + } + + public List visit(Paragraph paragraph) { + return new ArrayList<>(); + } + + public List visit(Section section) { + List gesamt = new ArrayList<>(); + + gesamt.add(section.getHeader()); + + for (int i = 0; i < section.getContent().size(); i++) { + List res = new ArrayList<>(); + res.addAll(section.getContent().get(i).accept(this)); + + for (String string : res) { + string = i +"." + string; + gesamt.add(string); + } + + } + + return gesamt; } }