Skip to content

Commit

Permalink
4. Переопределение метода;
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Nov 28, 2023
1 parent 9adae4e commit d310067
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/ru/j4j/inheritance/HtmlReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.j4j.inheritance;

public class HtmlReport extends TextReport {
public String generate(String name, String body) {
return "<h1>" + name + "</h1>"
+ "<br/>"
+ "<span>" + body + "</span>";
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/j4j/inheritance/JSONReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.j4j.inheritance;

public class JSONReport {
public String generate(String name, String body) {
String ln = System.lineSeparator();
return String.format("{" + ln
+ "\t\"name\" : \"%s\"," + ln
+ "\t\"body\" : \"%s\"" + ln
+ "}", name, body);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/j4j/inheritance/ReportUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.j4j.inheritance;

public class ReportUsage {
public static void main(String[] args) {
TextReport textReport = new TextReport();
HtmlReport htmlReport = new HtmlReport();
System.out.println(textReport.generate("Report's name", "Report's body"));
System.out.println(htmlReport.generate("Report's name", "Report's body"));

}
}
7 changes: 7 additions & 0 deletions src/main/java/ru/j4j/inheritance/TextReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.j4j.inheritance;

public class TextReport {
public String generate(String name, String body) {
return name + System.lineSeparator() + body;
}
}
34 changes: 34 additions & 0 deletions src/test/java/ru/j4j/inheritance/JSONReportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.j4j.inheritance;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

class JSONReportTest {
@Test
public void whenTestGenerateMethod() {
String ln = System.lineSeparator();
String expected = "{" + ln
+ "\t\"name\" : \"Report's name\"," + ln
+ "\t\"body\" : \"Report's body\"" + ln
+ "}";
String name = "Report's name";
String body = "Report's body";
String result = new JSONReport().generate(name, body);
assertThat(result).isEqualTo(expected);
}

@Test
public void whenNameIsDavidBodyIsNameSong() {
String ln = System.lineSeparator();
String expected = "{" + ln
+ "\t\"name\" : \"David Gilmour\"," + ln
+ "\t\"body\" : \"Shine On You Crazy Diamond\"" + ln
+ "}";
String name = "David Gilmour";
String body = "Shine On You Crazy Diamond";
String result = new JSONReport().generate(name, body);
assertThat(result).isEqualTo(expected);
}


}

0 comments on commit d310067

Please sign in to comment.