diff --git a/dseki/paiza/src/C084.java b/dseki/paiza/src/C084.java new file mode 100644 index 0000000000000000000000000000000000000000..ffe5bf46fa74e07436d92e17e3a2c8920326830f --- /dev/null +++ b/dseki/paiza/src/C084.java @@ -0,0 +1,79 @@ +import java.util.Scanner; + +/** + * 枠で囲むクラス. + */ +public class C084 { + private static final int NUMBER_TO_ENCLOSE_TEXT = 2; // 文字の左右を囲むための数 + private static final char DECORATION_SYMBOL = '+'; // 文字を囲むための装飾文字 + private static final String NEW_LINE = System.getProperty("line.separator"); // 改行のための文字列 + private final String text; // 読み込む文字列 + + C084(final String text) { + this.text = text; + } + + /** + * メインメソッド. + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + final String text = sc.nextLine(); + final C084 c084 = new C084(text); + c084.outputDecorateText(); + sc.close(); + } + + /** + * 文字の周りを囲むメソッド. + */ + String decorateText() { + String decoration = ""; + decoration += decorateUpperAndLowerRowsOfText(); + decoration += NEW_LINE; + decoration += decorateMiddleRowOfText(); + decoration += NEW_LINE; + decoration += decorateUpperAndLowerRowsOfText(); + return decoration; + } + + /** + * 文字の周りを囲む出力メソッド. + */ + public void outputDecorateText() { + System.out.println(decorateText()); + } + + /** + * 文字の上下に囲いを作るメソッド. + * + * @return 囲みたい文字列の上下に出力する枠を返す + */ + private String decorateUpperAndLowerRowsOfText() { + String decoration = ""; + final int textLength = countNumberOfCharacters(); + for (int i = 0; i < (textLength + NUMBER_TO_ENCLOSE_TEXT); i++) { + decoration += DECORATION_SYMBOL; + } + return decoration; + } + + /** + * 文字列の左右に囲いを作るメソッド. + * + * @return 左右を枠で囲んだ文字列を返す + */ + private String decorateMiddleRowOfText() { + final String decoration = DECORATION_SYMBOL + this.text + DECORATION_SYMBOL; + return decoration; + } + + /** + * 文字列の数を返すメソッド. + * + * @return 文字列の数を返す + */ + int countNumberOfCharacters() { + return this.text.length(); + } +} \ No newline at end of file diff --git a/dseki/paiza/test/C084Test.java b/dseki/paiza/test/C084Test.java new file mode 100644 index 0000000000000000000000000000000000000000..80294b33b17fdc8278b49fe772482559296bca39 --- /dev/null +++ b/dseki/paiza/test/C084Test.java @@ -0,0 +1,49 @@ +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.Test; + +public class C084Test { + private static final String NEW_LINE = System.getProperty("line.separator"); + + @Test + public void countNumberOfCharactersで_paiza_の文字列の数5を取得する() throws Exception { + final C084 c084 = new C084("paiza"); + assertThat(c084.countNumberOfCharacters(), is(5)); + } + + @Test + public void countNumberOfCharactersで文字列の数0を取得する() throws Exception { + final C084 c084 = new C084(""); + assertThat(c084.countNumberOfCharacters(), is(0)); + } + + @Test + public void countNumberOfCharactersでHello_Worldの文字列の数12を取得する() throws Exception { + final C084 c084 = new C084("Hello World!"); + assertThat(c084.countNumberOfCharacters(), is(12)); + } + + + @Test + public void decorateTextでpaizaの周りを枠で囲む() throws Exception { + final C084 c084 = new C084("paiza"); + assertThat(c084.decorateText(), is("+++++++" + NEW_LINE + + "+paiza+" + NEW_LINE + "+++++++")); + } + + @Test + public void decorateTextで空文字の周りを枠で囲む() throws Exception { + final C084 c084 = new C084(""); + assertThat(c084.decorateText(), is("++" + NEW_LINE + + "++" + NEW_LINE + "++")); + } + + @Test + public void decorateTextでHello_Worldの周りを枠で囲む() throws Exception { + final C084 c084 = new C084("Hello World!"); + assertThat(c084.decorateText(), is("++++++++++++++" + NEW_LINE + + "+Hello World!+" + NEW_LINE + "++++++++++++++")); + } + +} +