implemented ToTextVisitor

This commit is contained in:
Johannes Schmelz 2024-06-08 22:04:10 +02:00
parent 85e8396ce9
commit 8c224faa0e
15 changed files with 56 additions and 3 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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() {

View File

@ -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());
} }

View File

@ -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); }

View File

@ -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<>();

View 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();
}
}