diff --git a/src/uebung08/doc/Book.java b/src/uebung08/doc/Book.java index ae82e76..bc13d59 100644 --- a/src/uebung08/doc/Book.java +++ b/src/uebung08/doc/Book.java @@ -1,5 +1,6 @@ package uebung08.doc; +import java.util.ArrayList; import java.util.List; public class Book { @@ -28,10 +29,22 @@ public class Book { } public String tableOfContents() { - return ""; + List res = new ArrayList<>(); + + for (TextComponent textComponent : content) { + res.addAll(textComponent.accept(new TableOfContentsVisitor())); + } + + return res.toString(); } public int countWordsByVisitor() { - return 0; + int count = 0; + + for (TextComponent textComponent : content) { + count += textComponent.accept(new CountWordsVisitor()); + } + + return count; } } diff --git a/src/uebung08/doc/CountWordsVisitor.java b/src/uebung08/doc/CountWordsVisitor.java index 18e7472..d1dcff4 100644 --- a/src/uebung08/doc/CountWordsVisitor.java +++ b/src/uebung08/doc/CountWordsVisitor.java @@ -2,10 +2,36 @@ package uebung08.doc; public class CountWordsVisitor implements Visitor{ - - @Override - public Integer visit(TextComponent textComponent) { - image.accept(this); + public Integer visit(Image image) { return 0; } + + public Integer visit(Paragraph paragraph) { + int wordcount = 0; + boolean isInWord = false; + + + for (int i = 0; i < paragraph.getText().length(); i++) { + if(Character.isAlphabetic(paragraph.getText().charAt(i))){ + if (!isInWord) { + wordcount++; + isInWord = true; + } + } else { + isInWord = false; + } + } + + return wordcount; + } + + public Integer visit(Section section) { + int count = 0; + for (TextComponent textComponent : section.getContent()) { + count += textComponent.accept(this); + } + + return count; + } + } diff --git a/src/uebung08/doc/Paragraph.java b/src/uebung08/doc/Paragraph.java index bc45c52..b982c42 100644 --- a/src/uebung08/doc/Paragraph.java +++ b/src/uebung08/doc/Paragraph.java @@ -28,6 +28,10 @@ public class Paragraph implements TextComponent { return wordcount; } + public String getText(){ + return text; + } + @Override public T accept(Visitor visitor) { return visitor.visit(this); } } diff --git a/src/uebung08/doc/Section.java b/src/uebung08/doc/Section.java index d923468..6cf1aa2 100644 --- a/src/uebung08/doc/Section.java +++ b/src/uebung08/doc/Section.java @@ -20,6 +20,10 @@ public class Section implements TextComponent { return wordcount; } + public List getContent() { + return content; + } + @Override public T accept(Visitor visitor) { return visitor.visit(this); } } diff --git a/src/uebung08/doc/TableOfContentsVisitor.java b/src/uebung08/doc/TableOfContentsVisitor.java new file mode 100644 index 0000000..4f2e09e --- /dev/null +++ b/src/uebung08/doc/TableOfContentsVisitor.java @@ -0,0 +1,10 @@ +package uebung08.doc; + +import java.util.List; + +public class TableOfContentsVisitor implements Visitor>{ + + public List visit(Image image) { + + } +} diff --git a/src/uebung08/doc/Visitor.java b/src/uebung08/doc/Visitor.java index afa9658..71d34e2 100644 --- a/src/uebung08/doc/Visitor.java +++ b/src/uebung08/doc/Visitor.java @@ -1,8 +1,7 @@ package uebung08.doc; public interface Visitor { - T visit(TextComponent textComponent); - T visit(Book book); + T visit(Paragraph paragraph); T visit(Image image); T visit(Section section);