From f738f3036b268cb933430262c2f8b14c763a931d Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Sun, 6 Nov 2022 16:39:19 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20InputView=EB=8A=94=20=EC=9B=90?= =?UTF-8?q?=EC=8B=9C=EA=B0=92=EC=9D=84=20=EB=84=98=EA=B8=B0=EB=8A=94=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=EB=A7=8C=20=EA=B0=80=EC=A7=80=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/LadderMain.java | 10 ++++++++-- src/main/java/ladder/view/InputView.java | 20 ++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/ladder/LadderMain.java b/src/main/java/ladder/LadderMain.java index 39db11c5be..de54764890 100644 --- a/src/main/java/ladder/LadderMain.java +++ b/src/main/java/ladder/LadderMain.java @@ -2,16 +2,22 @@ import ladder.domain.Ladder; import ladder.domain.LadderHeight; +import ladder.domain.Participant; import ladder.domain.Participants; import ladder.strategy.BridgeLinesRandomStrategy; import ladder.view.InputView; import ladder.view.ResultView; +import java.util.stream.Collectors; + public class LadderMain { public static void main(String[] args) { - final Participants participants = InputView.inputParticipants(); - final LadderHeight ladderHeight = InputView.inputLadderHeight(); + final Participants participants = new Participants(InputView.inputParticipantNames() + .stream() + .map(Participant::new) + .collect(Collectors.toList())); + final LadderHeight ladderHeight = new LadderHeight(InputView.inputLadderHeight()); ResultView.print(new Ladder(participants, ladderHeight, new BridgeLinesRandomStrategy())); } } diff --git a/src/main/java/ladder/view/InputView.java b/src/main/java/ladder/view/InputView.java index ebc53a1374..7c7eaa8e20 100644 --- a/src/main/java/ladder/view/InputView.java +++ b/src/main/java/ladder/view/InputView.java @@ -1,9 +1,6 @@ package ladder.view; -import ladder.domain.LadderHeight; -import ladder.domain.Participant; -import ladder.domain.Participants; - +import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -15,20 +12,19 @@ public class InputView { private InputView() { } - public static Participants inputParticipants() { + public static List inputParticipantNames() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); - return new Participants(Stream.of(SCANNER.nextLine() - .split(",")) - .map(String::trim) - .map(Participant::new) - .collect(Collectors.toList())); + return Stream.of(SCANNER.nextLine() + .split(",")) + .map(String::trim) + .collect(Collectors.toList()); } - public static LadderHeight inputLadderHeight() { + public static int inputLadderHeight() { System.out.println(); System.out.println("최대 사다리 높이는 몇 개인가요?"); final int intLadderHeight = SCANNER.nextInt(); SCANNER.skip(System.lineSeparator()); - return new LadderHeight(intLadderHeight); + return intLadderHeight; } } From 0ae2c772a487d7993cddf406c7557bf8fe6d5a39 Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Sun, 6 Nov 2022 17:01:22 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=EC=9D=B4=ED=8E=99=ED=8B=B0?= =?UTF-8?q?=EB=B8=8C=20=EC=9E=90=EB=B0=94=2072=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20=ED=91=9C=EC=A4=80=20=EC=98=88=EC=99=B8=EB=A5=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EC=9D=B4=ED=8E=99=ED=8B=B0=EB=B8=8C=20=EC=9E=90?= =?UTF-8?q?=EB=B0=94=2072=20=ED=91=9C=EC=A4=80=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=9D=BC=EC=97=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/domain/BridgeLines.java | 10 +++------- src/main/java/ladder/domain/LadderHeight.java | 4 +--- src/main/java/ladder/domain/Participant.java | 4 +--- src/main/java/ladder/domain/Participants.java | 4 +--- .../BridgeLinesDifferenceHeightException.java | 8 -------- .../exception/ContinuousBridgeLinesException.java | 8 -------- .../CreatingParticipantFailureException.java | 12 ------------ .../exception/InvalidBridgeLinesSizeException.java | 8 -------- .../exception/InvalidLadderHeightException.java | 8 -------- .../exception/InvalidParticipantsSizeException.java | 8 -------- src/test/java/ladder/domain/BridgeLinesTest.java | 12 ++++++------ src/test/java/ladder/domain/LadderHeightTest.java | 4 ++-- src/test/java/ladder/domain/ParticipantTest.java | 3 +-- src/test/java/ladder/domain/ParticipantsTest.java | 4 ++-- 14 files changed, 17 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/ladder/exception/BridgeLinesDifferenceHeightException.java delete mode 100644 src/main/java/ladder/exception/ContinuousBridgeLinesException.java delete mode 100644 src/main/java/ladder/exception/CreatingParticipantFailureException.java delete mode 100644 src/main/java/ladder/exception/InvalidBridgeLinesSizeException.java delete mode 100644 src/main/java/ladder/exception/InvalidLadderHeightException.java delete mode 100644 src/main/java/ladder/exception/InvalidParticipantsSizeException.java diff --git a/src/main/java/ladder/domain/BridgeLines.java b/src/main/java/ladder/domain/BridgeLines.java index 97899d928f..d2ff5cb3d0 100644 --- a/src/main/java/ladder/domain/BridgeLines.java +++ b/src/main/java/ladder/domain/BridgeLines.java @@ -1,9 +1,5 @@ package ladder.domain; -import ladder.exception.BridgeLinesDifferenceHeightException; -import ladder.exception.ContinuousBridgeLinesException; -import ladder.exception.InvalidBridgeLinesSizeException; - import java.util.List; import java.util.stream.IntStream; @@ -35,7 +31,7 @@ private void validateBridgeLinesOrThrow(final List bridgeLines) { private void validateBridgeLinesSize(final List bridgeLines) { if (bridgeLines.size() < MINIMUM_BRIDGE_LINES_SIZE) { - throw new InvalidBridgeLinesSizeException(); + throw new IllegalArgumentException("사다리 다리 라인은 최소 2개 이상이여야 합니다."); } } @@ -44,7 +40,7 @@ private void validateBridgeLinesHeightOrThrow(final List bridgeLines .map(BridgeLine::getHeight) .distinct() .count() != 1) { - throw new BridgeLinesDifferenceHeightException(); + throw new IllegalArgumentException("사다리 다리 라인들의 높이는 동일해야 합니다."); } } @@ -58,7 +54,7 @@ private void validateContinuousBridgeLinesOrThrow(final List bridgeL }) .findFirst() .ifPresent(ignore -> { - throw new ContinuousBridgeLinesException(); + throw new IllegalArgumentException("사다리 다리 연결이 연속될 수 없습니다."); }); } } diff --git a/src/main/java/ladder/domain/LadderHeight.java b/src/main/java/ladder/domain/LadderHeight.java index 21a8e7d00e..e45867d103 100644 --- a/src/main/java/ladder/domain/LadderHeight.java +++ b/src/main/java/ladder/domain/LadderHeight.java @@ -1,7 +1,5 @@ package ladder.domain; -import ladder.exception.InvalidLadderHeightException; - import java.util.Objects; public class LadderHeight { @@ -16,7 +14,7 @@ public LadderHeight(final int value) { private void validateOrThrow(final int value) { if (value <= 0) { - throw new InvalidLadderHeightException(); + throw new IllegalArgumentException("유효하지 않은 사다리 높이입니다."); } } diff --git a/src/main/java/ladder/domain/Participant.java b/src/main/java/ladder/domain/Participant.java index 048b54e50f..e6e009d626 100644 --- a/src/main/java/ladder/domain/Participant.java +++ b/src/main/java/ladder/domain/Participant.java @@ -1,7 +1,5 @@ package ladder.domain; -import ladder.exception.CreatingParticipantFailureException; - import java.util.Objects; import java.util.Optional; @@ -34,7 +32,7 @@ private void validateNameOrThrow(final String name) { .orElseThrow(() -> { final String exceptionMessage = String.format("참가자 이름 길이는 최소 %d, 최대 %d 입니다.", MINIMUM_NAME_LENGTH, MAXIMUM_NAME_LENGTH); - throw new CreatingParticipantFailureException(exceptionMessage); + throw new IllegalArgumentException(exceptionMessage); }); } diff --git a/src/main/java/ladder/domain/Participants.java b/src/main/java/ladder/domain/Participants.java index a3259de3f3..cef3c24b31 100644 --- a/src/main/java/ladder/domain/Participants.java +++ b/src/main/java/ladder/domain/Participants.java @@ -1,7 +1,5 @@ package ladder.domain; -import ladder.exception.InvalidParticipantsSizeException; - import java.util.Collections; import java.util.List; import java.util.Objects; @@ -34,7 +32,7 @@ public int size() { private void validateOrThrow(final List participants) { if (Objects.isNull(participants) || participants.size() == 0) { - throw new InvalidParticipantsSizeException(); + throw new IllegalArgumentException("참가자는 최소 1명 이상이여야 합니다."); } } diff --git a/src/main/java/ladder/exception/BridgeLinesDifferenceHeightException.java b/src/main/java/ladder/exception/BridgeLinesDifferenceHeightException.java deleted file mode 100644 index 1466c6692a..0000000000 --- a/src/main/java/ladder/exception/BridgeLinesDifferenceHeightException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ladder.exception; - -public class BridgeLinesDifferenceHeightException extends RuntimeException { - - public BridgeLinesDifferenceHeightException() { - super("사다리 다리 라인들의 높이는 동일해야 합니다."); - } -} diff --git a/src/main/java/ladder/exception/ContinuousBridgeLinesException.java b/src/main/java/ladder/exception/ContinuousBridgeLinesException.java deleted file mode 100644 index ff5b69ad85..0000000000 --- a/src/main/java/ladder/exception/ContinuousBridgeLinesException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ladder.exception; - -public class ContinuousBridgeLinesException extends RuntimeException { - - public ContinuousBridgeLinesException() { - super("사다리 다리 연결이 연속될 수 없습니다."); - } -} diff --git a/src/main/java/ladder/exception/CreatingParticipantFailureException.java b/src/main/java/ladder/exception/CreatingParticipantFailureException.java deleted file mode 100644 index fdc9575df9..0000000000 --- a/src/main/java/ladder/exception/CreatingParticipantFailureException.java +++ /dev/null @@ -1,12 +0,0 @@ -package ladder.exception; - -public class CreatingParticipantFailureException extends RuntimeException { - - public CreatingParticipantFailureException() { - this("참가자 생성에 실패했습니다."); - } - - public CreatingParticipantFailureException(final String message) { - super(message); - } -} diff --git a/src/main/java/ladder/exception/InvalidBridgeLinesSizeException.java b/src/main/java/ladder/exception/InvalidBridgeLinesSizeException.java deleted file mode 100644 index f3ddef15d5..0000000000 --- a/src/main/java/ladder/exception/InvalidBridgeLinesSizeException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ladder.exception; - -public class InvalidBridgeLinesSizeException extends RuntimeException { - - public InvalidBridgeLinesSizeException() { - super("사다리 다리 라인은 최소 2개 이상이여야 합니다."); - } -} diff --git a/src/main/java/ladder/exception/InvalidLadderHeightException.java b/src/main/java/ladder/exception/InvalidLadderHeightException.java deleted file mode 100644 index d6890ce1ab..0000000000 --- a/src/main/java/ladder/exception/InvalidLadderHeightException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ladder.exception; - -public class InvalidLadderHeightException extends RuntimeException { - - public InvalidLadderHeightException() { - super("유효하지 않은 사다리 높이입니다."); - } -} diff --git a/src/main/java/ladder/exception/InvalidParticipantsSizeException.java b/src/main/java/ladder/exception/InvalidParticipantsSizeException.java deleted file mode 100644 index c267288490..0000000000 --- a/src/main/java/ladder/exception/InvalidParticipantsSizeException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ladder.exception; - -public class InvalidParticipantsSizeException extends RuntimeException { - - public InvalidParticipantsSizeException() { - super("참가자는 최소 1명 이상이여야 합니다."); - } -} diff --git a/src/test/java/ladder/domain/BridgeLinesTest.java b/src/test/java/ladder/domain/BridgeLinesTest.java index 635165a359..b15b9f026c 100644 --- a/src/test/java/ladder/domain/BridgeLinesTest.java +++ b/src/test/java/ladder/domain/BridgeLinesTest.java @@ -1,8 +1,5 @@ package ladder.domain; -import ladder.exception.BridgeLinesDifferenceHeightException; -import ladder.exception.ContinuousBridgeLinesException; -import ladder.exception.InvalidBridgeLinesSizeException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -29,7 +26,8 @@ void create() { void invalid_single() { assertThatThrownBy(() -> new BridgeLines(List.of( new BridgeLine(List.of(true)) - ))).isInstanceOf(InvalidBridgeLinesSizeException.class); + ))).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("사다리 다리 라인은 최소 2개 이상이여야 합니다."); } @DisplayName("사다리 다리 라인 목록 생성 실패 - 다리 라인 높이 다름") @@ -38,7 +36,8 @@ void invalid_differentHeight() { assertThatThrownBy(() -> new BridgeLines(List.of( new BridgeLine(List.of(true)), new BridgeLine(List.of(true, false)) - ))).isInstanceOf(BridgeLinesDifferenceHeightException.class); + ))).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("사다리 다리 라인들의 높이는 동일해야 합니다."); } @DisplayName("사다리 다리 라인 목록 생성 실패 - 다리 연결이 연속됨") @@ -47,6 +46,7 @@ void invalid_continuousConnection() { assertThatThrownBy(() -> new BridgeLines(List.of( new BridgeLine(List.of(false, true, true)), new BridgeLine(List.of(true, true, false)) - ))).isInstanceOf(ContinuousBridgeLinesException.class); + ))).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("사다리 다리 연결이 연속될 수 없습니다."); } } diff --git a/src/test/java/ladder/domain/LadderHeightTest.java b/src/test/java/ladder/domain/LadderHeightTest.java index 984e711b11..d8f3e43f89 100644 --- a/src/test/java/ladder/domain/LadderHeightTest.java +++ b/src/test/java/ladder/domain/LadderHeightTest.java @@ -1,6 +1,5 @@ package ladder.domain; -import ladder.exception.InvalidLadderHeightException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,6 +11,7 @@ public class LadderHeightTest { @Test void invalid() { assertThatThrownBy(() -> new LadderHeight(-1)) - .isInstanceOf(InvalidLadderHeightException.class); + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("유효하지 않은 사다리 높이입니다."); } } diff --git a/src/test/java/ladder/domain/ParticipantTest.java b/src/test/java/ladder/domain/ParticipantTest.java index 23ad70e92b..2ba2e45369 100644 --- a/src/test/java/ladder/domain/ParticipantTest.java +++ b/src/test/java/ladder/domain/ParticipantTest.java @@ -1,6 +1,5 @@ package ladder.domain; -import ladder.exception.CreatingParticipantFailureException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; @@ -23,7 +22,7 @@ void create(final String personName) { @ValueSource(strings = {"hannah"}) void invalidName(final String personName) { assertThatThrownBy(() -> new Participant(personName)) - .isInstanceOf(CreatingParticipantFailureException.class) + .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("1") .hasMessageContaining("5"); } diff --git a/src/test/java/ladder/domain/ParticipantsTest.java b/src/test/java/ladder/domain/ParticipantsTest.java index 283dbb1426..3e13dd20e4 100644 --- a/src/test/java/ladder/domain/ParticipantsTest.java +++ b/src/test/java/ladder/domain/ParticipantsTest.java @@ -1,6 +1,5 @@ package ladder.domain; -import ladder.exception.InvalidParticipantsSizeException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -14,6 +13,7 @@ public class ParticipantsTest { @Test void invalid() { assertThatThrownBy(() -> new Participants(Collections.emptyList())) - .isInstanceOf(InvalidParticipantsSizeException.class); + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("참가자는 최소 1명 이상이여야 합니다."); } } From 237bd4ee5225fbf3dd2c373f2a193fecd6ff798a Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Sun, 6 Nov 2022 17:07:13 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor:=20=EC=9E=90=EB=B0=94=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/LadderMain.java | 6 +----- .../{strategy => domain}/BridgeLinesRandomStrategy.java | 6 +----- .../ladder/{strategy => domain}/BridgeLinesStrategy.java | 5 +---- src/main/java/ladder/domain/Ladder.java | 2 -- src/test/java/ladder/domain/LadderTest.java | 1 - .../java/ladder/strategy/BridgeLinesRandomStrategyTest.java | 2 ++ 6 files changed, 5 insertions(+), 17 deletions(-) rename src/main/java/ladder/{strategy => domain}/BridgeLinesRandomStrategy.java (92%) rename src/main/java/ladder/{strategy => domain}/BridgeLinesStrategy.java (54%) diff --git a/src/main/java/ladder/LadderMain.java b/src/main/java/ladder/LadderMain.java index de54764890..126b9cf86b 100644 --- a/src/main/java/ladder/LadderMain.java +++ b/src/main/java/ladder/LadderMain.java @@ -1,10 +1,6 @@ package ladder; -import ladder.domain.Ladder; -import ladder.domain.LadderHeight; -import ladder.domain.Participant; -import ladder.domain.Participants; -import ladder.strategy.BridgeLinesRandomStrategy; +import ladder.domain.*; import ladder.view.InputView; import ladder.view.ResultView; diff --git a/src/main/java/ladder/strategy/BridgeLinesRandomStrategy.java b/src/main/java/ladder/domain/BridgeLinesRandomStrategy.java similarity index 92% rename from src/main/java/ladder/strategy/BridgeLinesRandomStrategy.java rename to src/main/java/ladder/domain/BridgeLinesRandomStrategy.java index 9cdce5e5bd..002cee3c1e 100644 --- a/src/main/java/ladder/strategy/BridgeLinesRandomStrategy.java +++ b/src/main/java/ladder/domain/BridgeLinesRandomStrategy.java @@ -1,8 +1,4 @@ -package ladder.strategy; - -import ladder.domain.BridgeLine; -import ladder.domain.BridgeLines; -import ladder.domain.LadderHeight; +package ladder.domain; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/ladder/strategy/BridgeLinesStrategy.java b/src/main/java/ladder/domain/BridgeLinesStrategy.java similarity index 54% rename from src/main/java/ladder/strategy/BridgeLinesStrategy.java rename to src/main/java/ladder/domain/BridgeLinesStrategy.java index ab76181ab3..e6b5f854cd 100644 --- a/src/main/java/ladder/strategy/BridgeLinesStrategy.java +++ b/src/main/java/ladder/domain/BridgeLinesStrategy.java @@ -1,7 +1,4 @@ -package ladder.strategy; - -import ladder.domain.BridgeLines; -import ladder.domain.LadderHeight; +package ladder.domain; public interface BridgeLinesStrategy { diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index 7abec0aa15..08c761567c 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -1,7 +1,5 @@ package ladder.domain; -import ladder.strategy.BridgeLinesStrategy; - public class Ladder { private final Participants participants; diff --git a/src/test/java/ladder/domain/LadderTest.java b/src/test/java/ladder/domain/LadderTest.java index 39b8a67543..29894fec6d 100644 --- a/src/test/java/ladder/domain/LadderTest.java +++ b/src/test/java/ladder/domain/LadderTest.java @@ -1,6 +1,5 @@ package ladder.domain; -import ladder.strategy.BridgeLinesRandomStrategy; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java b/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java index 02dddc9ead..5e85516275 100644 --- a/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java +++ b/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java @@ -1,6 +1,8 @@ package ladder.strategy; import ladder.domain.BridgeLines; +import ladder.domain.BridgeLinesRandomStrategy; +import ladder.domain.BridgeLinesStrategy; import ladder.domain.LadderHeight; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From 917f659d4ff546ba033247a809d4299c12aebf82 Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Tue, 8 Nov 2022 19:15:53 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EC=88=98=EC=A7=81=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=EC=97=90=EC=84=9C=20=EC=88=98=ED=8F=89=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/LadderMain.java | 3 +- src/main/java/ladder/domain/BridgeLine.java | 42 ------------- src/main/java/ladder/domain/BridgeLines.java | 60 ------------------- .../domain/BridgeLinesRandomStrategy.java | 46 -------------- .../ladder/domain/BridgeLinesStrategy.java | 6 -- src/main/java/ladder/domain/Ladder.java | 16 ++--- .../java/ladder/domain/LadderFactory.java | 17 ++++++ src/main/java/ladder/domain/Line.java | 44 ++++++++++++++ src/main/java/ladder/domain/LineFactory.java | 33 ++++++++++ src/main/java/ladder/domain/Lines.java | 36 +++++++++++ src/main/java/ladder/view/ResultView.java | 4 +- .../java/ladder/domain/BridgeLineTest.java | 19 ------ .../java/ladder/domain/BridgeLinesTest.java | 52 ---------------- src/test/java/ladder/domain/LadderTest.java | 7 ++- src/test/java/ladder/domain/LineTest.java | 40 +++++++++++++ src/test/java/ladder/domain/LinesTest.java | 52 ++++++++++++++++ .../BridgeLinesRandomStrategyTest.java | 25 -------- 17 files changed, 238 insertions(+), 264 deletions(-) delete mode 100644 src/main/java/ladder/domain/BridgeLine.java delete mode 100644 src/main/java/ladder/domain/BridgeLines.java delete mode 100644 src/main/java/ladder/domain/BridgeLinesRandomStrategy.java delete mode 100644 src/main/java/ladder/domain/BridgeLinesStrategy.java create mode 100644 src/main/java/ladder/domain/LadderFactory.java create mode 100644 src/main/java/ladder/domain/Line.java create mode 100644 src/main/java/ladder/domain/LineFactory.java create mode 100644 src/main/java/ladder/domain/Lines.java delete mode 100644 src/test/java/ladder/domain/BridgeLineTest.java delete mode 100644 src/test/java/ladder/domain/BridgeLinesTest.java create mode 100644 src/test/java/ladder/domain/LineTest.java create mode 100644 src/test/java/ladder/domain/LinesTest.java delete mode 100644 src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java diff --git a/src/main/java/ladder/LadderMain.java b/src/main/java/ladder/LadderMain.java index 126b9cf86b..613b10bb32 100644 --- a/src/main/java/ladder/LadderMain.java +++ b/src/main/java/ladder/LadderMain.java @@ -14,6 +14,7 @@ public static void main(String[] args) { .map(Participant::new) .collect(Collectors.toList())); final LadderHeight ladderHeight = new LadderHeight(InputView.inputLadderHeight()); - ResultView.print(new Ladder(participants, ladderHeight, new BridgeLinesRandomStrategy())); + final Ladder ladder = LadderFactory.create(participants, ladderHeight); + ResultView.print(ladder); } } diff --git a/src/main/java/ladder/domain/BridgeLine.java b/src/main/java/ladder/domain/BridgeLine.java deleted file mode 100644 index 09fdee2732..0000000000 --- a/src/main/java/ladder/domain/BridgeLine.java +++ /dev/null @@ -1,42 +0,0 @@ -package ladder.domain; - -import java.util.List; -import java.util.Objects; -import java.util.stream.IntStream; - -public class BridgeLine { - - private final List connections; - - public BridgeLine(final List connections) { - this.connections = connections; - } - - public int getHeight() { - return this.connections.size(); - } - - public boolean isConnected(final int height) { - return this.connections.get(height); - } - - public boolean hasSameHeightConnection(final BridgeLine bridgeLine) { - return IntStream.range(0, this.connections.size()) - .filter(height -> this.connections.get(height) && bridgeLine.connections.get(height)) - .findFirst() - .isPresent(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BridgeLine that = (BridgeLine) o; - return Objects.equals(connections, that.connections); - } - - @Override - public int hashCode() { - return Objects.hash(connections); - } -} diff --git a/src/main/java/ladder/domain/BridgeLines.java b/src/main/java/ladder/domain/BridgeLines.java deleted file mode 100644 index d2ff5cb3d0..0000000000 --- a/src/main/java/ladder/domain/BridgeLines.java +++ /dev/null @@ -1,60 +0,0 @@ -package ladder.domain; - -import java.util.List; -import java.util.stream.IntStream; - -public class BridgeLines { - - private static final int MINIMUM_BRIDGE_LINES_SIZE = 2; - - private final List bridgeLines; - - public BridgeLines(final List bridgeLines) { - validateBridgeLinesOrThrow(bridgeLines); - - this.bridgeLines = bridgeLines; - } - - public int size() { - return this.bridgeLines.size(); - } - - public List getBridgeLines() { - return bridgeLines; - } - - private void validateBridgeLinesOrThrow(final List bridgeLines) { - validateBridgeLinesSize(bridgeLines); - validateBridgeLinesHeightOrThrow(bridgeLines); - validateContinuousBridgeLinesOrThrow(bridgeLines); - } - - private void validateBridgeLinesSize(final List bridgeLines) { - if (bridgeLines.size() < MINIMUM_BRIDGE_LINES_SIZE) { - throw new IllegalArgumentException("사다리 다리 라인은 최소 2개 이상이여야 합니다."); - } - } - - private void validateBridgeLinesHeightOrThrow(final List bridgeLines) { - if (bridgeLines.stream() - .map(BridgeLine::getHeight) - .distinct() - .count() != 1) { - throw new IllegalArgumentException("사다리 다리 라인들의 높이는 동일해야 합니다."); - } - } - - private void validateContinuousBridgeLinesOrThrow(final List bridgeLines) { - IntStream.range(0, bridgeLines.size() - 1) - .filter(index -> { - final BridgeLine current = bridgeLines.get(index); - final BridgeLine next = bridgeLines.get(index + 1); - - return current.hasSameHeightConnection(next); - }) - .findFirst() - .ifPresent(ignore -> { - throw new IllegalArgumentException("사다리 다리 연결이 연속될 수 없습니다."); - }); - } -} diff --git a/src/main/java/ladder/domain/BridgeLinesRandomStrategy.java b/src/main/java/ladder/domain/BridgeLinesRandomStrategy.java deleted file mode 100644 index 002cee3c1e..0000000000 --- a/src/main/java/ladder/domain/BridgeLinesRandomStrategy.java +++ /dev/null @@ -1,46 +0,0 @@ -package ladder.domain; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Random; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -public class BridgeLinesRandomStrategy implements BridgeLinesStrategy { - - private static final Random RANDOM = new Random(); - - @Override - public BridgeLines create(final int amount, final LadderHeight height) { - List bridgeLines = new ArrayList<>() {{ - add(createBridgeLine(height)); - }}; - - IntStream.range(1, amount) - .forEach(index -> bridgeLines.add(createBridgeLine(bridgeLines.get(index - 1), height))); - - return new BridgeLines(bridgeLines); - } - - private BridgeLine createBridgeLine(final LadderHeight height) { - return this.createBridgeLine(null, height); - } - - private BridgeLine createBridgeLine(final BridgeLine previousBridgeLine, final LadderHeight height) { - List connections = IntStream.range(0, height.getValue()) - .mapToObj(h -> { - if (!Objects.isNull(previousBridgeLine) && previousBridgeLine.isConnected(h)) { - return false; - } - - return connectRandomly(); - }) - .collect(Collectors.toList()); - return new BridgeLine(connections); - } - - private boolean connectRandomly() { - return RANDOM.nextBoolean(); - } -} diff --git a/src/main/java/ladder/domain/BridgeLinesStrategy.java b/src/main/java/ladder/domain/BridgeLinesStrategy.java deleted file mode 100644 index e6b5f854cd..0000000000 --- a/src/main/java/ladder/domain/BridgeLinesStrategy.java +++ /dev/null @@ -1,6 +0,0 @@ -package ladder.domain; - -public interface BridgeLinesStrategy { - - BridgeLines create(final int amount, final LadderHeight height); -} diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index 08c761567c..c6b9efe1ae 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -4,12 +4,12 @@ public class Ladder { private final Participants participants; private final LadderHeight height; - private final BridgeLines bridgeLines; + private final Lines lines; - public Ladder(final Participants participants, final LadderHeight height, final BridgeLinesStrategy strategy) { + public Ladder(final Participants participants, final LadderHeight height, final Lines lines) { this.participants = participants; this.height = height; - this.bridgeLines = strategy.create(participants.size() - 1, height); + this.lines = lines; } public Participants getParticipants() { @@ -20,12 +20,12 @@ public LadderHeight getHeight() { return this.height; } - public BridgeLines getBridgeLines() { - return bridgeLines; + public Lines getLines() { + return this.lines; } - public BridgeLine getBridgeLine(final int index) { - return this.bridgeLines.getBridgeLines() - .get(index); + public Line getLine(final int index) { + return this.lines.getLines() + .get(index); } } diff --git a/src/main/java/ladder/domain/LadderFactory.java b/src/main/java/ladder/domain/LadderFactory.java new file mode 100644 index 0000000000..361db92b3e --- /dev/null +++ b/src/main/java/ladder/domain/LadderFactory.java @@ -0,0 +1,17 @@ +package ladder.domain; + +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LadderFactory { + + private LadderFactory() { + } + + public static Ladder create(final Participants participants, final LadderHeight height) { + Lines lines = new Lines(IntStream.range(0, height.getValue()) + .mapToObj(index -> LineFactory.create(participants.size())) + .collect(Collectors.toList())); + return new Ladder(participants, height, lines); + } +} diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java new file mode 100644 index 0000000000..a01bf70290 --- /dev/null +++ b/src/main/java/ladder/domain/Line.java @@ -0,0 +1,44 @@ +package ladder.domain; + +import java.util.List; +import java.util.Objects; +import java.util.stream.IntStream; + +public class Line { + + private final List connections; + + public Line(final List connections) { + validateSizeOrThrow(connections); + validateContinuousConnectionsOrThrow(connections); + + this.connections = connections; + } + + public int size() { + return this.connections.size(); + } + + public boolean isConnected(final int index) { + return this.connections.get(index); + } + + private void validateSizeOrThrow(final List connections) { + if (Objects.isNull(connections) || connections.isEmpty()) { + throw new IllegalArgumentException("사다리 라인은 최소 1개 이상의 연결 정보를 가져야합니다."); + } + } + + private void validateContinuousConnectionsOrThrow(final List connections) { + if (connections.size() == 1) { + return; + } + + IntStream.range(1, connections.size()) + .filter(index -> connections.get(index - 1) && connections.get(index)) + .findAny() + .ifPresent(index -> { + throw new IllegalArgumentException("2개 이상의 연결된 다리는 가질 수 없습니다."); + }); + } +} diff --git a/src/main/java/ladder/domain/LineFactory.java b/src/main/java/ladder/domain/LineFactory.java new file mode 100644 index 0000000000..8e22fcad8a --- /dev/null +++ b/src/main/java/ladder/domain/LineFactory.java @@ -0,0 +1,33 @@ +package ladder.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class LineFactory { + + private static final Random RANDOM = new Random(); + + private LineFactory() { + } + + public static Line create(final int size) { + List connections = new ArrayList<>(); + + for (int i = 0; i < size; i++) { + if (connections.isEmpty()) { + connections.add(RANDOM.nextBoolean()); + continue; + } + + if (connections.get(connections.size() - 1)) { + connections.add(false); + continue; + } + + connections.add(RANDOM.nextBoolean()); + } + + return new Line(connections); + } +} diff --git a/src/main/java/ladder/domain/Lines.java b/src/main/java/ladder/domain/Lines.java new file mode 100644 index 0000000000..aa72cd71bf --- /dev/null +++ b/src/main/java/ladder/domain/Lines.java @@ -0,0 +1,36 @@ +package ladder.domain; + +import java.util.List; +import java.util.Objects; + +public class Lines { + + private final List lines; + + public Lines(final List lines) { + validateLinesOrThrow(lines); + + this.lines = lines; + } + + public int size() { + return this.lines.size(); + } + + public List getLines() { + return this.lines; + } + + private void validateLinesOrThrow(final List lines) { + if (Objects.isNull(lines) || lines.isEmpty()) { + throw new IllegalArgumentException("최소 1개 이상의 사다리 라인이 필요합니다."); + } + + if (lines.stream() + .map(Line::size) + .distinct() + .count() != 1) { + throw new IllegalArgumentException("모든 사다리 라인의 사이즈는 동일해야 합니다."); + } + } +} diff --git a/src/main/java/ladder/view/ResultView.java b/src/main/java/ladder/view/ResultView.java index 4ea23286bd..3a55c8281b 100644 --- a/src/main/java/ladder/view/ResultView.java +++ b/src/main/java/ladder/view/ResultView.java @@ -38,8 +38,8 @@ public static void print(final Ladder ladder) { continue; } - if (ladder.getBridgeLine(i - 1) - .isConnected(h)) { + if (ladder.getLine(h) + .isConnected(i - 1)) { System.out.print(CONNECTION_STRING + LINE_STRING); continue; } diff --git a/src/test/java/ladder/domain/BridgeLineTest.java b/src/test/java/ladder/domain/BridgeLineTest.java deleted file mode 100644 index 05f74978e8..0000000000 --- a/src/test/java/ladder/domain/BridgeLineTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package ladder.domain; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -public class BridgeLineTest { - - @DisplayName("사다리 다리 라인 생성") - @Test - void create() { - BridgeLine bridgeLine = new BridgeLine(List.of(true, false, true, true)); - assertThat(bridgeLine) - .isEqualTo(new BridgeLine(List.of(true, false, true, true))); - } -} diff --git a/src/test/java/ladder/domain/BridgeLinesTest.java b/src/test/java/ladder/domain/BridgeLinesTest.java deleted file mode 100644 index b15b9f026c..0000000000 --- a/src/test/java/ladder/domain/BridgeLinesTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package ladder.domain; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -public class BridgeLinesTest { - - @DisplayName("사다리 다리 라인 목록 생성") - @Test - void create() { - assertDoesNotThrow(() -> { - new BridgeLines(List.of( - new BridgeLine(List.of(true)), - new BridgeLine(List.of(false)) - )); - }); - } - - @DisplayName("사다리 다리 라인 목록 생성 실패 - 외다리") - @Test - void invalid_single() { - assertThatThrownBy(() -> new BridgeLines(List.of( - new BridgeLine(List.of(true)) - ))).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("사다리 다리 라인은 최소 2개 이상이여야 합니다."); - } - - @DisplayName("사다리 다리 라인 목록 생성 실패 - 다리 라인 높이 다름") - @Test - void invalid_differentHeight() { - assertThatThrownBy(() -> new BridgeLines(List.of( - new BridgeLine(List.of(true)), - new BridgeLine(List.of(true, false)) - ))).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("사다리 다리 라인들의 높이는 동일해야 합니다."); - } - - @DisplayName("사다리 다리 라인 목록 생성 실패 - 다리 연결이 연속됨") - @Test - void invalid_continuousConnection() { - assertThatThrownBy(() -> new BridgeLines(List.of( - new BridgeLine(List.of(false, true, true)), - new BridgeLine(List.of(true, true, false)) - ))).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("사다리 다리 연결이 연속될 수 없습니다."); - } -} diff --git a/src/test/java/ladder/domain/LadderTest.java b/src/test/java/ladder/domain/LadderTest.java index 29894fec6d..41a4cb0363 100644 --- a/src/test/java/ladder/domain/LadderTest.java +++ b/src/test/java/ladder/domain/LadderTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -17,7 +18,7 @@ void create() { .map(Participant::new) .collect(Collectors.toList())); final LadderHeight ladderHeight = new LadderHeight(5); - final Ladder ladder = new Ladder(participants, ladderHeight, new BridgeLinesRandomStrategy()); + final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true))))); assertThat(ladder.getParticipants()).isEqualTo(new Participants(Stream.of("woody", "jacob", "tate", "test") .map(Participant::new) @@ -25,7 +26,7 @@ void create() { assertThat(ladder.getParticipants() .size()).isEqualTo(4); assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5)); - assertThat(ladder.getBridgeLines() - .size()).isEqualTo(3); + assertThat(ladder.getLines() + .size()).isEqualTo(1); } } diff --git a/src/test/java/ladder/domain/LineTest.java b/src/test/java/ladder/domain/LineTest.java new file mode 100644 index 0000000000..0a60dc66c7 --- /dev/null +++ b/src/test/java/ladder/domain/LineTest.java @@ -0,0 +1,40 @@ +package ladder.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class LineTest { + + @DisplayName("사다리 라인 생성") + @Test + void create() { + Line line = new Line(List.of(true, false, false)); + assertAll( + () -> assertThat(line).isNotNull(), + () -> assertThat(line.size()).isEqualTo(3) + ); + } + + @DisplayName("사다리 라인 실패 - 빈 연결") + @Test + void invalidConnections() { + assertThatThrownBy(() -> new Line(Collections.emptyList())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("사다리 라인은 최소 1개 이상의 연결 정보를 가져야합니다."); + } + + @DisplayName("사다리 라인 실패 - 연속된 연결 정보") + @Test + void continuousConnections() { + assertThatThrownBy(() -> new Line(List.of(true, true))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("2개 이상의 연결된 다리는 가질 수 없습니다."); + } +} diff --git a/src/test/java/ladder/domain/LinesTest.java b/src/test/java/ladder/domain/LinesTest.java new file mode 100644 index 0000000000..4827a07f07 --- /dev/null +++ b/src/test/java/ladder/domain/LinesTest.java @@ -0,0 +1,52 @@ +package ladder.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +public class LinesTest { + + @DisplayName("사다리 라인 목록 생성") + @Test + void create() { + assertDoesNotThrow(() -> new Lines( + Arrays.asList( + new Line(Arrays.asList(true, false, true, false)), + new Line(Arrays.asList(true, false, true, false)) + ) + )); + } + + @DisplayName("사다리 라인 목록 생성 실패 - null") + @Test + void invalid_null() { + assertThatThrownBy(() -> new Lines(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("최소 1개 이상의 사다리 라인이 필요합니다."); + } + + @DisplayName("사다리 라인 목록 생성 실패 - emptyList") + @Test + void invalid_emptyList() { + assertThatThrownBy(() -> new Lines(Collections.emptyList())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("최소 1개 이상의 사다리 라인이 필요합니다."); + } + + @DisplayName("사다리 라인 목록 생성 실패 - diffLineSize") + @Test + void invalid_diffLineSize() { + assertThatThrownBy(() -> new Lines( + Arrays.asList( + new Line(Arrays.asList(true, false, true, false)), + new Line(Arrays.asList(true, false, true)) + ) + )).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("모든 사다리 라인의 사이즈는 동일해야 합니다."); + } +} diff --git a/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java b/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java deleted file mode 100644 index 5e85516275..0000000000 --- a/src/test/java/ladder/strategy/BridgeLinesRandomStrategyTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package ladder.strategy; - -import ladder.domain.BridgeLines; -import ladder.domain.BridgeLinesRandomStrategy; -import ladder.domain.BridgeLinesStrategy; -import ladder.domain.LadderHeight; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -public class BridgeLinesRandomStrategyTest { - - @DisplayName("사다리 다리 라인들 생성 테스트") - @Test - void create() { - BridgeLines bridgeLines = assertDoesNotThrow(() -> { - BridgeLinesStrategy strategy = new BridgeLinesRandomStrategy(); - return strategy.create(4, new LadderHeight(5)); - }); - - assertThat(bridgeLines).isNotNull(); - } -} From 563d83ec2211412b90335c024d97b8a28b144645 Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Tue, 8 Nov 2022 19:33:55 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20Ladder=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=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 --- src/main/java/ladder/domain/Ladder.java | 10 ++++++ .../java/ladder/domain/LadderHeightTest.java | 8 +++++ src/test/java/ladder/domain/LadderTest.java | 35 ++++++++++++++----- .../java/ladder/domain/ParticipantsTest.java | 9 +++++ 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index c6b9efe1ae..ac6abf8784 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -1,5 +1,7 @@ package ladder.domain; +import java.util.Objects; + public class Ladder { private final Participants participants; @@ -7,6 +9,8 @@ public class Ladder { private final Lines lines; public Ladder(final Participants participants, final LadderHeight height, final Lines lines) { + validateOrThrow(participants, height, lines); + this.participants = participants; this.height = height; this.lines = lines; @@ -28,4 +32,10 @@ public Line getLine(final int index) { return this.lines.getLines() .get(index); } + + private void validateOrThrow(final Participants participants, final LadderHeight height, final Lines lines) { + if (Objects.isNull(participants) || Objects.isNull(height) || Objects.isNull(lines)) { + throw new IllegalArgumentException("null인 멤버 변수를 포함할 수 없습니다."); + } + } } diff --git a/src/test/java/ladder/domain/LadderHeightTest.java b/src/test/java/ladder/domain/LadderHeightTest.java index d8f3e43f89..8cfecea007 100644 --- a/src/test/java/ladder/domain/LadderHeightTest.java +++ b/src/test/java/ladder/domain/LadderHeightTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LadderHeightTest { @@ -14,4 +15,11 @@ void invalid() { .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("유효하지 않은 사다리 높이입니다."); } + + @DisplayName("생성") + @Test + void create() { + LadderHeight ladderHeight = new LadderHeight(7); + assertThat(ladderHeight).isEqualTo(new LadderHeight(7)); + } } diff --git a/src/test/java/ladder/domain/LadderTest.java b/src/test/java/ladder/domain/LadderTest.java index 41a4cb0363..505b27e91d 100644 --- a/src/test/java/ladder/domain/LadderTest.java +++ b/src/test/java/ladder/domain/LadderTest.java @@ -8,6 +8,8 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; public class LadderTest { @@ -20,13 +22,30 @@ void create() { final LadderHeight ladderHeight = new LadderHeight(5); final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true))))); - assertThat(ladder.getParticipants()).isEqualTo(new Participants(Stream.of("woody", "jacob", "tate", "test") - .map(Participant::new) - .collect(Collectors.toList()))); - assertThat(ladder.getParticipants() - .size()).isEqualTo(4); - assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5)); - assertThat(ladder.getLines() - .size()).isEqualTo(1); + assertAll( + () -> assertThat(ladder.getParticipants() + .size()).isEqualTo(4), + () -> assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5)), + () -> assertThat(ladder.getLines() + .size()).isEqualTo(1) + ); + } + + @DisplayName("사다리 생성 실패") + @Test + void invalid() { + final Participants participants = new Participants(Stream.of("woody", "jacob", "tate", "test") + .map(Participant::new) + .collect(Collectors.toList())); + final LadderHeight ladderHeight = new LadderHeight(5); + final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true))))); + + assertAll( + () -> assertThatThrownBy(() -> new Ladder(null, ladderHeight, + new Lines(List.of(new Line(List.of(true)))))), + () -> assertThatThrownBy(() -> new Ladder(participants, null, + new Lines(List.of(new Line(List.of(true)))))), + () -> assertThatThrownBy(() -> new Ladder(participants, ladderHeight, null)) + ); } } diff --git a/src/test/java/ladder/domain/ParticipantsTest.java b/src/test/java/ladder/domain/ParticipantsTest.java index 3e13dd20e4..c4f06ba0cb 100644 --- a/src/test/java/ladder/domain/ParticipantsTest.java +++ b/src/test/java/ladder/domain/ParticipantsTest.java @@ -4,7 +4,9 @@ import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class ParticipantsTest { @@ -16,4 +18,11 @@ void invalid() { .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("참가자는 최소 1명 이상이여야 합니다."); } + + @DisplayName("생성") + @Test + void create() { + Participants participants = new Participants(List.of(new Participant("woody"))); + assertThat(participants).isEqualTo(new Participants(List.of(new Participant("woody")))); + } } From b15eeced969a42e791ad2a9ec7c30cba2df2ac8f Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Tue, 8 Nov 2022 19:38:30 +0900 Subject: [PATCH 6/8] =?UTF-8?q?docs:=203=EB=8B=A8=EA=B3=84=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=20=EC=97=85=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/README.md b/README.md index d9d08fc49c..900dee3422 100644 --- a/README.md +++ b/README.md @@ -87,3 +87,57 @@ pobi honux crong jk | |-----| | |-----| |-----| ``` + +## 3단계 - 사다리(게임 실행) + +### 기능 요구사항 + +- 사다리 실행 결과를 출력해야 한다. +- 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다. + +### 프로그래밍 요구사항 + +- 자바 8의 스트림과 람다를 적용해 프로그래밍한다. +- 규칙 6: 모든 엔티티를 작게 유지한다. +- 규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. + +#### 실행 결과 + +- 위 요구사항에 따라 4명의 사람을 위한 5개 높이 사다리를 만들 경우, 프로그램을 실행한 결과는 다음과 같다. + +```text +참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요) +pobi,honux,crong,jk + +실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요) +꽝,5000,꽝,3000 + +최대 사다리 높이는 몇 개인가요? +5 + +사다리 결과 + +pobi honux crong jk + |-----| |-----| + | |-----| | + |-----| | | + | |-----| | + |-----| |-----| +꽝 5000 꽝 3000 + +결과를 보고 싶은 사람은? +pobi + +실행 결과 +꽝 + +결과를 보고 싶은 사람은? +all + +실행 결과 +pobi : 꽝 +honux : 3000 +crong : 꽝 +jk : 5000 +``` + From 92fd74d94d90770d240fe7caf5162214da2b6061 Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Tue, 8 Nov 2022 19:54:34 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20Ladder=20=EB=A9=A4=EB=B2=84?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=A4=91=20"LadderHeight"=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/domain/Ladder.java | 16 +++++++--------- src/main/java/ladder/domain/LadderFactory.java | 2 +- src/main/java/ladder/view/ResultView.java | 4 +--- src/test/java/ladder/domain/LadderTest.java | 12 +++--------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index ac6abf8784..c621894555 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -5,14 +5,12 @@ public class Ladder { private final Participants participants; - private final LadderHeight height; private final Lines lines; - public Ladder(final Participants participants, final LadderHeight height, final Lines lines) { - validateOrThrow(participants, height, lines); + public Ladder(final Participants participants, final Lines lines) { + validateOrThrow(participants, lines); this.participants = participants; - this.height = height; this.lines = lines; } @@ -20,8 +18,8 @@ public Participants getParticipants() { return new Participants(participants); } - public LadderHeight getHeight() { - return this.height; + public int getHeight() { + return this.lines.size(); } public Lines getLines() { @@ -33,9 +31,9 @@ public Line getLine(final int index) { .get(index); } - private void validateOrThrow(final Participants participants, final LadderHeight height, final Lines lines) { - if (Objects.isNull(participants) || Objects.isNull(height) || Objects.isNull(lines)) { - throw new IllegalArgumentException("null인 멤버 변수를 포함할 수 없습니다."); + private void validateOrThrow(final Participants participants, final Lines lines) { + if (Objects.isNull(participants) || Objects.isNull(lines)) { + throw new IllegalArgumentException("참가자나 사다리 라인은 null이 될 수 없습니다."); } } } diff --git a/src/main/java/ladder/domain/LadderFactory.java b/src/main/java/ladder/domain/LadderFactory.java index 361db92b3e..e4885f1bca 100644 --- a/src/main/java/ladder/domain/LadderFactory.java +++ b/src/main/java/ladder/domain/LadderFactory.java @@ -12,6 +12,6 @@ public static Ladder create(final Participants participants, final LadderHeight Lines lines = new Lines(IntStream.range(0, height.getValue()) .mapToObj(index -> LineFactory.create(participants.size())) .collect(Collectors.toList())); - return new Ladder(participants, height, lines); + return new Ladder(participants, lines); } } diff --git a/src/main/java/ladder/view/ResultView.java b/src/main/java/ladder/view/ResultView.java index 3a55c8281b..1f3f000f63 100644 --- a/src/main/java/ladder/view/ResultView.java +++ b/src/main/java/ladder/view/ResultView.java @@ -27,11 +27,9 @@ public static void print(final Ladder ladder) { .map(name -> addLeftPaddingString(name, Participant.MAXIMUM_NAME_LENGTH)) .collect(Collectors.joining(" "))); - final int intLadderHeight = ladder.getHeight() - .getValue(); final int participantsSize = ladder.getParticipants() .size(); - for (int h = 0; h < intLadderHeight; h++) { + for (int h = 0; h < ladder.getHeight(); h++) { for (int i = 0; i < participantsSize; i++) { if (i == 0) { System.out.print(DISCONNECTION_STRING + LINE_STRING); diff --git a/src/test/java/ladder/domain/LadderTest.java b/src/test/java/ladder/domain/LadderTest.java index 505b27e91d..d7294ca862 100644 --- a/src/test/java/ladder/domain/LadderTest.java +++ b/src/test/java/ladder/domain/LadderTest.java @@ -19,13 +19,11 @@ void create() { final Participants participants = new Participants(Stream.of("woody", "jacob", "tate", "test") .map(Participant::new) .collect(Collectors.toList())); - final LadderHeight ladderHeight = new LadderHeight(5); - final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true))))); + final Ladder ladder = new Ladder(participants, new Lines(List.of(new Line(List.of(true))))); assertAll( () -> assertThat(ladder.getParticipants() .size()).isEqualTo(4), - () -> assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5)), () -> assertThat(ladder.getLines() .size()).isEqualTo(1) ); @@ -37,15 +35,11 @@ void invalid() { final Participants participants = new Participants(Stream.of("woody", "jacob", "tate", "test") .map(Participant::new) .collect(Collectors.toList())); - final LadderHeight ladderHeight = new LadderHeight(5); - final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true))))); assertAll( - () -> assertThatThrownBy(() -> new Ladder(null, ladderHeight, + () -> assertThatThrownBy(() -> new Ladder(null, new Lines(List.of(new Line(List.of(true)))))), - () -> assertThatThrownBy(() -> new Ladder(participants, null, - new Lines(List.of(new Line(List.of(true)))))), - () -> assertThatThrownBy(() -> new Ladder(participants, ladderHeight, null)) + () -> assertThatThrownBy(() -> new Ladder(participants, null)) ); } } From dd87d38ee386424e70d069823b0bafb47fd4b1ce Mon Sep 17 00:00:00 2001 From: mjin1220 Date: Tue, 8 Nov 2022 21:04:00 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20LadderResult=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/LadderMain.java | 24 ++++++++- src/main/java/ladder/domain/Ladder.java | 50 ++++++++++++++++++- src/main/java/ladder/domain/LadderResult.java | 37 ++++++++++++++ .../java/ladder/domain/LadderResults.java | 42 ++++++++++++++++ src/main/java/ladder/domain/Lines.java | 11 +++- src/main/java/ladder/domain/Participants.java | 4 ++ src/main/java/ladder/view/InputView.java | 15 ++++++ src/main/java/ladder/view/ResultView.java | 38 +++++++++++++- .../java/ladder/domain/LadderResultTest.java | 30 +++++++++++ .../java/ladder/domain/LadderResultsTest.java | 39 +++++++++++++++ src/test/java/ladder/domain/LadderTest.java | 2 +- 11 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 src/main/java/ladder/domain/LadderResult.java create mode 100644 src/main/java/ladder/domain/LadderResults.java create mode 100644 src/test/java/ladder/domain/LadderResultTest.java create mode 100644 src/test/java/ladder/domain/LadderResultsTest.java diff --git a/src/main/java/ladder/LadderMain.java b/src/main/java/ladder/LadderMain.java index 613b10bb32..f033e59628 100644 --- a/src/main/java/ladder/LadderMain.java +++ b/src/main/java/ladder/LadderMain.java @@ -8,13 +8,35 @@ public class LadderMain { + private static final String LADDER_RESULT_CONFIRM_COMMAND_ALL = "all"; + private static final String LADDER_RESULT_CONFIRM_COMMAND_BREAK = "-1"; + public static void main(String[] args) { final Participants participants = new Participants(InputView.inputParticipantNames() .stream() .map(Participant::new) .collect(Collectors.toList())); + final LadderResults ladderResults = new LadderResults(InputView.inputLadderResults() + .stream() + .map(LadderResult::new) + .collect(Collectors.toList())); final LadderHeight ladderHeight = new LadderHeight(InputView.inputLadderHeight()); final Ladder ladder = LadderFactory.create(participants, ladderHeight); - ResultView.print(ladder); + ResultView.printLadder(ladder, ladderResults); + + while (true) { + final String targetParticipantName = InputView.inputTargetLadderParticipantName(); + if (LADDER_RESULT_CONFIRM_COMMAND_ALL.equalsIgnoreCase(targetParticipantName)) { + ResultView.printAllLadderResults(ladder, ladderResults); + continue; + } + + if (LADDER_RESULT_CONFIRM_COMMAND_BREAK.equalsIgnoreCase(targetParticipantName)) { + ResultView.printConfirmationEndMessage(); + break; + } + + ResultView.printLadderResult(ladder, ladderResults, new Participant(targetParticipantName)); + } } } diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index c621894555..0e3daeea0f 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -1,6 +1,7 @@ package ladder.domain; import java.util.Objects; +import java.util.stream.IntStream; public class Ladder { @@ -19,7 +20,7 @@ public Participants getParticipants() { } public int getHeight() { - return this.lines.size(); + return this.lines.height(); } public Lines getLines() { @@ -31,6 +32,53 @@ public Line getLine(final int index) { .get(index); } + public int getLastVerticalLineIndex(final Participant participant) { + return this.getLastVerticalLineIndex(this.participants.indexOf(participant)); + } + + private int getLastVerticalLineIndex(final int firstVerticalLineIndex) { + return IntStream.range(0, this.lines.height()) + .reduce(firstVerticalLineIndex, (curVerticalLineIndex, curLineHeight) -> { + if (canMoveLeft(curVerticalLineIndex, curLineHeight)) { + return curVerticalLineIndex - 1; + } + + if (canMoveRight(curVerticalLineIndex, curLineHeight)) { + return curVerticalLineIndex + 1; + } + + return curVerticalLineIndex; + }); + } + + private boolean canMoveLeft(final int verticalLineIndex, final int curLineHeight) { + if (isFirstVerticalLineIndex(verticalLineIndex)) { + return false; + } + + return this.lines.getLine(curLineHeight) + .isConnected(verticalLineIndex - 1); + } + + private boolean canMoveRight(final int verticalLineIndex, final int curLineHeight) { + if (isLastVerticalLineIndex(verticalLineIndex)) { + return false; + } + + return this.lines.getLine(curLineHeight) + .isConnected(verticalLineIndex); + } + + + private boolean isFirstVerticalLineIndex(final int verticalLineIndex) { + return verticalLineIndex == 0; + } + + + private boolean isLastVerticalLineIndex(final int verticalLineIndex) { + return verticalLineIndex == this.lines.width(); + } + private void validateOrThrow(final Participants participants, final Lines lines) { if (Objects.isNull(participants) || Objects.isNull(lines)) { throw new IllegalArgumentException("참가자나 사다리 라인은 null이 될 수 없습니다."); diff --git a/src/main/java/ladder/domain/LadderResult.java b/src/main/java/ladder/domain/LadderResult.java new file mode 100644 index 0000000000..6c3ef22325 --- /dev/null +++ b/src/main/java/ladder/domain/LadderResult.java @@ -0,0 +1,37 @@ +package ladder.domain; + +import java.util.Objects; + +public class LadderResult { + + private final String value; + + public LadderResult(final String value) { + validateOrThrow(value); + + this.value = value; + } + + private void validateOrThrow(final String value) { + if (Objects.isNull(value) || value.isBlank()) { + throw new IllegalArgumentException("사다리 결과는 빈 문자열일 수 없습니다."); + } + } + + public String getValue() { + return this.value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LadderResult that = (LadderResult) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } +} diff --git a/src/main/java/ladder/domain/LadderResults.java b/src/main/java/ladder/domain/LadderResults.java new file mode 100644 index 0000000000..7427b6b9a6 --- /dev/null +++ b/src/main/java/ladder/domain/LadderResults.java @@ -0,0 +1,42 @@ +package ladder.domain; + +import java.util.List; +import java.util.Objects; + +public class LadderResults { + + private final List ladderResults; + + public LadderResults(final List ladderResults) { + validateOrThrow(ladderResults); + + this.ladderResults = ladderResults; + } + + private void validateOrThrow(final List ladderResults) { + if (Objects.isNull(ladderResults) || ladderResults.isEmpty()) { + throw new IllegalArgumentException("빈 사다리 결과는 생성될 수 없습니다."); + } + } + + public List getLadderResults() { + return this.ladderResults; + } + + public LadderResult get(final int index) { + return this.ladderResults.get(index); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LadderResults that = (LadderResults) o; + return Objects.equals(ladderResults, that.ladderResults); + } + + @Override + public int hashCode() { + return Objects.hash(ladderResults); + } +} diff --git a/src/main/java/ladder/domain/Lines.java b/src/main/java/ladder/domain/Lines.java index aa72cd71bf..33043bf952 100644 --- a/src/main/java/ladder/domain/Lines.java +++ b/src/main/java/ladder/domain/Lines.java @@ -13,14 +13,23 @@ public Lines(final List lines) { this.lines = lines; } - public int size() { + public int height() { return this.lines.size(); } + public int width() { + return this.lines.get(0) + .size(); + } + public List getLines() { return this.lines; } + public Line getLine(final int index) { + return this.lines.get(index); + } + private void validateLinesOrThrow(final List lines) { if (Objects.isNull(lines) || lines.isEmpty()) { throw new IllegalArgumentException("최소 1개 이상의 사다리 라인이 필요합니다."); diff --git a/src/main/java/ladder/domain/Participants.java b/src/main/java/ladder/domain/Participants.java index cef3c24b31..d2e9bed4bb 100644 --- a/src/main/java/ladder/domain/Participants.java +++ b/src/main/java/ladder/domain/Participants.java @@ -30,6 +30,10 @@ public int size() { return this.participants.size(); } + public int indexOf(final Participant participant) { + return this.participants.indexOf(participant); + } + private void validateOrThrow(final List participants) { if (Objects.isNull(participants) || participants.size() == 0) { throw new IllegalArgumentException("참가자는 최소 1명 이상이여야 합니다."); diff --git a/src/main/java/ladder/view/InputView.java b/src/main/java/ladder/view/InputView.java index 7c7eaa8e20..f6c6537753 100644 --- a/src/main/java/ladder/view/InputView.java +++ b/src/main/java/ladder/view/InputView.java @@ -27,4 +27,19 @@ public static int inputLadderHeight() { SCANNER.skip(System.lineSeparator()); return intLadderHeight; } + + public static List inputLadderResults() { + System.out.println(); + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + return Stream.of(SCANNER.nextLine() + .split(",")) + .map(String::trim) + .collect(Collectors.toList()); + } + + public static String inputTargetLadderParticipantName() { + System.out.println(); + System.out.println("결과를 보고 싶은 사람은?"); + return SCANNER.nextLine(); + } } diff --git a/src/main/java/ladder/view/ResultView.java b/src/main/java/ladder/view/ResultView.java index 1f3f000f63..55352ec2f8 100644 --- a/src/main/java/ladder/view/ResultView.java +++ b/src/main/java/ladder/view/ResultView.java @@ -1,6 +1,8 @@ package ladder.view; import ladder.domain.Ladder; +import ladder.domain.LadderResult; +import ladder.domain.LadderResults; import ladder.domain.Participant; import java.util.stream.Collectors; @@ -14,9 +16,9 @@ public class ResultView { private ResultView() { } - public static void print(final Ladder ladder) { + public static void printLadder(final Ladder ladder, final LadderResults ladderResults) { System.out.println(); - System.out.println("실행결과"); + System.out.println("사다리 결과"); System.out.println(); System.out.print(" "); @@ -46,9 +48,41 @@ public static void print(final Ladder ladder) { } System.out.println(); } + + System.out.println(ladderResults.getLadderResults() + .stream() + .map(LadderResult::getValue) + .map(name -> addLeftPaddingString(name, Participant.MAXIMUM_NAME_LENGTH)) + .collect(Collectors.joining(" "))); + } + + public static void printAllLadderResults(final Ladder ladder, final LadderResults ladderResults) { + System.out.println(); + System.out.println("실행 결과"); + ladder.getParticipants() + .getParticipants() + .forEach(participant -> + System.out.printf("%s : %s%n", + participant.getName(), + ladderResults.get(ladder.getLastVerticalLineIndex(participant)) + .getValue())); + } + + public static void printLadderResult(final Ladder ladder, + final LadderResults ladderResults, + final Participant targetParticipant) { + System.out.println(); + System.out.println("실행 결과"); + System.out.println(ladderResults.get(ladder.getLastVerticalLineIndex(targetParticipant)) + .getValue()); } private static String addLeftPaddingString(final String string, final int size) { return String.format("%" + size + "s", string); } + + public static void printConfirmationEndMessage() { + System.out.println(); + System.out.println("-1을 입력하여 종료합니다."); + } } diff --git a/src/test/java/ladder/domain/LadderResultTest.java b/src/test/java/ladder/domain/LadderResultTest.java new file mode 100644 index 0000000000..99d4a1d96d --- /dev/null +++ b/src/test/java/ladder/domain/LadderResultTest.java @@ -0,0 +1,30 @@ +package ladder.domain; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class LadderResultTest { + + @DisplayName("생성") + @Test + void create() { + LadderResult ladderResult = new LadderResult("꽝"); + Assertions.assertThat(ladderResult) + .isEqualTo(new LadderResult("꽝")); + } + + @ParameterizedTest(name = "생성 실패 - {0}") + @NullSource + @ValueSource(strings = {"", " "}) + void invalid(final String input) { + assertThatThrownBy(() -> new LadderResult(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("사다리 결과는 빈 문자열일 수 없습니다."); + } +} diff --git a/src/test/java/ladder/domain/LadderResultsTest.java b/src/test/java/ladder/domain/LadderResultsTest.java new file mode 100644 index 0000000000..090e0d698f --- /dev/null +++ b/src/test/java/ladder/domain/LadderResultsTest.java @@ -0,0 +1,39 @@ +package ladder.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class LadderResultsTest { + + @DisplayName("생성") + @Test + void create() { + LadderResults ladderResults = new LadderResults(List.of(new LadderResult("꽝"))); + assertThat(ladderResults).isEqualTo(new LadderResults(List.of(new LadderResult("꽝")))); + } + + @DisplayName("실패") + @Test + void invalid() { + assertThatThrownBy(() -> new LadderResults(List.of())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("빈 사다리 결과는 생성될 수 없습니다."); + } + + @DisplayName("사다리 결과 가져오기") + @Test + void get() { + LadderResults ladderResults = new LadderResults( + List.of(new LadderResult("1000"), new LadderResult("2000"))); + assertAll( + () -> assertThat(ladderResults.get(0)).isEqualTo(new LadderResult("1000")), + () -> assertThat(ladderResults.get(1)).isEqualTo(new LadderResult("2000")) + ); + } +} diff --git a/src/test/java/ladder/domain/LadderTest.java b/src/test/java/ladder/domain/LadderTest.java index d7294ca862..13760e63ce 100644 --- a/src/test/java/ladder/domain/LadderTest.java +++ b/src/test/java/ladder/domain/LadderTest.java @@ -25,7 +25,7 @@ void create() { () -> assertThat(ladder.getParticipants() .size()).isEqualTo(4), () -> assertThat(ladder.getLines() - .size()).isEqualTo(1) + .height()).isEqualTo(1) ); }