added build method with working nesting

This commit is contained in:
Legit Cheetah 2024-02-05 14:32:51 +01:00
parent 14e66f5e15
commit c7d531a10c
4 changed files with 39 additions and 4 deletions

View File

@ -1,7 +1,16 @@
package main;
import main.htmlTags.*;
public class Main {
public static void main(String[] args) {
moduleBody body = new moduleBody();
moduleDiv mainDiv = new moduleDiv();
moduleDiv thirdDiv = new moduleDiv();
moduleDiv secondDiv = new moduleDiv();
body.addModule(mainDiv);
mainDiv.addModule(thirdDiv);
body.addModule(secondDiv);
System.out.println(body.build());
}
}

View File

@ -0,0 +1,7 @@
package main.htmlTags;
public class moduleBody extends moduleGeneric{
public moduleBody(){
setTag("body");
}
}

View File

@ -2,7 +2,7 @@ package main.htmlTags;
public class moduleDiv extends moduleGeneric {
public void tagDiv(){
public moduleDiv(){
setTag("div");
}
}

View File

@ -1,13 +1,16 @@
package main.htmlTags;
import java.util.ArrayList;
public class moduleGeneric {
String tag = "";
int ID;
ArrayList<moduleGeneric> content = new ArrayList<>();
public void addModule(moduleGeneric module){
content.add(module);
}
public void setID(int ID) {
@ -25,4 +28,20 @@ public class moduleGeneric {
public String getTag() {
return tag;
}
public StringBuilder build(){
StringBuilder buildString = new StringBuilder();
buildString
.append("\n<")
.append(tag)
.append(">");
for (main.htmlTags.moduleGeneric moduleGeneric : content) {
buildString.append(moduleGeneric.build());
}
buildString
.append("\n</")
.append(tag)
.append(">");
return buildString;
}
}