package doc; import java.util.List; import java.util.ArrayList; public class Book{ private String title; private String author; private List contents; public Book(String author, String title, List 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 tableOfContents(){ TableOfContentsVisitor contentTable = new TableOfContentsVisitor(); List returnedContent = new ArrayList(); for(TextComponent e : contents){ //contentTable.setPrefix(Integer.toString(itteration)); List 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(); } }