Skip to content

Commit

Permalink
Add abstract class Tag, extend Tag with SingleTag and PairedTag
Browse files Browse the repository at this point in the history
Complete inheritance task.
  • Loading branch information
JackKaif committed Feb 5, 2024
1 parent 247aeb4 commit d1dc5fa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
20 changes: 19 additions & 1 deletion java-oop-ru/inheritance/src/main/java/exercise/PairedTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@

import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

// BEGIN
public class PairedTag extends Tag {
private final String body;
private final List<Tag> singleTags;

public PairedTag(String tag, Map<String, String> attributes, String body, List<Tag> singleTags) {
super(tag, attributes);
this.body = body;
this.singleTags = singleTags;
}

public String toString() {
var result = new StringBuilder(String.format("<%s", this.getTag()));
this.getAttributes().forEach((k, v) -> result.append(String.format(" %s=\"%s\"", k, v)));
result.append(">");
singleTags.forEach(tag -> result.append(tag.toString()));
result.append(body);
result.append(String.format("</%s>", this.getTag()));
return result.toString();
}
}
// END
11 changes: 11 additions & 0 deletions java-oop-ru/inheritance/src/main/java/exercise/SingleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@
import java.util.Map;

// BEGIN
public class SingleTag extends Tag{
public SingleTag(String tag, Map<String, String> attributes) {
super(tag, attributes);
}

public String toString() {
var result = new StringBuilder(String.format("<%s", this.getTag()));
this.getAttributes().forEach((k, v) -> result.append(String.format(" %s=\"%s\"", k, v)));
result.append(">");
return result.toString();
}
}
// END
19 changes: 18 additions & 1 deletion java-oop-ru/inheritance/src/main/java/exercise/Tag.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package exercise;

import java.util.stream.Collectors;
import java.util.Map;

// BEGIN
public abstract class Tag {
private final String tag;
private final Map<String, String> attributes;

public Tag(String tag, Map<String, String> attributes) {
this.tag = tag;
this.attributes = attributes;
}

public abstract String toString();

public String getTag() {
return tag;
}

public Map<String, String> getAttributes() {
return attributes;
}
}
// END

0 comments on commit d1dc5fa

Please sign in to comment.