From 63f48412322ad471ea3ef9214214160a32804b5c Mon Sep 17 00:00:00 2001 From: ming Date: Tue, 26 Nov 2024 01:35:30 +0900 Subject: [PATCH 01/14] =?UTF-8?q?refactor:=203=EB=8B=A8=EA=B3=84=20?= =?UTF-8?q?=ED=94=BC=EB=93=9C=EB=B0=B1=20=EB=B0=98=EC=98=81=20-=20?= =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=ED=95=9C=EC=9D=98=20test=20fixture=EB=A7=8C=20?= =?UTF-8?q?=EB=82=A8=EA=B8=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/nextstep/ladder/LadderTest.java | 26 ++++++-------- src/test/java/nextstep/ladder/LineTest.java | 14 -------- src/test/java/nextstep/ladder/LinesTest.java | 35 +++---------------- src/test/java/nextstep/ladder/PointsTest.java | 5 ++- 4 files changed, 16 insertions(+), 64 deletions(-) delete mode 100644 src/test/java/nextstep/ladder/LineTest.java diff --git a/src/test/java/nextstep/ladder/LadderTest.java b/src/test/java/nextstep/ladder/LadderTest.java index cd281bfa05..688eeffc76 100644 --- a/src/test/java/nextstep/ladder/LadderTest.java +++ b/src/test/java/nextstep/ladder/LadderTest.java @@ -15,32 +15,26 @@ public class LadderTest { @BeforeAll static void setup() { ladder = new Ladder( - new Usernames(Arrays.asList("test", "test2", "test3", "test4", "test5")), + new Usernames(Arrays.asList("test", "test2", "test3")), new Lines(Arrays.asList( - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false), new Point(false, false), new Point(false, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false), new Point(false, false)))), - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, false), new Point(false, false), new Point(false, true), new Point(true, false)))) + new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false)))), + new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false)))) )) - , Arrays.asList("result1", "result2", "result3", "result4", "result5")); + , Arrays.asList("결과1", "결과2", "결과3")); } @Test @DisplayName("모든 사용자의 사다리 결과") - public void getAllResult() { + void getAllResult() { assertThat(ladder.getResult("all")) - .isEqualTo("test4 : result2\n" + - "test5 : result5\n" + - "test2 : result4\n" + - "test3 : result1\n" + - "test : result3"); + .isEqualTo("test2 : 결과3\n" + + "test3 : 결과1\n" + + "test : 결과2"); } @Test @DisplayName("사용자별 사다리 결과") - public void getResult() { - assertThat(ladder.getResult("test4")).isEqualTo("result2"); + void getResult() { + assertThat(ladder.getResult("test2")).isEqualTo("결과3"); } } diff --git a/src/test/java/nextstep/ladder/LineTest.java b/src/test/java/nextstep/ladder/LineTest.java deleted file mode 100644 index 78089515a0..0000000000 --- a/src/test/java/nextstep/ladder/LineTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package nextstep.ladder; - -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; - -public class LineTest { -// @Test -// @DisplayName("중복되는 가로선이 없는지 출력 확인") -// void check() { -// Line line2 = new Line(6); -// System.out.println(line2.toString("-----", " ")); -// } - -} diff --git a/src/test/java/nextstep/ladder/LinesTest.java b/src/test/java/nextstep/ladder/LinesTest.java index e7dc44ba49..c9df6ce522 100644 --- a/src/test/java/nextstep/ladder/LinesTest.java +++ b/src/test/java/nextstep/ladder/LinesTest.java @@ -1,46 +1,19 @@ package nextstep.ladder; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Arrays; - import static org.assertj.core.api.Assertions.assertThat; public class LinesTest { private static Lines lines; -// @BeforeAll -// static void setUpBeforeClass() { -// lines = new Lines(6, 5); -// } - - @BeforeEach - void setUp() { -// lines = new Lines(6, 5); - lines = new Lines( - Arrays.asList( - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false), new Point(false, false), new Point(false, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false), new Point(false, false)))), - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, false), new Point(false, false), new Point(false, true), new Point(true, false)))) - ) - ); + @BeforeAll + static void setUpBeforeClass() { + lines = new Lines(6, 5); } -// @Test -// @DisplayName("사다리 전체 출력 확인") -// void check() { -// for (Line line : lines.getValues()) { -// System.out.println(line.toString()); -// System.out.println(line.getResultIndexs()); -// } -// System.out.println(lines.toString("-----", " ")); -// } - @Test @DisplayName("사다리 라인 사이즈 확인") void size() { diff --git a/src/test/java/nextstep/ladder/PointsTest.java b/src/test/java/nextstep/ladder/PointsTest.java index ce025545cc..16c53fef42 100644 --- a/src/test/java/nextstep/ladder/PointsTest.java +++ b/src/test/java/nextstep/ladder/PointsTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -20,7 +19,7 @@ void size() { @Test void 결과_인덱스_목록_조회() { - points = new Points(List.of(new Point(false, false), new Point(false, true), new Point(true, false), new Point(false, true), new Point(true, false))); - assertThat(points.getResultIndex(3)).isEqualTo(4); + points = new Points(List.of(new Point(false, false), new Point(false, true), new Point(true, false))); + assertThat(points.getResultIndex(2)).isEqualTo(1); } } From 60fe96d4bdbf16f32e80290cfc81747149cdca1b Mon Sep 17 00:00:00 2001 From: ming Date: Thu, 28 Nov 2024 18:21:46 +0900 Subject: [PATCH 02/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20TDD?= =?UTF-8?q?=EB=A1=9C=20Point=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +++++++++++++++++++- src/main/java/nextstep/ladder/Direction.java | 7 +++++ src/main/java/nextstep/ladder/Point.java | 24 ++++++++++++---- src/test/java/nextstep/ladder/PointTest.java | 28 ++++++++++++++++++ 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/main/java/nextstep/ladder/Direction.java diff --git a/README.md b/README.md index 98fae86a00..535e9b23fb 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,32 @@ - [x] 사다리 높이만큼 반복 - 결과 -- [x] 사람별 사다리게임 결과 \ No newline at end of file +- [x] 사람별 사다리게임 결과 + + +--- + +TDD로 구현 + +도메인 +1. point + current, right + 둘다 true일 수는 없음 + current > left로 이동 + right > right로 이동 + 둘다 false > 그대로 아래로 이동 + + 오류 방지 + - 첫번째 point 생성 + - 다음 point 생성 + - 마지막 point 생성 + ++ Cross + 현재 위치 position + point + +2. line + crosses + +3. ladder + lines \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/Direction.java b/src/main/java/nextstep/ladder/Direction.java new file mode 100644 index 0000000000..0fe988dc95 --- /dev/null +++ b/src/main/java/nextstep/ladder/Direction.java @@ -0,0 +1,7 @@ +package nextstep.ladder; + +public enum Direction { + LEFT, + RIGHT, + DOWN; +} diff --git a/src/main/java/nextstep/ladder/Point.java b/src/main/java/nextstep/ladder/Point.java index 440367ac71..5142578ac1 100644 --- a/src/main/java/nextstep/ladder/Point.java +++ b/src/main/java/nextstep/ladder/Point.java @@ -1,11 +1,14 @@ package nextstep.ladder; public class Point { - private boolean left; + private boolean current; private boolean right; - public Point(boolean left, boolean right) { - this.left = left; + public Point(boolean current, boolean right) { + if(current && right) { + throw new IllegalArgumentException("유효하지 않은 값입니다."); + } + this.current = current; this.right = right; } @@ -18,14 +21,25 @@ public String getLineSymbol(String trueSymbol, String falseSymbol) { } public int getResultIndex(int index) { - return left ? index - 1 : (right ? index + 1 : index); + return current ? index - 1 : (right ? index + 1 : index); } @Override public String toString() { return "Point{" + - "left=" + left + + "left=" + current + ", right=" + right + '}'; } + + public Direction move() { + if (current) { + return Direction.LEFT; + } + if (right) { + return Direction.RIGHT; + } + + return Direction.DOWN; + } } diff --git a/src/test/java/nextstep/ladder/PointTest.java b/src/test/java/nextstep/ladder/PointTest.java index f72eb3d4f5..6c9638608b 100644 --- a/src/test/java/nextstep/ladder/PointTest.java +++ b/src/test/java/nextstep/ladder/PointTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; public class PointTest { private Point point; @@ -13,8 +14,35 @@ void setUp() { point = new Point(false, true); } + @Test void 결과_인덱스_조회() { assertThat(point.getResultIndex(0)).isEqualTo(1); } + + @Test + void 움직인_결과_조회__오른쪽() { + Point point = new Point(false, true); + assertThat(point.move()).isEqualTo(Direction.RIGHT); + } + + @Test + void 움직인_결과_조회__왼쪽() { + Point point = new Point(true, false); + assertThat(point.move()).isEqualTo(Direction.LEFT); + } + + @Test + void 움직인_결과_조회__아래() { + Point point = new Point(false, false); + assertThat(point.move()).isEqualTo(Direction.DOWN); + } + + @Test + void 유효하지_않은_상태값() { + assertThatIllegalArgumentException().isThrownBy(() -> { + Point point = new Point(true, true); + }); + } + } From b90c25bedaf1b5d1c90754daa373f6a741410eaa Mon Sep 17 00:00:00 2001 From: ming Date: Fri, 29 Nov 2024 20:51:59 +0900 Subject: [PATCH 03/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20Poi?= =?UTF-8?q?nt=EB=A5=BC=20=EB=8D=94=20=EC=95=88=EC=A0=84=ED=95=98=EA=B2=8C?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20first,=20next,=20last=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Point.java | 12 ++++++++++++ src/test/java/nextstep/ladder/PointTest.java | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/nextstep/ladder/Point.java b/src/main/java/nextstep/ladder/Point.java index 5142578ac1..08202d83bd 100644 --- a/src/main/java/nextstep/ladder/Point.java +++ b/src/main/java/nextstep/ladder/Point.java @@ -32,6 +32,18 @@ public String toString() { '}'; } + public static Point first(boolean right) { + return new Point(false, right); + } + + public Point next(boolean right) { + return new Point(this.right, right); + } + + public Point last() { + return new Point(this.right, false); + } + public Direction move() { if (current) { return Direction.LEFT; diff --git a/src/test/java/nextstep/ladder/PointTest.java b/src/test/java/nextstep/ladder/PointTest.java index 6c9638608b..59ad8fc340 100644 --- a/src/test/java/nextstep/ladder/PointTest.java +++ b/src/test/java/nextstep/ladder/PointTest.java @@ -22,19 +22,19 @@ void setUp() { @Test void 움직인_결과_조회__오른쪽() { - Point point = new Point(false, true); + Point point = Point.first(true); assertThat(point.move()).isEqualTo(Direction.RIGHT); } @Test void 움직인_결과_조회__왼쪽() { - Point point = new Point(true, false); + Point point = Point.first(true).next(false); assertThat(point.move()).isEqualTo(Direction.LEFT); } @Test void 움직인_결과_조회__아래() { - Point point = new Point(false, false); + Point point = Point.first(true).next(false).last(); assertThat(point.move()).isEqualTo(Direction.DOWN); } From 1f1fb92e546f873e94f71529bcc09f8ee563f2c9 Mon Sep 17 00:00:00 2001 From: ming Date: Fri, 29 Nov 2024 21:39:26 +0900 Subject: [PATCH 04/14] =?UTF-8?q?feat:=204=EB=8B=A8=EA=B3=84=20-=20Cross?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 ++++++++++++-------- src/main/java/nextstep/ladder/Cross.java | 21 ++++++++++++++ src/test/java/nextstep/ladder/CrossTest.java | 26 ++++++++++++++++++ 3 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 src/main/java/nextstep/ladder/Cross.java create mode 100644 src/test/java/nextstep/ladder/CrossTest.java diff --git a/README.md b/README.md index 535e9b23fb..a93f2fa564 100644 --- a/README.md +++ b/README.md @@ -62,23 +62,28 @@ TDD로 구현 도메인 1. point - current, right - 둘다 true일 수는 없음 - current > left로 이동 - right > right로 이동 - 둘다 false > 그대로 아래로 이동 + current, right + 둘다 true일 수는 없음 + current > left로 이동 + right > right로 이동 + 둘다 false > 그대로 아래로 이동 - 오류 방지 - - 첫번째 point 생성 - - 다음 point 생성 - - 마지막 point 생성 + 오류 방지 + - 첫번째 point 생성 + - 다음 point 생성 + - 마지막 point 생성 + Cross 현재 위치 position point -2. line - crosses + move + - right > index+1 + - left > index-1 + - down > index + +2. line + crosses 3. ladder - lines \ No newline at end of file + lines \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/Cross.java b/src/main/java/nextstep/ladder/Cross.java new file mode 100644 index 0000000000..2b3ce25712 --- /dev/null +++ b/src/main/java/nextstep/ladder/Cross.java @@ -0,0 +1,21 @@ +package nextstep.ladder; + +public class Cross { + private int position; + private Point point; + + public Cross(int position, Point point) { + this.position = position; + this.point = point; + } + + public int move() { + if(point.move() == Direction.RIGHT) { + return position + 1; + } else if(point.move() == Direction.LEFT) { + return position - 1; + } + + return position; + } +} diff --git a/src/test/java/nextstep/ladder/CrossTest.java b/src/test/java/nextstep/ladder/CrossTest.java new file mode 100644 index 0000000000..00c199572a --- /dev/null +++ b/src/test/java/nextstep/ladder/CrossTest.java @@ -0,0 +1,26 @@ +package nextstep.ladder; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CrossTest { + + @Test + void right() { + Cross cross = new Cross(0, Point.first(true)); + assertThat(cross.move()).isEqualTo(1); + } + + @Test + void left() { + Cross cross = new Cross(1, Point.first(true).next(false)); + assertThat(cross.move()).isEqualTo(0); + } + + @Test + void down() { + Cross cross = new Cross(0, Point.first(true).next(false).last()); + assertThat(cross.move()).isEqualTo(0); + } +} From 69de9c9a891584460488faa2be15ec19fd5e13d7 Mon Sep 17 00:00:00 2001 From: ming Date: Sun, 1 Dec 2024 17:53:27 +0900 Subject: [PATCH 05/14] =?UTF-8?q?fix:=204=EB=8B=A8=EA=B3=84=20-=20next=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Point.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/nextstep/ladder/Point.java b/src/main/java/nextstep/ladder/Point.java index 08202d83bd..8ba37482ee 100644 --- a/src/main/java/nextstep/ladder/Point.java +++ b/src/main/java/nextstep/ladder/Point.java @@ -37,6 +37,9 @@ public static Point first(boolean right) { } public Point next(boolean right) { + if(this.right) { + return new Point(this.right, false); + } return new Point(this.right, right); } From 73e54c95473ec1b8ba65e4d9d546917ec459a874 Mon Sep 17 00:00:00 2001 From: ming Date: Sun, 1 Dec 2024 18:41:29 +0900 Subject: [PATCH 06/14] =?UTF-8?q?fix:=204=EB=8B=A8=EA=B3=84=20-=20line,=20?= =?UTF-8?q?ladder=20=EC=9E=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Ladder2.java | 29 ++++++++++++++++++++++ src/main/java/nextstep/ladder/Line2.java | 29 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/main/java/nextstep/ladder/Ladder2.java create mode 100644 src/main/java/nextstep/ladder/Line2.java diff --git a/src/main/java/nextstep/ladder/Ladder2.java b/src/main/java/nextstep/ladder/Ladder2.java new file mode 100644 index 0000000000..9d49f821f9 --- /dev/null +++ b/src/main/java/nextstep/ladder/Ladder2.java @@ -0,0 +1,29 @@ +package nextstep.ladder; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder2 { + private List lines; + + public Ladder2(int countOfPerson, int height) { + createLines(countOfPerson, height); + } + + private void createLines(int countOfPerson, int height) { + lines = new ArrayList(); + + for(int i = 0; i < height; i++) { + lines.add(new Line2(countOfPerson)); + } + } + + public int move(int position) { + int index = position; + for(Line2 line : lines) { + index = line.move(index); + } + return index; + } + +} diff --git a/src/main/java/nextstep/ladder/Line2.java b/src/main/java/nextstep/ladder/Line2.java new file mode 100644 index 0000000000..e428c101d9 --- /dev/null +++ b/src/main/java/nextstep/ladder/Line2.java @@ -0,0 +1,29 @@ +package nextstep.ladder; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Line2 { + private static final Random RANDOM = new Random(); + + private List crosses; + + public Line2(int countOfPerson) { + createCrosses(countOfPerson); + } + + private void createCrosses(int countOfPerson) { + crosses = new ArrayList(); + + Point point = Point.first(RANDOM.nextBoolean()); + for (int i = 0; i < countOfPerson; i++) { + crosses.add(new Cross(i, i == 0 ? point : i == countOfPerson - 1 ? point.last() : point.next(RANDOM.nextBoolean()))); + } + } + + public int move(int position) { + return this.crosses.get(position).move(); + } + +} From e30b8adec7bc589e09e654aefec8477ac529962f Mon Sep 17 00:00:00 2001 From: mslee Date: Thu, 5 Dec 2024 12:43:55 +0900 Subject: [PATCH 07/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EC=B6=9C=EB=A0=A5=20=EC=9E=AC?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++++++- src/main/java/nextstep/ladder/Cross.java | 8 ++++ src/main/java/nextstep/ladder/Ladder2.java | 15 ++++++- .../java/nextstep/ladder/LadderMain2.java | 20 +++++++++ src/main/java/nextstep/ladder/Line2.java | 11 +++++ src/main/java/nextstep/ladder/ResultView.java | 19 ++++++++ src/main/java/nextstep/ladder/Usernames.java | 6 +++ .../java/nextstep/ladder/Ladder2Test.java | 45 +++++++++++++++++++ src/test/java/nextstep/ladder/Line2Test.java | 25 +++++++++++ 9 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 src/main/java/nextstep/ladder/LadderMain2.java create mode 100644 src/test/java/nextstep/ladder/Ladder2Test.java create mode 100644 src/test/java/nextstep/ladder/Line2Test.java diff --git a/README.md b/README.md index a93f2fa564..9289ad1f7f 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ TDD로 구현 - 다음 point 생성 - 마지막 point 생성 -+ Cross +2. Cross 현재 위치 position point @@ -82,8 +82,21 @@ TDD로 구현 - left > index-1 - down > index +- [x] 출력 + - "-----", " " + 2. line crosses +- [x] 출력 + - "|-----| |-----|" + 3. ladder - lines \ No newline at end of file + lines + +- [x] 출력 + - "|-----| |-----|" + - "| |------| |" + - "|-----| |-----|" + +- [ ] 결과 조회 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/Cross.java b/src/main/java/nextstep/ladder/Cross.java index 2b3ce25712..2b5a46ab34 100644 --- a/src/main/java/nextstep/ladder/Cross.java +++ b/src/main/java/nextstep/ladder/Cross.java @@ -18,4 +18,12 @@ public int move() { return position; } + + public String getCrossForPrint(String trueSymbol, String falseSymbol) { + if(point.move() == Direction.RIGHT) { + return trueSymbol; + } + + return falseSymbol; + } } diff --git a/src/main/java/nextstep/ladder/Ladder2.java b/src/main/java/nextstep/ladder/Ladder2.java index 9d49f821f9..20b5159d26 100644 --- a/src/main/java/nextstep/ladder/Ladder2.java +++ b/src/main/java/nextstep/ladder/Ladder2.java @@ -2,16 +2,23 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class Ladder2 { + private static final String LADDER_DELIMITER = "\n"; + private List lines; public Ladder2(int countOfPerson, int height) { createLines(countOfPerson, height); } + public Ladder2(List lines) { + this.lines = lines; + } + private void createLines(int countOfPerson, int height) { - lines = new ArrayList(); + lines = new ArrayList<>(); for(int i = 0; i < height; i++) { lines.add(new Line2(countOfPerson)); @@ -26,4 +33,10 @@ public int move(int position) { return index; } + public String getLadderForPrint(String trueSymbol, String falseSymbol) { + return lines.stream() + .map(line -> line.getLineForPrint(trueSymbol, falseSymbol)) + .collect(Collectors.joining(LADDER_DELIMITER)); + } + } diff --git a/src/main/java/nextstep/ladder/LadderMain2.java b/src/main/java/nextstep/ladder/LadderMain2.java new file mode 100644 index 0000000000..a0ff57d38a --- /dev/null +++ b/src/main/java/nextstep/ladder/LadderMain2.java @@ -0,0 +1,20 @@ +package nextstep.ladder; + +import java.util.List; + +public class LadderMain2 { + public static void main(String[] args) { + Usernames usernames = new Usernames(InputView.inputUserNames()); + List results = InputView.inputResults(); + if (usernames.size() != results.size()) { + throw new IllegalArgumentException("참여할 사람 수와 입력한 실행 결과의 수가 다릅니다."); + } + Ladder2 ladder = new Ladder2(InputView.inputHeight(), usernames.size()); + ResultView.printUsernames(usernames); + ResultView.printLadder2(ladder); + ResultView.printResults(results); + + String resultUserName = InputView.inputResultUserName(); +// ResultView.printResult(ladder.getResult(resultUserName)); + } +} diff --git a/src/main/java/nextstep/ladder/Line2.java b/src/main/java/nextstep/ladder/Line2.java index e428c101d9..5119a46db2 100644 --- a/src/main/java/nextstep/ladder/Line2.java +++ b/src/main/java/nextstep/ladder/Line2.java @@ -3,15 +3,20 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import java.util.stream.Collectors; public class Line2 { private static final Random RANDOM = new Random(); + private static final String LINE_DELIMITER = "|"; private List crosses; public Line2(int countOfPerson) { createCrosses(countOfPerson); } + public Line2(List crosses) { + this.crosses = crosses; + } private void createCrosses(int countOfPerson) { crosses = new ArrayList(); @@ -26,4 +31,10 @@ public int move(int position) { return this.crosses.get(position).move(); } + public String getLineForPrint(String trueSymbol, String falseSymbol) { + return LINE_DELIMITER + + crosses.stream() + .map(cross -> cross.getCrossForPrint(trueSymbol, falseSymbol)) + .collect(Collectors.joining(LINE_DELIMITER)); + } } diff --git a/src/main/java/nextstep/ladder/ResultView.java b/src/main/java/nextstep/ladder/ResultView.java index 96702cf294..1cdd1697e0 100644 --- a/src/main/java/nextstep/ladder/ResultView.java +++ b/src/main/java/nextstep/ladder/ResultView.java @@ -1,5 +1,8 @@ package nextstep.ladder; +import java.util.List; +import java.util.stream.Collectors; + public class ResultView { private static final String LINE_SYMBOL_TRUE = "-----"; private static final String LINE_SYMBOL_FALSE = " "; @@ -8,6 +11,22 @@ public static void printLadder(Ladder ladder) { System.out.println(ladder.getLadderForPrint(LINE_SYMBOL_TRUE, LINE_SYMBOL_FALSE)); } + public static void printLadder2(Ladder2 ladder) { + System.out.println(ladder.getLadderForPrint(LINE_SYMBOL_TRUE, LINE_SYMBOL_FALSE)); + } + + public static void printUsernames(Usernames usernames) { + System.out.println(usernames.getUsernamesForPrint()); + } + + public static void printResults(List results) { + String resultStr = results.stream() + .map(d -> String.format("%-6s", d)) + .collect(Collectors.joining()); + + System.out.println(resultStr); + } + public static void printResult(String result) { System.out.println("실행 결과"); System.out.println(result); diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index 1543bb5528..a1920743a9 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -37,4 +37,10 @@ public String toString() { .collect(Collectors.toList()) ); } + + public String getUsernamesForPrint() { + return usernames.stream() + .map(d -> String.format("%-6s", d.getUsername())) + .collect(Collectors.joining()); + } } diff --git a/src/test/java/nextstep/ladder/Ladder2Test.java b/src/test/java/nextstep/ladder/Ladder2Test.java new file mode 100644 index 0000000000..fb7bd763de --- /dev/null +++ b/src/test/java/nextstep/ladder/Ladder2Test.java @@ -0,0 +1,45 @@ +package nextstep.ladder; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +public class Ladder2Test { + + private static Ladder2 ladder; + + @BeforeAll + static void setup() { + ladder = new Ladder2(Arrays.asList( + new Line2( + Arrays.asList(new Cross(0, new Point(false, false)), new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false))) + ), new Line2( + Arrays.asList(new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false)), new Cross(0, new Point(false, false))) + ) + )); + } + + @Test + void move() { + assertThat(ladder.move(0)).isEqualTo(1); + } + + @Test + @DisplayName("모든 사용자의 사다리 출력") + void printResult() { + System.out.println(ladder.getLadderForPrint("-----", " ")); + } + +// @Test +// @DisplayName("모든 사용자의 사다리 결과") +// void getAllResult() { +// assertThat(ladder.getResult("all")) +// .isEqualTo("test2 : 결과3\n" + +// "test3 : 결과1\n" + +// "test : 결과2"); +// } +} diff --git a/src/test/java/nextstep/ladder/Line2Test.java b/src/test/java/nextstep/ladder/Line2Test.java new file mode 100644 index 0000000000..475c17eacd --- /dev/null +++ b/src/test/java/nextstep/ladder/Line2Test.java @@ -0,0 +1,25 @@ +package nextstep.ladder; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +public class Line2Test { + private static Line2 line; + + @BeforeAll + static void setUpBeforeClass() { + line = new Line2( + Arrays.asList(new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false)), new Cross(0, new Point(false, false))) + ); + } + + @Test + void move() { + assertThat(line.move(0)).isEqualTo(1); + } + +} From 3e6e7fef2c8ec0598154e8ce52c7d0a4de08c3a6 Mon Sep 17 00:00:00 2001 From: mslee Date: Tue, 10 Dec 2024 08:28:43 +0900 Subject: [PATCH 08/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EA=B2=B0=EA=B3=BC=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Ladder2.java | 13 +++++- src/main/java/nextstep/ladder/Line2.java | 7 ++++ .../java/nextstep/ladder/Ladder2Test.java | 40 ++++++++++++------- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/nextstep/ladder/Ladder2.java b/src/main/java/nextstep/ladder/Ladder2.java index 20b5159d26..1a1795af14 100644 --- a/src/main/java/nextstep/ladder/Ladder2.java +++ b/src/main/java/nextstep/ladder/Ladder2.java @@ -2,7 +2,9 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.IntStream; public class Ladder2 { private static final String LADDER_DELIMITER = "\n"; @@ -20,14 +22,14 @@ public Ladder2(List lines) { private void createLines(int countOfPerson, int height) { lines = new ArrayList<>(); - for(int i = 0; i < height; i++) { + for (int i = 0; i < height; i++) { lines.add(new Line2(countOfPerson)); } } public int move(int position) { int index = position; - for(Line2 line : lines) { + for (Line2 line : lines) { index = line.move(index); } return index; @@ -39,4 +41,11 @@ public String getLadderForPrint(String trueSymbol, String falseSymbol) { .collect(Collectors.joining(LADDER_DELIMITER)); } + //todo lines.get(0).getCrossSize() + public Map getAllResult() { + return IntStream.range(0, lines.get(0).getCrossSize()) + .boxed() + .collect(Collectors.toMap(i -> i, i -> move(i))); + } + } diff --git a/src/main/java/nextstep/ladder/Line2.java b/src/main/java/nextstep/ladder/Line2.java index 5119a46db2..dd118b8fe4 100644 --- a/src/main/java/nextstep/ladder/Line2.java +++ b/src/main/java/nextstep/ladder/Line2.java @@ -14,6 +14,7 @@ public class Line2 { public Line2(int countOfPerson) { createCrosses(countOfPerson); } + public Line2(List crosses) { this.crosses = crosses; } @@ -27,6 +28,7 @@ private void createCrosses(int countOfPerson) { } } + //todo public int move(int position) { return this.crosses.get(position).move(); } @@ -37,4 +39,9 @@ public String getLineForPrint(String trueSymbol, String falseSymbol) { .map(cross -> cross.getCrossForPrint(trueSymbol, falseSymbol)) .collect(Collectors.joining(LINE_DELIMITER)); } + + //todo + public int getCrossSize() { + return crosses.size(); + } } diff --git a/src/test/java/nextstep/ladder/Ladder2Test.java b/src/test/java/nextstep/ladder/Ladder2Test.java index fb7bd763de..586ecbd0a9 100644 --- a/src/test/java/nextstep/ladder/Ladder2Test.java +++ b/src/test/java/nextstep/ladder/Ladder2Test.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -15,17 +17,17 @@ public class Ladder2Test { @BeforeAll static void setup() { ladder = new Ladder2(Arrays.asList( - new Line2( - Arrays.asList(new Cross(0, new Point(false, false)), new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false))) - ), new Line2( - Arrays.asList(new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false)), new Cross(0, new Point(false, false))) - ) + new Line2( + Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) + ), new Line2( + Arrays.asList(new Cross(0, new Point(false, true)), new Cross(1, new Point(true, false)), new Cross(2, new Point(false, false))) + ) )); } @Test void move() { - assertThat(ladder.move(0)).isEqualTo(1); + assertThat(ladder.move(1)).isEqualTo(2); } @Test @@ -34,12 +36,22 @@ void printResult() { System.out.println(ladder.getLadderForPrint("-----", " ")); } -// @Test -// @DisplayName("모든 사용자의 사다리 결과") -// void getAllResult() { -// assertThat(ladder.getResult("all")) -// .isEqualTo("test2 : 결과3\n" + -// "test3 : 결과1\n" + -// "test : 결과2"); -// } + @Test + @DisplayName("특정 사용자의 사다리 결과") + void getResultByUserIndex() { + assertThat(ladder.move(0)) + .isEqualTo(1); + } + + @Test + @DisplayName("모든 사용자의 사다리 결과") + void getAllResult() { + Map result = new HashMap<>(); + result.put(0, 1); + result.put(1, 2); + result.put(2, 0); + + assertThat(ladder.getAllResult()) + .isEqualTo(result); + } } From 9b75744b8059512d51b3ef7c14e308bc3a4ba0e2 Mon Sep 17 00:00:00 2001 From: mslee Date: Tue, 10 Dec 2024 17:25:14 +0900 Subject: [PATCH 09/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EC=A4=91=EA=B0=84=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- 결과값 오류 수정 필요함 Ladder2 -- getAllResult lines.get(0).getCrossSize() Usernames -- getUsername --- README.md | 6 +++++- .../java/nextstep/ladder/LadderMain2.java | 3 ++- src/main/java/nextstep/ladder/ResultView.java | 21 +++++++++++++++++++ src/main/java/nextstep/ladder/Usernames.java | 7 +++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9289ad1f7f..aafc5f3712 100644 --- a/README.md +++ b/README.md @@ -99,4 +99,8 @@ TDD로 구현 - "| |------| |" - "|-----| |-----|" -- [ ] 결과 조회 \ No newline at end of file +- [ ] 결과 조회 + + +Usernames +- [ ] 이름 중복 체크 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/LadderMain2.java b/src/main/java/nextstep/ladder/LadderMain2.java index a0ff57d38a..0c11c44dc4 100644 --- a/src/main/java/nextstep/ladder/LadderMain2.java +++ b/src/main/java/nextstep/ladder/LadderMain2.java @@ -9,12 +9,13 @@ public static void main(String[] args) { if (usernames.size() != results.size()) { throw new IllegalArgumentException("참여할 사람 수와 입력한 실행 결과의 수가 다릅니다."); } - Ladder2 ladder = new Ladder2(InputView.inputHeight(), usernames.size()); + Ladder2 ladder = new Ladder2(usernames.size(), InputView.inputHeight()); ResultView.printUsernames(usernames); ResultView.printLadder2(ladder); ResultView.printResults(results); String resultUserName = InputView.inputResultUserName(); // ResultView.printResult(ladder.getResult(resultUserName)); + ResultView.printResult2(resultUserName, usernames, ladder, results); } } diff --git a/src/main/java/nextstep/ladder/ResultView.java b/src/main/java/nextstep/ladder/ResultView.java index 1cdd1697e0..4f5ab900c6 100644 --- a/src/main/java/nextstep/ladder/ResultView.java +++ b/src/main/java/nextstep/ladder/ResultView.java @@ -1,7 +1,9 @@ package nextstep.ladder; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.IntStream; public class ResultView { private static final String LINE_SYMBOL_TRUE = "-----"; @@ -32,4 +34,23 @@ public static void printResult(String result) { System.out.println(result); } + public static void printResult2(String resultUserName, Usernames usernames, Ladder2 ladder2, List results) { + if ("all".equals(resultUserName.toLowerCase())) { + String allResultStr = allResult(usernames, ladder2, results).entrySet() + .stream() + .map(result -> result.getKey() + " : " + result.getValue()) + .collect(Collectors.joining("\n")); + System.out.println(allResultStr); + } + } + + private static Map allResult(Usernames usernames, Ladder2 ladder2, List results) { + Map resultMap = ladder2.getAllResult(); +// return IntStream.range(0, usernames.size()).boxed() +// .collect(Collectors.toMap(i -> usernames.getValues().get(i).getUsername(), i -> results.get(resultMap.get(i)))); + + return IntStream.range(0, usernames.size()).boxed() + .collect(Collectors.toMap(usernames::getUsername, i -> results.get(resultMap.get(i)))); + } + } diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index a1920743a9..54f154d274 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -8,6 +8,7 @@ public class Usernames { private List usernames = new ArrayList<>(); public Usernames(List usernameStrs) { + //todo 이름 중복 if (usernameStrs.size() == 0) { throw new IllegalArgumentException("참여자가 없습니다."); } @@ -20,6 +21,12 @@ public List getValues() { return usernames; } + + //todo 이렇게 작성해도 되는지?? + public String getUsername(int index) { + return usernames.get(index).getUsername(); + } + public int size() { return usernames.size(); } From 7a616f8ce31670bfb60ff48340f589808295e6de Mon Sep 17 00:00:00 2001 From: ming Date: Wed, 11 Dec 2024 08:08:20 +0900 Subject: [PATCH 10/14] =?UTF-8?q?feat:=204=EB=8B=A8=EA=B3=84=20-=20Usernam?= =?UTF-8?q?es=20=EC=9D=B4=EB=A6=84=20=EC=A4=91=EB=B3=B5=20=EC=B2=B4?= =?UTF-8?q?=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/nextstep/ladder/Usernames.java | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aafc5f3712..d8b419e77c 100644 --- a/README.md +++ b/README.md @@ -103,4 +103,4 @@ TDD로 구현 Usernames -- [ ] 이름 중복 체크 \ No newline at end of file +- [x] 이름 중복 체크 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index 54f154d274..7cad9975d4 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -1,17 +1,24 @@ package nextstep.ladder; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; public class Usernames { private List usernames = new ArrayList<>(); public Usernames(List usernameStrs) { - //todo 이름 중복 if (usernameStrs.size() == 0) { throw new IllegalArgumentException("참여자가 없습니다."); } + + Set uniqueUsernames = new HashSet<>(usernameStrs); + if (uniqueUsernames.size() < usernameStrs.size()) { + throw new IllegalArgumentException("중복된 이름이 존재합니다."); + } + this.usernames = usernameStrs.stream() .map((str) -> new Username(str)) .collect(Collectors.toList()); From 63df125124d1c4d10e21e4e361cf6889b16a63dc Mon Sep 17 00:00:00 2001 From: ming Date: Wed, 11 Dec 2024 08:59:40 +0900 Subject: [PATCH 11/14] =?UTF-8?q?test:=204=EB=8B=A8=EA=B3=84=20-=20ladder,?= =?UTF-8?q?=20point=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/nextstep/ladder/Ladder2Test.java | 10 ++++++---- src/test/java/nextstep/ladder/PointTest.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/test/java/nextstep/ladder/Ladder2Test.java b/src/test/java/nextstep/ladder/Ladder2Test.java index 586ecbd0a9..18ce9ddea0 100644 --- a/src/test/java/nextstep/ladder/Ladder2Test.java +++ b/src/test/java/nextstep/ladder/Ladder2Test.java @@ -21,13 +21,15 @@ static void setup() { Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) ), new Line2( Arrays.asList(new Cross(0, new Point(false, true)), new Cross(1, new Point(true, false)), new Cross(2, new Point(false, false))) + ), new Line2( + Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) ) )); } @Test void move() { - assertThat(ladder.move(1)).isEqualTo(2); + assertThat(ladder.move(1)).isEqualTo(1); } @Test @@ -40,15 +42,15 @@ void printResult() { @DisplayName("특정 사용자의 사다리 결과") void getResultByUserIndex() { assertThat(ladder.move(0)) - .isEqualTo(1); + .isEqualTo(2); } @Test @DisplayName("모든 사용자의 사다리 결과") void getAllResult() { Map result = new HashMap<>(); - result.put(0, 1); - result.put(1, 2); + result.put(0, 2); + result.put(1, 1); result.put(2, 0); assertThat(ladder.getAllResult()) diff --git a/src/test/java/nextstep/ladder/PointTest.java b/src/test/java/nextstep/ladder/PointTest.java index 59ad8fc340..fde16d19f1 100644 --- a/src/test/java/nextstep/ladder/PointTest.java +++ b/src/test/java/nextstep/ladder/PointTest.java @@ -3,12 +3,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Random; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; public class PointTest { private Point point; + private static final Random RANDOM = new Random(); + @BeforeEach void setUp() { point = new Point(false, true); @@ -45,4 +49,14 @@ void setUp() { }); } + @Test + void test() { + Point point = Point.first(RANDOM.nextBoolean()); + System.out.println(point); + Point point2 = point.next(RANDOM.nextBoolean()); + System.out.println(point2); + Point point3 = point2.last(); + System.out.println(point3); + } + } From b5ce392ad714bd3666df7bcddccb973ecdd0420b Mon Sep 17 00:00:00 2001 From: ming Date: Wed, 11 Dec 2024 09:00:09 +0900 Subject: [PATCH 12/14] =?UTF-8?q?fix:=204=EB=8B=A8=EA=B3=84=20-=20all=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=EA=B0=92=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=A4=91=20=EC=A4=91=EA=B0=84=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/ResultView.java | 20 +++++++++++++++---- src/main/java/nextstep/ladder/Usernames.java | 8 ++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/nextstep/ladder/ResultView.java b/src/main/java/nextstep/ladder/ResultView.java index 4f5ab900c6..ef83676e7a 100644 --- a/src/main/java/nextstep/ladder/ResultView.java +++ b/src/main/java/nextstep/ladder/ResultView.java @@ -38,19 +38,31 @@ public static void printResult2(String resultUserName, Usernames usernames, Ladd if ("all".equals(resultUserName.toLowerCase())) { String allResultStr = allResult(usernames, ladder2, results).entrySet() .stream() - .map(result -> result.getKey() + " : " + result.getValue()) + .map(result -> { + System.out.println(result); + return result.getKey() + " : " + result.getValue(); + }) .collect(Collectors.joining("\n")); System.out.println(allResultStr); } +// else { +// Map resultMap = allResult(usernames, ladder2, results); +// String result = resultMap.get(resultUserName.toLowerCase()); +// System.out.println(result); +// } } private static Map allResult(Usernames usernames, Ladder2 ladder2, List results) { Map resultMap = ladder2.getAllResult(); -// return IntStream.range(0, usernames.size()).boxed() -// .collect(Collectors.toMap(i -> usernames.getValues().get(i).getUsername(), i -> results.get(resultMap.get(i)))); return IntStream.range(0, usernames.size()).boxed() - .collect(Collectors.toMap(usernames::getUsername, i -> results.get(resultMap.get(i)))); + .collect(Collectors.toMap( + i -> usernames.getValues().get(i).getUsername(), + i -> results.get(resultMap.get(i)) + )); + +// return IntStream.range(0, usernames.size()).boxed() +// .collect(Collectors.toMap(usernames::getUsername, i -> results.get(resultMap.get(i)))); } } diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index 7cad9975d4..a192ec7658 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -29,10 +29,10 @@ public List getValues() { } - //todo 이렇게 작성해도 되는지?? - public String getUsername(int index) { - return usernames.get(index).getUsername(); - } +// //todo 이렇게 작성해도 되는지?? +// public String getUsername(int index) { +// return usernames.get(index).getUsername(); +// } public int size() { return usernames.size(); From 68817affebc0b7f5b054436d092ce7f9642a3ade Mon Sep 17 00:00:00 2001 From: ming Date: Sun, 15 Dec 2024 05:09:20 +0900 Subject: [PATCH 13/14] =?UTF-8?q?fix:=204=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=EA=B0=92=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Cross.java | 1 + .../java/nextstep/ladder/LadderMain2.java | 1 - src/main/java/nextstep/ladder/Line2.java | 8 ++++++- src/main/java/nextstep/ladder/Point.java | 10 ++++---- src/main/java/nextstep/ladder/ResultView.java | 23 +++++-------------- src/main/java/nextstep/ladder/Usernames.java | 8 +++---- 6 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/java/nextstep/ladder/Cross.java b/src/main/java/nextstep/ladder/Cross.java index 2b5a46ab34..e9daa78472 100644 --- a/src/main/java/nextstep/ladder/Cross.java +++ b/src/main/java/nextstep/ladder/Cross.java @@ -26,4 +26,5 @@ public String getCrossForPrint(String trueSymbol, String falseSymbol) { return falseSymbol; } + } diff --git a/src/main/java/nextstep/ladder/LadderMain2.java b/src/main/java/nextstep/ladder/LadderMain2.java index 0c11c44dc4..79f06aafd0 100644 --- a/src/main/java/nextstep/ladder/LadderMain2.java +++ b/src/main/java/nextstep/ladder/LadderMain2.java @@ -15,7 +15,6 @@ public static void main(String[] args) { ResultView.printResults(results); String resultUserName = InputView.inputResultUserName(); -// ResultView.printResult(ladder.getResult(resultUserName)); ResultView.printResult2(resultUserName, usernames, ladder, results); } } diff --git a/src/main/java/nextstep/ladder/Line2.java b/src/main/java/nextstep/ladder/Line2.java index dd118b8fe4..a0736afdd4 100644 --- a/src/main/java/nextstep/ladder/Line2.java +++ b/src/main/java/nextstep/ladder/Line2.java @@ -24,7 +24,13 @@ private void createCrosses(int countOfPerson) { Point point = Point.first(RANDOM.nextBoolean()); for (int i = 0; i < countOfPerson; i++) { - crosses.add(new Cross(i, i == 0 ? point : i == countOfPerson - 1 ? point.last() : point.next(RANDOM.nextBoolean()))); + if (i == countOfPerson - 1) { + point = point.last(); + } + if (i > 0 && i < countOfPerson - 1) { + point = point.next(RANDOM.nextBoolean()); + } + crosses.add(new Cross(i, point)); } } diff --git a/src/main/java/nextstep/ladder/Point.java b/src/main/java/nextstep/ladder/Point.java index 8ba37482ee..1ad0f7aeca 100644 --- a/src/main/java/nextstep/ladder/Point.java +++ b/src/main/java/nextstep/ladder/Point.java @@ -5,7 +5,7 @@ public class Point { private boolean right; public Point(boolean current, boolean right) { - if(current && right) { + if (current && right) { throw new IllegalArgumentException("유효하지 않은 값입니다."); } this.current = current; @@ -17,7 +17,7 @@ public boolean getRight() { } public String getLineSymbol(String trueSymbol, String falseSymbol) { - return right ? trueSymbol : falseSymbol; + return current ? trueSymbol : falseSymbol; } public int getResultIndex(int index) { @@ -27,7 +27,7 @@ public int getResultIndex(int index) { @Override public String toString() { return "Point{" + - "left=" + current + + "current=" + current + ", right=" + right + '}'; } @@ -37,8 +37,8 @@ public static Point first(boolean right) { } public Point next(boolean right) { - if(this.right) { - return new Point(this.right, false); + if (this.right) { + return new Point(this.right, false); } return new Point(this.right, right); } diff --git a/src/main/java/nextstep/ladder/ResultView.java b/src/main/java/nextstep/ladder/ResultView.java index ef83676e7a..8ba79df682 100644 --- a/src/main/java/nextstep/ladder/ResultView.java +++ b/src/main/java/nextstep/ladder/ResultView.java @@ -35,34 +35,23 @@ public static void printResult(String result) { } public static void printResult2(String resultUserName, Usernames usernames, Ladder2 ladder2, List results) { + Map resultMap = allResult(usernames, ladder2, results); if ("all".equals(resultUserName.toLowerCase())) { - String allResultStr = allResult(usernames, ladder2, results).entrySet() + String allResultStr = resultMap.entrySet() .stream() - .map(result -> { - System.out.println(result); - return result.getKey() + " : " + result.getValue(); - }) + .map(entry -> entry.getKey() + " : " + entry.getValue()) .collect(Collectors.joining("\n")); System.out.println(allResultStr); + return; } -// else { -// Map resultMap = allResult(usernames, ladder2, results); -// String result = resultMap.get(resultUserName.toLowerCase()); -// System.out.println(result); -// } + System.out.println(resultMap.get(resultUserName)); } private static Map allResult(Usernames usernames, Ladder2 ladder2, List results) { Map resultMap = ladder2.getAllResult(); return IntStream.range(0, usernames.size()).boxed() - .collect(Collectors.toMap( - i -> usernames.getValues().get(i).getUsername(), - i -> results.get(resultMap.get(i)) - )); - -// return IntStream.range(0, usernames.size()).boxed() -// .collect(Collectors.toMap(usernames::getUsername, i -> results.get(resultMap.get(i)))); + .collect(Collectors.toMap(usernames::getUsername, i -> results.get(resultMap.get(i)))); } } diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index a192ec7658..77bb96ec14 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -29,10 +29,10 @@ public List getValues() { } -// //todo 이렇게 작성해도 되는지?? -// public String getUsername(int index) { -// return usernames.get(index).getUsername(); -// } + // //todo 이렇게 작성해도 되는지?? + public String getUsername(int index) { + return usernames.get(index).getUsername(); + } public int size() { return usernames.size(); From 61947bee45c7c036277a9bc8c0c2a7f92610bd49 Mon Sep 17 00:00:00 2001 From: ming Date: Sun, 15 Dec 2024 05:43:44 +0900 Subject: [PATCH 14/14] =?UTF-8?q?refactor:=204=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/nextstep/ladder/Ladder.java | 67 ++++++++----------- src/main/java/nextstep/ladder/Ladder2.java | 51 -------------- src/main/java/nextstep/ladder/LadderMain.java | 6 +- .../java/nextstep/ladder/LadderMain2.java | 20 ------ src/main/java/nextstep/ladder/Line.java | 54 ++++++++++++--- src/main/java/nextstep/ladder/Line2.java | 53 --------------- src/main/java/nextstep/ladder/Lines.java | 47 ------------- src/main/java/nextstep/ladder/Point.java | 20 ------ src/main/java/nextstep/ladder/Points.java | 53 --------------- src/main/java/nextstep/ladder/ResultView.java | 16 ++--- src/main/java/nextstep/ladder/Usernames.java | 21 +----- .../java/nextstep/ladder/Ladder2Test.java | 59 ---------------- src/test/java/nextstep/ladder/LadderTest.java | 51 +++++++++----- .../ladder/{Line2Test.java => LineTest.java} | 6 +- src/test/java/nextstep/ladder/LinesTest.java | 22 ------ src/test/java/nextstep/ladder/PointTest.java | 6 -- src/test/java/nextstep/ladder/PointsTest.java | 25 ------- 18 files changed, 119 insertions(+), 460 deletions(-) delete mode 100644 src/main/java/nextstep/ladder/Ladder2.java delete mode 100644 src/main/java/nextstep/ladder/LadderMain2.java delete mode 100644 src/main/java/nextstep/ladder/Line2.java delete mode 100644 src/main/java/nextstep/ladder/Lines.java delete mode 100644 src/main/java/nextstep/ladder/Points.java delete mode 100644 src/test/java/nextstep/ladder/Ladder2Test.java rename src/test/java/nextstep/ladder/{Line2Test.java => LineTest.java} (85%) delete mode 100644 src/test/java/nextstep/ladder/LinesTest.java delete mode 100644 src/test/java/nextstep/ladder/PointsTest.java diff --git a/README.md b/README.md index d8b419e77c..23da9acdc9 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ TDD로 구현 - "| |------| |" - "|-----| |-----|" -- [ ] 결과 조회 +- [x] 결과 조회 Usernames diff --git a/src/main/java/nextstep/ladder/Ladder.java b/src/main/java/nextstep/ladder/Ladder.java index ba41ef5564..096a7b2877 100644 --- a/src/main/java/nextstep/ladder/Ladder.java +++ b/src/main/java/nextstep/ladder/Ladder.java @@ -1,64 +1,53 @@ package nextstep.ladder; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Ladder { - private Usernames usernames; - private Lines lines; - private List results; + private static final String LADDER_DELIMITER = "\n"; - public Ladder(Usernames usernames, Lines lines, List results) { - this.usernames = usernames; - this.lines = lines; - this.results = results; - } + private List lines; - public String getResultsForPrint() { - return results.stream() - .map(d -> String.format("%-6s", d)) - .collect(Collectors.joining()); + public Ladder(int countOfPerson, int height) { + this.lines = createLines(countOfPerson, height); } - public String getLadderForPrint(String trueSymbol, String falseSymbol) { - return String.join("\n", - usernames.toString(), - lines.toString(trueSymbol, falseSymbol), - getResultsForPrint() - ); + public Ladder(List lines) { + this.lines = new ArrayList<>(lines); } - public String getResult(String resultUsername) { - if ("all".equals(resultUsername)) { - return getAllResult(); + private List createLines(int countOfPerson, int height) { + List nLines = new ArrayList<>(); + + for (int i = 0; i < height; i++) { + nLines.add(new Line(countOfPerson)); } - return getResultByUsername(resultUsername); + return nLines; } - private Map getResultMap() { - Map result = IntStream.range(0, usernames.size()) - .boxed() - .collect(Collectors.toMap( - i -> usernames.getValues().get(i).getUsername(), - i -> results.get(lines.getPersonFinalResultIndex(i)) - )); - - return result; + public int move(int position) { + int index = position; + for (Line line : lines) { + index = line.move(index); + } + return index; } - private String getAllResult() { - Map resultMap = getResultMap(); - - return resultMap.entrySet() - .stream() - .map(d -> d.getKey() + " : " + d.getValue()).collect(Collectors.joining("\n")); + public String getLadderForPrint(String trueSymbol, String falseSymbol) { + return lines.stream() + .map(line -> line.getLineForPrint(trueSymbol, falseSymbol)) + .collect(Collectors.joining(LADDER_DELIMITER)); } - private String getResultByUsername(String resultUsername) { - return getResultMap().get(resultUsername); + //todo lines.get(0).getCrossSize() + public Map getAllResult() { + return IntStream.range(0, lines.get(0).getCrossSize()) + .boxed() + .collect(Collectors.toMap(i -> i, i -> move(i))); } } diff --git a/src/main/java/nextstep/ladder/Ladder2.java b/src/main/java/nextstep/ladder/Ladder2.java deleted file mode 100644 index 1a1795af14..0000000000 --- a/src/main/java/nextstep/ladder/Ladder2.java +++ /dev/null @@ -1,51 +0,0 @@ -package nextstep.ladder; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -public class Ladder2 { - private static final String LADDER_DELIMITER = "\n"; - - private List lines; - - public Ladder2(int countOfPerson, int height) { - createLines(countOfPerson, height); - } - - public Ladder2(List lines) { - this.lines = lines; - } - - private void createLines(int countOfPerson, int height) { - lines = new ArrayList<>(); - - for (int i = 0; i < height; i++) { - lines.add(new Line2(countOfPerson)); - } - } - - public int move(int position) { - int index = position; - for (Line2 line : lines) { - index = line.move(index); - } - return index; - } - - public String getLadderForPrint(String trueSymbol, String falseSymbol) { - return lines.stream() - .map(line -> line.getLineForPrint(trueSymbol, falseSymbol)) - .collect(Collectors.joining(LADDER_DELIMITER)); - } - - //todo lines.get(0).getCrossSize() - public Map getAllResult() { - return IntStream.range(0, lines.get(0).getCrossSize()) - .boxed() - .collect(Collectors.toMap(i -> i, i -> move(i))); - } - -} diff --git a/src/main/java/nextstep/ladder/LadderMain.java b/src/main/java/nextstep/ladder/LadderMain.java index 996be917b7..90ad6fbc00 100644 --- a/src/main/java/nextstep/ladder/LadderMain.java +++ b/src/main/java/nextstep/ladder/LadderMain.java @@ -9,10 +9,12 @@ public static void main(String[] args) { if (usernames.size() != results.size()) { throw new IllegalArgumentException("참여할 사람 수와 입력한 실행 결과의 수가 다릅니다."); } - Ladder ladder = new Ladder(usernames, new Lines(InputView.inputHeight(), usernames.size()), results); + Ladder ladder = new Ladder(usernames.size(), InputView.inputHeight()); + ResultView.printUsernames(usernames); ResultView.printLadder(ladder); + ResultView.printResults(results); String resultUserName = InputView.inputResultUserName(); - ResultView.printResult(ladder.getResult(resultUserName)); + ResultView.printResult(resultUserName, usernames, ladder, results); } } diff --git a/src/main/java/nextstep/ladder/LadderMain2.java b/src/main/java/nextstep/ladder/LadderMain2.java deleted file mode 100644 index 79f06aafd0..0000000000 --- a/src/main/java/nextstep/ladder/LadderMain2.java +++ /dev/null @@ -1,20 +0,0 @@ -package nextstep.ladder; - -import java.util.List; - -public class LadderMain2 { - public static void main(String[] args) { - Usernames usernames = new Usernames(InputView.inputUserNames()); - List results = InputView.inputResults(); - if (usernames.size() != results.size()) { - throw new IllegalArgumentException("참여할 사람 수와 입력한 실행 결과의 수가 다릅니다."); - } - Ladder2 ladder = new Ladder2(usernames.size(), InputView.inputHeight()); - ResultView.printUsernames(usernames); - ResultView.printLadder2(ladder); - ResultView.printResults(results); - - String resultUserName = InputView.inputResultUserName(); - ResultView.printResult2(resultUserName, usernames, ladder, results); - } -} diff --git a/src/main/java/nextstep/ladder/Line.java b/src/main/java/nextstep/ladder/Line.java index 73a602d1f4..017be82867 100644 --- a/src/main/java/nextstep/ladder/Line.java +++ b/src/main/java/nextstep/ladder/Line.java @@ -1,27 +1,59 @@ package nextstep.ladder; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; public class Line { - private Points points; + private static final Random RANDOM = new Random(); + private static final String LINE_DELIMITER = "|"; + + private List crosses; public Line(int countOfPerson) { - points = new Points(countOfPerson); + createCrosses(countOfPerson); + } + + public Line(List crosses) { + this.crosses = crosses; } - public Line(Points points) { - this.points = points; + private void createCrosses(int countOfPerson) { + crosses = new ArrayList<>(); + + Point point = Point.first(RANDOM.nextBoolean()); + for (int i = 0; i < countOfPerson; i++) { + point = updatePoint(i, countOfPerson, point); + crosses.add(new Cross(i, point)); + } } - public String toString(String trueSymbol, String falseSymbol) { - return points.toString(trueSymbol, falseSymbol); + private Point updatePoint(int index, int countOfPerson, Point currentPoint) { + if (index == countOfPerson - 1) { + return currentPoint.last(); + } + if (index > 0) { + return currentPoint.next(RANDOM.nextBoolean()); + } + return currentPoint; } - public Integer getResultIndex(int index) { - return points.getResultIndex(index); + //todo + public int move(int position) { + return this.crosses.get(position).move(); } - @Override - public String toString() { - return points.toString(); + //todo + public int getCrossSize() { + return crosses.size(); } + + public String getLineForPrint(String trueSymbol, String falseSymbol) { + return LINE_DELIMITER + + crosses.stream() + .map(cross -> cross.getCrossForPrint(trueSymbol, falseSymbol)) + .collect(Collectors.joining(LINE_DELIMITER)); + } + } diff --git a/src/main/java/nextstep/ladder/Line2.java b/src/main/java/nextstep/ladder/Line2.java deleted file mode 100644 index a0736afdd4..0000000000 --- a/src/main/java/nextstep/ladder/Line2.java +++ /dev/null @@ -1,53 +0,0 @@ -package nextstep.ladder; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.stream.Collectors; - -public class Line2 { - private static final Random RANDOM = new Random(); - private static final String LINE_DELIMITER = "|"; - - private List crosses; - - public Line2(int countOfPerson) { - createCrosses(countOfPerson); - } - - public Line2(List crosses) { - this.crosses = crosses; - } - - private void createCrosses(int countOfPerson) { - crosses = new ArrayList(); - - Point point = Point.first(RANDOM.nextBoolean()); - for (int i = 0; i < countOfPerson; i++) { - if (i == countOfPerson - 1) { - point = point.last(); - } - if (i > 0 && i < countOfPerson - 1) { - point = point.next(RANDOM.nextBoolean()); - } - crosses.add(new Cross(i, point)); - } - } - - //todo - public int move(int position) { - return this.crosses.get(position).move(); - } - - public String getLineForPrint(String trueSymbol, String falseSymbol) { - return LINE_DELIMITER - + crosses.stream() - .map(cross -> cross.getCrossForPrint(trueSymbol, falseSymbol)) - .collect(Collectors.joining(LINE_DELIMITER)); - } - - //todo - public int getCrossSize() { - return crosses.size(); - } -} diff --git a/src/main/java/nextstep/ladder/Lines.java b/src/main/java/nextstep/ladder/Lines.java deleted file mode 100644 index 0da6aad371..0000000000 --- a/src/main/java/nextstep/ladder/Lines.java +++ /dev/null @@ -1,47 +0,0 @@ -package nextstep.ladder; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class Lines { - private final static String LINES_DELIMITER = "\n"; - private List lines = new ArrayList<>(); - - public Lines(int height, int countOfPerson) { - if (height <= 0) { - throw new IllegalArgumentException("사다리 높이는 0 이상 입력해주세요."); - } - generate(height, countOfPerson); - } - - public Lines(List lines) { - this.lines = lines; - } - - private void generate(int height, int countOfPerson) { - this.lines = Stream.generate(() -> new Line(countOfPerson)) - .limit(height) - .collect(Collectors.toList()); - } - - public List getValues() { - return this.lines; - } - - public String toString(String lineTrueSymbol, String lineFalseSymbol) { - return String.join( - LINES_DELIMITER, - this.lines - .stream() - .map((line) -> line.toString(lineTrueSymbol, lineFalseSymbol)) - .collect(Collectors.toList()) - ); - } - - public int getPersonFinalResultIndex(int personIndex) { - return lines.stream() - .reduce(personIndex, (index, line) -> line.getResultIndex(index), (a, b) -> b); - } -} diff --git a/src/main/java/nextstep/ladder/Point.java b/src/main/java/nextstep/ladder/Point.java index 1ad0f7aeca..8d197a576d 100644 --- a/src/main/java/nextstep/ladder/Point.java +++ b/src/main/java/nextstep/ladder/Point.java @@ -12,26 +12,6 @@ public Point(boolean current, boolean right) { this.right = right; } - public boolean getRight() { - return right; - } - - public String getLineSymbol(String trueSymbol, String falseSymbol) { - return current ? trueSymbol : falseSymbol; - } - - public int getResultIndex(int index) { - return current ? index - 1 : (right ? index + 1 : index); - } - - @Override - public String toString() { - return "Point{" + - "current=" + current + - ", right=" + right + - '}'; - } - public static Point first(boolean right) { return new Point(false, right); } diff --git a/src/main/java/nextstep/ladder/Points.java b/src/main/java/nextstep/ladder/Points.java deleted file mode 100644 index b903b599a4..0000000000 --- a/src/main/java/nextstep/ladder/Points.java +++ /dev/null @@ -1,53 +0,0 @@ -package nextstep.ladder; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -public class Points { - private static final Random RANDOM = new Random(); - private static final String LINE_DELIMITER = "|"; - private List points = new ArrayList<>(); - - public Points(int size) { - IntStream.range(0, size).forEach((i) -> { - boolean prevRight = i == 0 ? false : points.get(i - 1).getRight(); - boolean right = i == size - 1 ? false : generateRandomRightPoint(prevRight); - - points.add(new Point(prevRight, right)); - }); - } - - public Points(List points) { - this.points = points; - } - - private boolean generateRandomRightPoint(boolean prev) { - return prev ? false : RANDOM.nextBoolean(); - } - - public int size() { - return this.points.size(); - } - - public String toString(String trueSymbol, String falseSymbol) { - List result = this.points - .stream() - .map(point -> point.getLineSymbol(trueSymbol, falseSymbol)) - .limit(this.points.size() - 1) - .collect(Collectors.toList()); - - return LINE_DELIMITER + String.join(LINE_DELIMITER, result) + LINE_DELIMITER; - } - - public Integer getResultIndex(int index) { - return points.get(index).getResultIndex(index); - } - - @Override - public String toString() { - return points.stream().map(Point::toString).collect(Collectors.joining()); - } -} diff --git a/src/main/java/nextstep/ladder/ResultView.java b/src/main/java/nextstep/ladder/ResultView.java index 8ba79df682..38e19cd008 100644 --- a/src/main/java/nextstep/ladder/ResultView.java +++ b/src/main/java/nextstep/ladder/ResultView.java @@ -8,15 +8,12 @@ public class ResultView { private static final String LINE_SYMBOL_TRUE = "-----"; private static final String LINE_SYMBOL_FALSE = " "; + private static final String LINE_SEPARATOR = "\n"; public static void printLadder(Ladder ladder) { System.out.println(ladder.getLadderForPrint(LINE_SYMBOL_TRUE, LINE_SYMBOL_FALSE)); } - public static void printLadder2(Ladder2 ladder) { - System.out.println(ladder.getLadderForPrint(LINE_SYMBOL_TRUE, LINE_SYMBOL_FALSE)); - } - public static void printUsernames(Usernames usernames) { System.out.println(usernames.getUsernamesForPrint()); } @@ -29,25 +26,20 @@ public static void printResults(List results) { System.out.println(resultStr); } - public static void printResult(String result) { - System.out.println("실행 결과"); - System.out.println(result); - } - - public static void printResult2(String resultUserName, Usernames usernames, Ladder2 ladder2, List results) { + public static void printResult(String resultUserName, Usernames usernames, Ladder ladder2, List results) { Map resultMap = allResult(usernames, ladder2, results); if ("all".equals(resultUserName.toLowerCase())) { String allResultStr = resultMap.entrySet() .stream() .map(entry -> entry.getKey() + " : " + entry.getValue()) - .collect(Collectors.joining("\n")); + .collect(Collectors.joining(LINE_SEPARATOR)); System.out.println(allResultStr); return; } System.out.println(resultMap.get(resultUserName)); } - private static Map allResult(Usernames usernames, Ladder2 ladder2, List results) { + private static Map allResult(Usernames usernames, Ladder ladder2, List results) { Map resultMap = ladder2.getAllResult(); return IntStream.range(0, usernames.size()).boxed() diff --git a/src/main/java/nextstep/ladder/Usernames.java b/src/main/java/nextstep/ladder/Usernames.java index 77bb96ec14..65c097d8f1 100644 --- a/src/main/java/nextstep/ladder/Usernames.java +++ b/src/main/java/nextstep/ladder/Usernames.java @@ -24,11 +24,6 @@ public Usernames(List usernameStrs) { .collect(Collectors.toList()); } - public List getValues() { - return usernames; - } - - // //todo 이렇게 작성해도 되는지?? public String getUsername(int index) { return usernames.get(index).getUsername(); @@ -38,23 +33,9 @@ public int size() { return usernames.size(); } - public int indexOf(String username) { - return usernames.indexOf(new Username(username)); - } - - public String toString() { - return String.join( - "", - this.usernames - .stream() - .map(str -> str.getUsernameForPrint()) - .collect(Collectors.toList()) - ); - } - public String getUsernamesForPrint() { return usernames.stream() - .map(d -> String.format("%-6s", d.getUsername())) + .map(Username::getUsernameForPrint) .collect(Collectors.joining()); } } diff --git a/src/test/java/nextstep/ladder/Ladder2Test.java b/src/test/java/nextstep/ladder/Ladder2Test.java deleted file mode 100644 index 18ce9ddea0..0000000000 --- a/src/test/java/nextstep/ladder/Ladder2Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package nextstep.ladder; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -public class Ladder2Test { - - private static Ladder2 ladder; - - @BeforeAll - static void setup() { - ladder = new Ladder2(Arrays.asList( - new Line2( - Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) - ), new Line2( - Arrays.asList(new Cross(0, new Point(false, true)), new Cross(1, new Point(true, false)), new Cross(2, new Point(false, false))) - ), new Line2( - Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) - ) - )); - } - - @Test - void move() { - assertThat(ladder.move(1)).isEqualTo(1); - } - - @Test - @DisplayName("모든 사용자의 사다리 출력") - void printResult() { - System.out.println(ladder.getLadderForPrint("-----", " ")); - } - - @Test - @DisplayName("특정 사용자의 사다리 결과") - void getResultByUserIndex() { - assertThat(ladder.move(0)) - .isEqualTo(2); - } - - @Test - @DisplayName("모든 사용자의 사다리 결과") - void getAllResult() { - Map result = new HashMap<>(); - result.put(0, 2); - result.put(1, 1); - result.put(2, 0); - - assertThat(ladder.getAllResult()) - .isEqualTo(result); - } -} diff --git a/src/test/java/nextstep/ladder/LadderTest.java b/src/test/java/nextstep/ladder/LadderTest.java index 688eeffc76..d8f1176521 100644 --- a/src/test/java/nextstep/ladder/LadderTest.java +++ b/src/test/java/nextstep/ladder/LadderTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -14,27 +16,44 @@ public class LadderTest { @BeforeAll static void setup() { - ladder = new Ladder( - new Usernames(Arrays.asList("test", "test2", "test3")), - new Lines(Arrays.asList( - new Line(new Points(Arrays.asList(new Point(false, false), new Point(false, true), new Point(true, false)))), - new Line(new Points(Arrays.asList(new Point(false, true), new Point(true, false), new Point(false, false)))) - )) - , Arrays.asList("결과1", "결과2", "결과3")); + ladder = new Ladder(Arrays.asList( + new Line( + Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) + ), new Line( + Arrays.asList(new Cross(0, new Point(false, true)), new Cross(1, new Point(true, false)), new Cross(2, new Point(false, false))) + ), new Line( + Arrays.asList(new Cross(0, new Point(false, false)), new Cross(1, new Point(false, true)), new Cross(2, new Point(true, false))) + ) + )); } @Test - @DisplayName("모든 사용자의 사다리 결과") - void getAllResult() { - assertThat(ladder.getResult("all")) - .isEqualTo("test2 : 결과3\n" + - "test3 : 결과1\n" + - "test : 결과2"); + void move() { + assertThat(ladder.move(1)).isEqualTo(1); + } + + @Test + @DisplayName("모든 사용자의 사다리 출력") + void printResult() { + System.out.println(ladder.getLadderForPrint("-----", " ")); } @Test - @DisplayName("사용자별 사다리 결과") - void getResult() { - assertThat(ladder.getResult("test2")).isEqualTo("결과3"); + @DisplayName("특정 사용자의 사다리 결과") + void getResultByUserIndex() { + assertThat(ladder.move(0)) + .isEqualTo(2); + } + + @Test + @DisplayName("모든 사용자의 사다리 결과") + void getAllResult() { + Map result = new HashMap<>(); + result.put(0, 2); + result.put(1, 1); + result.put(2, 0); + + assertThat(ladder.getAllResult()) + .isEqualTo(result); } } diff --git a/src/test/java/nextstep/ladder/Line2Test.java b/src/test/java/nextstep/ladder/LineTest.java similarity index 85% rename from src/test/java/nextstep/ladder/Line2Test.java rename to src/test/java/nextstep/ladder/LineTest.java index 475c17eacd..2ab886154c 100644 --- a/src/test/java/nextstep/ladder/Line2Test.java +++ b/src/test/java/nextstep/ladder/LineTest.java @@ -7,12 +7,12 @@ import static org.assertj.core.api.Assertions.assertThat; -public class Line2Test { - private static Line2 line; +public class LineTest { + private static Line line; @BeforeAll static void setUpBeforeClass() { - line = new Line2( + line = new Line( Arrays.asList(new Cross(0, new Point(false, true)), new Cross(0, new Point(true, false)), new Cross(0, new Point(false, false))) ); } diff --git a/src/test/java/nextstep/ladder/LinesTest.java b/src/test/java/nextstep/ladder/LinesTest.java deleted file mode 100644 index c9df6ce522..0000000000 --- a/src/test/java/nextstep/ladder/LinesTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package nextstep.ladder; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class LinesTest { - private static Lines lines; - - @BeforeAll - static void setUpBeforeClass() { - lines = new Lines(6, 5); - } - - @Test - @DisplayName("사다리 라인 사이즈 확인") - void size() { - assertThat(lines.getValues()).hasSize(6); - } -} diff --git a/src/test/java/nextstep/ladder/PointTest.java b/src/test/java/nextstep/ladder/PointTest.java index fde16d19f1..43ee94e073 100644 --- a/src/test/java/nextstep/ladder/PointTest.java +++ b/src/test/java/nextstep/ladder/PointTest.java @@ -18,12 +18,6 @@ void setUp() { point = new Point(false, true); } - - @Test - void 결과_인덱스_조회() { - assertThat(point.getResultIndex(0)).isEqualTo(1); - } - @Test void 움직인_결과_조회__오른쪽() { Point point = Point.first(true); diff --git a/src/test/java/nextstep/ladder/PointsTest.java b/src/test/java/nextstep/ladder/PointsTest.java deleted file mode 100644 index 16c53fef42..0000000000 --- a/src/test/java/nextstep/ladder/PointsTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package nextstep.ladder; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PointsTest { - private Points points; - - @Test - @DisplayName("생성된 Points 사이즈 확인") - void size() { - points = new Points(5); - assertThat(points.size()).isEqualTo(5); - } - - @Test - void 결과_인덱스_목록_조회() { - points = new Points(List.of(new Point(false, false), new Point(false, true), new Point(true, false))); - assertThat(points.getResultIndex(2)).isEqualTo(1); - } -}