finished TableofContents

This commit is contained in:
Johannes Schmelz 2024-06-08 19:49:45 +00:00
parent 81ea768dca
commit 85e8396ce9
3 changed files with 40 additions and 5 deletions

View File

@ -29,13 +29,20 @@ public class Book {
} }
public String tableOfContents() { public String tableOfContents() {
List<String> res = new ArrayList<>(); List<String> gesamt = new ArrayList<>();
for (int i = 0; i < content.size(); i++) {
List<String> 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() { public int countWordsByVisitor() {

View File

@ -24,6 +24,10 @@ public class Section implements TextComponent {
return content; return content;
} }
public String getHeader() {
return header;
}
@Override @Override
public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); } public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
} }

View File

@ -1,10 +1,34 @@
package uebung08.doc; package uebung08.doc;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TableOfContentsVisitor implements Visitor<List<String>>{ public class TableOfContentsVisitor implements Visitor<List<String>>{
public List<String> visit(Image image) { public List<String> visit(Image image) {
return new ArrayList<>();
}
public List<String> visit(Paragraph paragraph) {
return new ArrayList<>();
}
public List<String> visit(Section section) {
List<String> gesamt = new ArrayList<>();
gesamt.add(section.getHeader());
for (int i = 0; i < section.getContent().size(); i++) {
List<String> res = new ArrayList<>();
res.addAll(section.getContent().get(i).accept(this));
for (String string : res) {
string = i +"." + string;
gesamt.add(string);
}
}
return gesamt;
} }
} }