From 0756ca3b86dffb1ff85cb1f22d4aab2778c4cfca Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Sat, 16 Dec 2023 09:55:14 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor=20:=20=EB=9D=BC=EC=9D=B8=EC=9D=98?= =?UTF-8?q?=20=EC=9D=B4=EC=A0=84,=20=ED=98=84=EC=9E=AC=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=EB=A5=BC=20=EA=B0=80=EC=A7=80=EB=8A=94=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 10 ++++ .../java/nextstep/ladder/domain/Line.java | 46 ++++--------------- .../java/nextstep/ladder/domain/Point.java | 39 ++++++++++++++++ .../java/nextstep/ladder/view/ResultView.java | 2 +- .../java/nextstep/ladder/domain/LineTest.java | 2 +- 5 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 STEP3.md create mode 100644 src/main/java/nextstep/ladder/domain/Point.java diff --git a/STEP3.md b/STEP3.md new file mode 100644 index 0000000000..2fdb1a8fc8 --- /dev/null +++ b/STEP3.md @@ -0,0 +1,10 @@ +## STEP3 기능 요구사항 +* 사다리 실행 결과를 출력해야 한다. +* 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다. +## 프로그래밍 요구사항 +자바 8의 스트림과 람다를 적용해 프로그래밍한다. +규칙 6: 모든 엔티티를 작게 유지한다. +규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. + +## STEP2 보완사항 +* [X] Ladder 생성, List -> 이전상태와 현재상태를 가지고 있는 객체 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index 4732bee05e..fbc70dc896 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -1,59 +1,31 @@ package nextstep.ladder.domain; -import nextstep.ladder.utils.RandomLineGenerator; - import java.util.ArrayList; import java.util.List; -import java.util.stream.IntStream; public class Line { - private final List points; + private final ArrayList points; public Line(int countOfParticipant) { this(generateRandomLine(countOfParticipant)); - checkForConsecutiveTrues(this.getPoints()); - } - - public Line(int countOfParticipant, LineGenerator lineGenerator) { - this(generateRandomLine(countOfParticipant, lineGenerator)); - checkForConsecutiveTrues(this.getPoints()); - } - - private static ArrayList generateRandomLine(int countOfParticipant) { - LineGenerator lineGenerator = new RandomLineGenerator(); - ArrayList line = new ArrayList<>(); - line.add(false); // 사다리 라인의 맨 왼쪽은 생성될 수 없다. - for (int i = 1; i < countOfParticipant; i++) { - boolean shouldGenerateLine = !line.get(i - 1) && lineGenerator.generateLine(); - line.add(shouldGenerateLine); - } - return line; } - private static ArrayList generateRandomLine(int countOfParticipant, LineGenerator lineGenerator) { - ArrayList line = new ArrayList<>(); - line.add(false); // 사다리 라인의 맨 왼쪽은 생성될 수 없다. + private static ArrayList generateRandomLine(int countOfParticipant) { + ArrayList line = new ArrayList<>(); + Point firstPoint = new Point(false, false); // 사다리 라인의 맨 왼쪽은 생성될 수 없다. + line.add(firstPoint); for (int i = 1; i < countOfParticipant; i++) { - boolean shouldGenerateLine = lineGenerator.generateLine(); - line.add(shouldGenerateLine); + Point point = Point.previousOf(line.get(i-1).getCurrent()); + line.add(point); } return line; } - private static void checkForConsecutiveTrues(List points) { - IntStream.range(0, points.size() - 1) - .filter(i -> points.get(i) && points.get(i + 1)) - .findFirst() - .ifPresent(i -> { - throw new IllegalArgumentException("사다리 라인은 연속으로 겹칠 수 없습니다."); - }); - } - - public Line(List points) { + public Line(ArrayList points) { this.points = points; } - public List getPoints() { + public List getPoints() { return points; } diff --git a/src/main/java/nextstep/ladder/domain/Point.java b/src/main/java/nextstep/ladder/domain/Point.java new file mode 100644 index 0000000000..5c1e1ce377 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/Point.java @@ -0,0 +1,39 @@ +package nextstep.ladder.domain; + +import nextstep.ladder.utils.RandomLineGenerator; + +public class Point { + private boolean previous; + private boolean current; + + + public static Point previousOf(boolean previous) { + LineGenerator lineGenerator = new RandomLineGenerator(); + return new Point(previous, !previous && lineGenerator.generateLine()); + } + + public static Point previousOf(boolean previous, LineGenerator lineGenerator) { + return new Point(previous, lineGenerator.generateLine()); + } + + public Point(boolean previous, boolean current) { + checkForConsecutiveTrue(previous, current); + this.previous = previous; + this.current = current; + } + + public boolean getPrevious() { + return previous; + } + + public boolean getCurrent() { + return current; + } + + private void checkForConsecutiveTrue(boolean previous, boolean current) { + if (previous && current) { + throw new IllegalArgumentException("사다리 라인은 연속으로 겹칠 수 없습니다."); + } + } + +} diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 65a7775d5c..7d92b23f71 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -32,7 +32,7 @@ public static void printLadder(Ladder ladder) { private static void printPoints(Line line) { line.getPoints().forEach(point -> { - System.out.print(point ? LINE : BLANK); + System.out.print(point.getCurrent() ? LINE : BLANK); System.out.print(COLUMN); }); } diff --git a/src/test/java/nextstep/ladder/domain/LineTest.java b/src/test/java/nextstep/ladder/domain/LineTest.java index 8a4a61b2d6..190a5e2fe3 100644 --- a/src/test/java/nextstep/ladder/domain/LineTest.java +++ b/src/test/java/nextstep/ladder/domain/LineTest.java @@ -16,6 +16,6 @@ public boolean generateLine() { } }; - assertThrows(IllegalArgumentException.class, () -> new Line (3, lineGenerator)); + assertThrows(IllegalArgumentException.class, () -> Point.previousOf(true , lineGenerator)); } } From 03341d1b2d684f569e999eecb7ba3df089bfd369 Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Sat, 16 Dec 2023 10:20:31 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat=20:=20=EC=8B=A4=ED=96=89=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EB=A5=BC=20=EC=9E=85=EB=A0=A5=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 13 ++++++++++-- .../java/nextstep/ladder/domain/Line.java | 14 ++++++------- .../domain/{Point.java => LineState.java} | 16 +++++++------- .../nextstep/ladder/domain/ResultInfo.java | 21 +++++++++++++++++++ .../ladder/service/LadderGameHandler.java | 16 ++++++++++---- .../java/nextstep/ladder/view/InputView.java | 7 ++++++- .../java/nextstep/ladder/view/ResultView.java | 5 +++++ .../java/nextstep/ladder/domain/LineTest.java | 2 +- 8 files changed, 71 insertions(+), 23 deletions(-) rename src/main/java/nextstep/ladder/domain/{Point.java => LineState.java} (59%) create mode 100644 src/main/java/nextstep/ladder/domain/ResultInfo.java diff --git a/STEP3.md b/STEP3.md index 2fdb1a8fc8..39130f0a9e 100644 --- a/STEP3.md +++ b/STEP3.md @@ -1,10 +1,19 @@ ## STEP3 기능 요구사항 * 사다리 실행 결과를 출력해야 한다. * 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다. + ## 프로그래밍 요구사항 자바 8의 스트림과 람다를 적용해 프로그래밍한다. 규칙 6: 모든 엔티티를 작게 유지한다. -규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. +규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. (= 2개까지만 허용) ## STEP2 보완사항 -* [X] Ladder 생성, List -> 이전상태와 현재상태를 가지고 있는 객체 \ No newline at end of file +* [X] Ladder 생성, List -> 이전상태와 현재상태를 가지고 있는 객체 + +## Step3 기능분해 +* [X] 실행 결과를 입력할 수 있다. +* [ ] 참가자는 현재 위치를 가지고 있는다. +* [ ] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. +* [ ] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. +* [ ] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. +* [ ] 전체 결과를 보는 경우 각 참가자의 모든 실행결과를 보여준다. \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index fbc70dc896..cdb296388e 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -4,28 +4,28 @@ import java.util.List; public class Line { - private final ArrayList points; + private final ArrayList points; public Line(int countOfParticipant) { this(generateRandomLine(countOfParticipant)); } - private static ArrayList generateRandomLine(int countOfParticipant) { - ArrayList line = new ArrayList<>(); - Point firstPoint = new Point(false, false); // 사다리 라인의 맨 왼쪽은 생성될 수 없다. + private static ArrayList generateRandomLine(int countOfParticipant) { + ArrayList line = new ArrayList<>(); + LineState firstPoint = new LineState(false, false); // 사다리 라인의 맨 왼쪽은 생성될 수 없다. line.add(firstPoint); for (int i = 1; i < countOfParticipant; i++) { - Point point = Point.previousOf(line.get(i-1).getCurrent()); + LineState point = LineState.previousOf(line.get(i-1).getCurrent()); line.add(point); } return line; } - public Line(ArrayList points) { + public Line(ArrayList points) { this.points = points; } - public List getPoints() { + public List getPoints() { return points; } diff --git a/src/main/java/nextstep/ladder/domain/Point.java b/src/main/java/nextstep/ladder/domain/LineState.java similarity index 59% rename from src/main/java/nextstep/ladder/domain/Point.java rename to src/main/java/nextstep/ladder/domain/LineState.java index 5c1e1ce377..b657dca7e6 100644 --- a/src/main/java/nextstep/ladder/domain/Point.java +++ b/src/main/java/nextstep/ladder/domain/LineState.java @@ -2,21 +2,21 @@ import nextstep.ladder.utils.RandomLineGenerator; -public class Point { - private boolean previous; - private boolean current; +public class LineState { + private final boolean previous; + private final boolean current; - public static Point previousOf(boolean previous) { + public static LineState previousOf(boolean previous) { LineGenerator lineGenerator = new RandomLineGenerator(); - return new Point(previous, !previous && lineGenerator.generateLine()); + return new LineState(previous, !previous && lineGenerator.generateLine()); } - public static Point previousOf(boolean previous, LineGenerator lineGenerator) { - return new Point(previous, lineGenerator.generateLine()); + public static LineState previousOf(boolean previous, LineGenerator lineGenerator) { + return new LineState(previous, lineGenerator.generateLine()); } - public Point(boolean previous, boolean current) { + public LineState(boolean previous, boolean current) { checkForConsecutiveTrue(previous, current); this.previous = previous; this.current = current; diff --git a/src/main/java/nextstep/ladder/domain/ResultInfo.java b/src/main/java/nextstep/ladder/domain/ResultInfo.java new file mode 100644 index 0000000000..9bdeb21cc5 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/ResultInfo.java @@ -0,0 +1,21 @@ +package nextstep.ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class ResultInfo { + private final List result; + + public ResultInfo(String result) { + this(Arrays.stream(result.split(",")).collect(Collectors.toList())); + } + + public ResultInfo(List result) { + this.result = result; + } + + public List getResult() { + return result; + } +} diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index 643c4bf115..abd014cc09 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -3,6 +3,7 @@ import nextstep.ladder.domain.Ladder; import nextstep.ladder.domain.LadderInfo; import nextstep.ladder.domain.Participants; +import nextstep.ladder.domain.ResultInfo; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -13,17 +14,23 @@ private LadderGameHandler() { // 인스턴스화 방지 public static void runGame() { Participants participants = inputAndRegisterParticipant(); - ResultView.enter(); - + ResultInfo resultInfo = inputGameResultInfo(); Ladder ladder = drawLadder(participants); - printLadder(participants, ladder); + printLadder(participants, ladder, resultInfo); + } + + private static ResultInfo inputGameResultInfo() { + String gameResults = InputView.inputGameResult(); + ResultView.enter(); + return new ResultInfo(gameResults); } - private static void printLadder(Participants participants, Ladder ladder) { + private static void printLadder(Participants participants, Ladder ladder, ResultInfo resultInfo) { ResultView.printResultWord(); ResultView.printParticipantsName(participants); ResultView.printLadder(ladder); + ResultView.printResultInfo(resultInfo); } private static Ladder drawLadder(Participants participants) { @@ -36,6 +43,7 @@ private static Ladder drawLadder(Participants participants) { private static Participants inputAndRegisterParticipant() { String inputParticipantName = InputView.inputParticipantName(); + ResultView.enter(); return new Participants(inputParticipantName); } } diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index 6659a7341c..34f2372d66 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -9,7 +9,12 @@ private InputView() { // 인스턴스화 방지 } public static String inputParticipantName() { - System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요.)"); + return scanner.nextLine(); + } + + public static String inputGameResult() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요.)"); return scanner.nextLine(); } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 7d92b23f71..a71fb9479e 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -3,6 +3,7 @@ import nextstep.ladder.domain.Ladder; import nextstep.ladder.domain.Line; import nextstep.ladder.domain.Participants; +import nextstep.ladder.domain.ResultInfo; public class ResultView { @@ -40,4 +41,8 @@ private static void printPoints(Line line) { public static void enter() { System.out.println(); } + + public static void printResultInfo(ResultInfo resultInfo) { + resultInfo.getResult().forEach(result -> System.out.print(String.format("%-8s", result))); + } } diff --git a/src/test/java/nextstep/ladder/domain/LineTest.java b/src/test/java/nextstep/ladder/domain/LineTest.java index 190a5e2fe3..47105d33d0 100644 --- a/src/test/java/nextstep/ladder/domain/LineTest.java +++ b/src/test/java/nextstep/ladder/domain/LineTest.java @@ -16,6 +16,6 @@ public boolean generateLine() { } }; - assertThrows(IllegalArgumentException.class, () -> Point.previousOf(true , lineGenerator)); + assertThrows(IllegalArgumentException.class, () -> LineState.previousOf(true , lineGenerator)); } } From 8cea432410e778b58f87643b877ccc14d38d595a Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Sat, 16 Dec 2023 10:42:25 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat=20:=20=EC=97=AC=EB=9F=AC=20=EC=B0=B8?= =?UTF-8?q?=EA=B0=80=EC=9E=90=EB=8A=94=20=EC=B5=9C=EC=B4=88=EC=97=90=20?= =?UTF-8?q?=ED=98=84=EC=9E=AC=20=EC=9C=84=EC=B9=98=EB=A5=BC=20=EA=B0=80?= =?UTF-8?q?=EC=A7=80=EA=B3=A0=20=EC=9E=88=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 3 ++- .../nextstep/ladder/domain/Participant.java | 14 ++++++++++---- .../nextstep/ladder/domain/Participants.java | 4 +++- .../ladder/domain/ParticipantTest.java | 18 +++++++++++++----- .../ladder/domain/ParticipantsTest.java | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 src/test/java/nextstep/ladder/domain/ParticipantsTest.java diff --git a/STEP3.md b/STEP3.md index 39130f0a9e..95a6b5e93c 100644 --- a/STEP3.md +++ b/STEP3.md @@ -12,7 +12,8 @@ ## Step3 기능분해 * [X] 실행 결과를 입력할 수 있다. -* [ ] 참가자는 현재 위치를 가지고 있는다. +* [X] 참가자는 현재 위치를 가지고 있는다. +* [X] 참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다. * [ ] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. * [ ] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. * [ ] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. diff --git a/src/main/java/nextstep/ladder/domain/Participant.java b/src/main/java/nextstep/ladder/domain/Participant.java index 3f6f3341a7..7c028629ad 100644 --- a/src/main/java/nextstep/ladder/domain/Participant.java +++ b/src/main/java/nextstep/ladder/domain/Participant.java @@ -6,16 +6,18 @@ public class Participant { public static final int MIN_NAME_LENGTH = 1; public static final int MAX_NAME_LENGTH = 5; - private String name; + private final String name; + private int position; - public static Participant nameOf(String name) { + public static Participant nameOf(String name, int position) { validateNameLength(name.trim()); - return new Participant(name.trim()); + return new Participant(name.trim(), position); } - private Participant(String name) { + private Participant(String name, int position) { this.name = name; + this.position = position; } private static void validateNameLength(String name) { @@ -31,6 +33,10 @@ public String getName() { return name; } + public int getPosition() { + return position; + } + @Override public String toString() { return "Participant{" + diff --git a/src/main/java/nextstep/ladder/domain/Participants.java b/src/main/java/nextstep/ladder/domain/Participants.java index 8d647ddeec..1404fd6146 100644 --- a/src/main/java/nextstep/ladder/domain/Participants.java +++ b/src/main/java/nextstep/ladder/domain/Participants.java @@ -3,14 +3,16 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class Participants { private final List players; + private static final AtomicInteger sequence = new AtomicInteger(1); public Participants(String names) { this(Arrays.stream(names.split(",")) - .map(Participant::nameOf).collect(Collectors.toList())); + .map(name -> Participant.nameOf(name, sequence.getAndIncrement())).collect(Collectors.toList())); } public Participants(List players) { diff --git a/src/test/java/nextstep/ladder/domain/ParticipantTest.java b/src/test/java/nextstep/ladder/domain/ParticipantTest.java index e700771096..a44b98d1da 100644 --- a/src/test/java/nextstep/ladder/domain/ParticipantTest.java +++ b/src/test/java/nextstep/ladder/domain/ParticipantTest.java @@ -4,26 +4,34 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; public class ParticipantTest { @Test - @DisplayName("참가자의 이름이 5글자를 초과하는 경우 Exception Throw") + @DisplayName("참가자의 이름이 5글자를 초과하는 경우 Exception 밝생") void participantName_5글자초과_Test() { String name = "hvoiunq"; - assertThrows(CannotRegisterNameException.class, () -> Participant.nameOf(name)); + assertThrows(CannotRegisterNameException.class, () -> Participant.nameOf(name, 0)); } @Test - @DisplayName("참가자의 이름이 1글자 미만인 경우 Exception Throw") + @DisplayName("참가자의 이름이 1글자 미만인 경우 Exception 발생") void participantName_1글자미만_Test() { String name = " "; - assertThrows(CannotRegisterNameException.class, () -> Participant.nameOf(name)); + assertThrows(CannotRegisterNameException.class, () -> Participant.nameOf(name, 0)); } @Test @DisplayName("참가자의 이름이 1글자~5글자인 경우 정상") void participantNameTest() { String name = "hvo"; - assertDoesNotThrow(() -> Participant.nameOf(name)); + assertDoesNotThrow(() -> Participant.nameOf(name, 0)); + } + + @Test + @DisplayName("참가자는 현재 위치를 가질 수 있다.") + void ParticipantPositionTest() { + Participant participant = Participant.nameOf("test", 1); + assertThat(participant.getPosition()).isEqualTo(1); } } diff --git a/src/test/java/nextstep/ladder/domain/ParticipantsTest.java b/src/test/java/nextstep/ladder/domain/ParticipantsTest.java new file mode 100644 index 0000000000..c6b4660600 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ParticipantsTest.java @@ -0,0 +1,16 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class ParticipantsTest { + @Test + @DisplayName("참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다.") + void participantsPositionTest() { + Participants participants = new Participants("a,b"); + assertThat(participants.getParticipants().get(0).getPosition()).isEqualTo(1); + assertThat(participants.getParticipants().get(1).getPosition()).isEqualTo(2); + } +} From 2936cb6619ecab872e11de855d8d54c8d071ac3d Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Mon, 18 Dec 2023 23:43:24 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=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 --- STEP3.md | 4 +- .../nextstep/ladder/domain/Participants.java | 14 ++++++- .../nextstep/ladder/domain/ResultInfo.java | 12 ++++-- .../ladder/exception/NotFoundException.java | 7 ++++ .../ladder/service/LadderGameHandler.java | 21 +++++++--- .../java/nextstep/ladder/view/InputView.java | 6 +++ .../java/nextstep/ladder/view/ResultView.java | 38 +++++++++++++------ 7 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 src/main/java/nextstep/ladder/exception/NotFoundException.java diff --git a/STEP3.md b/STEP3.md index 95a6b5e93c..d6127537ea 100644 --- a/STEP3.md +++ b/STEP3.md @@ -16,5 +16,5 @@ * [X] 참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다. * [ ] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. * [ ] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. -* [ ] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. -* [ ] 전체 결과를 보는 경우 각 참가자의 모든 실행결과를 보여준다. \ No newline at end of file +* [X] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. +* [X] 전체 결과를 보는 경우 각 참가자의 모든 실행결과를 보여준다. \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/domain/Participants.java b/src/main/java/nextstep/ladder/domain/Participants.java index 1404fd6146..23bbcfa681 100644 --- a/src/main/java/nextstep/ladder/domain/Participants.java +++ b/src/main/java/nextstep/ladder/domain/Participants.java @@ -1,5 +1,7 @@ package nextstep.ladder.domain; +import nextstep.ladder.exception.NotFoundException; + import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -8,7 +10,7 @@ public class Participants { private final List players; - private static final AtomicInteger sequence = new AtomicInteger(1); + private static final AtomicInteger sequence = new AtomicInteger(0); public Participants(String names) { this(Arrays.stream(names.split(",")) @@ -19,6 +21,16 @@ public Participants(List players) { this.players = players; } + public Participant getParticipant(String inputName) { + return validateInputName(inputName); + } + + private Participant validateInputName(String inputName) { + return players.stream().filter(participant -> participant.getName().equals(inputName.trim())) + .findAny() + .orElseThrow(() -> new NotFoundException("입력하신 이름과 일치하는 참가자가 없습니다.")); + } + public List getParticipants() { return Collections.unmodifiableList(players); } diff --git a/src/main/java/nextstep/ladder/domain/ResultInfo.java b/src/main/java/nextstep/ladder/domain/ResultInfo.java index 9bdeb21cc5..3990a15f56 100644 --- a/src/main/java/nextstep/ladder/domain/ResultInfo.java +++ b/src/main/java/nextstep/ladder/domain/ResultInfo.java @@ -5,17 +5,21 @@ import java.util.stream.Collectors; public class ResultInfo { - private final List result; + private final List results; public ResultInfo(String result) { this(Arrays.stream(result.split(",")).collect(Collectors.toList())); } public ResultInfo(List result) { - this.result = result; + this.results = result; } - public List getResult() { - return result; + public List getResults() { + return results; + } + + public String getResult(int position) { + return results.get(position); } } diff --git a/src/main/java/nextstep/ladder/exception/NotFoundException.java b/src/main/java/nextstep/ladder/exception/NotFoundException.java new file mode 100644 index 0000000000..a9be4a2f1b --- /dev/null +++ b/src/main/java/nextstep/ladder/exception/NotFoundException.java @@ -0,0 +1,7 @@ +package nextstep.ladder.exception; + +public class NotFoundException extends RuntimeException{ + public NotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index abd014cc09..9b6fe239ed 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -1,9 +1,6 @@ package nextstep.ladder.service; -import nextstep.ladder.domain.Ladder; -import nextstep.ladder.domain.LadderInfo; -import nextstep.ladder.domain.Participants; -import nextstep.ladder.domain.ResultInfo; +import nextstep.ladder.domain.*; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -18,6 +15,20 @@ public static void runGame() { Ladder ladder = drawLadder(participants); printLadder(participants, ladder, resultInfo); + + printResult(participants, resultInfo); + } + + private static void printResult(Participants participants, ResultInfo resultInfo) { + while (true) { + String inputName = InputView.inputForParticipantResult(); + if (inputName.trim().equals("all")) { + ResultView.printResultAll(participants, resultInfo); + return; + } + Participant participant = participants.getParticipant(inputName); + ResultView.printResultOfParticipant(resultInfo.getResult(participant.getPosition())); + } } private static ResultInfo inputGameResultInfo() { @@ -27,7 +38,7 @@ private static ResultInfo inputGameResultInfo() { } private static void printLadder(Participants participants, Ladder ladder, ResultInfo resultInfo) { - ResultView.printResultWord(); + ResultView.printLadderWord(); ResultView.printParticipantsName(participants); ResultView.printLadder(ladder); ResultView.printResultInfo(resultInfo); diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index 34f2372d66..b0c5347bcb 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -22,4 +22,10 @@ public static int inputLadderHeight() { System.out.println("최대 사다리 높이는 몇 개인가요?"); return scanner.nextInt(); } + + public static String inputForParticipantResult() { + System.out.println("결과를 보고 싶은 사람은?"); + scanner.nextLine(); + return scanner.next(); + } } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index a71fb9479e..243addfaf9 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -1,9 +1,6 @@ package nextstep.ladder.view; -import nextstep.ladder.domain.Ladder; -import nextstep.ladder.domain.Line; -import nextstep.ladder.domain.Participants; -import nextstep.ladder.domain.ResultInfo; +import nextstep.ladder.domain.*; public class ResultView { @@ -14,14 +11,13 @@ public class ResultView { private ResultView() { // 인스턴스화 방지 } - public static void printResultWord() { - System.out.println("실행결과"); - System.out.println(); - } - public static void printParticipantsName(Participants participants) { participants.getParticipants().forEach(participant -> System.out.print(String.format("%-8s", participant.getName()))); - System.out.println(); + enter(); + } + public static void printLadderWord() { + System.out.println("사다리 결과"); + enter(); } public static void printLadder(Ladder ladder) { @@ -42,7 +38,27 @@ public static void enter() { System.out.println(); } + public static void printResultWord() { + System.out.println("실행결과"); + } + public static void printResultInfo(ResultInfo resultInfo) { - resultInfo.getResult().forEach(result -> System.out.print(String.format("%-8s", result))); + resultInfo.getResults().forEach(result -> System.out.print(String.format("%-8s", result))); + enter(); + enter(); + } + + public static void printResultAll(Participants participants, ResultInfo resultInfo) { + printResultWord(); + for (int i = 0; i < participants.count(); i++) { + System.out.println(participants.getParticipants().get(i).getName() + " : " + resultInfo.getResults().get(i)); + } + enter(); + } + + public static void printResultOfParticipant(String result) { + printResultWord(); + System.out.println(result); + enter(); } } From 5dd9c6a3a0eb7b1389e05a3e9483d27c95bec61d Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Tue, 19 Dec 2023 00:31:00 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat=20:=20=EC=82=AC=EB=8B=A4=EB=A6=AC?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EC=B0=B8=EA=B0=80=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=9D=B4=EB=8F=99=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 4 +-- .../java/nextstep/ladder/domain/Ladder.java | 4 +++ .../java/nextstep/ladder/domain/Line.java | 5 ++++ .../nextstep/ladder/domain/LineState.java | 7 +++++ .../nextstep/ladder/domain/Participant.java | 9 +++++++ .../nextstep/ladder/domain/Participants.java | 8 +++++- .../ladder/service/LadderGameHandler.java | 26 +++++++++++++++++-- .../java/nextstep/ladder/view/ResultView.java | 6 ++++- 8 files changed, 63 insertions(+), 6 deletions(-) diff --git a/STEP3.md b/STEP3.md index d6127537ea..e605c714d8 100644 --- a/STEP3.md +++ b/STEP3.md @@ -14,7 +14,7 @@ * [X] 실행 결과를 입력할 수 있다. * [X] 참가자는 현재 위치를 가지고 있는다. * [X] 참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다. -* [ ] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. -* [ ] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. +* [X] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. +* [X] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. * [X] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. * [X] 전체 결과를 보는 경우 각 참가자의 모든 실행결과를 보여준다. \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 0d80c55f8e..a22827501d 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -35,6 +35,10 @@ public List getLines() { return lines; } + public Line getLineByHeight(int height) { + return lines.get(height); + } + @Override public String toString() { return "Ladder{" + diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index cdb296388e..9a6181df6f 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -29,6 +29,11 @@ public List getPoints() { return points; } + + public LineState getPointsByIndex(int index) { + return points.get(index); + } + @Override public String toString() { return "Line{" + diff --git a/src/main/java/nextstep/ladder/domain/LineState.java b/src/main/java/nextstep/ladder/domain/LineState.java index b657dca7e6..044287a619 100644 --- a/src/main/java/nextstep/ladder/domain/LineState.java +++ b/src/main/java/nextstep/ladder/domain/LineState.java @@ -36,4 +36,11 @@ private void checkForConsecutiveTrue(boolean previous, boolean current) { } } + @Override + public String toString() { + return "LineState{" + + "previous=" + previous + + ", current=" + current + + '}'; + } } diff --git a/src/main/java/nextstep/ladder/domain/Participant.java b/src/main/java/nextstep/ladder/domain/Participant.java index 7c028629ad..c9f4b14aff 100644 --- a/src/main/java/nextstep/ladder/domain/Participant.java +++ b/src/main/java/nextstep/ladder/domain/Participant.java @@ -29,6 +29,14 @@ private static void validateNameLength(String name) { } } + public void moveFront() { + this.position++; + } + + public void moveBack() { + this.position--; + } + public String getName() { return name; } @@ -41,6 +49,7 @@ public int getPosition() { public String toString() { return "Participant{" + "name='" + name + '\'' + + ", position=" + position + '}'; } } diff --git a/src/main/java/nextstep/ladder/domain/Participants.java b/src/main/java/nextstep/ladder/domain/Participants.java index 23bbcfa681..26efe1f38a 100644 --- a/src/main/java/nextstep/ladder/domain/Participants.java +++ b/src/main/java/nextstep/ladder/domain/Participants.java @@ -21,7 +21,7 @@ public Participants(List players) { this.players = players; } - public Participant getParticipant(String inputName) { + public Participant getParticipantByName(String inputName) { return validateInputName(inputName); } @@ -31,6 +31,12 @@ private Participant validateInputName(String inputName) { .orElseThrow(() -> new NotFoundException("입력하신 이름과 일치하는 참가자가 없습니다.")); } + public Participant getParticipantByPosition(int position) { + return players.stream() + .filter(player -> player.getPosition() == position) + .findAny().orElseThrow(() -> new NotFoundException("해당하는 위치의 참가자가 없습니다.")); + } + public List getParticipants() { return Collections.unmodifiableList(players); } diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index 9b6fe239ed..598e24f28f 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -14,11 +14,32 @@ public static void runGame() { ResultInfo resultInfo = inputGameResultInfo(); Ladder ladder = drawLadder(participants); - printLadder(participants, ladder, resultInfo); + moveParticipants(participants, ladder); + printLadder(participants, ladder, resultInfo); printResult(participants, resultInfo); } + private static void moveParticipants(Participants participants, Ladder ladder) { + for (int i = 0; i < ladder.getHeight(); i++) { + Line lineByHeight = ladder.getLineByHeight(i); + moveParticipantsOnLadder(participants, lineByHeight); + } + } + + private static void moveParticipantsOnLadder(Participants participants, Line line) { + for (int i = 0; i < participants.count(); i++) { + if (line.getPointsByIndex(i).getCurrent()) { + Participant currentParticipant = participants.getParticipantByPosition(i); + currentParticipant.moveBack(); + System.out.println(currentParticipant); + Participant previousParticipant = participants.getParticipantByPosition(i - 1); + previousParticipant.moveFront(); + System.out.println(previousParticipant); + } + } + } + private static void printResult(Participants participants, ResultInfo resultInfo) { while (true) { String inputName = InputView.inputForParticipantResult(); @@ -26,7 +47,8 @@ private static void printResult(Participants participants, ResultInfo resultInfo ResultView.printResultAll(participants, resultInfo); return; } - Participant participant = participants.getParticipant(inputName); + Participant participant = participants.getParticipantByName(inputName); + participant.toString(); ResultView.printResultOfParticipant(resultInfo.getResult(participant.getPosition())); } } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 243addfaf9..42f0dab2d0 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -49,14 +49,18 @@ public static void printResultInfo(ResultInfo resultInfo) { } public static void printResultAll(Participants participants, ResultInfo resultInfo) { + enter(); printResultWord(); for (int i = 0; i < participants.count(); i++) { - System.out.println(participants.getParticipants().get(i).getName() + " : " + resultInfo.getResults().get(i)); + Participant participant = participants.getParticipants().get(i); + System.out.println(participant.getName() + + " : " + resultInfo.getResults().get(participant.getPosition())); } enter(); } public static void printResultOfParticipant(String result) { + enter(); printResultWord(); System.out.println(result); enter(); From e72e9fbb89c664ab36d882fa23d1ea885fa55847 Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Tue, 19 Dec 2023 00:53:31 +0900 Subject: [PATCH 6/8] =?UTF-8?q?test=20:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EA=B2=80=EC=A6=9D=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 2 ++ .../nextstep/ladder/domain/Participant.java | 17 +++++++++- .../ladder/exception/CanNotMoveException.java | 7 ++++ .../ladder/service/LadderGameHandler.java | 4 +-- .../ladder/domain/ParticipantTest.java | 32 +++++++++++++++++++ .../ladder/domain/ParticipantsTest.java | 10 ++++++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/main/java/nextstep/ladder/exception/CanNotMoveException.java diff --git a/STEP3.md b/STEP3.md index e605c714d8..ccfc3c29db 100644 --- a/STEP3.md +++ b/STEP3.md @@ -15,6 +15,8 @@ * [X] 참가자는 현재 위치를 가지고 있는다. * [X] 참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다. * [X] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. + * [X] 제일 왼쪽에 위치한 참가자가 한칸 더 왼쪽으로 이동하는 경우 Exception 발생 * [X] 참가자가 위치한 라인의 현재상태가 true면 앞으로 이동한다. + * [X] 제일 오른쪽에 위치한 참가자가 한칸 더 오른쪽으로 이동하는 경우 Exception 발생 * [X] 특정 참가자의 이름을 입력하는 경우 현재 위치의 실행결과를 보여준다. * [X] 전체 결과를 보는 경우 각 참가자의 모든 실행결과를 보여준다. \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/domain/Participant.java b/src/main/java/nextstep/ladder/domain/Participant.java index c9f4b14aff..4bd7a80f60 100644 --- a/src/main/java/nextstep/ladder/domain/Participant.java +++ b/src/main/java/nextstep/ladder/domain/Participant.java @@ -1,5 +1,6 @@ package nextstep.ladder.domain; +import nextstep.ladder.exception.CanNotMoveException; import nextstep.ladder.exception.CannotRegisterNameException; public class Participant { @@ -29,14 +30,28 @@ private static void validateNameLength(String name) { } } - public void moveFront() { + public void moveFront(int participantCount) { + canMoveFront(participantCount); this.position++; } + private void canMoveFront(int participantCount) { + if (position == participantCount - 1) { + throw new CanNotMoveException("제일 오른쪽에 위치해, 더 이상 오른쪽으로 이동할 수 없습니다."); + } + } + public void moveBack() { + canMoveBack(); this.position--; } + private void canMoveBack() { + if (this.position == 0) { + throw new CanNotMoveException("제일 왼쪽에 위치해, 더 이상 왼쪽으로 이동할 수 없습니다."); + } + } + public String getName() { return name; } diff --git a/src/main/java/nextstep/ladder/exception/CanNotMoveException.java b/src/main/java/nextstep/ladder/exception/CanNotMoveException.java new file mode 100644 index 0000000000..bb16cb80f5 --- /dev/null +++ b/src/main/java/nextstep/ladder/exception/CanNotMoveException.java @@ -0,0 +1,7 @@ +package nextstep.ladder.exception; + +public class CanNotMoveException extends RuntimeException{ + public CanNotMoveException(String message) { + super(message); + } +} diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index 598e24f28f..ec16815ae8 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -32,10 +32,8 @@ private static void moveParticipantsOnLadder(Participants participants, Line lin if (line.getPointsByIndex(i).getCurrent()) { Participant currentParticipant = participants.getParticipantByPosition(i); currentParticipant.moveBack(); - System.out.println(currentParticipant); Participant previousParticipant = participants.getParticipantByPosition(i - 1); - previousParticipant.moveFront(); - System.out.println(previousParticipant); + previousParticipant.moveFront(participants.count()); } } } diff --git a/src/test/java/nextstep/ladder/domain/ParticipantTest.java b/src/test/java/nextstep/ladder/domain/ParticipantTest.java index a44b98d1da..bc4bf60fb7 100644 --- a/src/test/java/nextstep/ladder/domain/ParticipantTest.java +++ b/src/test/java/nextstep/ladder/domain/ParticipantTest.java @@ -1,5 +1,6 @@ package nextstep.ladder.domain; +import nextstep.ladder.exception.CanNotMoveException; import nextstep.ladder.exception.CannotRegisterNameException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -34,4 +35,35 @@ void ParticipantPositionTest() { Participant participant = Participant.nameOf("test", 1); assertThat(participant.getPosition()).isEqualTo(1); } + + @Test + @DisplayName("참가자는 오른쪽으로 이동할 수 있다.") + void moveFrontTest() { + Participant participant = Participant.nameOf("test", 1); + participant.moveFront(1); + + assertThat(participant.getPosition()).isEqualTo(2); + } + @Test + @DisplayName("제일 왼쪽에 위치한 참가자가 한칸 더 왼쪽으로 이동하는 경우 Exception 발생") + void canNotMoveFrontTest() { + Participants participants = new Participants("a,b"); + + assertThrows(CanNotMoveException.class, () -> participants.getParticipantByName("b").moveFront(participants.count())); + } + @Test + @DisplayName("참가자는 왼쪽으로 이동할 수 있다.") + void moveBackTest() { + Participant participant = Participant.nameOf("test", 1); + participant.moveBack(); + + assertThat(participant.getPosition()).isEqualTo(0); + } + @Test + @DisplayName("제일 왼쪽에 위치한 참가자가 한칸 더 왼쪽으로 이동하는 경우 Exception 발생") + void canNotMoveBackTest() { + Participant participant = Participant.nameOf("test", 0); + + assertThrows(CanNotMoveException.class, () -> participant.moveBack()); + } } diff --git a/src/test/java/nextstep/ladder/domain/ParticipantsTest.java b/src/test/java/nextstep/ladder/domain/ParticipantsTest.java index c6b4660600..90f00e1c89 100644 --- a/src/test/java/nextstep/ladder/domain/ParticipantsTest.java +++ b/src/test/java/nextstep/ladder/domain/ParticipantsTest.java @@ -1,9 +1,11 @@ package nextstep.ladder.domain; +import nextstep.ladder.exception.NotFoundException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; public class ParticipantsTest { @Test @@ -13,4 +15,12 @@ void participantsPositionTest() { assertThat(participants.getParticipants().get(0).getPosition()).isEqualTo(1); assertThat(participants.getParticipants().get(1).getPosition()).isEqualTo(2); } + + @Test + @DisplayName("참가자 명단에 없는 이름을 입력하는 경우 Exception 발생") + void wrongInputParticipantName() { + Participants participants = new Participants("a,b"); + assertThrows(NotFoundException.class, () -> participants.getParticipantByName("c")); + } + } From 5ea1b29c1196d07d02c272cd8ee4f4c21124f7c9 Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Tue, 19 Dec 2023 00:54:18 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20toString=20=ED=98=B8=EC=B6=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/ladder/service/LadderGameHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index ec16815ae8..d68b96a4bc 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -46,7 +46,6 @@ private static void printResult(Participants participants, ResultInfo resultInfo return; } Participant participant = participants.getParticipantByName(inputName); - participant.toString(); ResultView.printResultOfParticipant(resultInfo.getResult(participant.getPosition())); } } From a066e944f9fd37b644d813da98270d28a03db9b3 Mon Sep 17 00:00:00 2001 From: hvoiunq Date: Tue, 19 Dec 2023 01:07:22 +0900 Subject: [PATCH 8/8] =?UTF-8?q?test=20:=20=EC=8B=A4=ED=96=89=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=9E=85=EB=A0=A5=20Test=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP3.md | 1 + .../nextstep/ladder/domain/ResultInfo.java | 21 +++++++++++++--- .../ladder/service/LadderGameHandler.java | 6 ++--- .../ladder/domain/ResultInfoTest.java | 25 +++++++++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/test/java/nextstep/ladder/domain/ResultInfoTest.java diff --git a/STEP3.md b/STEP3.md index ccfc3c29db..7686fe5f1a 100644 --- a/STEP3.md +++ b/STEP3.md @@ -12,6 +12,7 @@ ## Step3 기능분해 * [X] 실행 결과를 입력할 수 있다. + * [X] 참가자보다 적게 입력하는 경우 Exception 발생 * [X] 참가자는 현재 위치를 가지고 있는다. * [X] 참가자가 여러명인 경우 자동으로 현재 위치를 가지고 있는다. * [X] 참가자가 위치한 라인의 이전상태가 true면 뒤로 이동한다. diff --git a/src/main/java/nextstep/ladder/domain/ResultInfo.java b/src/main/java/nextstep/ladder/domain/ResultInfo.java index 3990a15f56..d11cd3e655 100644 --- a/src/main/java/nextstep/ladder/domain/ResultInfo.java +++ b/src/main/java/nextstep/ladder/domain/ResultInfo.java @@ -5,14 +5,27 @@ import java.util.stream.Collectors; public class ResultInfo { + private final List results; - public ResultInfo(String result) { - this(Arrays.stream(result.split(",")).collect(Collectors.toList())); + public ResultInfo(String result, int participantCount) { + this(checkResultSize(Arrays.stream(result.split(",")).collect(Collectors.toList()), participantCount)); + + } + + private static List checkResultSize(List result, int participantCount) { + checkResultAndParticipantCount(result, participantCount); + return result; + } + + public ResultInfo(List results) { + this.results = results; } - public ResultInfo(List result) { - this.results = result; + private static void checkResultAndParticipantCount(List result, int participantCount) { + if (result.size() != participantCount) { + throw new IllegalArgumentException("결과 갯수와 참가자 수와 다릅니다."); + } } public List getResults() { diff --git a/src/main/java/nextstep/ladder/service/LadderGameHandler.java b/src/main/java/nextstep/ladder/service/LadderGameHandler.java index d68b96a4bc..6d8ef65065 100644 --- a/src/main/java/nextstep/ladder/service/LadderGameHandler.java +++ b/src/main/java/nextstep/ladder/service/LadderGameHandler.java @@ -11,7 +11,7 @@ private LadderGameHandler() { // 인스턴스화 방지 public static void runGame() { Participants participants = inputAndRegisterParticipant(); - ResultInfo resultInfo = inputGameResultInfo(); + ResultInfo resultInfo = inputGameResultInfo(participants.count()); Ladder ladder = drawLadder(participants); moveParticipants(participants, ladder); @@ -50,10 +50,10 @@ private static void printResult(Participants participants, ResultInfo resultInfo } } - private static ResultInfo inputGameResultInfo() { + private static ResultInfo inputGameResultInfo(int participantCount) { String gameResults = InputView.inputGameResult(); ResultView.enter(); - return new ResultInfo(gameResults); + return new ResultInfo(gameResults, participantCount); } private static void printLadder(Participants participants, Ladder ladder, ResultInfo resultInfo) { diff --git a/src/test/java/nextstep/ladder/domain/ResultInfoTest.java b/src/test/java/nextstep/ladder/domain/ResultInfoTest.java new file mode 100644 index 0000000000..06f6439a16 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ResultInfoTest.java @@ -0,0 +1,25 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ResultInfoTest { + @Test + @DisplayName("실행 결과를 입력할 수 있다.") + void saveResultInfoTest() { + ResultInfo resultInfo = new ResultInfo("a,b", 2); + + assertThat(resultInfo.getResults().size()).isEqualTo(2); + } + + @Test + @DisplayName("참가자보다 적게 입력하는 경우 Exception 발생") + void wrongResultInfoExceptionTest() { + Participants participants = new Participants("a,b"); + + assertThrows(IllegalArgumentException.class, () -> new ResultInfo("a", participants.count())); + } +}