From d2165e2c9be04c2aa59fc00e0f8af92317ee50ad Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 25 Oct 2022 20:27:59 +0900 Subject: [PATCH 01/10] =?UTF-8?q?docs=20:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EB=B0=8F=20=EC=9A=94=EA=B5=AC=20=EC=82=AC=ED=95=AD?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b853592f59..3fd044ef28 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,17 @@ * 코드 리뷰 피드백에 대한 개선 작업을 하고 다시 PUSH한다. * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. +## 기능 목록 +- [ ] 쉼표로 구분된 참여할 사람을 입력 받는다. (단, 이름은 최대 5자까지 부여) +- [ ] 최대 사다리 높이 개수를 입력 받는다. +- [ ] 입력 받은 사람과 사다리 높이를 이용해 사다리를 출력한다. + +## 기능 요구 사항 +- 사다리 게임에 참여하는 사람에 이름을 최대5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다. +- 사람 이름은 쉼표(,)를 기준으로 구분한다. +- 사람 이름을 5자 기준으로 출력하기 때문에 사다리 폭도 넓어져야 한다. +- 사다리 타기가 정상적으로 동작하려면 라인이 겹치지 않도록 해야 한다. + - |-----|-----| 모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할지 결정할 수 없다. + ## 온라인 코드 리뷰 과정 -* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview) \ No newline at end of file +* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview) From b1c82f5fd1190b9b1cd2fb638e29f784061bb625 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 25 Oct 2022 20:53:28 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feature=20:=20=EC=89=BC=ED=91=9C=EB=A1=9C?= =?UTF-8?q?=20=EA=B5=AC=EB=B6=84=EB=90=9C=20=EC=82=AC=EB=9E=8C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=EC=9D=84=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EA=B3=A0=20?= =?UTF-8?q?Request=20=EB=AA=A8=EB=8D=B8=EB=A1=9C=20=EA=B5=AC=EC=84=B1?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladderapplication/LadderApplication.java | 14 ++++++++++++++ .../java/ladderapplication/models/Player.java | 16 ++++++++++++++++ .../models/requests/PlayerRequest.java | 18 ++++++++++++++++++ .../ladderapplication/ui/InputScanner.java | 17 +++++++++++++++++ .../java/ladderapplication/ui/Printer.java | 18 ++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 src/main/java/ladderapplication/LadderApplication.java create mode 100644 src/main/java/ladderapplication/models/Player.java create mode 100644 src/main/java/ladderapplication/models/requests/PlayerRequest.java create mode 100644 src/main/java/ladderapplication/ui/InputScanner.java create mode 100644 src/main/java/ladderapplication/ui/Printer.java diff --git a/src/main/java/ladderapplication/LadderApplication.java b/src/main/java/ladderapplication/LadderApplication.java new file mode 100644 index 0000000000..06fb1a4284 --- /dev/null +++ b/src/main/java/ladderapplication/LadderApplication.java @@ -0,0 +1,14 @@ +package ladderapplication; + +import ladderapplication.models.requests.PlayerRequest; +import ladderapplication.ui.Printer; + +import java.util.List; + +public class LadderApplication { + + public static void main(String[] args) { + List playerRequests = Printer.requestPlayer(); + } + +} diff --git a/src/main/java/ladderapplication/models/Player.java b/src/main/java/ladderapplication/models/Player.java new file mode 100644 index 0000000000..d4d0de8daa --- /dev/null +++ b/src/main/java/ladderapplication/models/Player.java @@ -0,0 +1,16 @@ +package ladderapplication.models; + +import ladderapplication.models.requests.PlayerRequest; + +public class Player { + + private final String name; + + private Player(String name) { + this.name = name; + } + + public static Player from(PlayerRequest playerRequest) { + return new Player(playerRequest.getName()); + } +} diff --git a/src/main/java/ladderapplication/models/requests/PlayerRequest.java b/src/main/java/ladderapplication/models/requests/PlayerRequest.java new file mode 100644 index 0000000000..a858183f5a --- /dev/null +++ b/src/main/java/ladderapplication/models/requests/PlayerRequest.java @@ -0,0 +1,18 @@ +package ladderapplication.models.requests; + +public class PlayerRequest { + + private final String name; + + private PlayerRequest(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static PlayerRequest of(String name) { + return new PlayerRequest(name); + } +} diff --git a/src/main/java/ladderapplication/ui/InputScanner.java b/src/main/java/ladderapplication/ui/InputScanner.java new file mode 100644 index 0000000000..f1cc554b2a --- /dev/null +++ b/src/main/java/ladderapplication/ui/InputScanner.java @@ -0,0 +1,17 @@ +package ladderapplication.ui; + +import java.util.Scanner; + +public class InputScanner { + + private static final Scanner SCANNER = new Scanner(System.in); + + public static String stringScan() { + return SCANNER.nextLine(); + } + + public static int intScan() { + return SCANNER.nextInt(); + } + +} diff --git a/src/main/java/ladderapplication/ui/Printer.java b/src/main/java/ladderapplication/ui/Printer.java new file mode 100644 index 0000000000..da73cd6226 --- /dev/null +++ b/src/main/java/ladderapplication/ui/Printer.java @@ -0,0 +1,18 @@ +package ladderapplication.ui; + +import ladderapplication.models.requests.PlayerRequest; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Printer { + + public static List requestPlayer() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String players = InputScanner.stringScan(); + + String[] playerArr = players.split(","); + return Stream.of(playerArr).map(PlayerRequest::of).collect(Collectors.toList()); + } +} From 1ff12a8735ff14423843411c1d631f88e833c0cf Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 25 Oct 2022 21:02:02 +0900 Subject: [PATCH 03/10] =?UTF-8?q?test=20:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=9D=B4=EB=A6=84=EC=9D=80=205=EA=B8=80=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EB=84=98=EC=A7=80=EC=95=8A=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20=EB=B2=A8=EB=A6=AC?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=9D=98=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/requests/LadderRequest.java | 19 ++++++++++ .../validator/PlayerRequestValidator.java | 10 ++++++ .../validator/PlayerRequestValidatorTest.java | 35 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/main/java/ladderapplication/models/requests/LadderRequest.java create mode 100644 src/main/java/ladderapplication/validator/PlayerRequestValidator.java create mode 100644 src/test/java/ladderapplication/validator/PlayerRequestValidatorTest.java diff --git a/src/main/java/ladderapplication/models/requests/LadderRequest.java b/src/main/java/ladderapplication/models/requests/LadderRequest.java new file mode 100644 index 0000000000..b42be4f1c1 --- /dev/null +++ b/src/main/java/ladderapplication/models/requests/LadderRequest.java @@ -0,0 +1,19 @@ +package ladderapplication.models.requests; + +public class LadderRequest { + + private final int height; + + private LadderRequest(int height) { + this.height = height; + } + + public int getHeight() { + return height; + } + + public static LadderRequest of(int height) { + return new LadderRequest(height); + } + +} diff --git a/src/main/java/ladderapplication/validator/PlayerRequestValidator.java b/src/main/java/ladderapplication/validator/PlayerRequestValidator.java new file mode 100644 index 0000000000..a88b0509b6 --- /dev/null +++ b/src/main/java/ladderapplication/validator/PlayerRequestValidator.java @@ -0,0 +1,10 @@ +package ladderapplication.validator; + +import ladderapplication.models.requests.PlayerRequest; + +public class PlayerRequestValidator { + + public static void validation(PlayerRequest playerRequest) { + } + +} diff --git a/src/test/java/ladderapplication/validator/PlayerRequestValidatorTest.java b/src/test/java/ladderapplication/validator/PlayerRequestValidatorTest.java new file mode 100644 index 0000000000..398605b74d --- /dev/null +++ b/src/test/java/ladderapplication/validator/PlayerRequestValidatorTest.java @@ -0,0 +1,35 @@ +package ladderapplication.validator; + +import ladderapplication.models.requests.PlayerRequest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class PlayerRequestValidatorTest { + + private static final String WRONG_NAME = "플레이어 이름음 5글자 이하 여야 합니다."; + private static final List playerRequests = new ArrayList<>(); + + @BeforeAll + public static void setPlayerRequest() { + String players = "pobi,honux,crong,jk"; + String[] playerArr = players.split(","); + Stream.of(playerArr).forEach(name -> playerRequests.add(PlayerRequest.of(name))); + } + + @Test + @DisplayName("입력받은 사용자 이름이 5글자가 넘으면 Exception이 발생한다.") + void test1() { + PlayerRequest playerRequest = PlayerRequest.of("robinson"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> PlayerRequestValidator.validation(playerRequest)); + assertThat(exception.getMessage()).isEqualTo(WRONG_NAME); + + } +} From 23a65d22a81b8f9f31d8b1ddbe3c8af0f22ccfc1 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 25 Oct 2022 21:03:59 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feature=20:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=82=AC=EB=9E=8C=EC=9D=98=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=EC=9D=80=205=EA=B8=80=EC=9E=90=EB=A5=BC=20=EB=84=98=EC=A7=80?= =?UTF-8?q?=20=EC=95=8A=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../ladderapplication/validator/PlayerRequestValidator.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3fd044ef28..f3842fb366 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. ## 기능 목록 -- [ ] 쉼표로 구분된 참여할 사람을 입력 받는다. (단, 이름은 최대 5자까지 부여) +- [x] 쉼표로 구분된 참여할 사람을 입력 받는다. (단, 이름은 최대 5자까지 부여) - [ ] 최대 사다리 높이 개수를 입력 받는다. - [ ] 입력 받은 사람과 사다리 높이를 이용해 사다리를 출력한다. diff --git a/src/main/java/ladderapplication/validator/PlayerRequestValidator.java b/src/main/java/ladderapplication/validator/PlayerRequestValidator.java index a88b0509b6..fbdf59f872 100644 --- a/src/main/java/ladderapplication/validator/PlayerRequestValidator.java +++ b/src/main/java/ladderapplication/validator/PlayerRequestValidator.java @@ -4,7 +4,12 @@ public class PlayerRequestValidator { + private static final String WRONG_NAME = "플레이어 이름음 5글자 이하 여야 합니다."; + public static void validation(PlayerRequest playerRequest) { + if (playerRequest.getName().length() > 5) { + throw new IllegalArgumentException(WRONG_NAME); + } } } From 3e6c1320a42e089e744bb68afbcdc12917b93b1c Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 25 Oct 2022 21:05:58 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feature=20:=20=EC=B5=9C=EB=8C=80=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EB=86=92=EC=9D=B4=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EB=8A=94?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/ladderapplication/LadderApplication.java | 2 ++ src/main/java/ladderapplication/ui/Printer.java | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f3842fb366..dcc41239b5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## 기능 목록 - [x] 쉼표로 구분된 참여할 사람을 입력 받는다. (단, 이름은 최대 5자까지 부여) -- [ ] 최대 사다리 높이 개수를 입력 받는다. +- [x] 최대 사다리 높이 개수를 입력 받는다. - [ ] 입력 받은 사람과 사다리 높이를 이용해 사다리를 출력한다. ## 기능 요구 사항 diff --git a/src/main/java/ladderapplication/LadderApplication.java b/src/main/java/ladderapplication/LadderApplication.java index 06fb1a4284..9d0bb0309b 100644 --- a/src/main/java/ladderapplication/LadderApplication.java +++ b/src/main/java/ladderapplication/LadderApplication.java @@ -1,5 +1,6 @@ package ladderapplication; +import ladderapplication.models.requests.LadderRequest; import ladderapplication.models.requests.PlayerRequest; import ladderapplication.ui.Printer; @@ -9,6 +10,7 @@ public class LadderApplication { public static void main(String[] args) { List playerRequests = Printer.requestPlayer(); + LadderRequest ladderRequest = Printer.requestLadder(); } } diff --git a/src/main/java/ladderapplication/ui/Printer.java b/src/main/java/ladderapplication/ui/Printer.java index da73cd6226..b1a1b0ab62 100644 --- a/src/main/java/ladderapplication/ui/Printer.java +++ b/src/main/java/ladderapplication/ui/Printer.java @@ -1,5 +1,6 @@ package ladderapplication.ui; +import ladderapplication.models.requests.LadderRequest; import ladderapplication.models.requests.PlayerRequest; import java.util.List; @@ -15,4 +16,11 @@ public static List requestPlayer() { String[] playerArr = players.split(","); return Stream.of(playerArr).map(PlayerRequest::of).collect(Collectors.toList()); } + + public static LadderRequest requestLadder() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + int height = InputScanner.intScan(); + + return LadderRequest.of(height); + } } From 089fa54259148c821702fb6d1d0fe299b36bb085 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Wed, 26 Oct 2022 16:30:17 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feature=20:=20=EC=84=B8=EB=A1=9C=EC=84=A0?= =?UTF-8?q?=EB=A7=8C=20=EC=A1=B4=EC=9E=AC=ED=95=98=EB=8A=94=20=EC=82=AC?= =?UTF-8?q?=EB=8B=A4=EB=A6=AC=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladderapplication/LadderApplication.java | 7 +++ .../java/ladderapplication/models/Ladder.java | 52 +++++++++++++++++++ .../java/ladderapplication/models/Line.java | 40 ++++++++++++++ .../java/ladderapplication/models/Player.java | 4 ++ 4 files changed, 103 insertions(+) create mode 100644 src/main/java/ladderapplication/models/Ladder.java create mode 100644 src/main/java/ladderapplication/models/Line.java diff --git a/src/main/java/ladderapplication/LadderApplication.java b/src/main/java/ladderapplication/LadderApplication.java index 9d0bb0309b..ca891dc6d2 100644 --- a/src/main/java/ladderapplication/LadderApplication.java +++ b/src/main/java/ladderapplication/LadderApplication.java @@ -1,16 +1,23 @@ package ladderapplication; +import ladderapplication.models.Ladder; +import ladderapplication.models.Player; import ladderapplication.models.requests.LadderRequest; import ladderapplication.models.requests.PlayerRequest; import ladderapplication.ui.Printer; import java.util.List; +import java.util.stream.Collectors; public class LadderApplication { public static void main(String[] args) { List playerRequests = Printer.requestPlayer(); LadderRequest ladderRequest = Printer.requestLadder(); + + List players = playerRequests.stream().map(Player::from).collect(Collectors.toList()); + Ladder ladder = Ladder.of(ladderRequest, players); + ladder.print(); } } diff --git a/src/main/java/ladderapplication/models/Ladder.java b/src/main/java/ladderapplication/models/Ladder.java new file mode 100644 index 0000000000..2e18a22e98 --- /dev/null +++ b/src/main/java/ladderapplication/models/Ladder.java @@ -0,0 +1,52 @@ +package ladderapplication.models; + +import ladderapplication.models.requests.LadderRequest; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Ladder { + + private static final int STANDARD_SIZE = 4; + private final List players; + private final List lines; + private final int height; + + public Ladder(List players, List lines, int height) { + this.players = players; + this.lines = lines; + this.height = height; + } + + public static Ladder of(LadderRequest ladderRequest, List players) { + List newLines = Stream.generate(() -> Line.of(players.size())) + .limit(ladderRequest.getHeight()) + .collect(Collectors.toList()); + return new Ladder(players, newLines, ladderRequest.getHeight()); + } + + public void print() { + StringBuilder sb = new StringBuilder(); + players.forEach(player -> sb.append(DecoratingName(player.getName()))); + sb.append("\n"); + + lines.forEach(line -> sb.append(line.getLine()).append("\n")); + System.out.print(sb); + } + + private String DecoratingName(String name) { + StringBuilder sb = new StringBuilder(); + if (name.length() < 4) { + sb.append(getSpace(4 - name.length())); + } + sb.append(name); + sb.append(getSpace(sb.length())); + return sb.toString(); + } + + private String getSpace(int count) { + String space = " "; + return space.repeat(count); + } +} diff --git a/src/main/java/ladderapplication/models/Line.java b/src/main/java/ladderapplication/models/Line.java new file mode 100644 index 0000000000..77ea4f806e --- /dev/null +++ b/src/main/java/ladderapplication/models/Line.java @@ -0,0 +1,40 @@ +package ladderapplication.models; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Line { + private final List hasSteps; + + private Line(List hasSteps) { + this.hasSteps = hasSteps; + } + + public List getHasSteps() { + return hasSteps; + } + + public static Line of(int playerCount) { + List hasStep = Stream.generate(() -> Boolean.FALSE) + .limit(playerCount - 1) + .collect(Collectors.toList()); + return new Line(hasStep); + } + + public String getLine() { + StringBuilder sb = new StringBuilder(); + sb.append(" "); + hasSteps.forEach(hasStep -> { + if (hasStep) { + sb.append("|-----"); + return; + } + + sb.append("| "); + }); + + sb.append("|"); + return sb.toString(); + } +} diff --git a/src/main/java/ladderapplication/models/Player.java b/src/main/java/ladderapplication/models/Player.java index d4d0de8daa..7f6063033b 100644 --- a/src/main/java/ladderapplication/models/Player.java +++ b/src/main/java/ladderapplication/models/Player.java @@ -10,6 +10,10 @@ private Player(String name) { this.name = name; } + public String getName() { + return name; + } + public static Player from(PlayerRequest playerRequest) { return new Player(playerRequest.getName()); } From d2d9e98a15af94e8da9358ab7f590251875581e0 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 1 Nov 2022 17:23:57 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feature=20:=20=EA=B0=80=EB=A1=9C=EC=A4=84?= =?UTF-8?q?=EC=9D=B4=20=EC=A1=B4=EC=9E=AC=ED=95=98=EB=8A=94=20=EC=82=AC?= =?UTF-8?q?=EB=8B=A4=EB=A6=AC=20=EA=B7=B8=EB=A6=AC=EA=B8=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ladderapplication/models/Ladder.java | 4 ++-- .../java/ladderapplication/models/Line.java | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/ladderapplication/models/Ladder.java b/src/main/java/ladderapplication/models/Ladder.java index 2e18a22e98..6116087a05 100644 --- a/src/main/java/ladderapplication/models/Ladder.java +++ b/src/main/java/ladderapplication/models/Ladder.java @@ -37,8 +37,8 @@ public void print() { private String DecoratingName(String name) { StringBuilder sb = new StringBuilder(); - if (name.length() < 4) { - sb.append(getSpace(4 - name.length())); + if (name.length() < STANDARD_SIZE) { + sb.append(getSpace(STANDARD_SIZE - name.length())); } sb.append(name); sb.append(getSpace(sb.length())); diff --git a/src/main/java/ladderapplication/models/Line.java b/src/main/java/ladderapplication/models/Line.java index 77ea4f806e..6a55924d88 100644 --- a/src/main/java/ladderapplication/models/Line.java +++ b/src/main/java/ladderapplication/models/Line.java @@ -1,25 +1,36 @@ package ladderapplication.models; import java.util.List; +import java.util.Random; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; public class Line { + private static final Random RANDOM_GENERATOR = new Random(); + private static final int RANDOM_NUMBER_BOUND = 10; private final List hasSteps; private Line(List hasSteps) { this.hasSteps = hasSteps; } - public List getHasSteps() { - return hasSteps; - } - public static Line of(int playerCount) { - List hasStep = Stream.generate(() -> Boolean.FALSE) + List hasSteps = Stream.generate(() -> Boolean.FALSE) .limit(playerCount - 1) .collect(Collectors.toList()); - return new Line(hasStep); + + IntStream.range(0, hasSteps.size()) + .forEach(index -> { + if ((index == 0 || !hasSteps.get(index - 1)) && hasStep()) { + hasSteps.set(index, Boolean.TRUE); + } + }); + return new Line(hasSteps); + } + + private static boolean hasStep() { + return RANDOM_GENERATOR.nextInt(RANDOM_NUMBER_BOUND) >= 3; } public String getLine() { From 059a290efcf02158545088d9b23f6c39bb8fb974 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 1 Nov 2022 17:32:03 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor=20:=20=EC=82=AC=EB=8B=A4?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=9C=EB=A0=A5=20=EC=8B=9C=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=20=EA=B0=84=EA=B2=A9=20=EC=9E=98=EB=AA=BB=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EC=98=A4=EB=A5=98=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/ladderapplication/models/Ladder.java | 4 +++- src/main/java/ladderapplication/models/Line.java | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladderapplication/models/Ladder.java b/src/main/java/ladderapplication/models/Ladder.java index 6116087a05..2238615daa 100644 --- a/src/main/java/ladderapplication/models/Ladder.java +++ b/src/main/java/ladderapplication/models/Ladder.java @@ -8,6 +8,7 @@ public class Ladder { + private static final int NAME_SPACE = 6; private static final int STANDARD_SIZE = 4; private final List players; private final List lines; @@ -41,7 +42,8 @@ private String DecoratingName(String name) { sb.append(getSpace(STANDARD_SIZE - name.length())); } sb.append(name); - sb.append(getSpace(sb.length())); + sb.append(getSpace(NAME_SPACE - sb.length())); + System.out.println(sb); return sb.toString(); } diff --git a/src/main/java/ladderapplication/models/Line.java b/src/main/java/ladderapplication/models/Line.java index 6a55924d88..acf2e0af96 100644 --- a/src/main/java/ladderapplication/models/Line.java +++ b/src/main/java/ladderapplication/models/Line.java @@ -8,6 +8,7 @@ public class Line { private static final Random RANDOM_GENERATOR = new Random(); + private static final int HAS_STEP_BOUND = 4; private static final int RANDOM_NUMBER_BOUND = 10; private final List hasSteps; @@ -30,7 +31,7 @@ public static Line of(int playerCount) { } private static boolean hasStep() { - return RANDOM_GENERATOR.nextInt(RANDOM_NUMBER_BOUND) >= 3; + return RANDOM_GENERATOR.nextInt(RANDOM_NUMBER_BOUND) >= HAS_STEP_BOUND; } public String getLine() { From 967eb85913569a899bdead60fbc62f164635ded5 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 1 Nov 2022 17:47:40 +0900 Subject: [PATCH 09/10] =?UTF-8?q?refactor=20:=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EB=AA=A8=EB=8D=B8=20=ED=95=98=EB=82=98=EB=A1=9C=20=EB=B0=9B?= =?UTF-8?q?=EC=95=84=EC=84=9C=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EB=8F=84=EB=A1=9D=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladderapplication/LadderApplication.java | 13 +++------- .../java/ladderapplication/models/Ladder.java | 19 ++++++++------ .../models/requests/GameSettingRequest.java | 26 +++++++++++++++++++ .../java/ladderapplication/ui/Printer.java | 11 ++++---- 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/main/java/ladderapplication/models/requests/GameSettingRequest.java diff --git a/src/main/java/ladderapplication/LadderApplication.java b/src/main/java/ladderapplication/LadderApplication.java index ca891dc6d2..fa8245387a 100644 --- a/src/main/java/ladderapplication/LadderApplication.java +++ b/src/main/java/ladderapplication/LadderApplication.java @@ -1,22 +1,15 @@ package ladderapplication; import ladderapplication.models.Ladder; -import ladderapplication.models.Player; -import ladderapplication.models.requests.LadderRequest; -import ladderapplication.models.requests.PlayerRequest; +import ladderapplication.models.requests.GameSettingRequest; import ladderapplication.ui.Printer; -import java.util.List; -import java.util.stream.Collectors; - public class LadderApplication { public static void main(String[] args) { - List playerRequests = Printer.requestPlayer(); - LadderRequest ladderRequest = Printer.requestLadder(); + GameSettingRequest gameSettingRequest = Printer.requestGameSetting(); - List players = playerRequests.stream().map(Player::from).collect(Collectors.toList()); - Ladder ladder = Ladder.of(ladderRequest, players); + Ladder ladder = Ladder.of(gameSettingRequest); ladder.print(); } diff --git a/src/main/java/ladderapplication/models/Ladder.java b/src/main/java/ladderapplication/models/Ladder.java index 2238615daa..3afcf4916b 100644 --- a/src/main/java/ladderapplication/models/Ladder.java +++ b/src/main/java/ladderapplication/models/Ladder.java @@ -1,5 +1,6 @@ package ladderapplication.models; +import ladderapplication.models.requests.GameSettingRequest; import ladderapplication.models.requests.LadderRequest; import java.util.List; @@ -12,19 +13,22 @@ public class Ladder { private static final int STANDARD_SIZE = 4; private final List players; private final List lines; - private final int height; - public Ladder(List players, List lines, int height) { + public Ladder(List players, List lines) { this.players = players; this.lines = lines; - this.height = height; } - public static Ladder of(LadderRequest ladderRequest, List players) { - List newLines = Stream.generate(() -> Line.of(players.size())) - .limit(ladderRequest.getHeight()) + public static Ladder of(GameSettingRequest gameSettingRequest) { + List newPlayers = gameSettingRequest.getPlayerRequests() + .stream() + .map(Player::from) .collect(Collectors.toList()); - return new Ladder(players, newLines, ladderRequest.getHeight()); + + List newLines = Stream.generate(() -> Line.of(gameSettingRequest.getPlayerRequests().size())) + .limit(gameSettingRequest.getLadderRequest().getHeight()) + .collect(Collectors.toList()); + return new Ladder(newPlayers, newLines); } public void print() { @@ -43,7 +47,6 @@ private String DecoratingName(String name) { } sb.append(name); sb.append(getSpace(NAME_SPACE - sb.length())); - System.out.println(sb); return sb.toString(); } diff --git a/src/main/java/ladderapplication/models/requests/GameSettingRequest.java b/src/main/java/ladderapplication/models/requests/GameSettingRequest.java new file mode 100644 index 0000000000..7b6c589132 --- /dev/null +++ b/src/main/java/ladderapplication/models/requests/GameSettingRequest.java @@ -0,0 +1,26 @@ +package ladderapplication.models.requests; + +import java.util.List; + +public class GameSettingRequest { + + private final LadderRequest ladderRequest; + private final List playerRequests; + + private GameSettingRequest(LadderRequest ladderRequest, List playerRequests) { + this.ladderRequest = ladderRequest; + this.playerRequests = playerRequests; + } + + public LadderRequest getLadderRequest() { + return ladderRequest; + } + + public List getPlayerRequests() { + return playerRequests; + } + + public static GameSettingRequest of(LadderRequest ladderRequest, List playerRequests) { + return new GameSettingRequest(ladderRequest, playerRequests); + } +} diff --git a/src/main/java/ladderapplication/ui/Printer.java b/src/main/java/ladderapplication/ui/Printer.java index b1a1b0ab62..4b09d0f0f5 100644 --- a/src/main/java/ladderapplication/ui/Printer.java +++ b/src/main/java/ladderapplication/ui/Printer.java @@ -1,5 +1,6 @@ package ladderapplication.ui; +import ladderapplication.models.requests.GameSettingRequest; import ladderapplication.models.requests.LadderRequest; import ladderapplication.models.requests.PlayerRequest; @@ -9,18 +10,16 @@ public class Printer { - public static List requestPlayer() { + public static GameSettingRequest requestGameSetting() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); String players = InputScanner.stringScan(); - String[] playerArr = players.split(","); - return Stream.of(playerArr).map(PlayerRequest::of).collect(Collectors.toList()); - } + List playerRequests = Stream.of(playerArr).map(PlayerRequest::of).collect(Collectors.toList()); - public static LadderRequest requestLadder() { System.out.println("최대 사다리 높이는 몇 개인가요?"); int height = InputScanner.intScan(); + LadderRequest ladderRequest = LadderRequest.of(height); - return LadderRequest.of(height); + return GameSettingRequest.of(ladderRequest, playerRequests); } } From 2268c3f6016fef6b84b562fa12c83fda21e67b04 Mon Sep 17 00:00:00 2001 From: "robin.son" Date: Tue, 1 Nov 2022 18:56:33 +0900 Subject: [PATCH 10/10] =?UTF-8?q?refactor=20:=20DecoratingUtils=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EC=97=AC=20=EC=82=AC=EB=8B=A4?= =?UTF-8?q?=EB=A6=AC=20=EA=B7=B8=EB=A6=B4=20=EB=95=8C=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?,=20=EA=B7=B8=EB=A0=A4=EC=A7=84=20=EC=82=AC=EB=8B=A4=EB=A6=AC?= =?UTF-8?q?=EB=8A=94=20Ladder=20=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=EC=9D=B8=20drawing=EC=9D=B4=20=EA=B0=80=EC=A7=80?= =?UTF-8?q?=EA=B3=A0=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../ladderapplication/LadderApplication.java | 2 +- .../java/ladderapplication/models/Ladder.java | 48 +++++++++---------- .../java/ladderapplication/models/Line.java | 29 ++++++----- .../utils/DecoratingUtils.java | 32 +++++++++++++ 5 files changed, 75 insertions(+), 38 deletions(-) create mode 100644 src/main/java/ladderapplication/utils/DecoratingUtils.java diff --git a/README.md b/README.md index dcc41239b5..49f14c5705 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## 기능 목록 - [x] 쉼표로 구분된 참여할 사람을 입력 받는다. (단, 이름은 최대 5자까지 부여) - [x] 최대 사다리 높이 개수를 입력 받는다. -- [ ] 입력 받은 사람과 사다리 높이를 이용해 사다리를 출력한다. +- [x] 입력 받은 사람과 사다리 높이를 이용해 사다리를 출력한다. ## 기능 요구 사항 - 사다리 게임에 참여하는 사람에 이름을 최대5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다. diff --git a/src/main/java/ladderapplication/LadderApplication.java b/src/main/java/ladderapplication/LadderApplication.java index fa8245387a..43fae74ab6 100644 --- a/src/main/java/ladderapplication/LadderApplication.java +++ b/src/main/java/ladderapplication/LadderApplication.java @@ -9,7 +9,7 @@ public class LadderApplication { public static void main(String[] args) { GameSettingRequest gameSettingRequest = Printer.requestGameSetting(); - Ladder ladder = Ladder.of(gameSettingRequest); + Ladder ladder = Ladder.from(gameSettingRequest); ladder.print(); } diff --git a/src/main/java/ladderapplication/models/Ladder.java b/src/main/java/ladderapplication/models/Ladder.java index 3afcf4916b..8825004bb6 100644 --- a/src/main/java/ladderapplication/models/Ladder.java +++ b/src/main/java/ladderapplication/models/Ladder.java @@ -1,7 +1,7 @@ package ladderapplication.models; import ladderapplication.models.requests.GameSettingRequest; -import ladderapplication.models.requests.LadderRequest; +import ladderapplication.utils.DecoratingUtils; import java.util.List; import java.util.stream.Collectors; @@ -9,49 +9,49 @@ public class Ladder { - private static final int NAME_SPACE = 6; - private static final int STANDARD_SIZE = 4; private final List players; private final List lines; + private final String drawing; - public Ladder(List players, List lines) { + private Ladder(List players, List lines, String drawing) { this.players = players; this.lines = lines; + this.drawing = drawing; } - public static Ladder of(GameSettingRequest gameSettingRequest) { + public static Ladder from(GameSettingRequest gameSettingRequest) { List newPlayers = gameSettingRequest.getPlayerRequests() .stream() .map(Player::from) .collect(Collectors.toList()); - List newLines = Stream.generate(() -> Line.of(gameSettingRequest.getPlayerRequests().size())) - .limit(gameSettingRequest.getLadderRequest().getHeight()) + int playerCount = gameSettingRequest.getPlayerRequests().size(); + int ladderHeight = gameSettingRequest.getLadderRequest().getHeight(); + List newLines = Stream.generate(() -> Line.of(playerCount)) + .limit(ladderHeight) .collect(Collectors.toList()); - return new Ladder(newPlayers, newLines); + + String drawing = createDrawing(newPlayers, newLines); + return new Ladder(newPlayers, newLines, drawing); } - public void print() { + private static String createDrawing(List players, List lines) { StringBuilder sb = new StringBuilder(); - players.forEach(player -> sb.append(DecoratingName(player.getName()))); - sb.append("\n"); + addPlayerNames(sb, players); + addLines(sb, lines); + return sb.toString(); + } - lines.forEach(line -> sb.append(line.getLine()).append("\n")); - System.out.print(sb); + private static void addPlayerNames(StringBuilder sb, List players) { + players.forEach(player -> sb.append(DecoratingUtils.getDecoratedName(player.getName()))); + sb.append("\n"); } - private String DecoratingName(String name) { - StringBuilder sb = new StringBuilder(); - if (name.length() < STANDARD_SIZE) { - sb.append(getSpace(STANDARD_SIZE - name.length())); - } - sb.append(name); - sb.append(getSpace(NAME_SPACE - sb.length())); - return sb.toString(); + private static void addLines(StringBuilder sb, List lines) { + lines.forEach(line -> sb.append(line.print()).append("\n")); } - private String getSpace(int count) { - String space = " "; - return space.repeat(count); + public void print() { + System.out.println(this.drawing); } } diff --git a/src/main/java/ladderapplication/models/Line.java b/src/main/java/ladderapplication/models/Line.java index acf2e0af96..5ceb6f4fa5 100644 --- a/src/main/java/ladderapplication/models/Line.java +++ b/src/main/java/ladderapplication/models/Line.java @@ -1,5 +1,7 @@ package ladderapplication.models; +import ladderapplication.utils.DecoratingUtils; + import java.util.List; import java.util.Random; import java.util.stream.Collectors; @@ -22,28 +24,31 @@ public static Line of(int playerCount) { .collect(Collectors.toList()); IntStream.range(0, hasSteps.size()) - .forEach(index -> { - if ((index == 0 || !hasSteps.get(index - 1)) && hasStep()) { - hasSteps.set(index, Boolean.TRUE); - } - }); + .forEach(index -> setHasStep(hasSteps, index)); + return new Line(hasSteps); } + private static void setHasStep(List hasSteps, int index) { + if (prevLadderHasNotStep(hasSteps, index) && hasStep()) { + hasSteps.set(index, Boolean.TRUE); + } + } + + private static boolean prevLadderHasNotStep(List hasSteps, int index) { + return index == 0 || !hasSteps.get(index - 1); + } + private static boolean hasStep() { return RANDOM_GENERATOR.nextInt(RANDOM_NUMBER_BOUND) >= HAS_STEP_BOUND; } - public String getLine() { + public String print() { StringBuilder sb = new StringBuilder(); sb.append(" "); hasSteps.forEach(hasStep -> { - if (hasStep) { - sb.append("|-----"); - return; - } - - sb.append("| "); + sb.append("|") + .append(DecoratingUtils.getStep(hasStep)); }); sb.append("|"); diff --git a/src/main/java/ladderapplication/utils/DecoratingUtils.java b/src/main/java/ladderapplication/utils/DecoratingUtils.java new file mode 100644 index 0000000000..850bd9e4bf --- /dev/null +++ b/src/main/java/ladderapplication/utils/DecoratingUtils.java @@ -0,0 +1,32 @@ +package ladderapplication.utils; + +public class DecoratingUtils { + + private static final String EMPTY = " "; + private static final String STEP_PIECE = "-"; + private static final int NAME_SPACE = 6; + private static final int STANDARD_SIZE = 4; + private static final int STEP_LENGTH = 5; + + public static String getDecoratedName(String name) { + StringBuilder sb = new StringBuilder(); + if (name.length() < STANDARD_SIZE) { + sb.append(getRepeatCharacter(EMPTY, STANDARD_SIZE - name.length())); + } + sb.append(name) + .append(getRepeatCharacter(EMPTY, NAME_SPACE - sb.length())); + return sb.toString(); + } + + private static String getRepeatCharacter(String character, int repeatCount) { + return character.repeat(repeatCount); + } + + public static String getStep(boolean hasStep) { + if (hasStep) { + return getRepeatCharacter(STEP_PIECE, STEP_LENGTH); + } + + return getRepeatCharacter(EMPTY, STEP_LENGTH); + } +}