Files
oopuebung/uebung08/src/doc/Book.java
2025-06-04 18:25:35 +02:00

64 lines
1.7 KiB
Java

package doc;
import java.util.List;
import java.util.ArrayList;
public class Book{
private String title;
private String author;
private List<TextComponent> contents;
public Book(String author, String title, List<TextComponent> contents){
this.title = title;
this.author = author;
this.contents = contents;
}
public int countWords(){
Section helpSection = new Section("help", contents);
return helpSection.countWords();
}
public int countWordsByVisitor(){
CountWordsVisitor countWords = new CountWordsVisitor();
int wordCount = 0;
for(TextComponent i : contents){
wordCount += i.accept(countWords);
}
return wordCount;
}
public List<String> tableOfContents(){
TableOfContentsVisitor contentTable = new TableOfContentsVisitor();
List<String> returnedContent = new ArrayList<String>();
for(TextComponent e : contents){
//contentTable.setPrefix(Integer.toString(itteration));
List<String> returned = e.accept(contentTable);
if(returned != null){
returnedContent.addAll(returned);
}
}
return returnedContent;
}
public String toText(){
StringBuilder st = new StringBuilder();
ToTextVisitor textVisitor = new ToTextVisitor();
st.append(" ");
st.append(author);
st.append("\n");
st.append(title);
st.append("\n\n");
for(TextComponent i : contents){
st.append(i.accept(textVisitor));
st.append("\n");
}
return st.toString();
}
}