diff --git a/htabuchi/src/FormatInfo.java b/htabuchi/src/FormatInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..6427920f3a57e755d9689cb988b8c02d65661af8 --- /dev/null +++ b/htabuchi/src/FormatInfo.java @@ -0,0 +1,48 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * create 2025/07/18 + * + * @author 田渕日奈子 + * @version 2.0 + */ + + +/** + * 表示用にコードをまとめるクラス + */ +public class FormatInfo { + + public FormatInfo(final String firstRowCode, final String secondRowCode, final String thirdRowCode) { + this.firstRowCode = firstRowCode; + this.secondRowCode = secondRowCode; + this.thirdRowCode = thirdRowCode; + } + + private String firstRowCode; + private String secondRowCode; + private String thirdRowCode; + + public String getFirstRowCode() { + return this.firstRowCode; + } + + public String getSecondRowCode() { + return this.secondRowCode; + } + + public String getThirdRowCode() { + return this.thirdRowCode; + } + + public void addFirstRowCode(String firstRowCode) { + this.firstRowCode = this.firstRowCode + firstRowCode; + } + + public void addSecondRowCode(String secondRowCode) { + this.secondRowCode = this.secondRowCode + secondRowCode; + } + + public void addThirdRowCode(String thirdRowCode) { + this.thirdRowCode = this.thirdRowCode + thirdRowCode; + } +} diff --git a/htabuchi/src/SimpleCode.java b/htabuchi/src/SimpleCode.java new file mode 100644 index 0000000000000000000000000000000000000000..7785dd75d83b6f8db9b7707400caab5112a34dcd --- /dev/null +++ b/htabuchi/src/SimpleCode.java @@ -0,0 +1,189 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * create 2025/07/18 + * + * @author 田渕日奈子 + * @version 2.0 + */ + + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class SimpleCode { + + /** + * 数字の場合分けによる各コード作成 + */ + public enum Num { + NUM_VALUE_1(1), NUM_VALUE_2(2), NUM_VALUE_3(3), NUM_VALUE_4(4), NUM_VALUE_5(5), NUM_VALUE_6(6), NUM_VALUE_7(7), + NUM_VALUE_8(8), NUM_VALUE_9(9); + + Num(final int numValue) { + this.numValue = numValue; + this.numMatrix = new String[][]{ + {".",".","."}, + {".",".","."}, + {".",".","."} + }; + + makeCode(numValue); + } + + private int numValue; + private String[][] numMatrix; + private final int rowCode = 3; // コードの縦幅 + private final int columnCode = 3; // コードの横幅 + + /** + * 数字に応じてコードを生成する + */ + public void makeCode(final int numValue) { + int replacedCount = 0; // 「.」を「#」に置き換えた回数 + + for (int row = 0; row < rowCode; row++) { + for (int column = 0; column < columnCode; column++) { + if (replacedCount == numValue) + break; + this.numMatrix[row][column] = "#"; + replacedCount++; + } + if (replacedCount == numValue) + break; + } + } + + public String[][] getNumMatrix() { + return this.numMatrix; + } + + public int getNumValue() { + return this.numValue; + } + + } + + /* メイン関数 */ + public static void main(String[] args) { + SimpleCode simpleCode = new SimpleCode(); + List resultList = simpleCode.summarizeCode(simpleCode.scanAllNum(System.in)); + List cleanFormat = cleanFormat(resultList); + simpleCode.displayResult(cleanFormat); + } + + /** + * 数字の情報を読み取る + * + * @inputStream inputStream 入力ストリーム + * @return numIntList コードの値である数字をまとめたList + */ + public List scanAllNum(InputStream inputStream) { + List numIntList = new ArrayList<>(); // 型を変更した後のList + + try (Scanner sc = new Scanner(inputStream)){ + final String numSentence = sc.nextLine(); // お題のすべての数字 + final List numStrList = changeStringList(numSentence); // 1文字ずつ格納したString型のList + numIntList = changeListType(numStrList); + + } catch (NumberFormatException e) { + throw new IllegalArgumentException("エラー: 数字以外が入力されました。", e); + } catch (NoSuchElementException e) { + throw new IllegalArgumentException("エラー: 入力が見つかりません。", e); + } + + return numIntList; + } + + /** + * 1文字ずつ分割してArrayListに変換 + * + * @param sentence String型の文 + * @return stringList String型の配列 + */ + public List changeStringList(final String sentence) { + final List stringList = Arrays.asList(sentence.split("")); + return stringList; + } + + /** + * Listの型変換 String→int + * + * @param strList String型のList配列 + * @return intList Integer型のList配列 + */ + public List changeListType(final List strList) { + List intList = strList.stream().map(str -> Integer.parseInt(str)).collect(Collectors.toList()); + return intList; + } + + /** + * Numを用いてコードを判別して結果としてListにまとめる + * + * @param numIntList お題の数字をまとめたList + * @return resultList お題の数字から導かれたコードのList + */ + public List summarizeCode(final List numIntList) { + List resultList = new ArrayList<>(); // 結果格納用 + for (int numValue : numIntList) { + for (Num num : Num.values()) { + if (numValue == num.getNumValue()) { + resultList.add(num.getNumMatrix()); + } + } + } + + return resultList; + } + + /** + * 3つのコードを1行として表示できるListを生成する + * + * @param resultList お題の数字から導かれたコードのList + * @return formatInfoList 3つのコードを1行表示としたList + */ + public static List cleanFormat(final List resultList) { + final int maxColumn = 3; // 横に並べることができるコードの最大数 + + List formatInfoList = new ArrayList<>(); // 結果表示用List + int count = 0; // コード全体の横幅を超えないか数える + + FormatInfo format = new FormatInfo("", "", ""); + + for (String[][] result : resultList) { + + for (int column = 0; column < maxColumn; column++) { + format.addFirstRowCode(result[0][column]); + format.addSecondRowCode(result[1][column]); + format.addThirdRowCode(result[2][column]); + } + count++; + if (count == maxColumn) { + formatInfoList.add(format); + format = new FormatInfo("", "", ""); + count = 0; + continue; + } + } + + return formatInfoList; + } + + /** + * 結果のコードをまとめたListから横幅にあてはまるように表示を行う + * + * @param resultArray お題の数字から導かれたコードのList + */ + public void displayResult(final List formatInfoList) { + for (FormatInfo oneCodeRow : formatInfoList) { + System.out.println(oneCodeRow.getFirstRowCode()); + System.out.println(oneCodeRow.getSecondRowCode()); + System.out.println(oneCodeRow.getThirdRowCode()); + } + } + +} diff --git a/htabuchi/test/FormatInfoTest.java b/htabuchi/test/FormatInfoTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1e460944125a954df3834b7714ddaf5346b59586 --- /dev/null +++ b/htabuchi/test/FormatInfoTest.java @@ -0,0 +1,84 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * テストコード
+ * create 2025/07/22 + * + * @author 田渕日奈子 + * @version 2.0 + */ + + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +import B128_htabuchi.FormatInfo; + +@RunWith(Enclosed.class) +public class FormatInfoTest { + + public static class GetTest { + FormatInfo formatInfo; + + @Before + public void setUp() { + formatInfo = new FormatInfo("First", "Second", "Third"); + } + + @Test + public void getFirstRowCodeで正しい値を返す_Firstを返す場合() { + assertEquals("First", formatInfo.getFirstRowCode()); + } + + @Test + public void getSecondRowCodeで正しい値を返す_Secondを返す場合() { + assertEquals("Second", formatInfo.getSecondRowCode()); + } + + @Test + public void getThirdRowCodeで正しい値を返す_Thirdを返す場合() { + assertEquals("Third", formatInfo.getThirdRowCode()); + } + + } + + public static class SetTest { + FormatInfo formatInfo; + String message; + + @Before + public void setUp() { + formatInfo = new FormatInfo("First", "Second", "Third"); + message = "sentence"; + } + + @Test + public void setFirstRowCodeで正しい値を返す__Firstにsentenceをつなげる場合() { + String expectedSentence = "Firstsentence"; + formatInfo.addFirstRowCode(message); + + assertEquals(expectedSentence, formatInfo.getFirstRowCode()); + } + + @Test + public void setSecondRowCodeで正しい値を返す__Secondにsentenceをつなげる場合() { + String expectedSentence = "Secondsentence"; + formatInfo.addSecondRowCode(message); + + assertEquals(expectedSentence, formatInfo.getSecondRowCode()); + } + + @Test + public void setThirdRowCodeで正しい値を返す__Thirdにsentenceをつなげる場合() { + String expectedSentence = "Thirdsentence"; + formatInfo.addThirdRowCode(message); + + assertEquals(expectedSentence, formatInfo.getThirdRowCode()); + } + + } + +} diff --git a/htabuchi/test/SimpleCodeTest.java b/htabuchi/test/SimpleCodeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f0b1067feefa0c3776942acdad89bf48684333a4 --- /dev/null +++ b/htabuchi/test/SimpleCodeTest.java @@ -0,0 +1,303 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * テストコード
+ * create 2025/07/22 + * + * @author 田渕日奈子 + * @version 2.0 + */ + + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import B128_htabuchi.FormatInfo; +import B128_htabuchi.SimpleCode; +import B128_htabuchi.SimpleCode.Num; + +@RunWith(Enclosed.class) +public class SimpleCodeTest { + + @RunWith(Theories.class) + public static class EnumTest { + + static class EnumInfo{ + private Num actualEnum; + private int expectedValue; + private String[][] expectedMatrix; + + public EnumInfo(final Num actualEnum, final int expectedValue, final String[][] expectedMatrix) { + this.actualEnum = actualEnum; + this.expectedValue = expectedValue; + this.expectedMatrix = expectedMatrix; + } + } + + @DataPoints + public static EnumInfo[] enumParams = { + new EnumInfo(Num.NUM_VALUE_1, 1, + new String[][] { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_2, 2, + new String[][] { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_3, 3, + new String[][] { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_4, 4, + new String[][] { { "#", "#", "#" }, { "#", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_5, 5, + new String[][] { { "#", "#", "#" }, { "#", "#", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_6, 6, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_7, 7, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_8, 8, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", "#", "." } }), + new EnumInfo(Num.NUM_VALUE_9, 9, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", "#", "#" } }) }; + + @Theory + public void enumが正しくgetNumMatrixを生成して返す(EnumInfo param) throws Exception{ + assertArrayEquals(param.expectedMatrix, param.actualEnum.getNumMatrix()); + } + + @Theory + public void enumが正しくgetNumValueを生成して返す(EnumInfo param) throws Exception{ + assertEquals(param.expectedValue, param.actualEnum.getNumValue()); + } + } + + public static class ScanAllNumTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test(expected = IllegalArgumentException.class) + public void Scannerに例外を返す_入力値がない場合() { + final String input = "\n"; + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); + } + + @Test + public void scanAllNumがInteger型のListを返す() { + final String input = "123456"; + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + final List actualList = simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); + + final List expectedNum = List.of(1, 2, 3, 4, 5, 6); + + assertEquals(expectedNum, actualList); + } + + @Test(expected = IllegalArgumentException.class) + public void scanAllNumが例外を返す_値がない場合() { + final String input = ""; + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); + } + + } + + public static class ChangeStringListTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test(expected = NullPointerException.class) + public void changeStringListにnullを渡す_例外を返す場合() { + final String sentence = null; + simpleCode.changeStringList(sentence); + } + + @Test + public void changeStringListに空白を渡す_空のリストを返す場合() { + final String sentence = ""; + final List actualList = simpleCode.changeStringList(sentence); + + assertEquals(List.of(""), actualList); + } + + @Test + public void changeStringListに数字文字列を渡す_正常に動作する場合() { + final String sentence = "123"; + final List actualList = simpleCode.changeStringList(sentence); + + assertEquals(List.of("1", "2", "3"), actualList); + } + } + + public static class ChangeListTypeTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test + public void changeStringListが正常にコードを返す_1を渡した場合() { + final List strList = List.of("1"); + final List actualList = simpleCode.changeListType(strList); + final List expectedList = List.of(1); + + assertEquals(expectedList, actualList); + } + + @Test(expected = NumberFormatException.class) + public void changeStringListが例外を返す_Aを渡した場合() { + final List strList = List.of("A"); + simpleCode.changeListType(strList); + } + + } + + public static class FinaLizeCodeTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test + public void summarizeCode正常にコードを返す_1を渡した場合() { + final List numIntList = List.of(1); + final List actualList = simpleCode.summarizeCode(numIntList); + final String[][] expectedMatrix = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + + assertArrayEquals(expectedMatrix, actualList.get(0)); + } + + @Test + public void summarizeCodeが空白リストを返す_0を渡してenumに当てはまらない場合() { + final List numIntList = List.of(0); + final List actualList = simpleCode.summarizeCode(numIntList); + + assertTrue(actualList.isEmpty()); + } + + @Test + public void summarizeCodeが空のリストを返す_空リストを渡した場合() { + final List numIntList = List.of(); + final List actualList = simpleCode.summarizeCode(numIntList); + + assertTrue(actualList.isEmpty()); + } + } + + public static class CleanFormatTest { + + @Test + public void cleanFormatに3つの行列を渡して正常にコードを返す() { + final String[][] firstArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] secondArray = { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] thirdArray = { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }; + final List resultList = new ArrayList<>(); + resultList.add(firstArray); + resultList.add(secondArray); + resultList.add(thirdArray); + + final List actualArray = SimpleCode.cleanFormat(resultList); + + assertEquals("#..##.###", actualArray.get(0).getFirstRowCode()); + assertEquals(".........", actualArray.get(0).getSecondRowCode()); + assertEquals(".........", actualArray.get(0).getThirdRowCode()); + } + + @Test + public void cleanFormatに6つの行列を渡して正常にコードを返す() { + final String[][] firstArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] secondArray = { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] thirdArray = { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] fourthArray = { { "#", "#", "#" }, { "#", ".", "." }, { ".", ".", "." } }; + final String[][] fifthArray = { { "#", "#", "#" }, { "#", "#", "." }, { ".", ".", "." } }; + final String[][] sixthArray = { { "#", "#", "#" }, { "#", "#", "#" }, { ".", ".", "." } }; + + List resultList = new ArrayList<>(); + + resultList.add(firstArray); + resultList.add(secondArray); + resultList.add(thirdArray); + resultList.add(fourthArray); + resultList.add(fifthArray); + resultList.add(sixthArray); + + List actualArray = SimpleCode.cleanFormat(resultList); + + assertEquals("#..##.###", actualArray.get(0).getFirstRowCode()); + assertEquals(".........", actualArray.get(0).getSecondRowCode()); + assertEquals(".........", actualArray.get(0).getThirdRowCode()); + assertEquals("#########", actualArray.get(1).getFirstRowCode()); + assertEquals("#..##.###", actualArray.get(1).getSecondRowCode()); + assertEquals(".........", actualArray.get(1).getThirdRowCode()); + } + + @Test(expected = NullPointerException.class) + public void cleanFormatにnullの行列を渡して例外を返す() { + List resultArray = null; + SimpleCode.cleanFormat(resultArray); + } + } + + public static class DisplayResultTest { + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + FormatInfo formatInfo; + SimpleCode simpleCode; + + @Before + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + simpleCode = new SimpleCode(); + } + + @Test + public void displayResultが正しい表示をする_3つのコードの場合() { + List formatInfoList = new ArrayList<>(); + formatInfoList.add(new FormatInfo("#..##.###",".........",".........")); + + simpleCode.displayResult(formatInfoList); + + assertEquals("#..##.###\r\n.........\r\n.........\r\n", outputStreamCaptor.toString()); + + } + + @Test + public void displayResultが正しい表示をする_6つのコードの場合() { + List formatInfoList = new ArrayList<>(); + formatInfoList.add(new FormatInfo("#..##.###",".........",".........")); + formatInfoList.add(new FormatInfo("#########","#..##.###",".........")); + + simpleCode.displayResult(formatInfoList); + + assertEquals("#..##.###\r\n.........\r\n.........\r\n#########\r\n#..##.###\r\n.........\r\n", outputStreamCaptor.toString()); + + } + + @After + public void tearDown() { + System.setOut(standardOut); + } + } +}