Refactor HTML generation in ContactFormData
Extract the HTML generation logic to a reusable method in the Form class. This change reduces code duplication and enhances maintainability by centralizing the table generation functionality.
This commit is contained in:
parent
ac5448385d
commit
d53ba82e3e
|
|
@ -38,26 +38,9 @@ public class ContactFormData extends Form {
|
|||
|
||||
@Override
|
||||
public String toHtml() {
|
||||
StringBuilder htmlOutput = new StringBuilder();
|
||||
|
||||
htmlOutput.append("<table style='border-collapse: collapse; width: 100%;'>");
|
||||
|
||||
String[] fields = {"Username", "Email", "Question"};
|
||||
String[] values = {username, email, question};
|
||||
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
htmlOutput.append("<tr style='border: 1px solid #ddd;'>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px; font-weight: bold;'>");
|
||||
htmlOutput.append(fields[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px;'>");
|
||||
htmlOutput.append(values[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("</tr>");
|
||||
}
|
||||
|
||||
htmlOutput.append("</table>");
|
||||
|
||||
return htmlOutput.toString();
|
||||
return toHtml(fields, values);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,25 @@ public abstract class Form {
|
|||
|
||||
public abstract String toHtml();
|
||||
|
||||
public String toHtml(String[] fields, String[] values) {
|
||||
StringBuilder htmlOutput = new StringBuilder();
|
||||
htmlOutput.append("<table style='border-collapse: collapse; width: 100%;'>");
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
htmlOutput.append("<tr style='border: 1px solid #ddd;'>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px; font-weight: bold;'>");
|
||||
htmlOutput.append(fields[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px;'>");
|
||||
htmlOutput.append(values[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("</tr>");
|
||||
}
|
||||
|
||||
htmlOutput.append("</table>");
|
||||
|
||||
return htmlOutput.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user