From 650c60d6e7ebae7d40a72c4a51c525f457b55778 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Fri, 4 Nov 2022 20:51:31 +0900 Subject: [PATCH 01/12] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=85?= =?UTF-8?q?=EC=84=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b853592f59..61ba0e3ddb 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,18 @@ * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. ## 온라인 코드 리뷰 과정 -* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](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) + +## 기능정의 2단계 - 사다리(생성) +* 참여자 + * 참여자의 이름은 5자를 넘지 않는다. + +* 라인 + * 라인은 가로로 겹쳐지지 않는다. + * 이전 값을 조회하여 라인이 있다면 라인은 만들지 말고 없다면 랜덤으로 생성한다. + * 라인은 있거나 없다. + * 라인은 인원수 -1 만큼 존재한다. + +* 사다리 + * 사다리의 높이만큼 사다리의 갯수를 생성한다. + From df26b5a169ba66a34654c30526ee4391128908d3 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Fri, 4 Nov 2022 21:04:15 +0900 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20=EC=B0=B8=EC=97=AC=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EC=9D=B4=EB=A6=84=EC=9D=80=205=EC=9E=90=EB=A5=BC?= =?UTF-8?q?=20=EB=84=98=EC=A7=80=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 --- .../java/nextstep/ladder/model/Player.java | 16 ++++++++++++++++ src/test/java/nextstep/ladder/PlayerTest.java | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/nextstep/ladder/model/Player.java create mode 100644 src/test/java/nextstep/ladder/PlayerTest.java diff --git a/src/main/java/nextstep/ladder/model/Player.java b/src/main/java/nextstep/ladder/model/Player.java new file mode 100644 index 0000000000..b32dc8fa6a --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Player.java @@ -0,0 +1,16 @@ +package nextstep.ladder.model; + +public class Player { + private final String name; + + public Player(final String name) { + validate(name); + this.name = name; + } + + private void validate(final String name) { + if (name.length() > 5) { + throw new IllegalArgumentException("사용자의 이름은 5자를 넘을 수 없습니다"); + } + } +} diff --git a/src/test/java/nextstep/ladder/PlayerTest.java b/src/test/java/nextstep/ladder/PlayerTest.java new file mode 100644 index 0000000000..75592b06bc --- /dev/null +++ b/src/test/java/nextstep/ladder/PlayerTest.java @@ -0,0 +1,18 @@ +package nextstep.ladder; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import nextstep.ladder.model.Player; + +class PlayerTest { + + @Test + void 사용자의_이름은_5자를_넘길수_없다() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new Player("abcdef")) + .withMessage("사용자의 이름은 5자를 넘을 수 없습니다"); + } + +} From 50422300ad4d6703c27115138d60ece2fb256370 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Fri, 4 Nov 2022 21:37:45 +0900 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20=EC=9D=B4=EC=A0=84=20=EA=B0=92?= =?UTF-8?q?=EC=9D=84=20=EC=A1=B0=ED=9A=8C=ED=95=98=EC=97=AC=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=EC=9D=B4=20=EC=9E=88=EB=8B=A4=EB=A9=B4=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=EC=9D=80=20=EB=A7=8C=EB=93=A4=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/model/Line.java | 35 +++++++++++++++++++ .../java/nextstep/ladder/model/LineTest.java | 21 +++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/nextstep/ladder/model/Line.java create mode 100644 src/test/java/nextstep/ladder/model/LineTest.java diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java new file mode 100644 index 0000000000..2d483c206e --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -0,0 +1,35 @@ +package nextstep.ladder.model; + +import java.util.Objects; + +public class Line { + private final Boolean hasLine; + + public Line(final boolean hasLine) { + this.hasLine = hasLine; + } + + public static Line create(final Line prevLine) { + if (prevLine == null || !prevLine.hasLine) { + return new Line(true); + } + return new Line(false); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Line line = (Line)o; + return Objects.equals(hasLine, line.hasLine); + } + + @Override + public int hashCode() { + return Objects.hash(hasLine); + } +} diff --git a/src/test/java/nextstep/ladder/model/LineTest.java b/src/test/java/nextstep/ladder/model/LineTest.java new file mode 100644 index 0000000000..d6ff63be12 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LineTest.java @@ -0,0 +1,21 @@ +package nextstep.ladder.model; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import nextstep.ladder.model.Line; + +class LineTest { + + @Test + void 이전_라인이_잇을경우_무조건_해당라인은_없다(){ + Assertions.assertThat(Line.create(new Line(true))).isEqualTo(new Line(false)); + } + + +} From a08e9c36f618b12236285f5e6186285812f5c1a4 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Fri, 4 Nov 2022 21:40:10 +0900 Subject: [PATCH 04/12] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EC=9E=AC?= =?UTF-8?q?=EB=AA=85=EC=84=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 61ba0e3ddb..858b7c70b0 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ * 라인 * 라인은 가로로 겹쳐지지 않는다. * 이전 값을 조회하여 라인이 있다면 라인은 만들지 말고 없다면 랜덤으로 생성한다. - * 라인은 있거나 없다. - * 라인은 인원수 -1 만큼 존재한다. + +* 라인들 + * 라인들은 인원수 -1 만큼 존재한다. * 사다리 * 사다리의 높이만큼 사다리의 갯수를 생성한다. From ddf92f014216d4be583e0b7112c7b184f767ad36 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Fri, 4 Nov 2022 21:51:21 +0900 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20=EB=9D=BC=EC=9D=B8=EB=93=A4?= =?UTF-8?q?=EC=9D=80=20=EC=9D=B8=EC=9B=90=EC=88=98=20-1=20=EB=A7=8C?= =?UTF-8?q?=ED=81=BC=20=EC=A1=B4=EC=9E=AC=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/model/Lines.java | 27 +++++++++++++++++++ .../java/nextstep/ladder/model/LinesTest.java | 17 ++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/nextstep/ladder/model/Lines.java create mode 100644 src/test/java/nextstep/ladder/model/LinesTest.java diff --git a/src/main/java/nextstep/ladder/model/Lines.java b/src/main/java/nextstep/ladder/model/Lines.java new file mode 100644 index 0000000000..9114ce5292 --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Lines.java @@ -0,0 +1,27 @@ +package nextstep.ladder.model; + +import java.util.ArrayList; +import java.util.List; + +public class Lines { + private final List lines; + + public Lines(final int countOfPlayer) { + List result = new ArrayList<>(); + for (int i = 0; i < countOfPlayer - 1; i++) { + result.add(Line.create(getPrevLine(result, i))); + } + this.lines = result; + } + + private Line getPrevLine(final List result, final int i) { + if(i == 0){ + return null; + } + return result.get(i - 1); + } + + public int size(){ + return lines.size(); + } +} diff --git a/src/test/java/nextstep/ladder/model/LinesTest.java b/src/test/java/nextstep/ladder/model/LinesTest.java new file mode 100644 index 0000000000..66e06e8dd5 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LinesTest.java @@ -0,0 +1,17 @@ +package nextstep.ladder.model; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class LinesTest { + + @Test + void 라인들은_플레이어의_숫자보다_1작다(){ + Lines lines = new Lines(4); + assertThat(lines.size()).isEqualTo(3); + } + +} From 76e6b3a6f2ae5bf0f36f674edd2ef53ce2e27951 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Sat, 5 Nov 2022 21:06:03 +0900 Subject: [PATCH 06/12] =?UTF-8?q?feat:=20=EB=9D=BC=EC=9D=B8=EC=9D=84=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=EC=9C=BC=EB=A1=9C=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/model/Line.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java index 2d483c206e..f042c15667 100644 --- a/src/main/java/nextstep/ladder/model/Line.java +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -1,5 +1,6 @@ package nextstep.ladder.model; +import java.security.SecureRandom; import java.util.Objects; public class Line { @@ -11,7 +12,8 @@ public Line(final boolean hasLine) { public static Line create(final Line prevLine) { if (prevLine == null || !prevLine.hasLine) { - return new Line(true); + SecureRandom secureRandom = new SecureRandom(); + return new Line(secureRandom.nextBoolean()); } return new Line(false); } From 769a9ce06eeaf16df00a65faf9e7cdac1a89e4d8 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Sat, 5 Nov 2022 21:16:18 +0900 Subject: [PATCH 07/12] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=9E=85=EB=A0=A5=ED=95=9C=20=EB=A7=8C=ED=81=BC=20?= =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=EC=9D=98=20=EC=B8=B5=EC=9D=84=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A0=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/model/Ladder.java | 21 +++++++++++++++++++ .../nextstep/ladder/model/LadderTest.java | 17 +++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/nextstep/ladder/model/Ladder.java create mode 100644 src/test/java/nextstep/ladder/model/LadderTest.java diff --git a/src/main/java/nextstep/ladder/model/Ladder.java b/src/main/java/nextstep/ladder/model/Ladder.java new file mode 100644 index 0000000000..2de001180c --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Ladder.java @@ -0,0 +1,21 @@ +package nextstep.ladder.model; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder { + private final List lines; + + public Ladder(final int numberOfFloors, final int numberOfPlayers) { + List result = new ArrayList<>(); + for (int i = 0; i < numberOfFloors; i++) { + result.add(new Lines(numberOfPlayers)); + } + this.lines = result; + } + + + public int numberOfFloors() { + return lines.size(); + } +} diff --git a/src/test/java/nextstep/ladder/model/LadderTest.java b/src/test/java/nextstep/ladder/model/LadderTest.java new file mode 100644 index 0000000000..c3fa455afb --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LadderTest.java @@ -0,0 +1,17 @@ +package nextstep.ladder.model; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class LadderTest { + + @Test + void 사용자가_입력한_만큼_사다리의_층을_만든다() { + Ladder ladder = new Ladder(5, 4); + assertThat(ladder.numberOfFloors()).isEqualTo(5); + } + +} \ No newline at end of file From 581332a0f1bca0d48b570c4ac1f9b7f9927c8a56 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Sat, 5 Nov 2022 22:02:53 +0900 Subject: [PATCH 08/12] =?UTF-8?q?feat:=20View=20=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=EC=B6=9C=EB=A0=A5=EB=AC=B8=20=EC=83=9D=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nextstep/ladder/model/Ladder.java | 4 +++ src/main/java/nextstep/ladder/model/Line.java | 8 +++++ .../java/nextstep/ladder/model/Lines.java | 5 +++ .../java/nextstep/ladder/model/Player.java | 5 +++ .../java/nextstep/ladder/model/Players.java | 16 ++++++++++ .../ladder/strategy/LineStrategy.java | 5 +++ .../ladder/strategy/RandomLineStrategy.java | 11 +++++++ src/main/java/nextstep/ladder/view/Input.java | 32 +++++++++++++++++++ .../java/nextstep/ladder/view/Output.java | 23 +++++++++++++ 9 files changed, 109 insertions(+) create mode 100644 src/main/java/nextstep/ladder/model/Players.java create mode 100644 src/main/java/nextstep/ladder/strategy/LineStrategy.java create mode 100644 src/main/java/nextstep/ladder/strategy/RandomLineStrategy.java create mode 100644 src/main/java/nextstep/ladder/view/Input.java create mode 100644 src/main/java/nextstep/ladder/view/Output.java diff --git a/src/main/java/nextstep/ladder/model/Ladder.java b/src/main/java/nextstep/ladder/model/Ladder.java index 2de001180c..f848a6443a 100644 --- a/src/main/java/nextstep/ladder/model/Ladder.java +++ b/src/main/java/nextstep/ladder/model/Ladder.java @@ -18,4 +18,8 @@ public Ladder(final int numberOfFloors, final int numberOfPlayers) { public int numberOfFloors() { return lines.size(); } + + public List getLines() { + return lines; + } } diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java index f042c15667..78167931ff 100644 --- a/src/main/java/nextstep/ladder/model/Line.java +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -34,4 +34,12 @@ public boolean equals(final Object o) { public int hashCode() { return Objects.hash(hasLine); } + + @Override + public String toString() { + if(hasLine){ + return "-----|"; + } + return " |"; + } } diff --git a/src/main/java/nextstep/ladder/model/Lines.java b/src/main/java/nextstep/ladder/model/Lines.java index 9114ce5292..f529304e33 100644 --- a/src/main/java/nextstep/ladder/model/Lines.java +++ b/src/main/java/nextstep/ladder/model/Lines.java @@ -24,4 +24,9 @@ private Line getPrevLine(final List result, final int i) { public int size(){ return lines.size(); } + + @Override + public String toString() { + return lines.toString(); + } } diff --git a/src/main/java/nextstep/ladder/model/Player.java b/src/main/java/nextstep/ladder/model/Player.java index b32dc8fa6a..76660157e8 100644 --- a/src/main/java/nextstep/ladder/model/Player.java +++ b/src/main/java/nextstep/ladder/model/Player.java @@ -13,4 +13,9 @@ private void validate(final String name) { throw new IllegalArgumentException("사용자의 이름은 5자를 넘을 수 없습니다"); } } + + @Override + public String toString() { + return name; + } } diff --git a/src/main/java/nextstep/ladder/model/Players.java b/src/main/java/nextstep/ladder/model/Players.java new file mode 100644 index 0000000000..c0ea1a35b8 --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Players.java @@ -0,0 +1,16 @@ +package nextstep.ladder.model; + +import java.util.List; + +public class Players { + private List players; + + public Players(List players) { + this.players = players; + } + + @Override + public String toString() { + return players + " "; + } +} diff --git a/src/main/java/nextstep/ladder/strategy/LineStrategy.java b/src/main/java/nextstep/ladder/strategy/LineStrategy.java new file mode 100644 index 0000000000..c95ef0d3e3 --- /dev/null +++ b/src/main/java/nextstep/ladder/strategy/LineStrategy.java @@ -0,0 +1,5 @@ +package nextstep.ladder.strategy; + +public interface LineStrategy { + boolean hasLine(); +} diff --git a/src/main/java/nextstep/ladder/strategy/RandomLineStrategy.java b/src/main/java/nextstep/ladder/strategy/RandomLineStrategy.java new file mode 100644 index 0000000000..4b7fa24f44 --- /dev/null +++ b/src/main/java/nextstep/ladder/strategy/RandomLineStrategy.java @@ -0,0 +1,11 @@ +package nextstep.ladder.strategy; + +import java.security.SecureRandom; + +public class RandomLineStrategy implements LineStrategy { + @Override + public boolean hasLine() { + SecureRandom secureRandom = new SecureRandom(); + return secureRandom.nextBoolean(); + } +} diff --git a/src/main/java/nextstep/ladder/view/Input.java b/src/main/java/nextstep/ladder/view/Input.java new file mode 100644 index 0000000000..d7aa1e8604 --- /dev/null +++ b/src/main/java/nextstep/ladder/view/Input.java @@ -0,0 +1,32 @@ +package nextstep.ladder.view; + +import java.util.List; +import java.util.Scanner; + +public class Input { + private final static Scanner SCANNER = new Scanner(System.in); + private final static String SEPARATOR = ","; + + private Input() { + } + + public static String[] inputPlayers(){ + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String players = nextLine(); + System.out.println(); + return players.split(SEPARATOR); + } + + public static int inputLadderHeights(){ + System.out.println("최대 사다리 높이는 몇 개인가요?"); + String height = nextLine(); + System.out.println(); + return Integer.parseInt(height); + } + + private static String nextLine(){ + return SCANNER.nextLine(); + } + + +} diff --git a/src/main/java/nextstep/ladder/view/Output.java b/src/main/java/nextstep/ladder/view/Output.java new file mode 100644 index 0000000000..ac209f35c1 --- /dev/null +++ b/src/main/java/nextstep/ladder/view/Output.java @@ -0,0 +1,23 @@ +package nextstep.ladder.view; + +import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.Line; +import nextstep.ladder.model.Lines; +import nextstep.ladder.model.Players; + +import java.util.List; + +public class Output { + private Output() { + } + + public static void outputResult(Ladder ladder, Players players){ + System.out.println(players); + List lines = ladder.getLines(); + for (Lines line : lines) { + System.out.print("|"); + System.out.print(line); + System.out.println(); + } + } +} From 49aa28c69ed0c238794d862512785ccf16b4285d Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Sat, 5 Nov 2022 22:22:29 +0900 Subject: [PATCH 09/12] =?UTF-8?q?feat:=20=EC=97=B0=EA=B2=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LadderGameController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/nextstep/ladder/controller/LadderGameController.java diff --git a/src/main/java/nextstep/ladder/controller/LadderGameController.java b/src/main/java/nextstep/ladder/controller/LadderGameController.java new file mode 100644 index 0000000000..4bcf5073bd --- /dev/null +++ b/src/main/java/nextstep/ladder/controller/LadderGameController.java @@ -0,0 +1,25 @@ +package nextstep.ladder.controller; + +import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.Lines; +import nextstep.ladder.model.Player; +import nextstep.ladder.model.Players; +import nextstep.ladder.view.Input; +import nextstep.ladder.view.Output; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class LadderGameController { + + public static void main(String[] args) { + String[] strings = Input.inputPlayers(); + Players players = Arrays.stream(strings) + .map(Player::new) + .collect(Collectors.collectingAndThen(Collectors.toList(), Players::new)); + + Ladder ladder = new Ladder(Input.inputLadderHeights(), players.number()); + + Output.outputResult(ladder, players); + } +} From 294206f6a7c360da142772bb6f45d0ea6be6f26c Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Sat, 5 Nov 2022 22:22:47 +0900 Subject: [PATCH 10/12] =?UTF-8?q?style:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EA=B0=84=EA=B2=A9=20=EC=A1=B0=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/model/Lines.java | 5 ++++- src/main/java/nextstep/ladder/model/Player.java | 2 +- src/main/java/nextstep/ladder/model/Players.java | 9 ++++++++- src/main/java/nextstep/ladder/view/Output.java | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/nextstep/ladder/model/Lines.java b/src/main/java/nextstep/ladder/model/Lines.java index f529304e33..db94a10c41 100644 --- a/src/main/java/nextstep/ladder/model/Lines.java +++ b/src/main/java/nextstep/ladder/model/Lines.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class Lines { private final List lines; @@ -27,6 +28,8 @@ public int size(){ @Override public String toString() { - return lines.toString(); + return lines.stream() + .map(Line::toString) + .collect(Collectors.joining()); } } diff --git a/src/main/java/nextstep/ladder/model/Player.java b/src/main/java/nextstep/ladder/model/Player.java index 76660157e8..4804702eee 100644 --- a/src/main/java/nextstep/ladder/model/Player.java +++ b/src/main/java/nextstep/ladder/model/Player.java @@ -16,6 +16,6 @@ private void validate(final String name) { @Override public String toString() { - return name; + return String.format("%6s", name); } } diff --git a/src/main/java/nextstep/ladder/model/Players.java b/src/main/java/nextstep/ladder/model/Players.java index c0ea1a35b8..aaa0afd8c4 100644 --- a/src/main/java/nextstep/ladder/model/Players.java +++ b/src/main/java/nextstep/ladder/model/Players.java @@ -1,6 +1,7 @@ package nextstep.ladder.model; import java.util.List; +import java.util.stream.Collectors; public class Players { private List players; @@ -9,8 +10,14 @@ public Players(List players) { this.players = players; } + public int number() { + return players.size(); + } + @Override public String toString() { - return players + " "; + return players.stream() + .map(Player::toString) + .collect(Collectors.joining()); } } diff --git a/src/main/java/nextstep/ladder/view/Output.java b/src/main/java/nextstep/ladder/view/Output.java index ac209f35c1..7be4a459a5 100644 --- a/src/main/java/nextstep/ladder/view/Output.java +++ b/src/main/java/nextstep/ladder/view/Output.java @@ -15,7 +15,7 @@ public static void outputResult(Ladder ladder, Players players){ System.out.println(players); List lines = ladder.getLines(); for (Lines line : lines) { - System.out.print("|"); + System.out.print(" |"); System.out.print(line); System.out.println(); } From 38370f1944168fb75cd9f490bf800ba1cf4e28a6 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Mon, 7 Nov 2022 21:04:57 +0900 Subject: [PATCH 11/12] =?UTF-8?q?refactor:=20=EC=BB=A8=EB=B2=A4=EC=85=98?= =?UTF-8?q?=20=EC=A0=95=EB=A6=AC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LadderGameController.java | 16 +-- .../java/nextstep/ladder/model/Ladder.java | 29 +++-- src/main/java/nextstep/ladder/model/Line.java | 2 +- .../java/nextstep/ladder/model/Lines.java | 4 +- .../java/nextstep/ladder/model/Players.java | 26 ++-- src/main/java/nextstep/ladder/view/Input.java | 47 ++++--- .../java/nextstep/ladder/view/Output.java | 22 ++-- src/main/java/nextstep/optional/Computer.java | 54 ++++---- .../java/nextstep/optional/ComputerStore.java | 34 ++--- .../java/nextstep/optional/Expression.java | 28 ++--- src/main/java/nextstep/optional/User.java | 118 +++++++++--------- src/main/java/nextstep/optional/Users.java | 25 ++-- .../nextstep/ladder/model/LadderTest.java | 12 +- .../java/nextstep/ladder/model/LineTest.java | 3 +- .../java/nextstep/ladder/model/LinesTest.java | 2 +- .../ladder/{ => model}/PlayerTest.java | 5 +- 16 files changed, 212 insertions(+), 215 deletions(-) rename src/test/java/nextstep/ladder/{ => model}/PlayerTest.java (72%) diff --git a/src/main/java/nextstep/ladder/controller/LadderGameController.java b/src/main/java/nextstep/ladder/controller/LadderGameController.java index 4bcf5073bd..dcd0d5d343 100644 --- a/src/main/java/nextstep/ladder/controller/LadderGameController.java +++ b/src/main/java/nextstep/ladder/controller/LadderGameController.java @@ -12,14 +12,14 @@ public class LadderGameController { - public static void main(String[] args) { - String[] strings = Input.inputPlayers(); - Players players = Arrays.stream(strings) - .map(Player::new) - .collect(Collectors.collectingAndThen(Collectors.toList(), Players::new)); + public static void main(String[] args) { + String[] strings = Input.inputPlayers(); + Players players = Arrays.stream(strings) + .map(Player::new) + .collect(Collectors.collectingAndThen(Collectors.toList(), Players::new)); - Ladder ladder = new Ladder(Input.inputLadderHeights(), players.number()); + Ladder ladder = new Ladder(Input.inputLadderHeights(), players.number()); - Output.outputResult(ladder, players); - } + Output.outputResult(ladder, players); + } } diff --git a/src/main/java/nextstep/ladder/model/Ladder.java b/src/main/java/nextstep/ladder/model/Ladder.java index f848a6443a..eee0dd72c0 100644 --- a/src/main/java/nextstep/ladder/model/Ladder.java +++ b/src/main/java/nextstep/ladder/model/Ladder.java @@ -4,22 +4,21 @@ import java.util.List; public class Ladder { - private final List lines; + private final List lines; - public Ladder(final int numberOfFloors, final int numberOfPlayers) { - List result = new ArrayList<>(); - for (int i = 0; i < numberOfFloors; i++) { - result.add(new Lines(numberOfPlayers)); - } - this.lines = result; - } + public Ladder(final int numberOfFloors, final int numberOfPlayers) { + List result = new ArrayList<>(); + for (int i = 0; i < numberOfFloors; i++) { + result.add(new Lines(numberOfPlayers)); + } + this.lines = result; + } + public int numberOfFloors() { + return lines.size(); + } - public int numberOfFloors() { - return lines.size(); - } - - public List getLines() { - return lines; - } + public List getLines() { + return lines; + } } diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java index 78167931ff..1ce5302827 100644 --- a/src/main/java/nextstep/ladder/model/Line.java +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -37,7 +37,7 @@ public int hashCode() { @Override public String toString() { - if(hasLine){ + if (hasLine) { return "-----|"; } return " |"; diff --git a/src/main/java/nextstep/ladder/model/Lines.java b/src/main/java/nextstep/ladder/model/Lines.java index db94a10c41..6b5c85af49 100644 --- a/src/main/java/nextstep/ladder/model/Lines.java +++ b/src/main/java/nextstep/ladder/model/Lines.java @@ -16,13 +16,13 @@ public Lines(final int countOfPlayer) { } private Line getPrevLine(final List result, final int i) { - if(i == 0){ + if (i == 0) { return null; } return result.get(i - 1); } - public int size(){ + public int size() { return lines.size(); } diff --git a/src/main/java/nextstep/ladder/model/Players.java b/src/main/java/nextstep/ladder/model/Players.java index aaa0afd8c4..7eb9ca620a 100644 --- a/src/main/java/nextstep/ladder/model/Players.java +++ b/src/main/java/nextstep/ladder/model/Players.java @@ -4,20 +4,20 @@ import java.util.stream.Collectors; public class Players { - private List players; + private List players; - public Players(List players) { - this.players = players; - } + public Players(List players) { + this.players = players; + } - public int number() { - return players.size(); - } + public int number() { + return players.size(); + } - @Override - public String toString() { - return players.stream() - .map(Player::toString) - .collect(Collectors.joining()); - } + @Override + public String toString() { + return players.stream() + .map(Player::toString) + .collect(Collectors.joining()); + } } diff --git a/src/main/java/nextstep/ladder/view/Input.java b/src/main/java/nextstep/ladder/view/Input.java index d7aa1e8604..49401baff3 100644 --- a/src/main/java/nextstep/ladder/view/Input.java +++ b/src/main/java/nextstep/ladder/view/Input.java @@ -4,29 +4,28 @@ import java.util.Scanner; public class Input { - private final static Scanner SCANNER = new Scanner(System.in); - private final static String SEPARATOR = ","; - - private Input() { - } - - public static String[] inputPlayers(){ - System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); - String players = nextLine(); - System.out.println(); - return players.split(SEPARATOR); - } - - public static int inputLadderHeights(){ - System.out.println("최대 사다리 높이는 몇 개인가요?"); - String height = nextLine(); - System.out.println(); - return Integer.parseInt(height); - } - - private static String nextLine(){ - return SCANNER.nextLine(); - } - + private final static Scanner SCANNER = new Scanner(System.in); + private final static String SEPARATOR = ","; + + private Input() { + } + + public static String[] inputPlayers() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String players = nextLine(); + System.out.println(); + return players.split(SEPARATOR); + } + + public static int inputLadderHeights() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + String height = nextLine(); + System.out.println(); + return Integer.parseInt(height); + } + + private static String nextLine() { + return SCANNER.nextLine(); + } } diff --git a/src/main/java/nextstep/ladder/view/Output.java b/src/main/java/nextstep/ladder/view/Output.java index 7be4a459a5..2269932ca4 100644 --- a/src/main/java/nextstep/ladder/view/Output.java +++ b/src/main/java/nextstep/ladder/view/Output.java @@ -8,16 +8,16 @@ import java.util.List; public class Output { - private Output() { - } + private Output() { + } - public static void outputResult(Ladder ladder, Players players){ - System.out.println(players); - List lines = ladder.getLines(); - for (Lines line : lines) { - System.out.print(" |"); - System.out.print(line); - System.out.println(); - } - } + public static void outputResult(Ladder ladder, Players players) { + System.out.println(players); + List lines = ladder.getLines(); + for (Lines line : lines) { + System.out.print(" |"); + System.out.print(line); + System.out.println(); + } + } } diff --git a/src/main/java/nextstep/optional/Computer.java b/src/main/java/nextstep/optional/Computer.java index cc0af4d795..12c62e4840 100644 --- a/src/main/java/nextstep/optional/Computer.java +++ b/src/main/java/nextstep/optional/Computer.java @@ -1,39 +1,39 @@ package nextstep.optional; public class Computer { - private Soundcard soundcard; + private Soundcard soundcard; - public Computer(Soundcard soundcard) { - this.soundcard = soundcard; - } + public Computer(Soundcard soundcard) { + this.soundcard = soundcard; + } - public Soundcard getSoundcard() { - return soundcard; - } + public Soundcard getSoundcard() { + return soundcard; + } - public static class Soundcard { - private USB usb; + public static class Soundcard { + private USB usb; - public Soundcard(USB usb) { - super(); - this.usb = usb; - } + public Soundcard(USB usb) { + super(); + this.usb = usb; + } - public USB getUsb() { - return usb; - } - } + public USB getUsb() { + return usb; + } + } - public static class USB { - private String version; + public static class USB { + private String version; - public USB(String version) { - super(); - this.version = version; - } + public USB(String version) { + super(); + this.version = version; + } - public String getVersion() { - return this.version; - } - } + public String getVersion() { + return this.version; + } + } } diff --git a/src/main/java/nextstep/optional/ComputerStore.java b/src/main/java/nextstep/optional/ComputerStore.java index 2695c967ac..280c1b450a 100644 --- a/src/main/java/nextstep/optional/ComputerStore.java +++ b/src/main/java/nextstep/optional/ComputerStore.java @@ -4,23 +4,23 @@ import nextstep.optional.Computer.USB; public class ComputerStore { - public static final String UNKNOWN_VERSION = "UNKNOWN"; + public static final String UNKNOWN_VERSION = "UNKNOWN"; - public static String getVersion(Computer computer) { - String version = UNKNOWN_VERSION; - if (computer != null) { - Soundcard soundcard = computer.getSoundcard(); - if (soundcard != null) { - USB usb = soundcard.getUsb(); - if (usb != null) { - version = usb.getVersion(); - } - } - } - return version; - } + public static String getVersion(Computer computer) { + String version = UNKNOWN_VERSION; + if (computer != null) { + Soundcard soundcard = computer.getSoundcard(); + if (soundcard != null) { + USB usb = soundcard.getUsb(); + if (usb != null) { + version = usb.getVersion(); + } + } + } + return version; + } - public static String getVersionOptional(Computer computer) { - return null; - } + public static String getVersionOptional(Computer computer) { + return null; + } } diff --git a/src/main/java/nextstep/optional/Expression.java b/src/main/java/nextstep/optional/Expression.java index 1bae90a18c..000b37f443 100644 --- a/src/main/java/nextstep/optional/Expression.java +++ b/src/main/java/nextstep/optional/Expression.java @@ -3,22 +3,22 @@ import java.util.Arrays; enum Expression { - PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"); + PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"); - private String expression; + private String expression; - Expression(String expression) { - this.expression = expression; - } + Expression(String expression) { + this.expression = expression; + } - private static boolean matchExpression(Expression e, String expression) { - return expression.equals(e.expression); - } + private static boolean matchExpression(Expression e, String expression) { + return expression.equals(e.expression); + } - static Expression of(String expression) { - return Arrays.stream(values()) - .filter(value -> matchExpression(value, expression)) - .findAny() - .orElseThrow(() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression))); - } + static Expression of(String expression) { + return Arrays.stream(values()) + .filter(value -> matchExpression(value, expression)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression))); + } } diff --git a/src/main/java/nextstep/optional/User.java b/src/main/java/nextstep/optional/User.java index b79a621c9c..6c49b81571 100644 --- a/src/main/java/nextstep/optional/User.java +++ b/src/main/java/nextstep/optional/User.java @@ -3,72 +3,72 @@ import java.util.Optional; public class User { - private String name; - private Integer age; + private String name; + private Integer age; - public User(String name, Integer age) { - this.name = name; - this.age = age; - } + public User(String name, Integer age) { + this.name = name; + this.age = age; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public Integer getAge() { - return age; - } + public Integer getAge() { + return age; + } - public boolean matchName(String name) { - return this.name.equals(name); - } + public boolean matchName(String name) { + return this.name.equals(name); + } - public static boolean ageIsInRange1(User user) { - boolean isInRange = false; + public static boolean ageIsInRange1(User user) { + boolean isInRange = false; - if (user != null && user.getAge() != null - && (user.getAge() >= 30 - && user.getAge() <= 45)) { - isInRange = true; - } - return isInRange; - } + if (user != null && user.getAge() != null + && (user.getAge() >= 30 + && user.getAge() <= 45)) { + isInRange = true; + } + return isInRange; + } - public static boolean ageIsInRange2(User user) { - return Optional.ofNullable(user) - .map(User::getAge) - .filter(age -> age >= 30 && age <= 45) - .isPresent(); - } + public static boolean ageIsInRange2(User user) { + return Optional.ofNullable(user) + .map(User::getAge) + .filter(age -> age >= 30 && age <= 45) + .isPresent(); + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((age == null) ? 0 : age.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((age == null) ? 0 : age.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - User other = (User) obj; - if (age == null) { - if (other.age != null) - return false; - } else if (!age.equals(other.age)) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; - } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + User other = (User)obj; + if (age == null) { + if (other.age != null) + return false; + } else if (!age.equals(other.age)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } } diff --git a/src/main/java/nextstep/optional/Users.java b/src/main/java/nextstep/optional/Users.java index 97de23f229..42f6d4b886 100644 --- a/src/main/java/nextstep/optional/Users.java +++ b/src/main/java/nextstep/optional/Users.java @@ -4,18 +4,19 @@ import java.util.List; public class Users { - static final User DEFAULT_USER = new User("codesquad", 100); + static final User DEFAULT_USER = new User("codesquad", 100); - List users = Arrays.asList( - new User("crong", 35), - new User("pobi", 30), - new User("jk", 40), - new User("honux", 45)); + List users = Arrays.asList( + new User("crong", 35), + new User("pobi", 30), + new User("jk", 40), + new User("honux", 45) + ); - User getUser(String name) { - return users.stream() - .filter(user -> user.matchName(name)) - .findAny() - .orElse(DEFAULT_USER); - } + User getUser(String name) { + return users.stream() + .filter(user -> user.matchName(name)) + .findAny() + .orElse(DEFAULT_USER); + } } diff --git a/src/test/java/nextstep/ladder/model/LadderTest.java b/src/test/java/nextstep/ladder/model/LadderTest.java index c3fa455afb..cd0512d339 100644 --- a/src/test/java/nextstep/ladder/model/LadderTest.java +++ b/src/test/java/nextstep/ladder/model/LadderTest.java @@ -8,10 +8,10 @@ class LadderTest { - @Test - void 사용자가_입력한_만큼_사다리의_층을_만든다() { - Ladder ladder = new Ladder(5, 4); - assertThat(ladder.numberOfFloors()).isEqualTo(5); - } + @Test + void 사용자가_입력한_만큼_사다리의_층을_만든다() { + Ladder ladder = new Ladder(5, 4); + assertThat(ladder.numberOfFloors()).isEqualTo(5); + } -} \ No newline at end of file +} diff --git a/src/test/java/nextstep/ladder/model/LineTest.java b/src/test/java/nextstep/ladder/model/LineTest.java index d6ff63be12..0327c09080 100644 --- a/src/test/java/nextstep/ladder/model/LineTest.java +++ b/src/test/java/nextstep/ladder/model/LineTest.java @@ -13,9 +13,8 @@ class LineTest { @Test - void 이전_라인이_잇을경우_무조건_해당라인은_없다(){ + void 이전_라인이_잇을경우_무조건_해당라인은_없다() { Assertions.assertThat(Line.create(new Line(true))).isEqualTo(new Line(false)); } - } diff --git a/src/test/java/nextstep/ladder/model/LinesTest.java b/src/test/java/nextstep/ladder/model/LinesTest.java index 66e06e8dd5..a5734990bf 100644 --- a/src/test/java/nextstep/ladder/model/LinesTest.java +++ b/src/test/java/nextstep/ladder/model/LinesTest.java @@ -9,7 +9,7 @@ class LinesTest { @Test - void 라인들은_플레이어의_숫자보다_1작다(){ + void 라인들은_플레이어의_숫자보다_1작다() { Lines lines = new Lines(4); assertThat(lines.size()).isEqualTo(3); } diff --git a/src/test/java/nextstep/ladder/PlayerTest.java b/src/test/java/nextstep/ladder/model/PlayerTest.java similarity index 72% rename from src/test/java/nextstep/ladder/PlayerTest.java rename to src/test/java/nextstep/ladder/model/PlayerTest.java index 75592b06bc..335fad89cd 100644 --- a/src/test/java/nextstep/ladder/PlayerTest.java +++ b/src/test/java/nextstep/ladder/model/PlayerTest.java @@ -1,4 +1,4 @@ -package nextstep.ladder; +package nextstep.ladder.model; import static org.assertj.core.api.Assertions.*; @@ -10,8 +10,7 @@ class PlayerTest { @Test void 사용자의_이름은_5자를_넘길수_없다() { - assertThatIllegalArgumentException() - .isThrownBy(() -> new Player("abcdef")) + assertThatIllegalArgumentException().isThrownBy(() -> new Player("abcdef")) .withMessage("사용자의 이름은 5자를 넘을 수 없습니다"); } From a43e2297e13597bbad5571016b8fe3807ba4d1d6 Mon Sep 17 00:00:00 2001 From: jimbaemon Date: Mon, 7 Nov 2022 21:07:26 +0900 Subject: [PATCH 12/12] =?UTF-8?q?refactor:=20Magic=20Int=20=EC=83=81?= =?UTF-8?q?=EC=88=98=20=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/model/Lines.java | 9 ++++++--- src/main/java/nextstep/ladder/model/Player.java | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/nextstep/ladder/model/Lines.java b/src/main/java/nextstep/ladder/model/Lines.java index 6b5c85af49..99f4896702 100644 --- a/src/main/java/nextstep/ladder/model/Lines.java +++ b/src/main/java/nextstep/ladder/model/Lines.java @@ -5,6 +5,9 @@ import java.util.stream.Collectors; public class Lines { + private final static int FIRST_INDEX = 0; + private final static int PREV_INDEX = 0; + private final List lines; public Lines(final int countOfPlayer) { @@ -15,11 +18,11 @@ public Lines(final int countOfPlayer) { this.lines = result; } - private Line getPrevLine(final List result, final int i) { - if (i == 0) { + private Line getPrevLine(final List result, final int index) { + if (index == 0) { return null; } - return result.get(i - 1); + return result.get(index - PREV_INDEX); } public int size() { diff --git a/src/main/java/nextstep/ladder/model/Player.java b/src/main/java/nextstep/ladder/model/Player.java index 4804702eee..11f55798f5 100644 --- a/src/main/java/nextstep/ladder/model/Player.java +++ b/src/main/java/nextstep/ladder/model/Player.java @@ -1,6 +1,7 @@ package nextstep.ladder.model; public class Player { + private final static int MAX_NAME_LENGTH = 5; private final String name; public Player(final String name) { @@ -9,7 +10,7 @@ public Player(final String name) { } private void validate(final String name) { - if (name.length() > 5) { + if (name.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException("사용자의 이름은 5자를 넘을 수 없습니다"); } }