implemented ToTextVisitor
This commit is contained in:
parent
85e8396ce9
commit
8c224faa0e
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
bin/uebung08/doc/TableOfContentsVisitor.class
Normal file
BIN
bin/uebung08/doc/TableOfContentsVisitor.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
bin/uebung08/doc/ToTextVisitor.class
Normal file
BIN
bin/uebung08/doc/ToTextVisitor.class
Normal file
Binary file not shown.
Binary file not shown.
@ -25,7 +25,20 @@ public class Book {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toText() {
|
public String toText() {
|
||||||
return "";
|
List<String> gesamt = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < content.size(); i++) {
|
||||||
|
List<String> res = new ArrayList<>();
|
||||||
|
res.add(content.get(i).accept(new ToTextVisitor()));
|
||||||
|
|
||||||
|
for (String string : res) {
|
||||||
|
string = i +"." + string;
|
||||||
|
gesamt.add(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return gesamt.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String tableOfContents() {
|
public String tableOfContents() {
|
||||||
|
@ -106,7 +106,8 @@ public class BookDemo {
|
|||||||
|
|
||||||
// Aufgabe 2
|
// Aufgabe 2
|
||||||
System.out.println("Buch enthält " + book.countWordsByVisitor() + " Wörter");
|
System.out.println("Buch enthält " + book.countWordsByVisitor() + " Wörter");
|
||||||
System.out.println("Inhalt: " + book.tableOfContents());
|
System.out.println("TOC:\n" + book.tableOfContents());
|
||||||
|
System.out.println("Inhalt:");
|
||||||
System.out.println(book.toText());
|
System.out.println(book.toText());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,10 @@ public class Image implements TextComponent {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getCaption() {
|
||||||
|
return caption;
|
||||||
|
}
|
||||||
|
|
||||||
// immer diese accept methode verwenden
|
// immer diese accept methode verwenden
|
||||||
@Override
|
@Override
|
||||||
public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
|
public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
|
||||||
|
@ -16,7 +16,7 @@ public class TableOfContentsVisitor implements Visitor<List<String>>{
|
|||||||
public List<String> visit(Section section) {
|
public List<String> visit(Section section) {
|
||||||
List<String> gesamt = new ArrayList<>();
|
List<String> gesamt = new ArrayList<>();
|
||||||
|
|
||||||
gesamt.add(section.getHeader());
|
gesamt.add(" "+ section.getHeader());
|
||||||
|
|
||||||
for (int i = 0; i < section.getContent().size(); i++) {
|
for (int i = 0; i < section.getContent().size(); i++) {
|
||||||
List<String> res = new ArrayList<>();
|
List<String> res = new ArrayList<>();
|
||||||
|
35
src/uebung08/doc/ToTextVisitor.java
Normal file
35
src/uebung08/doc/ToTextVisitor.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package uebung08.doc;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ToTextVisitor implements Visitor<String>{
|
||||||
|
|
||||||
|
public String visit(Image image) {
|
||||||
|
return image.getCaption();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String visit(Paragraph paragraph) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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.add(section.getContent().get(i).accept(this));
|
||||||
|
|
||||||
|
for (String string : res) {
|
||||||
|
string = i +"." + string;
|
||||||
|
gesamt.add(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return gesamt.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user