Compare commits
2 Commits
ac9d7fdd39
...
1b07a3b6ab
Author | SHA1 | Date | |
---|---|---|---|
1b07a3b6ab | |||
701e732eb6 |
@@ -58,6 +58,12 @@ public class CashPoint implements SecurityClient {
|
||||
|
||||
void charge(AccountCard accountCard) {
|
||||
final int price = getPrice(accountCard.color);
|
||||
// zahlen methode
|
||||
/*
|
||||
|
||||
zahlen bein acmgmt
|
||||
|
||||
*/
|
||||
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
|
||||
cents += price;
|
||||
}
|
||||
|
22
uebung07/src/quantities/generic/Length.java
Normal file
22
uebung07/src/quantities/generic/Length.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package quantities.generic;
|
||||
|
||||
import static quantities.generic.LengthUnit.METER;
|
||||
import static quantities.generic.TimeUnit.SECOND;
|
||||
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Length extends Quantity<Length>{
|
||||
|
||||
public Length(double value, Unit<Length> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Velocity div(Time t) {
|
||||
return METER_PER_SECOND.quantity(this.value(METER) / t.value(SECOND));
|
||||
}
|
||||
|
||||
public Time div(Velocity v) {
|
||||
return SECOND.quantity(this.value(METER) / v.value(METER_PER_SECOND));
|
||||
}
|
||||
|
||||
|
||||
}
|
19
uebung07/src/quantities/generic/LengthUnit.java
Normal file
19
uebung07/src/quantities/generic/LengthUnit.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class LengthUnit extends Unit<Length> {
|
||||
public LengthUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final LengthUnit METER = new LengthUnit("m", 1);
|
||||
public static final LengthUnit MILLIMETER = new LengthUnit("mm", 0.001);
|
||||
public static final LengthUnit KILOMETER = new LengthUnit("km", 1000);
|
||||
public static final LengthUnit MILE = new LengthUnit("mi", 1609.344);
|
||||
public static final LengthUnit LIGHTYEAR = new LengthUnit("ly", 9460730472580800.0);
|
||||
public static final LengthUnit PARSEC = new LengthUnit("pc", 3.0856776e16);
|
||||
|
||||
@Override
|
||||
public Length quantity(double value) {
|
||||
return new Length(value, this);
|
||||
}
|
||||
}
|
55
uebung07/src/quantities/generic/Quantity.java
Normal file
55
uebung07/src/quantities/generic/Quantity.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package quantities.generic;
|
||||
|
||||
public abstract class Quantity<Q extends Quantity<Q>> {
|
||||
public final double value;
|
||||
public final Unit<Q> unit;
|
||||
|
||||
protected Quantity(double value, Unit<Q> unit) {
|
||||
this.value = value;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public double getBaseValue() {
|
||||
return value * unit.baseFactor;
|
||||
}
|
||||
|
||||
public double value(Unit<Q> unit) {
|
||||
return getBaseValue() / unit.baseFactor;
|
||||
}
|
||||
|
||||
//neue Methoden
|
||||
|
||||
public Q plus(Q other) {
|
||||
return unit.quantity(value + other.getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public Q minus(Q other) {
|
||||
return unit.quantity(value - other.getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public Q mult(double f) {
|
||||
return unit.quantity(value * f);
|
||||
}
|
||||
|
||||
public Q div(double f) {
|
||||
return unit.quantity(value / f);
|
||||
}
|
||||
|
||||
public Q to(Unit<Q> unit) {
|
||||
return unit.quantity(getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public double div(Q other) {
|
||||
return getBaseValue() / other.getBaseValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value + " " + unit;
|
||||
}
|
||||
|
||||
public String format(String fmt, Unit unit) {
|
||||
return String.format(fmt, value(unit), unit);
|
||||
}
|
||||
}
|
11
uebung07/src/quantities/generic/Time.java
Normal file
11
uebung07/src/quantities/generic/Time.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class Time extends Quantity<Time> {
|
||||
public Time(double value, Unit<Time> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Length mult(Velocity v) {
|
||||
return v.mult(this);
|
||||
}
|
||||
}
|
17
uebung07/src/quantities/generic/TimeUnit.java
Normal file
17
uebung07/src/quantities/generic/TimeUnit.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class TimeUnit extends Unit<Time> {
|
||||
public TimeUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time quantity(double value) {
|
||||
return new Time(value, this);
|
||||
}
|
||||
|
||||
public static final TimeUnit SECOND = new TimeUnit("s", 1);
|
||||
public static final TimeUnit MINUTE = new TimeUnit("min", 60);
|
||||
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
|
||||
public static final TimeUnit DAY = new TimeUnit("d", 86400);
|
||||
}
|
18
uebung07/src/quantities/generic/Unit.java
Normal file
18
uebung07/src/quantities/generic/Unit.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package quantities.generic;
|
||||
|
||||
public abstract class Unit<T extends Quantity<T>> {
|
||||
public final String name;
|
||||
public final double baseFactor;
|
||||
|
||||
public Unit(String name, double baseFactor) {
|
||||
this.name = name;
|
||||
this.baseFactor = baseFactor;
|
||||
}
|
||||
|
||||
public abstract T quantity(double value);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
15
uebung07/src/quantities/generic/Velocity.java
Normal file
15
uebung07/src/quantities/generic/Velocity.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package quantities.generic;
|
||||
|
||||
import static quantities.generic.LengthUnit.METER;
|
||||
import static quantities.generic.TimeUnit.SECOND;
|
||||
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Velocity extends Quantity<Velocity> {
|
||||
public Velocity(double value, Unit<Velocity> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Length mult(Time t) {
|
||||
return METER.quantity(this.value(METER_PER_SECOND) * t.value(SECOND));
|
||||
}
|
||||
}
|
16
uebung07/src/quantities/generic/VelocityUnit.java
Normal file
16
uebung07/src/quantities/generic/VelocityUnit.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class VelocityUnit extends Unit<Velocity> {
|
||||
public VelocityUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Velocity quantity(double value) {
|
||||
return new Velocity(value, this);
|
||||
}
|
||||
|
||||
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("m/s", 1);
|
||||
public static final VelocityUnit KMH = new VelocityUnit("km/h", 1.0 / 3.6);
|
||||
public static final VelocityUnit MPH = new VelocityUnit("mph", 0.44704);
|
||||
}
|
34
uebung07/src/quantities/plain/Time.java
Normal file
34
uebung07/src/quantities/plain/Time.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class Time extends Quantity{
|
||||
private final TimeUnit unit;
|
||||
|
||||
public Time(double value, TimeUnit unit) {
|
||||
super(value, unit);
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public Time plus(Time other) {
|
||||
return new Time(value + other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Time minus(Time other) {
|
||||
return new Time(value - other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Time mult(double f) {
|
||||
return new Time(value * f, unit);
|
||||
}
|
||||
|
||||
public Time div(double f) {
|
||||
return new Time(value / f, unit);
|
||||
}
|
||||
|
||||
public Time to(TimeUnit unit) {
|
||||
return new Time(getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Length mult(Velocity v) {
|
||||
return v.mult(this);
|
||||
}
|
||||
}
|
13
uebung07/src/quantities/plain/TimeUnit.java
Normal file
13
uebung07/src/quantities/plain/TimeUnit.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class TimeUnit extends Unit{
|
||||
|
||||
public TimeUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final TimeUnit SECOND = new TimeUnit("s", 1);
|
||||
public static final TimeUnit MINUTE = new TimeUnit("m", 60);
|
||||
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
|
||||
|
||||
}
|
38
uebung07/src/quantities/plain/Velocity.java
Normal file
38
uebung07/src/quantities/plain/Velocity.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package quantities.plain;
|
||||
|
||||
import static quantities.plain.LengthUnit.METER;
|
||||
import static quantities.plain.TimeUnit.SECOND;
|
||||
import static quantities.plain.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Velocity extends Quantity {
|
||||
private final VelocityUnit unit;
|
||||
|
||||
public Velocity(double value, VelocityUnit unit) {
|
||||
super(value, unit);
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public Velocity plus(Velocity other) {
|
||||
return new Velocity(value + other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Velocity minus(Velocity other) {
|
||||
return new Velocity(value - other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Velocity mult(double f) {
|
||||
return new Velocity(value * f, unit);
|
||||
}
|
||||
|
||||
public Velocity div(double f) {
|
||||
return new Velocity(value / f, unit);
|
||||
}
|
||||
|
||||
public Velocity to(VelocityUnit unit) {
|
||||
return new Velocity(getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Length mult(Time t) {
|
||||
return new Length(this.value(METER_PER_SECOND) * t.value(SECOND), METER);
|
||||
}
|
||||
}
|
13
uebung07/src/quantities/plain/VelocityUnit.java
Normal file
13
uebung07/src/quantities/plain/VelocityUnit.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class VelocityUnit extends Unit{
|
||||
|
||||
public VelocityUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final VelocityUnit KMH = new VelocityUnit("kmh", 1000/3600); // 1/3.6
|
||||
public static final VelocityUnit MPH = new VelocityUnit("mph", 1609.344/3600);
|
||||
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("meter per second", 1);
|
||||
|
||||
}
|
63
uebung08/src/doc/Book.java
Normal file
63
uebung08/src/doc/Book.java
Normal file
@@ -0,0 +1,63 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
148
uebung08/src/doc/BookDemo.java
Normal file
148
uebung08/src/doc/BookDemo.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package doc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BookDemo {
|
||||
private static final String TEXT0 = "Über Entwurf und Realisierung eierlegender Wollmilchsäue.";
|
||||
private static final String TEXT1 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod " +
|
||||
"tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. " +
|
||||
"At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd " +
|
||||
"gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum " +
|
||||
"dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor " +
|
||||
"invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
|
||||
private static final String TEXT2 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
|
||||
"consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan " +
|
||||
"et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis " +
|
||||
"dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer " +
|
||||
"adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore " +
|
||||
"magna aliquam erat volutpat.";
|
||||
private static final String TEXT3 = "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit " +
|
||||
"lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure " +
|
||||
"dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore " +
|
||||
"eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim " +
|
||||
"qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla " +
|
||||
"facilisi.";
|
||||
private static final String TEXT4 = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming " +
|
||||
"id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, " +
|
||||
"consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet " +
|
||||
"dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud " +
|
||||
"exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo " +
|
||||
"consequat.";
|
||||
private static final String TEXT5 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
|
||||
"consequat, vel illum dolore eu feugiat nulla facilisis.";
|
||||
private static final String TEXT6 = "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, " +
|
||||
"no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, " +
|
||||
"consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et " +
|
||||
"dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo " +
|
||||
"duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est " +
|
||||
"Lorem ipsum dolor sit amet.";
|
||||
private static final String TEXT7 = "Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore " +
|
||||
"et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et " +
|
||||
"justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata " +
|
||||
"sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur " +
|
||||
"sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore " +
|
||||
"magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo " +
|
||||
"dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est " +
|
||||
"Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing " +
|
||||
"elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " +
|
||||
"erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea " +
|
||||
"rebum. Stet clita kasd gubergren, no sea takimata sanctus.";
|
||||
private static final String TEXT8 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
|
||||
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
|
||||
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
|
||||
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit " +
|
||||
"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " +
|
||||
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed " +
|
||||
"diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
|
||||
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ";
|
||||
private static final String TEXT9 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
|
||||
"consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan " +
|
||||
"et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis " +
|
||||
"dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer " +
|
||||
"adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore " +
|
||||
"magna aliquam erat volutpat.";
|
||||
private static final String TEXT10 = "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit " +
|
||||
"lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure " +
|
||||
"dolor in hendrerit in vulputate velit esse molestie consequat, vel illum " +
|
||||
"dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio " +
|
||||
"dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te " +
|
||||
"feugait nulla facilisi.";
|
||||
private static final String TEXT11 = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet " +
|
||||
"doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, " +
|
||||
"consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut " +
|
||||
"laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, " +
|
||||
"quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex " +
|
||||
"ea commodo";
|
||||
private static final String TEXT12 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
|
||||
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
|
||||
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
|
||||
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit " +
|
||||
"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " +
|
||||
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed " +
|
||||
"diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
|
||||
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
|
||||
private static final String TEXT13 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
|
||||
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
|
||||
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita " +
|
||||
"kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
|
||||
private static final String TEXT14 = "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, " +
|
||||
"no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit " +
|
||||
"amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut " +
|
||||
"labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam " +
|
||||
"et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata " +
|
||||
"sanctus est Lorem ipsum dolor sit amet.";
|
||||
private static final String TEXT15 = "Lorem ipsum dolor sit amet, consetetur sadipscing " +
|
||||
"elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et " +
|
||||
"nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd " +
|
||||
"magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum " +
|
||||
"dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed " +
|
||||
"diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Book book = makeBook();
|
||||
|
||||
// Aufgabe 1
|
||||
System.out.println("Buch enthält " + book.countWords() + " Wörter");
|
||||
|
||||
// Aufgabe 2
|
||||
System.out.println("Buch enthält " + book.countWordsByVisitor() + " Wörter");
|
||||
System.out.println("Inhalt: " + book.tableOfContents());
|
||||
System.out.println(book.toText());
|
||||
}
|
||||
|
||||
private static Book makeBook() {
|
||||
final Section sec1 = new Section("Einführung",
|
||||
List.of(new Paragraph(TEXT1),
|
||||
new Paragraph(TEXT2)));
|
||||
final Section sec2 = new Section("Hintergrund",
|
||||
List.of(new Paragraph(TEXT3),
|
||||
new Section("Geschichte",
|
||||
List.of(new Paragraph(TEXT4),
|
||||
new Image("https://www.bekannt.de/p1.pdf",
|
||||
"Bild 1"),
|
||||
new Paragraph(TEXT5))),
|
||||
new Section("Literatur",
|
||||
List.of(new Paragraph(TEXT6),
|
||||
new Image("https://www.eh-klar.de/p2.pdf",
|
||||
"Bild 2"),
|
||||
new Paragraph(TEXT7),
|
||||
new Image("https://www.jeder-weiss-es.de/p3.pdf",
|
||||
"Bild 3"),
|
||||
new Paragraph(TEXT8)))));
|
||||
final Section sec3 = new Section("Idee",
|
||||
List.of(new Paragraph(TEXT9)));
|
||||
final Section sec4 = new Section("Umsetzung",
|
||||
List.of(new Paragraph(TEXT10),
|
||||
new Section("Entwurf",
|
||||
List.of(new Paragraph(TEXT11),
|
||||
new Section("Grobentwurf",
|
||||
List.of(new Paragraph(TEXT12))),
|
||||
new Section("Feinentwurf",
|
||||
List.of(new Paragraph(TEXT13))))),
|
||||
new Section("Realisierung",
|
||||
List.of(new Paragraph(TEXT14)))));
|
||||
final Section sec5 = new Section("Zusammenfassung und Ausblick", List.of(new Paragraph(TEXT15)));
|
||||
return new Book("Peter Mustermann", "Die eierlegende Wollmilchsau",
|
||||
List.of(new Paragraph(TEXT0), sec1, sec2, sec3, sec4, sec5));
|
||||
}
|
||||
}
|
38
uebung08/src/doc/CountWordsVisitor.java
Normal file
38
uebung08/src/doc/CountWordsVisitor.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package doc;
|
||||
import java.util.List;
|
||||
|
||||
public class CountWordsVisitor implements Visitor<Integer>{
|
||||
|
||||
public Integer visit(Paragraph e){
|
||||
String text = e.getText();
|
||||
int wordCount = 0;
|
||||
boolean open = false;
|
||||
for (char i : text.toCharArray()){
|
||||
if(!open && Character.isAlphabetic(i)){
|
||||
open = true;
|
||||
}
|
||||
else if(open && !Character.isAlphabetic(i)){
|
||||
open = false;
|
||||
wordCount++;
|
||||
}
|
||||
}
|
||||
if (open){
|
||||
open = false;
|
||||
wordCount++;
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
|
||||
public Integer visit(Image e){
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Integer visit(Section e){
|
||||
List<TextComponent> contents = e.getContents();
|
||||
int wordCount = 0;
|
||||
for (TextComponent c: contents){
|
||||
wordCount += c.accept(this);
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
}
|
29
uebung08/src/doc/Image.java
Normal file
29
uebung08/src/doc/Image.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package doc;
|
||||
|
||||
|
||||
public class Image implements TextComponent{
|
||||
private String url;
|
||||
private String caption;
|
||||
|
||||
public Image(String url, String caption) {
|
||||
|
||||
this.url = url;
|
||||
this.caption = caption;
|
||||
}
|
||||
|
||||
public int countWords(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getUrl(){
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getCaption(){
|
||||
return caption;
|
||||
}
|
||||
|
||||
public <T> T accept(Visitor<T> v){
|
||||
return v.visit(this);
|
||||
}
|
||||
}
|
39
uebung08/src/doc/Paragraph.java
Normal file
39
uebung08/src/doc/Paragraph.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package doc;
|
||||
|
||||
public class Paragraph implements TextComponent{
|
||||
private String text;
|
||||
|
||||
public Paragraph(String text){
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText(){
|
||||
return text;
|
||||
}
|
||||
|
||||
public int countWords(){
|
||||
int wordCount = 0;
|
||||
boolean open = false;
|
||||
|
||||
for(char i : text.toCharArray()){
|
||||
if(!open && Character.isAlphabetic(i)){
|
||||
open = true;}
|
||||
else if(open && !Character.isAlphabetic(i)){
|
||||
open = false;
|
||||
wordCount ++;
|
||||
}
|
||||
}
|
||||
if(open){
|
||||
open = false;
|
||||
wordCount++;
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
|
||||
public <T> T accept(Visitor<T> v){
|
||||
return v.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
35
uebung08/src/doc/Section.java
Normal file
35
uebung08/src/doc/Section.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package doc;
|
||||
import java.util.List;
|
||||
|
||||
public class Section implements TextComponent{
|
||||
|
||||
private String header;
|
||||
private List<TextComponent> contents;
|
||||
|
||||
public Section(String header, List<TextComponent> contents){
|
||||
this.header = header;
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
public String getHeader(){
|
||||
return header;
|
||||
}
|
||||
|
||||
public List<TextComponent> getContents(){
|
||||
return contents;
|
||||
}
|
||||
|
||||
public int countWords(){
|
||||
int wordCount = 0;
|
||||
for(TextComponent i : contents){
|
||||
wordCount = wordCount + i.countWords();
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public <T> T accept(Visitor<T> v){
|
||||
return v.visit(this);
|
||||
}
|
||||
}
|
77
uebung08/src/doc/TableOfContentsVisitor.java
Normal file
77
uebung08/src/doc/TableOfContentsVisitor.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package doc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TableOfContentsVisitor implements Visitor <List<String>>{
|
||||
|
||||
private String prefix = "1";
|
||||
|
||||
public TableOfContentsVisitor(){
|
||||
}
|
||||
|
||||
public void setPrefix(String pre){
|
||||
prefix = pre;
|
||||
}
|
||||
|
||||
public List<String> visit(Paragraph e){
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public List<String> visit(Image e){
|
||||
return null;
|
||||
}
|
||||
|
||||
/*public List<String> visit(Section e){
|
||||
List<String> tabelOfContentsList = new ArrayList<String>();
|
||||
List<TextComponent> copied = e.getContents();
|
||||
|
||||
String oldPrefix = prefix;
|
||||
|
||||
if(prefix.length() == 0){
|
||||
tabelOfContentsList.add(prefix+ " " + e.getHeader());
|
||||
}
|
||||
else {
|
||||
tabelOfContentsList.add(prefix + " " + e.getHeader());
|
||||
}
|
||||
|
||||
int chapter = 1;
|
||||
for(TextComponent i : e.getContents()){
|
||||
|
||||
String savedPrefix = prefix;
|
||||
setPrefix(prefix + "." + Integer.toString(chapter));
|
||||
|
||||
List<String> returnedContent = i.accept(this);
|
||||
if (returnedContent != null){
|
||||
tabelOfContentsList.addAll(i.accept(this));
|
||||
chapter++;
|
||||
}
|
||||
setPrefix(savedPrefix);
|
||||
}
|
||||
setPrefix(oldPrefix);
|
||||
return tabelOfContentsList;
|
||||
|
||||
}*/
|
||||
|
||||
//Alternative die immer den aktuellen Stand übergibt und somit auf jeder "Ebene" ein eigener Visitor agiert
|
||||
|
||||
private int sectionCtr = 1;
|
||||
|
||||
public TableOfContentsVisitor(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> visit(Section section) {
|
||||
final List<String> list = new ArrayList<>();
|
||||
sectionCtr++;
|
||||
list.add(prefix + sectionCtr + " " + section.getHeader());
|
||||
final TableOfContentsVisitor subVisitor =
|
||||
new TableOfContentsVisitor(prefix + sectionCtr + ".");
|
||||
for (TextComponent c : section.getContents())
|
||||
list.addAll(c.accept(subVisitor));
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
7
uebung08/src/doc/TextComponent.java
Normal file
7
uebung08/src/doc/TextComponent.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package doc;
|
||||
|
||||
public interface TextComponent {
|
||||
int countWords();
|
||||
<T> T accept(Visitor<T> visitor);
|
||||
|
||||
}
|
38
uebung08/src/doc/ToTextVisitor.java
Normal file
38
uebung08/src/doc/ToTextVisitor.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package doc;
|
||||
|
||||
class ToTextVisitor implements Visitor<String> {
|
||||
private final String prefix;
|
||||
private int sectionCtr = 0;
|
||||
private int imageCtr = 0;
|
||||
|
||||
public ToTextVisitor() {
|
||||
this("");
|
||||
}
|
||||
|
||||
public ToTextVisitor(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visit(Paragraph section) {
|
||||
return section.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visit(Image image) {
|
||||
imageCtr++;
|
||||
final String num = prefix + imageCtr;
|
||||
return "<image " + image.getUrl() + ">\nFig. " + num + ": " + image.getCaption();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visit(Section section) {
|
||||
final StringBuilder b = new StringBuilder();
|
||||
sectionCtr++;
|
||||
b.append(prefix).append(sectionCtr).append(" ").append(section.getHeader());
|
||||
final ToTextVisitor subVisitor = new ToTextVisitor(prefix + sectionCtr + ".");
|
||||
for (TextComponent c : section.getContents())
|
||||
b.append("\n\n").append(c.accept(subVisitor));
|
||||
return b.toString();
|
||||
}
|
||||
}
|
10
uebung08/src/doc/Visitor.java
Normal file
10
uebung08/src/doc/Visitor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package doc;
|
||||
|
||||
public interface Visitor<T> {
|
||||
T visit(Image image);
|
||||
T visit(Paragraph paragraph);
|
||||
T visit(Section section);
|
||||
|
||||
// semantisch falsch
|
||||
//T visit(TextComponent c);
|
||||
}
|
Reference in New Issue
Block a user