finished CountWordsVisitor
This commit is contained in:
parent
dcf2a95b4f
commit
81ea768dca
@ -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<String> 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;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,36 @@ package uebung08.doc;
|
||||
|
||||
public class CountWordsVisitor implements Visitor<Integer>{
|
||||
|
||||
|
||||
@Override
|
||||
public <T> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,6 +28,10 @@ public class Paragraph implements TextComponent {
|
||||
return wordcount;
|
||||
}
|
||||
|
||||
public String getText(){
|
||||
return text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
|
||||
}
|
||||
|
@ -20,6 +20,10 @@ public class Section implements TextComponent {
|
||||
return wordcount;
|
||||
}
|
||||
|
||||
public List<TextComponent> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
|
||||
}
|
||||
|
10
src/uebung08/doc/TableOfContentsVisitor.java
Normal file
10
src/uebung08/doc/TableOfContentsVisitor.java
Normal file
@ -0,0 +1,10 @@
|
||||
package uebung08.doc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TableOfContentsVisitor implements Visitor<List<String>>{
|
||||
|
||||
public List<String> visit(Image image) {
|
||||
|
||||
}
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
package uebung08.doc;
|
||||
|
||||
public interface Visitor<T> {
|
||||
T visit(TextComponent textComponent);
|
||||
T visit(Book book);
|
||||
|
||||
T visit(Paragraph paragraph);
|
||||
T visit(Image image);
|
||||
T visit(Section section);
|
||||
|
Loading…
Reference in New Issue
Block a user