From 97f2c065ebac721c698fd98caedbff7c5a478338 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Sun, 28 May 2023 15:47:25 +0900 Subject: [PATCH 01/21] =?UTF-8?q?docs:=203=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC(=EA=B2=8C=EC=9E=84=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89)=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/README.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index 6189ef2e5f..af4c597f0a 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -3,14 +3,26 @@ ## 2단계 - 사다리(생성) -[x] 참여할 사람 이름 입력 (,로 구분) - - [x] 최대 다섯 글자 + - [x] 최대 다섯 글자 -[x] 사람(Person) 객체 구현 -[x] 사람들(People) 객체 구현 -[x] 사다리 높이 입력 -[x] 사다리 가로 라인 만들기(라인(Line) 객체 구현) - -[x] 라인을 생성 할지/말지 판단하는 1/2 랜덤 함수 사용 - -[x] 가로 라인 확인 (겹치지 않기 위해) + -[x] 라인을 생성 할지/말지 판단하는 1/2 랜덤 함수 사용 + -[x] 가로 라인 확인 (겹치지 않기 위해) -[x] 사다리(Ladder) 객체 구현 --[ ] 실행결과 출력 - - [x] 이름 출력 - - [x] 사다리 출력 +-[x] 실행결과 출력 + - [x] 이름 출력 + - [x] 사다리 출력 + +## 3단계 - 사다리(게임 실행) + +-[ ] 실행 결과 입력 추가 (,로 구분) + - [ ] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 +-[ ] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 +-[ ] 결과를 보고 싶은 사람 입력 + - all 아닐 경우 + - [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 + - [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 + - all 일 경우 + - [ ] 모든 사람의 실행 결과 출력 From 7e1b66c9bbcb7ecb539b77a5d5e00b77f60332de Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Sun, 28 May 2023 15:54:34 +0900 Subject: [PATCH 02/21] =?UTF-8?q?test:=20=EC=8B=A4=ED=96=89=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=83=9D=EC=84=B1,=20=EA=B8=80=EC=9E=90=EC=88=98?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nextstep/ladder/domain/ExecuteResult.java | 25 +++++++++++++++++++ .../ladder/domain/ExecuteResultTest.java | 22 ++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/nextstep/ladder/domain/ExecuteResult.java create mode 100644 src/test/java/nextstep/ladder/domain/ExecuteResultTest.java diff --git a/src/main/java/nextstep/ladder/domain/ExecuteResult.java b/src/main/java/nextstep/ladder/domain/ExecuteResult.java new file mode 100644 index 0000000000..85457ded3d --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/ExecuteResult.java @@ -0,0 +1,25 @@ +package nextstep.ladder.domain; + +public class ExecuteResult { + private static final int NAME_LENGTH_MAX = 5; + + private final String name; + + public ExecuteResult(String name) { + if (name.length() > NAME_LENGTH_MAX) { + throw new IllegalArgumentException("글자수는 최대 5글자까지 부여할 수 있습니다."); + } + this.name = name; + } + + public String name() { + return this.name; + } + + @Override + public String toString() { + return "ExecuteResult{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java b/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java new file mode 100644 index 0000000000..54ddf534e8 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java @@ -0,0 +1,22 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class ExecuteResultTest { + + @Test + void create() { + ExecuteResult result = new ExecuteResult("꽝"); + assertThat(result.name()).isEqualTo(new ExecuteResult("꽝").name()); + } + + @Test + void 글자수_5이상시_예외() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new ExecuteResult("500000")) + .withMessageContaining("글자수는 최대 5글자까지 부여할 수 있습니다."); + } +} From 0796fcd126ab9a5a97b82c308f10f0c020fede78 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Sun, 28 May 2023 16:15:00 +0900 Subject: [PATCH 03/21] =?UTF-8?q?feat:=20=EC=8B=A4=ED=96=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=9E=85=EB=A0=A5=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/Main.java | 2 +- src/main/java/nextstep/ladder/README.md | 24 +++++++++---------- .../ladder/domain/ExecuteResults.java | 23 ++++++++++++++++++ .../java/nextstep/ladder/domain/Ladder.java | 11 ++++++++- .../java/nextstep/ladder/view/InputView.java | 6 +++++ .../java/nextstep/ladder/view/ResultView.java | 5 +++- .../ladder/domain/ExecuteResultsTest.java | 15 ++++++++++++ .../nextstep/ladder/domain/LadderTest.java | 4 +++- 8 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 src/main/java/nextstep/ladder/domain/ExecuteResults.java create mode 100644 src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index 0965b25dc4..e81e7ba7ee 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -8,7 +8,7 @@ public class Main { public static void main(String[] args) { InputView inputView = new InputView(); - Ladder ladder = new Ladder(inputView.people(), inputView.height()); + Ladder ladder = new Ladder(inputView.people(), inputView.executeResults(), inputView.height()); ResultView.printResult(ladder); inputView.close(); diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index af4c597f0a..d7522127ba 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -3,26 +3,26 @@ ## 2단계 - 사다리(생성) -[x] 참여할 사람 이름 입력 (,로 구분) - - [x] 최대 다섯 글자 + - [x] 최대 다섯 글자 -[x] 사람(Person) 객체 구현 -[x] 사람들(People) 객체 구현 -[x] 사다리 높이 입력 -[x] 사다리 가로 라인 만들기(라인(Line) 객체 구현) - -[x] 라인을 생성 할지/말지 판단하는 1/2 랜덤 함수 사용 - -[x] 가로 라인 확인 (겹치지 않기 위해) + -[x] 라인을 생성 할지/말지 판단하는 1/2 랜덤 함수 사용 + -[x] 가로 라인 확인 (겹치지 않기 위해) -[x] 사다리(Ladder) 객체 구현 -[x] 실행결과 출력 - - [x] 이름 출력 - - [x] 사다리 출력 + - [x] 이름 출력 + - [x] 사다리 출력 ## 3단계 - 사다리(게임 실행) --[ ] 실행 결과 입력 추가 (,로 구분) - - [ ] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 +-[x] 실행 결과 입력 추가 (,로 구분) + - [x] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 -[ ] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 -[ ] 결과를 보고 싶은 사람 입력 - - all 아닐 경우 - - [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 - - [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 - - all 일 경우 - - [ ] 모든 사람의 실행 결과 출력 + - all 아닐 경우 + - [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 + - [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 + - all 일 경우 + - [ ] 모든 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/domain/ExecuteResults.java b/src/main/java/nextstep/ladder/domain/ExecuteResults.java new file mode 100644 index 0000000000..105fb5e117 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/ExecuteResults.java @@ -0,0 +1,23 @@ +package nextstep.ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class ExecuteResults { + + private final List executeResults; + + public ExecuteResults(String[] executeResults) { + if (executeResults.length == 0) { + throw new IllegalStateException("실행 결과가 입력되지 않았습니다."); + } + this.executeResults = Arrays.stream(executeResults). + map(ExecuteResult::new). + collect(Collectors.toList()); + } + + public List value() { + return executeResults; + } +} diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 148ccb928b..7811149a27 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -7,10 +7,15 @@ public class Ladder { private final People people; + private final ExecuteResults executeResults; private final Lines lines; - public Ladder(People people, int height) { + public Ladder(People people, ExecuteResults executeResults, int height) { + if (people.value().size() != executeResults.value().size()) { + throw new RuntimeException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } this.people = people; + this.executeResults = executeResults; this.lines = new Lines(generateLadder(height)); } @@ -24,6 +29,10 @@ public People people() { return people; } + public ExecuteResults executeResults() { + return executeResults; + } + public Lines lines() { return lines; } diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index 8bb4c76b5f..0cff6f7d6a 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -1,5 +1,6 @@ package nextstep.ladder.view; +import nextstep.ladder.domain.ExecuteResults; import nextstep.ladder.domain.People; import java.util.Scanner; @@ -13,6 +14,11 @@ public People people() { return new People(nextLine().split(",")); } + public ExecuteResults executeResults() { + ResultView.printExecuteResultCommand(); + return new ExecuteResults(nextLine().split(",")); + } + public int height() { ResultView.printHeightInputCommand(); return Integer.parseInt(nextLine()); diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 5afa636ceb..937a0d3dd3 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -15,6 +15,10 @@ static void printPeopleInputCommand() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); } + static void printExecuteResultCommand() { + System.out.println("\n실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + } + static void printHeightInputCommand() { System.out.println("\n최대 사다리 높이는 몇 개인가요?"); } @@ -79,6 +83,5 @@ private static String generatePointString(People people, Line line, int idx) { } return line.points().get(idx) ? "-----|" : " |"; } - } diff --git a/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java b/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java new file mode 100644 index 0000000000..db3c41685c --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java @@ -0,0 +1,15 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + +public class ExecuteResultsTest { + + @Test + void 입력된_사람_없을시_예외() { + assertThatIllegalStateException() + .isThrownBy(() -> new ExecuteResults(new String[]{})) + .withMessageContaining("실행 결과가 입력되지 않았습니다."); + } +} diff --git a/src/test/java/nextstep/ladder/domain/LadderTest.java b/src/test/java/nextstep/ladder/domain/LadderTest.java index 606daf4d3a..6fd3752375 100644 --- a/src/test/java/nextstep/ladder/domain/LadderTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderTest.java @@ -9,9 +9,11 @@ public class LadderTest { @Test void create() { People people = new People(new String[]{"pobi", "honux", "crong", "jk"}); + ExecuteResults executeResults = new ExecuteResults(new String[]{"꽝", "5000", "꽝", "3000"}); int height = 5; - Ladder ladder = new Ladder(people, height); + Ladder ladder = new Ladder(people, executeResults, height); assertThat(ladder.lines().value().size()).isEqualTo(height); + } } From 7db91d924771844fe20fc2d722acd397ab573105 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Sun, 28 May 2023 18:41:37 +0900 Subject: [PATCH 04/21] =?UTF-8?q?feat:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=EC=8B=9C=20=EC=8B=A4=ED=96=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=ED=8F=AC=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/README.md | 2 +- .../java/nextstep/ladder/view/ResultView.java | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index d7522127ba..ae1cd5a14e 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -19,7 +19,7 @@ -[x] 실행 결과 입력 추가 (,로 구분) - [x] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 --[ ] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 +-[x] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 -[ ] 결과를 보고 싶은 사람 입력 - all 아닐 경우 - [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 937a0d3dd3..3d19cc1c97 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -24,13 +24,14 @@ static void printHeightInputCommand() { } public static void printResult(Ladder ladder) { - System.out.println("\n실행결과\n"); printLadder(ladder); } private static void printLadder(Ladder ladder) { + System.out.println("\n사다리 결과\n"); printNames(ladder); printLines(ladder); + printExecuteResults(ladder); } private static void printNames(Ladder ladder) { @@ -70,8 +71,32 @@ private static String makeLines(Ladder ladder) { .mapToObj(idx -> generatePointString(people, line, idx) ) .forEach(sb::append); + return sb.toString(); + }) + .collect(Collectors.joining("\n")); + } + + private static void printExecuteResults(Ladder ladder) { + System.out.println(makeExecuteResults(ladder)); + } + + private static String makeExecuteResults(Ladder ladder) { + List executeResults = ladder.executeResults().value(); + List people = ladder.people().value(); + + return IntStream.range(BEGIN_INDEX, executeResults.size()) + .mapToObj(i -> { + String executeName = executeResults.get(i).name(); + String peopleName = people.get(i).name(); - sb.append("\n"); + StringBuilder sb = new StringBuilder(); + if (BEGIN_INDEX == i) { + sb.append(executeName); + sb.append(" ".repeat(NAME_SPACE - peopleName.length())); + return sb.toString(); + } + sb.append(" ".repeat(NAME_SPACE - executeName.length())); + sb.append(executeName); return sb.toString(); }) .collect(Collectors.joining()); From 56f35bb52002d7467f953b9301e29b05d42b01a7 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Mon, 29 May 2023 01:30:16 +0900 Subject: [PATCH 05/21] =?UTF-8?q?test:=20=ED=8A=B9=EC=A0=95=EC=9D=B8?= =?UTF-8?q?=EC=9D=98=20=EA=B2=B0=EA=B3=BC=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Main.java | 5 +++ src/main/java/nextstep/ladder/README.md | 10 ++--- .../java/nextstep/ladder/domain/Ladder.java | 9 ++++ .../java/nextstep/ladder/domain/Line.java | 6 ++- .../nextstep/ladder/domain/MatchResult.java | 32 +++++++++++++ .../ladder/domain/MatchResultTest.java | 45 +++++++++++++++++++ 6 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/main/java/nextstep/ladder/domain/MatchResult.java create mode 100644 src/test/java/nextstep/ladder/domain/MatchResultTest.java diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index e81e7ba7ee..59f3303954 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -1,6 +1,7 @@ package nextstep.ladder; import nextstep.ladder.domain.Ladder; +import nextstep.ladder.domain.MatchResult; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -11,6 +12,10 @@ public static void main(String[] args) { Ladder ladder = new Ladder(inputView.people(), inputView.executeResults(), inputView.height()); ResultView.printResult(ladder); + MatchResult matchResult = new MatchResult(ladder); + + matchResult.result("pobi"); + inputView.close(); } } diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index ae1cd5a14e..a286d9e259 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -21,8 +21,8 @@ - [x] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 -[x] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 -[ ] 결과를 보고 싶은 사람 입력 - - all 아닐 경우 - - [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 - - [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 - - all 일 경우 - - [ ] 모든 사람의 실행 결과 출력 +- all 아닐 경우 +- [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 +- [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 +- all 일 경우 +- [ ] 모든 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 7811149a27..2a0533117d 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -19,6 +19,15 @@ public Ladder(People people, ExecuteResults executeResults, int height) { this.lines = new Lines(generateLadder(height)); } + public Ladder(People people, ExecuteResults executeResults, Lines mockLines) { + if (people.value().size() != executeResults.value().size()) { + throw new RuntimeException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } + this.people = people; + this.executeResults = executeResults; + this.lines = mockLines; + } + private List generateLadder(int height) { return Stream.generate(() -> new Line(people.value().size())) .limit(height) diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index b451344a85..d493845d9a 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -13,7 +13,11 @@ public class Line { private final List points; public Line(int countOfPerson) { - points = generateLine(countOfPerson); + this.points = generateLine(countOfPerson); + } + + public Line(List mockPoints) { + this.points = mockPoints; } public List generateLine(int countOfPerson) { diff --git a/src/main/java/nextstep/ladder/domain/MatchResult.java b/src/main/java/nextstep/ladder/domain/MatchResult.java new file mode 100644 index 0000000000..6b85661bc4 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/MatchResult.java @@ -0,0 +1,32 @@ +package nextstep.ladder.domain; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MatchResult { + + private final Ladder ladder; + private Map results = new HashMap<>(); + + public MatchResult(Ladder ladder) { + this.ladder = ladder; + } + + public String result(String person) { + List people = ladder.people().value(); + List lines = ladder.lines().value(); + List executeResults = ladder.executeResults().value(); + + int peopleCnt = people.size(); + int height = lines.size(); + + boolean[][] visited; + + return ""; + } + + public Map value() { + return results; + } +} diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java new file mode 100644 index 0000000000..10a1386582 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -0,0 +1,45 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +class MatchResultTest { + + private People people; + private Line line1, line2, line3, line4, line5; + private Lines lines; + private ExecuteResults executeResults; + private Ladder ladder; + + @BeforeEach + void setup() { + people = new People(new String[]{"pobi, honux, crong, jk"}); + + line1 = new Line(Arrays.asList(true, false, true)); + line2 = new Line(Arrays.asList(false, true, false)); + line3 = new Line(Arrays.asList(true, false, false)); + line4 = new Line(Arrays.asList(false, true, false)); + line5 = new Line(Arrays.asList(true, false, true)); + lines = new Lines(Arrays.asList(line1, line2, line3, line4, line5)); + + executeResults = new ExecuteResults(new String[]{"꽝", "5000", "꽝", "3000"}); + + ladder = new Ladder(people, executeResults, lines); + } + + @Test + void 특정인의_결과_확인() { + //given + MatchResult matchResult = new MatchResult(ladder); + + //when + String result = matchResult.result("pobi"); + + //then + assertThat(result).isEqualTo("꽝"); + } +} From c9688d4ae4c9e3565d525159069140df35db280c Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Mon, 29 May 2023 23:25:47 +0900 Subject: [PATCH 06/21] =?UTF-8?q?refactor:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Main.java | 20 +++++++--- .../nextstep/ladder/domain/InputOutput.java | 23 +++++++++++ .../java/nextstep/ladder/domain/Ladder.java | 34 ++++------------- .../java/nextstep/ladder/domain/Line.java | 16 ++++---- .../nextstep/ladder/domain/MatchResult.java | 24 ++++++------ .../java/nextstep/ladder/view/ResultView.java | 38 +++++++++---------- .../ladder/domain/InputOutputTest.java | 21 ++++++++++ .../nextstep/ladder/domain/LadderTest.java | 5 +-- .../ladder/domain/MatchResultTest.java | 11 +++--- 9 files changed, 112 insertions(+), 80 deletions(-) create mode 100644 src/main/java/nextstep/ladder/domain/InputOutput.java create mode 100644 src/test/java/nextstep/ladder/domain/InputOutputTest.java diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index 59f3303954..d3b8acc722 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -1,7 +1,9 @@ package nextstep.ladder; +import nextstep.ladder.domain.ExecuteResults; +import nextstep.ladder.domain.InputOutput; import nextstep.ladder.domain.Ladder; -import nextstep.ladder.domain.MatchResult; +import nextstep.ladder.domain.People; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -9,12 +11,20 @@ public class Main { public static void main(String[] args) { InputView inputView = new InputView(); - Ladder ladder = new Ladder(inputView.people(), inputView.executeResults(), inputView.height()); - ResultView.printResult(ladder); + People people = inputView.people(); + ExecuteResults executeResults = inputView.executeResults(); + InputOutput inputOutput = new InputOutput(people, executeResults); - MatchResult matchResult = new MatchResult(ladder); + int verticalLineCount = people.value().size(); + int height = inputView.height(); - matchResult.result("pobi"); + Ladder ladder = new Ladder(verticalLineCount, height); + + ResultView.printResult(inputOutput, ladder); + +// MatchResult matchResult = new MatchResult(ladder); + +// matchResult.result("pobi"); inputView.close(); } diff --git a/src/main/java/nextstep/ladder/domain/InputOutput.java b/src/main/java/nextstep/ladder/domain/InputOutput.java new file mode 100644 index 0000000000..a5ae02e2c1 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/InputOutput.java @@ -0,0 +1,23 @@ +package nextstep.ladder.domain; + +public class InputOutput { + + private final People people; + private final ExecuteResults executeResults; + + public InputOutput(People people, ExecuteResults executeResults) { + if (people.value().size() != executeResults.value().size()) { + throw new IllegalArgumentException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } + this.people = people; + this.executeResults = executeResults; + } + + public People people() { + return people; + } + + public ExecuteResults executeResults() { + return executeResults; + } +} diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 2a0533117d..7071fff20a 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -6,42 +6,24 @@ public class Ladder { - private final People people; - private final ExecuteResults executeResults; + private final int verticalLineCount; private final Lines lines; - public Ladder(People people, ExecuteResults executeResults, int height) { - if (people.value().size() != executeResults.value().size()) { - throw new RuntimeException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); - } - this.people = people; - this.executeResults = executeResults; - this.lines = new Lines(generateLadder(height)); + public Ladder(int verticalLineCount, Lines lines) { + this.verticalLineCount = verticalLineCount; + this.lines = lines; } - public Ladder(People people, ExecuteResults executeResults, Lines mockLines) { - if (people.value().size() != executeResults.value().size()) { - throw new RuntimeException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); - } - this.people = people; - this.executeResults = executeResults; - this.lines = mockLines; + public Ladder(int verticalLineCount, int height) { + this(verticalLineCount, new Lines(generateLadder(verticalLineCount, height))); } - private List generateLadder(int height) { - return Stream.generate(() -> new Line(people.value().size())) + private static List generateLadder(int verticalLineCount, int height) { + return Stream.generate(() -> new Line(verticalLineCount)) .limit(height) .collect(Collectors.toList()); } - public People people() { - return people; - } - - public ExecuteResults executeResults() { - return executeResults; - } - public Lines lines() { return lines; } diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index d493845d9a..79f9c5f8c2 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -12,15 +12,15 @@ public class Line { private final List points; - public Line(int countOfPerson) { - this.points = generateLine(countOfPerson); + public Line(List points) { + this.points = points; } - public Line(List mockPoints) { - this.points = mockPoints; + public Line(int countOfPerson) { + this(generateLine(countOfPerson)); } - public List generateLine(int countOfPerson) { + public static List generateLine(int countOfPerson) { List points = new ArrayList<>(); IntStream.range(BEGIN_INDEX, countOfPerson) @@ -29,18 +29,18 @@ public List generateLine(int countOfPerson) { return points; } - private Boolean createPoint(int idx, List points) { + private static Boolean createPoint(int idx, List points) { if (BEGIN_INDEX == idx) { return EMPTY_POINT; } return isPrevPointEmpty(idx, points) && isCurrPointNonEmpty(); } - private boolean isPrevPointEmpty(int idx, List points) { + private static boolean isPrevPointEmpty(int idx, List points) { return EMPTY_POINT == points.get(idx - 1); } - private boolean isCurrPointNonEmpty() { + private static boolean isCurrPointNonEmpty() { return HALF < Math.random(); } diff --git a/src/main/java/nextstep/ladder/domain/MatchResult.java b/src/main/java/nextstep/ladder/domain/MatchResult.java index 6b85661bc4..242cea98ee 100644 --- a/src/main/java/nextstep/ladder/domain/MatchResult.java +++ b/src/main/java/nextstep/ladder/domain/MatchResult.java @@ -1,7 +1,6 @@ package nextstep.ladder.domain; import java.util.HashMap; -import java.util.List; import java.util.Map; public class MatchResult { @@ -13,18 +12,17 @@ public MatchResult(Ladder ladder) { this.ladder = ladder; } - public String result(String person) { - List people = ladder.people().value(); - List lines = ladder.lines().value(); - List executeResults = ladder.executeResults().value(); - - int peopleCnt = people.size(); - int height = lines.size(); - - boolean[][] visited; - - return ""; - } +// public String result(String personName) { +// List people = ladder.people().value(); +// List lines = ladder.lines().value(); +// List executeResults = ladder.executeResults().value(); +// +// int peopleCnt = people.size(); +// int height = lines.size(); +// +// +// return ""; +// } public Map value() { return results; diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 3d19cc1c97..12078086c5 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -23,23 +23,23 @@ static void printHeightInputCommand() { System.out.println("\n최대 사다리 높이는 몇 개인가요?"); } - public static void printResult(Ladder ladder) { - printLadder(ladder); + public static void printResult(InputOutput inputOutput, Ladder ladder) { + printLadder(inputOutput, ladder); } - private static void printLadder(Ladder ladder) { + private static void printLadder(InputOutput inputOutput, Ladder ladder) { System.out.println("\n사다리 결과\n"); - printNames(ladder); - printLines(ladder); - printExecuteResults(ladder); + printNames(inputOutput, ladder); + printLines(inputOutput, ladder); + printExecuteResults(inputOutput); } - private static void printNames(Ladder ladder) { - System.out.println(makeNames(ladder)); + private static void printNames(InputOutput inputOutput, Ladder ladder) { + System.out.println(makeNames(inputOutput, ladder)); } - private static String makeNames(Ladder ladder) { - List people = ladder.people().value(); + private static String makeNames(InputOutput inputOutput, Ladder ladder) { + List people = inputOutput.people().value(); return IntStream.range(BEGIN_INDEX, people.size()) .mapToObj(i -> { @@ -56,12 +56,12 @@ private static String makeNames(Ladder ladder) { .collect(Collectors.joining()); } - private static void printLines(Ladder ladder) { - System.out.println(makeLines(ladder)); + private static void printLines(InputOutput inputOutput, Ladder ladder) { + System.out.println(makeLines(inputOutput, ladder)); } - private static String makeLines(Ladder ladder) { - People people = ladder.people(); + private static String makeLines(InputOutput inputOutput, Ladder ladder) { + People people = inputOutput.people(); Lines lines = ladder.lines(); return lines.value().stream() @@ -76,13 +76,13 @@ private static String makeLines(Ladder ladder) { .collect(Collectors.joining("\n")); } - private static void printExecuteResults(Ladder ladder) { - System.out.println(makeExecuteResults(ladder)); + private static void printExecuteResults(InputOutput inputOutput) { + System.out.println(makeExecuteResults(inputOutput)); } - private static String makeExecuteResults(Ladder ladder) { - List executeResults = ladder.executeResults().value(); - List people = ladder.people().value(); + private static String makeExecuteResults(InputOutput inputOutput) { + List executeResults = inputOutput.executeResults().value(); + List people = inputOutput.people().value(); return IntStream.range(BEGIN_INDEX, executeResults.size()) .mapToObj(i -> { diff --git a/src/test/java/nextstep/ladder/domain/InputOutputTest.java b/src/test/java/nextstep/ladder/domain/InputOutputTest.java new file mode 100644 index 0000000000..efec4b208c --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/InputOutputTest.java @@ -0,0 +1,21 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +public class InputOutputTest { + + @Test + void input_output_개수_다를경우_예외처리() { + //given + People people = new People(new String[]{"a", "b"}); + ExecuteResults executeResults = new ExecuteResults(new String[]{"꽝"}); + + //when + //then + assertThatIllegalArgumentException() + .isThrownBy(() -> new InputOutput(people, executeResults)) + .withMessageContaining("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } +} diff --git a/src/test/java/nextstep/ladder/domain/LadderTest.java b/src/test/java/nextstep/ladder/domain/LadderTest.java index 6fd3752375..7a02581175 100644 --- a/src/test/java/nextstep/ladder/domain/LadderTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderTest.java @@ -8,11 +8,10 @@ public class LadderTest { @Test void create() { - People people = new People(new String[]{"pobi", "honux", "crong", "jk"}); - ExecuteResults executeResults = new ExecuteResults(new String[]{"꽝", "5000", "꽝", "3000"}); + int verticalLineCount = 4; int height = 5; - Ladder ladder = new Ladder(people, executeResults, height); + Ladder ladder = new Ladder(verticalLineCount, height); assertThat(ladder.lines().value().size()).isEqualTo(height); } diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 10a1386582..889dfa5438 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -5,8 +5,6 @@ import java.util.Arrays; -import static org.assertj.core.api.Assertions.assertThat; - class MatchResultTest { private People people; @@ -17,7 +15,8 @@ class MatchResultTest { @BeforeEach void setup() { - people = new People(new String[]{"pobi, honux, crong, jk"}); + people = new People(new String[]{"pobi", "honux", "crong", "jk"}); + int verticalLineSize = people.value().size(); line1 = new Line(Arrays.asList(true, false, true)); line2 = new Line(Arrays.asList(false, true, false)); @@ -28,7 +27,7 @@ void setup() { executeResults = new ExecuteResults(new String[]{"꽝", "5000", "꽝", "3000"}); - ladder = new Ladder(people, executeResults, lines); + ladder = new Ladder(verticalLineSize, lines); } @Test @@ -37,9 +36,9 @@ void setup() { MatchResult matchResult = new MatchResult(ladder); //when - String result = matchResult.result("pobi"); +// String result = matchResult.result("pobi"); //then - assertThat(result).isEqualTo("꽝"); +// assertThat(result).isEqualTo("꽝"); } } From fb045754807a1015f7acea8d293dee2804cfd04e Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Mon, 29 May 2023 23:30:33 +0900 Subject: [PATCH 07/21] =?UTF-8?q?refactor:=20=EC=9D=B8=EC=8A=A4=ED=84=B4?= =?UTF-8?q?=EC=8A=A4=20=EB=B3=80=EC=88=98=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Line.java | 26 +++++++++---------- .../java/nextstep/ladder/view/ResultView.java | 4 +-- .../java/nextstep/ladder/domain/LineTest.java | 6 ++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index 79f9c5f8c2..dcdfc5447e 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -10,10 +10,10 @@ public class Line { private static final boolean EMPTY_POINT = false; private static final double HALF = 0.5; - private final List points; + private final List horizontalLines; - public Line(List points) { - this.points = points; + public Line(List horizontalLines) { + this.horizontalLines = horizontalLines; } public Line(int countOfPerson) { @@ -21,37 +21,37 @@ public Line(int countOfPerson) { } public static List generateLine(int countOfPerson) { - List points = new ArrayList<>(); + List horizontalLines = new ArrayList<>(); IntStream.range(BEGIN_INDEX, countOfPerson) - .forEach(idx -> points.add(createPoint(idx, points))); + .forEach(idx -> horizontalLines.add(createPoint(idx, horizontalLines))); - return points; + return horizontalLines; } - private static Boolean createPoint(int idx, List points) { + private static Boolean createPoint(int idx, List horizontalLines) { if (BEGIN_INDEX == idx) { return EMPTY_POINT; } - return isPrevPointEmpty(idx, points) && isCurrPointNonEmpty(); + return isPrevPointEmpty(idx, horizontalLines) && isCurrPointNonEmpty(); } - private static boolean isPrevPointEmpty(int idx, List points) { - return EMPTY_POINT == points.get(idx - 1); + private static boolean isPrevPointEmpty(int idx, List horizontalLines) { + return EMPTY_POINT == horizontalLines.get(idx - 1); } private static boolean isCurrPointNonEmpty() { return HALF < Math.random(); } - public List points() { - return points; + public List value() { + return horizontalLines; } @Override public String toString() { return "Line{" + - "points=" + points + + "horizontalLines=" + horizontalLines + '}'; } } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 12078086c5..c6da2a6e71 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -67,7 +67,7 @@ private static String makeLines(InputOutput inputOutput, Ladder ladder) { return lines.value().stream() .map(line -> { StringBuilder sb = new StringBuilder(); - IntStream.range(BEGIN_INDEX, line.points().size()) + IntStream.range(BEGIN_INDEX, line.value().size()) .mapToObj(idx -> generatePointString(people, line, idx) ) .forEach(sb::append); @@ -106,7 +106,7 @@ private static String generatePointString(People people, Line line, int idx) { if (BEGIN_INDEX == idx) { return " ".repeat(people.firstPersonNameLength()) + "|"; } - return line.points().get(idx) ? "-----|" : " |"; + return line.value().get(idx) ? "-----|" : " |"; } } diff --git a/src/test/java/nextstep/ladder/domain/LineTest.java b/src/test/java/nextstep/ladder/domain/LineTest.java index 6d36b4ee5d..eda684b019 100644 --- a/src/test/java/nextstep/ladder/domain/LineTest.java +++ b/src/test/java/nextstep/ladder/domain/LineTest.java @@ -18,14 +18,14 @@ void setup() { @Test void create() { - assertThat(line.points().size()).isEqualTo(4); + assertThat(line.value().size()).isEqualTo(4); } @ParameterizedTest @ValueSource(ints = {1, 2, 3}) void 라인이_겹치지_않는지_확인(int idx) { - Boolean prevPoint = line.points().get(idx - 1); - Boolean currPoint = line.points().get(idx); + Boolean prevPoint = line.value().get(idx - 1); + Boolean currPoint = line.value().get(idx); if (prevPoint) assertThat(true).isNotEqualTo(currPoint); From 17e3d2e685a3969fdd00e38b8f722b2ccf53f6c4 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 00:11:10 +0900 Subject: [PATCH 08/21] =?UTF-8?q?feat:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=202?= =?UTF-8?q?=EC=B0=A8=EC=9B=90=20=EB=B0=B0=EC=97=B4=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Ladder.java | 34 +++++++++++++++++++ .../nextstep/ladder/domain/LadderTest.java | 22 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 7071fff20a..60ad6201ad 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -2,10 +2,13 @@ import java.util.List; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; public class Ladder { + private static final int BEGIN_INDEX = 0; + private final int verticalLineCount; private final Lines lines; @@ -27,4 +30,35 @@ private static List generateLadder(int verticalLineCount, int height) { public Lines lines() { return lines; } + + public String[][] result() { + List lines = this.lines.value(); + + int width = verticalLineCount * 2 - 1; + int height = this.lines.value().size(); + + String[][] ladder = new String[height][width]; + + IntStream.range(BEGIN_INDEX, height) + .forEach(i -> IntStream.range(BEGIN_INDEX, width) + .forEach(j -> { + if (isVerticalLine(j)) { + ladder[i][j] = "v"; + return; + } + if (isHorizontalLine(lines, i, j)) { + ladder[i][j] = "h"; + } + })); + return ladder; + } + + private boolean isVerticalLine(int j) { + return j % 2 == 0; + } + + private Boolean isHorizontalLine(List lines, int i, int j) { + return lines.get(i).value().get((j - 1) / 2); + } + } diff --git a/src/test/java/nextstep/ladder/domain/LadderTest.java b/src/test/java/nextstep/ladder/domain/LadderTest.java index 7a02581175..39f5f19976 100644 --- a/src/test/java/nextstep/ladder/domain/LadderTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; +import java.util.Arrays; + import static org.assertj.core.api.Assertions.assertThat; public class LadderTest { @@ -15,4 +17,24 @@ void create() { assertThat(ladder.lines().value().size()).isEqualTo(height); } + + @Test + void 사다리_2차원배열_만들기() { + //given + int verticalLineCount = 4; + Lines lines = new Lines(Arrays.asList( + new Line(Arrays.asList(true, false, true)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, false)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, true)) + )); + + //when + Ladder ladder = new Ladder(verticalLineCount, lines); + String[][] result = ladder.result(); + + //then + System.out.println(Arrays.deepToString(result)); + } } From 02d71a8f9b7da90083f516ba807cbc7cab98fe33 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 00:17:56 +0900 Subject: [PATCH 09/21] =?UTF-8?q?fix:=20=EC=8B=A4=ED=96=89=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=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/view/ResultView.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index c6da2a6e71..e3086891c8 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -29,16 +29,16 @@ public static void printResult(InputOutput inputOutput, Ladder ladder) { private static void printLadder(InputOutput inputOutput, Ladder ladder) { System.out.println("\n사다리 결과\n"); - printNames(inputOutput, ladder); + printNames(inputOutput); printLines(inputOutput, ladder); printExecuteResults(inputOutput); } - private static void printNames(InputOutput inputOutput, Ladder ladder) { - System.out.println(makeNames(inputOutput, ladder)); + private static void printNames(InputOutput inputOutput) { + System.out.println(makeNames(inputOutput)); } - private static String makeNames(InputOutput inputOutput, Ladder ladder) { + private static String makeNames(InputOutput inputOutput) { List people = inputOutput.people().value(); return IntStream.range(BEGIN_INDEX, people.size()) @@ -91,8 +91,8 @@ private static String makeExecuteResults(InputOutput inputOutput) { StringBuilder sb = new StringBuilder(); if (BEGIN_INDEX == i) { + sb.append(" ".repeat(Math.abs(peopleName.length() - executeName.length()))); sb.append(executeName); - sb.append(" ".repeat(NAME_SPACE - peopleName.length())); return sb.toString(); } sb.append(" ".repeat(NAME_SPACE - executeName.length())); From d2c1b028606dfc0c0f4c765fc7511967583ebcd6 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 00:27:30 +0900 Subject: [PATCH 10/21] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=88=98=EC=A0=95,=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=AA=85?= =?UTF-8?q?=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/Main.java | 7 ++-- .../domain/{MatchResult.java => Match.java} | 4 +-- .../ladder/domain/MatchResultTest.java | 32 +++++++++++-------- 3 files changed, 22 insertions(+), 21 deletions(-) rename src/main/java/nextstep/ladder/domain/{MatchResult.java => Match.java} (90%) diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index d3b8acc722..5f8178a84d 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -1,9 +1,6 @@ package nextstep.ladder; -import nextstep.ladder.domain.ExecuteResults; -import nextstep.ladder.domain.InputOutput; -import nextstep.ladder.domain.Ladder; -import nextstep.ladder.domain.People; +import nextstep.ladder.domain.*; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -22,7 +19,7 @@ public static void main(String[] args) { ResultView.printResult(inputOutput, ladder); -// MatchResult matchResult = new MatchResult(ladder); + Match match = new Match(ladder); // matchResult.result("pobi"); diff --git a/src/main/java/nextstep/ladder/domain/MatchResult.java b/src/main/java/nextstep/ladder/domain/Match.java similarity index 90% rename from src/main/java/nextstep/ladder/domain/MatchResult.java rename to src/main/java/nextstep/ladder/domain/Match.java index 242cea98ee..80c4915b86 100644 --- a/src/main/java/nextstep/ladder/domain/MatchResult.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -3,12 +3,12 @@ import java.util.HashMap; import java.util.Map; -public class MatchResult { +public class Match { private final Ladder ladder; private Map results = new HashMap<>(); - public MatchResult(Ladder ladder) { + public Match(Ladder ladder) { this.ladder = ladder; } diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 889dfa5438..602c5eddd6 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -1,14 +1,16 @@ package nextstep.ladder.domain; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.util.Arrays; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + class MatchResultTest { private People people; - private Line line1, line2, line3, line4, line5; private Lines lines; private ExecuteResults executeResults; private Ladder ladder; @@ -18,27 +20,29 @@ void setup() { people = new People(new String[]{"pobi", "honux", "crong", "jk"}); int verticalLineSize = people.value().size(); - line1 = new Line(Arrays.asList(true, false, true)); - line2 = new Line(Arrays.asList(false, true, false)); - line3 = new Line(Arrays.asList(true, false, false)); - line4 = new Line(Arrays.asList(false, true, false)); - line5 = new Line(Arrays.asList(true, false, true)); - lines = new Lines(Arrays.asList(line1, line2, line3, line4, line5)); + lines = new Lines(Arrays.asList( + new Line(Arrays.asList(true, false, true)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, false)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, true)) + )); - executeResults = new ExecuteResults(new String[]{"꽝", "5000", "꽝", "3000"}); + executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); ladder = new Ladder(verticalLineSize, lines); } - @Test - void 특정인의_결과_확인() { + @ParameterizedTest + @CsvSource(value = {"pobi:꽝1", "honux:3000", "crong:꽝2", "jk:5000"}, delimiter = ':') + void 특정인의_결과_확인(String input, String output) { //given - MatchResult matchResult = new MatchResult(ladder); + Match match = new Match(ladder); //when -// String result = matchResult.result("pobi"); + String result = match.result(input); //then -// assertThat(result).isEqualTo("꽝"); + assertThat(result).isEqualTo(output); } } From 5ce386b4b1b2dadd3d1c4d153290ec5f4e0f279d Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 01:25:11 +0900 Subject: [PATCH 11/21] =?UTF-8?q?refactor:=20=EC=82=AC=EB=8B=A4=EB=A6=AC?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Ladder.java | 4 +- .../java/nextstep/ladder/domain/Line.java | 5 +- .../java/nextstep/ladder/domain/Match.java | 16 ++--- .../java/nextstep/ladder/view/ResultView.java | 58 ++++++++++++------- .../ladder/domain/MatchResultTest.java | 10 ++-- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 60ad6201ad..e24ac0c1a1 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -18,10 +18,10 @@ public Ladder(int verticalLineCount, Lines lines) { } public Ladder(int verticalLineCount, int height) { - this(verticalLineCount, new Lines(generateLadder(verticalLineCount, height))); + this(verticalLineCount, new Lines(generateLines(verticalLineCount, height))); } - private static List generateLadder(int verticalLineCount, int height) { + private static List generateLines(int verticalLineCount, int height) { return Stream.generate(() -> new Line(verticalLineCount)) .limit(height) .collect(Collectors.toList()); diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index dcdfc5447e..60282e9869 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -23,15 +23,14 @@ public Line(int countOfPerson) { public static List generateLine(int countOfPerson) { List horizontalLines = new ArrayList<>(); - IntStream.range(BEGIN_INDEX, countOfPerson) + IntStream.range(BEGIN_INDEX, countOfPerson - 1) .forEach(idx -> horizontalLines.add(createPoint(idx, horizontalLines))); - return horizontalLines; } private static Boolean createPoint(int idx, List horizontalLines) { if (BEGIN_INDEX == idx) { - return EMPTY_POINT; + return isCurrPointNonEmpty(); } return isPrevPointEmpty(idx, horizontalLines) && isCurrPointNonEmpty(); } diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index 80c4915b86..a9912babe3 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -12,17 +12,11 @@ public Match(Ladder ladder) { this.ladder = ladder; } -// public String result(String personName) { -// List people = ladder.people().value(); -// List lines = ladder.lines().value(); -// List executeResults = ladder.executeResults().value(); -// -// int peopleCnt = people.size(); -// int height = lines.size(); -// -// -// return ""; -// } + public int resultIdx(int input) { + String[][] result = ladder.result(); + + return 0; + } public Map value() { return results; diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index e3086891c8..25a78c610f 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -1,6 +1,9 @@ package nextstep.ladder.view; -import nextstep.ladder.domain.*; +import nextstep.ladder.domain.ExecuteResult; +import nextstep.ladder.domain.InputOutput; +import nextstep.ladder.domain.Ladder; +import nextstep.ladder.domain.Person; import java.util.List; import java.util.stream.Collectors; @@ -61,21 +64,43 @@ private static void printLines(InputOutput inputOutput, Ladder ladder) { } private static String makeLines(InputOutput inputOutput, Ladder ladder) { - People people = inputOutput.people(); - Lines lines = ladder.lines(); - - return lines.value().stream() - .map(line -> { - StringBuilder sb = new StringBuilder(); - IntStream.range(BEGIN_INDEX, line.value().size()) - .mapToObj(idx -> generatePointString(people, line, idx) - ) - .forEach(sb::append); - return sb.toString(); - }) + int firstPersonNameLength = inputOutput.people().firstPersonNameLength(); + String[][] result = ladder.result(); + + return IntStream.range(BEGIN_INDEX, result.length) + .mapToObj(i -> IntStream.range(BEGIN_INDEX, result[BEGIN_INDEX].length) + .mapToObj(j -> { + if (BEGIN_INDEX == j) { + return generateSpace(firstPersonNameLength); + } + return generateLine(result[i][j]); + }) + .collect(Collectors.joining())) .collect(Collectors.joining("\n")); } + private static String generateSpace(int firstPersonNameLength) { + return " ".repeat(firstPersonNameLength) + "|"; + } + + private static String generateLine(String result) { + if (isVerticalLine(result)) { + return "|"; + } + if (isHorizontalLine(result)) { + return "-----"; + } + return " "; + } + + private static boolean isVerticalLine(String result) { + return "v".equals(result); + } + + private static boolean isHorizontalLine(String result) { + return "h".equals(result); + } + private static void printExecuteResults(InputOutput inputOutput) { System.out.println(makeExecuteResults(inputOutput)); } @@ -101,12 +126,5 @@ private static String makeExecuteResults(InputOutput inputOutput) { }) .collect(Collectors.joining()); } - - private static String generatePointString(People people, Line line, int idx) { - if (BEGIN_INDEX == idx) { - return " ".repeat(people.firstPersonNameLength()) + "|"; - } - return line.value().get(idx) ? "-----|" : " |"; - } } diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 602c5eddd6..ff27b7b897 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -28,21 +28,21 @@ void setup() { new Line(Arrays.asList(true, false, true)) )); - executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); +// executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); ladder = new Ladder(verticalLineSize, lines); } @ParameterizedTest - @CsvSource(value = {"pobi:꽝1", "honux:3000", "crong:꽝2", "jk:5000"}, delimiter = ':') - void 특정인의_결과_확인(String input, String output) { + @CsvSource(value = {"0:0", "1:3", "2:2", "3:1"}, delimiter = ':') + void result_메서드(int input, int output) { //given Match match = new Match(ladder); //when - String result = match.result(input); + int resultIdx = match.resultIdx(input); //then - assertThat(result).isEqualTo(output); + assertThat(resultIdx).isEqualTo(output); } } From 85628d90b202cc14defd8b3bb804a5af6c968eca Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 01:36:32 +0900 Subject: [PATCH 12/21] =?UTF-8?q?refactor:=20=EA=B3=B5=EB=B0=B1=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/view/ResultView.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 25a78c610f..1766ca7b77 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -51,10 +51,7 @@ private static String makeNames(InputOutput inputOutput) { if (BEGIN_INDEX == i) { return name; } - StringBuilder sb = new StringBuilder(); - sb.append(" ".repeat(NAME_SPACE - name.length())); - sb.append(name); - return sb.toString(); + return generateSpace(name) + name; }) .collect(Collectors.joining()); } @@ -71,7 +68,7 @@ private static String makeLines(InputOutput inputOutput, Ladder ladder) { .mapToObj(i -> IntStream.range(BEGIN_INDEX, result[BEGIN_INDEX].length) .mapToObj(j -> { if (BEGIN_INDEX == j) { - return generateSpace(firstPersonNameLength); + return generateSpace(firstPersonNameLength) + "|"; } return generateLine(result[i][j]); }) @@ -80,7 +77,7 @@ private static String makeLines(InputOutput inputOutput, Ladder ladder) { } private static String generateSpace(int firstPersonNameLength) { - return " ".repeat(firstPersonNameLength) + "|"; + return " ".repeat(firstPersonNameLength); } private static String generateLine(String result) { @@ -114,17 +111,23 @@ private static String makeExecuteResults(InputOutput inputOutput) { String executeName = executeResults.get(i).name(); String peopleName = people.get(i).name(); - StringBuilder sb = new StringBuilder(); if (BEGIN_INDEX == i) { - sb.append(" ".repeat(Math.abs(peopleName.length() - executeName.length()))); - sb.append(executeName); - return sb.toString(); + return generateSpace(executeName, peopleName) + executeName; } - sb.append(" ".repeat(NAME_SPACE - executeName.length())); - sb.append(executeName); - return sb.toString(); + return generateSpace(executeName) + executeName; }) .collect(Collectors.joining()); } + + private static String generateSpace(String executeName, String peopleName) { + if (peopleName.length() - executeName.length() > 0) { + return " ".repeat(peopleName.length() - executeName.length()); + } + return ""; + } + + private static String generateSpace(String executeName) { + return " ".repeat(NAME_SPACE - executeName.length()); + } } From 9cfc31558bd026d537590771db4baed2bfb09ecf Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 02:14:29 +0900 Subject: [PATCH 13/21] =?UTF-8?q?feat:=20=EC=8B=9C=EC=9E=91=EC=9D=B8?= =?UTF-8?q?=EB=8D=B1=EC=8A=A4=EB=A5=BC=20=EB=84=A3=EC=97=88=EC=9D=84=20?= =?UTF-8?q?=EB=95=8C=20=EB=8F=84=EC=B0=A9=EC=9D=B8=EB=8D=B1=EC=8A=A4?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Match.java | 27 ++++++++++++++++--- .../ladder/domain/MatchResultTest.java | 4 +-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index a9912babe3..0925e4cf2d 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -2,9 +2,11 @@ import java.util.HashMap; import java.util.Map; +import java.util.stream.IntStream; public class Match { + private static final int BEGIN_IDX = 0; private final Ladder ladder; private Map results = new HashMap<>(); @@ -13,9 +15,28 @@ public Match(Ladder ladder) { } public int resultIdx(int input) { - String[][] result = ladder.result(); - - return 0; + + return findDestinationIdx(input); + } + + int findDestinationIdx(int startIdx) { + int currentIdx = startIdx * 2; + String[][] ladder = this.ladder.result(); + int rowCount = ladder.length; + int columnLength = ladder[BEGIN_IDX].length; + + currentIdx = IntStream.range(BEGIN_IDX, rowCount) + .reduce(currentIdx, (idx, i) -> { + String[] row = ladder[i]; + if (idx > BEGIN_IDX && "h".equals(row[idx - 1])) { + return idx - 2; + } + if (idx < columnLength - 1 && "h".equals(row[idx + 1])) { + return idx + 2; + } + return idx; + }); + return currentIdx / 2; } public Map value() { diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index ff27b7b897..1ec31841b4 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -35,12 +35,12 @@ void setup() { @ParameterizedTest @CsvSource(value = {"0:0", "1:3", "2:2", "3:1"}, delimiter = ':') - void result_메서드(int input, int output) { + void findDestinationIndex(int input, int output) { //given Match match = new Match(ladder); //when - int resultIdx = match.resultIdx(input); + int resultIdx = match.findDestinationIdx(input); //then assertThat(resultIdx).isEqualTo(output); From c457816fd0f9e528b745ba722c6355790e9b0a9f Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 02:17:20 +0900 Subject: [PATCH 14/21] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/domain/Match.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index 0925e4cf2d..f9b2485680 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -28,10 +28,10 @@ int findDestinationIdx(int startIdx) { currentIdx = IntStream.range(BEGIN_IDX, rowCount) .reduce(currentIdx, (idx, i) -> { String[] row = ladder[i]; - if (idx > BEGIN_IDX && "h".equals(row[idx - 1])) { + if (isMovableToLeft(idx, row)) { return idx - 2; } - if (idx < columnLength - 1 && "h".equals(row[idx + 1])) { + if (isMovableToRight(idx, row, columnLength)) { return idx + 2; } return idx; @@ -39,6 +39,14 @@ int findDestinationIdx(int startIdx) { return currentIdx / 2; } + private static boolean isMovableToLeft(int idx, String[] row) { + return idx > BEGIN_IDX && "h".equals(row[idx - 1]); + } + + private static boolean isMovableToRight(int idx, String[] row, int columnLength) { + return idx < columnLength - 1 && "h".equals(row[idx + 1]); + } + public Map value() { return results; } From 4fd74e11baf2cb2ca4a6baeb210edfcaa16244e8 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 02:30:30 +0900 Subject: [PATCH 15/21] =?UTF-8?q?test:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Match.java | 19 +++++++++---- .../ladder/domain/MatchResultTest.java | 27 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index f9b2485680..cc8515b4d0 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -7,16 +7,25 @@ public class Match { private static final int BEGIN_IDX = 0; + + private final InputOutput inputOutput; private final Ladder ladder; private Map results = new HashMap<>(); - public Match(Ladder ladder) { + public Match(InputOutput inputOutput, Ladder ladder) { + this.inputOutput = inputOutput; this.ladder = ladder; } - public int resultIdx(int input) { + public Match(Ladder ladder) { + this(null, ladder); + } + + public String result(String input) { + People people = inputOutput.people(); + ExecuteResults executeResults = inputOutput.executeResults(); - return findDestinationIdx(input); + return ""; } int findDestinationIdx(int startIdx) { @@ -39,11 +48,11 @@ int findDestinationIdx(int startIdx) { return currentIdx / 2; } - private static boolean isMovableToLeft(int idx, String[] row) { + private boolean isMovableToLeft(int idx, String[] row) { return idx > BEGIN_IDX && "h".equals(row[idx - 1]); } - private static boolean isMovableToRight(int idx, String[] row, int columnLength) { + private boolean isMovableToRight(int idx, String[] row, int columnLength) { return idx < columnLength - 1 && "h".equals(row[idx + 1]); } diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 1ec31841b4..2e745271d0 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -14,11 +14,13 @@ class MatchResultTest { private Lines lines; private ExecuteResults executeResults; private Ladder ladder; + private InputOutput inputOutput; @BeforeEach void setup() { people = new People(new String[]{"pobi", "honux", "crong", "jk"}); - int verticalLineSize = people.value().size(); + executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); + inputOutput = new InputOutput(people, executeResults); lines = new Lines(Arrays.asList( new Line(Arrays.asList(true, false, true)), @@ -27,22 +29,33 @@ void setup() { new Line(Arrays.asList(false, true, false)), new Line(Arrays.asList(true, false, true)) )); - -// executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); - + int verticalLineSize = people.value().size(); ladder = new Ladder(verticalLineSize, lines); } @ParameterizedTest @CsvSource(value = {"0:0", "1:3", "2:2", "3:1"}, delimiter = ':') - void findDestinationIndex(int input, int output) { + void findDestinationIndex(int startIdx, int destIdx) { //given Match match = new Match(ladder); //when - int resultIdx = match.findDestinationIdx(input); + int resultIdx = match.findDestinationIdx(startIdx); + + //then + assertThat(resultIdx).isEqualTo(destIdx); + } + + @ParameterizedTest + @CsvSource(value = {"pobi:꽝", "honux:3000", "crong:꽝", "jk:5000"}, delimiter = ':') + void 사다리_결과(String input, String output) { + //given + Match match = new Match(inputOutput, ladder); + + //when + String result = match.result(input); //then - assertThat(resultIdx).isEqualTo(output); + assertThat(result).isEqualTo(output); } } From 35046c790dd9ef93ebcdf70826391f9eabbd23bf Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 03:08:19 +0900 Subject: [PATCH 16/21] =?UTF-8?q?feat:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/domain/Match.java | 23 +++++++++++-------- .../java/nextstep/ladder/domain/Person.java | 15 ++++++++++++ .../ladder/domain/MatchResultTest.java | 8 +++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index cc8515b4d0..9e88412e2e 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -1,7 +1,8 @@ package nextstep.ladder.domain; -import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import java.util.stream.IntStream; public class Match { @@ -10,7 +11,6 @@ public class Match { private final InputOutput inputOutput; private final Ladder ladder; - private Map results = new HashMap<>(); public Match(InputOutput inputOutput, Ladder ladder) { this.inputOutput = inputOutput; @@ -22,13 +22,20 @@ public Match(Ladder ladder) { } public String result(String input) { - People people = inputOutput.people(); - ExecuteResults executeResults = inputOutput.executeResults(); + Map result = makeResult(); + return result.get(input); + } + + private Map makeResult() { + List people = inputOutput.people().value(); + List executeResults = inputOutput.executeResults().value(); - return ""; + return IntStream.range(BEGIN_IDX, people.size()) + .boxed() + .collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name())); } - int findDestinationIdx(int startIdx) { + int findOutputIdx(int startIdx) { int currentIdx = startIdx * 2; String[][] ladder = this.ladder.result(); int rowCount = ladder.length; @@ -55,8 +62,4 @@ private boolean isMovableToLeft(int idx, String[] row) { private boolean isMovableToRight(int idx, String[] row, int columnLength) { return idx < columnLength - 1 && "h".equals(row[idx + 1]); } - - public Map value() { - return results; - } } diff --git a/src/main/java/nextstep/ladder/domain/Person.java b/src/main/java/nextstep/ladder/domain/Person.java index 00c8814058..c9705f123a 100644 --- a/src/main/java/nextstep/ladder/domain/Person.java +++ b/src/main/java/nextstep/ladder/domain/Person.java @@ -1,5 +1,7 @@ package nextstep.ladder.domain; +import java.util.Objects; + public class Person { private static final int NAME_LENGTH_MAX = 5; @@ -17,6 +19,19 @@ public String name() { return this.name; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + @Override public String toString() { return "Person{" + diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 2e745271d0..91841ced30 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -35,19 +35,19 @@ void setup() { @ParameterizedTest @CsvSource(value = {"0:0", "1:3", "2:2", "3:1"}, delimiter = ':') - void findDestinationIndex(int startIdx, int destIdx) { + void findOutputIdx(int inputIdx, int outputIdx) { //given Match match = new Match(ladder); //when - int resultIdx = match.findDestinationIdx(startIdx); + int resultIdx = match.findOutputIdx(inputIdx); //then - assertThat(resultIdx).isEqualTo(destIdx); + assertThat(resultIdx).isEqualTo(outputIdx); } @ParameterizedTest - @CsvSource(value = {"pobi:꽝", "honux:3000", "crong:꽝", "jk:5000"}, delimiter = ':') + @CsvSource(value = {"pobi:꽝1", "honux:3000", "crong:꽝2", "jk:5000"}, delimiter = ':') void 사다리_결과(String input, String output) { //given Match match = new Match(inputOutput, ladder); From 110379122fb11b9aeb79672a8e1a3c9e450ecb54 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 04:01:53 +0900 Subject: [PATCH 17/21] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=82=AC=EB=9E=8C=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/Main.java | 8 +++-- src/main/java/nextstep/ladder/README.md | 7 +++-- .../java/nextstep/ladder/domain/Match.java | 14 +++------ .../java/nextstep/ladder/domain/Result.java | 16 ++++++++++ .../java/nextstep/ladder/view/InputView.java | 5 +++ .../java/nextstep/ladder/view/ResultView.java | 31 +++++++++++++------ .../ladder/domain/MatchResultTest.java | 4 +-- 7 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/main/java/nextstep/ladder/domain/Result.java diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index 5f8178a84d..e5c60c47b9 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -17,11 +17,13 @@ public static void main(String[] args) { Ladder ladder = new Ladder(verticalLineCount, height); - ResultView.printResult(inputOutput, ladder); + ResultView.printLadder(inputOutput, ladder); - Match match = new Match(ladder); + Match match = new Match(inputOutput, ladder); + Result result = match.makeResult(); + String person = inputView.person(); -// matchResult.result("pobi"); + ResultView.printResult(result, person); inputView.close(); } diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index a286d9e259..ca27b4f27a 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -20,9 +20,10 @@ -[x] 실행 결과 입력 추가 (,로 구분) - [x] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 -[x] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 --[ ] 결과를 보고 싶은 사람 입력 +-[x] 사다리 결과 로직 구현 +-[x] 결과를 보고 싶은 사람 입력 - all 아닐 경우 -- [ ] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 +- [x] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 - [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 - all 일 경우 -- [ ] 모든 사람의 실행 결과 출력 +- [x] 모든 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java index 9e88412e2e..df779ccd1b 100644 --- a/src/main/java/nextstep/ladder/domain/Match.java +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -1,7 +1,7 @@ package nextstep.ladder.domain; +import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -21,18 +21,14 @@ public Match(Ladder ladder) { this(null, ladder); } - public String result(String input) { - Map result = makeResult(); - return result.get(input); - } - - private Map makeResult() { + public Result makeResult() { List people = inputOutput.people().value(); List executeResults = inputOutput.executeResults().value(); - return IntStream.range(BEGIN_IDX, people.size()) + return new Result(IntStream.range(BEGIN_IDX, people.size()) .boxed() - .collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name())); + .collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name(), + (v1, v2) -> v1, LinkedHashMap::new))); } int findOutputIdx(int startIdx) { diff --git a/src/main/java/nextstep/ladder/domain/Result.java b/src/main/java/nextstep/ladder/domain/Result.java new file mode 100644 index 0000000000..972ee9a5cf --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/Result.java @@ -0,0 +1,16 @@ +package nextstep.ladder.domain; + +import java.util.Map; + +public class Result { + + private final Map result; + + public Result(Map result) { + this.result = result; + } + + public Map value() { + return this.result; + } +} diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index 0cff6f7d6a..e5daec952c 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -24,6 +24,11 @@ public int height() { return Integer.parseInt(nextLine()); } + public String person() { + ResultView.printPersonInputCommand(); + return nextLine(); + } + private String nextLine() { return scanner.nextLine(); } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 1766ca7b77..b8810ebfb2 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -1,9 +1,6 @@ package nextstep.ladder.view; -import nextstep.ladder.domain.ExecuteResult; -import nextstep.ladder.domain.InputOutput; -import nextstep.ladder.domain.Ladder; -import nextstep.ladder.domain.Person; +import nextstep.ladder.domain.*; import java.util.List; import java.util.stream.Collectors; @@ -26,11 +23,7 @@ static void printHeightInputCommand() { System.out.println("\n최대 사다리 높이는 몇 개인가요?"); } - public static void printResult(InputOutput inputOutput, Ladder ladder) { - printLadder(inputOutput, ladder); - } - - private static void printLadder(InputOutput inputOutput, Ladder ladder) { + public static void printLadder(InputOutput inputOutput, Ladder ladder) { System.out.println("\n사다리 결과\n"); printNames(inputOutput); printLines(inputOutput, ladder); @@ -129,5 +122,25 @@ private static String generateSpace(String executeName, String peopleName) { private static String generateSpace(String executeName) { return " ".repeat(NAME_SPACE - executeName.length()); } + + public static void printPersonInputCommand() { + System.out.println("\n결과를 보고 싶은 사람은?"); + } + + public static void printResult(Result result, String person) { + printResultCommand(); + + if ("all".equals(person)) { + result.value().entrySet().stream() + .forEachOrdered(entry -> System.out.println(entry.getKey() + " : " + entry.getValue())); + return; + } + System.out.println(result.value().get(person)); + + } + + private static void printResultCommand() { + System.out.println("\n실행 결과"); + } } diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java index 91841ced30..bafeea8ced 100644 --- a/src/test/java/nextstep/ladder/domain/MatchResultTest.java +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -53,9 +53,9 @@ void findOutputIdx(int inputIdx, int outputIdx) { Match match = new Match(inputOutput, ladder); //when - String result = match.result(input); + Result result = match.makeResult(); //then - assertThat(result).isEqualTo(output); + assertThat(result.value().get(input)).isEqualTo(output); } } From b91f2b28adb9b405112ffeffc672af51296ede03 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 04:09:41 +0900 Subject: [PATCH 18/21] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=82=AC=EB=9E=8C=20=EC=9D=B4=EB=A6=84=EC=9D=B4=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=ED=95=A0=20=EC=82=AC=EB=9E=8C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=AA=A9=EB=A1=9D=EC=97=90=20=EC=97=86=EC=9D=84=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/README.md | 2 +- src/main/java/nextstep/ladder/domain/Result.java | 7 +++++++ src/main/java/nextstep/ladder/view/ResultView.java | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index ca27b4f27a..e0ce62edc4 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -24,6 +24,6 @@ -[x] 결과를 보고 싶은 사람 입력 - all 아닐 경우 - [x] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 -- [ ] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 +- [x] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 - all 일 경우 - [x] 모든 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/domain/Result.java b/src/main/java/nextstep/ladder/domain/Result.java index 972ee9a5cf..85424b55f8 100644 --- a/src/main/java/nextstep/ladder/domain/Result.java +++ b/src/main/java/nextstep/ladder/domain/Result.java @@ -10,6 +10,13 @@ public Result(Map result) { this.result = result; } + public String get(String person) { + if (result.get(person) == null) { + throw new IllegalArgumentException("해당하는 사람이 존재하지 않습니다."); + } + return result.get(person); + } + public Map value() { return this.result; } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index b8810ebfb2..bdb38b20ff 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -135,7 +135,7 @@ public static void printResult(Result result, String person) { .forEachOrdered(entry -> System.out.println(entry.getKey() + " : " + entry.getValue())); return; } - System.out.println(result.value().get(person)); + System.out.println(result.get(person)); } From 74beb3796f667306a1843cf8d9d1ae8e2c0ced73 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 04:15:54 +0900 Subject: [PATCH 19/21] =?UTF-8?q?test:=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=82=AC=EB=9E=8C=20=EC=9D=B4=EB=A6=84=EC=9D=B4=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=ED=95=A0=20=EC=82=AC=EB=9E=8C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=AA=A9=EB=A1=9D=EC=97=90=20=EC=97=86=EC=9D=84=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nextstep/ladder/domain/ResultTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/nextstep/ladder/domain/ResultTest.java diff --git a/src/test/java/nextstep/ladder/domain/ResultTest.java b/src/test/java/nextstep/ladder/domain/ResultTest.java new file mode 100644 index 0000000000..9ecb6db0c8 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ResultTest.java @@ -0,0 +1,19 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class ResultTest { + + @Test + void 입력받은_이름이_참여할_이름목록에_없을경우_예외처리() { + Result result = new Result(Map.of("pobi", "꽝")); + + assertThatIllegalArgumentException() + .isThrownBy(() -> result.get("crong")) + .withMessageContaining("해당하는 사람이 존재하지 않습니다."); + } +} From 42f3c435a206010d2088c670b13e65b3f959794d Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 04:19:05 +0900 Subject: [PATCH 20/21] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/nextstep/ladder/domain/LineTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/nextstep/ladder/domain/LineTest.java b/src/test/java/nextstep/ladder/domain/LineTest.java index eda684b019..1eb079e88e 100644 --- a/src/test/java/nextstep/ladder/domain/LineTest.java +++ b/src/test/java/nextstep/ladder/domain/LineTest.java @@ -18,11 +18,11 @@ void setup() { @Test void create() { - assertThat(line.value().size()).isEqualTo(4); + assertThat(line.value().size()).isEqualTo(3); } @ParameterizedTest - @ValueSource(ints = {1, 2, 3}) + @ValueSource(ints = {1, 2}) void 라인이_겹치지_않는지_확인(int idx) { Boolean prevPoint = line.value().get(idx - 1); Boolean currPoint = line.value().get(idx); From 5aea879b20e8af6952c91085bc8c0ef5cb00a0c1 Mon Sep 17 00:00:00 2001 From: hoyeoon Date: Tue, 30 May 2023 04:27:25 +0900 Subject: [PATCH 21/21] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nextstep/ladder/domain/LadderTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/nextstep/ladder/domain/LadderTest.java b/src/test/java/nextstep/ladder/domain/LadderTest.java index 39f5f19976..5f623e0622 100644 --- a/src/test/java/nextstep/ladder/domain/LadderTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderTest.java @@ -5,6 +5,8 @@ import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class LadderTest { @@ -35,6 +37,20 @@ void create() { String[][] result = ladder.result(); //then - System.out.println(Arrays.deepToString(result)); + assertTrue(arrayContainsValue(result, "v")); + assertTrue(arrayContainsValue(result, "h")); + assertTrue(arrayContainsValue(result, null)); + assertFalse(arrayContainsValue(result, "x")); + } + + private boolean arrayContainsValue(String[][] array, String value) { + return Arrays.stream(array) + .flatMap(Arrays::stream) + .anyMatch(element -> { + if (value == null) { + return element == null; + } + return value.equals(element); + }); } }