From 179a889d184851332c417ee85fa8071aaeaa1e15 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 10 Nov 2024 20:27:16 +0900 Subject: [PATCH 1/9] =?UTF-8?q?step4=20:=20docs=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b39b831a..958903aa 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ - 사다리의 크기를 입력받아 생성할 수 있다. ### step3 - 사다리를 보여준 후 결과를 출력한다. +### step4 +- 사다리 게임에 참여하는 사람에 이름을 최대 5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다. +- 사람 이름은 쉼표(,)를 기준으로 구분한다. +- 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한 **📠 도메인 분석 내용** - 사용자는 "참여 인원 수"를 입력한다 (네이버엔 구현되어있지만 실행예시엔 해당 없음) @@ -47,23 +51,38 @@ ## 실행결과 ``` -사다리의 넓이는 몇 개인가요? -4 +참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요) +neo,brown,brie,tommy -사다리의 높이는 몇 개인가요? +실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요) +꽝,5000,꽝,3000 + +최대 사다리 높이는 몇 개인가요? 5 -실행결과 +사다리 결과 + neo brown brie tommy |-----| |-----| | |-----| | |-----| | | | |-----| | |-----| |-----| - -0 -> 0 -1 -> 3 -2 -> 2 -3 -> 1 + 꽝 5000 꽝 3000 + +결과를 보고 싶은 사람은? +neo + +실행 결과 +꽝 + +결과를 보고 싶은 사람은? +all + +실행 결과 +neo : 꽝 +brown : 3000 +brie : 꽝 +tommy : 5000 ``` From 24846dd2b3a6c1568ffd41a9aedf17f6621929e8 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Mon, 11 Nov 2024 00:15:08 +0900 Subject: [PATCH 2/9] =?UTF-8?q?step4=20:=20Line=EC=97=90=20'=EC=9D=B4?= =?UTF-8?q?=EB=A6=84',=20'=EA=B2=B0=EA=B3=BC'=20=EC=83=81=ED=83=9C?= =?UTF-8?q?=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Line.java | 20 +++++++++++++++++--- src/test/java/domain/LineTest.java | 8 +++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/domain/Line.java b/src/main/java/domain/Line.java index 36a6f0fa..88134988 100644 --- a/src/main/java/domain/Line.java +++ b/src/main/java/domain/Line.java @@ -6,13 +6,18 @@ public class Line { + private final String name; + private final String outcome; + private final List points; - private Line(List points) { + private Line(String name, String outcome, List points) { + this.name = name; + this.outcome = outcome; this.points = points; } - public static Line of(List leftRungsStatus, List rightRungsStatus) { + public static Line of(String name, String outcome, List leftRungsStatus, List rightRungsStatus) { validateHeight(leftRungsStatus, rightRungsStatus); int maxPosition = leftRungsStatus.size(); @@ -20,7 +25,7 @@ public static Line of(List leftRungsStatus, List rightRungsSta for (int position = 0; position < maxPosition; position++) { points.add(new Point(leftRungsStatus.get(position), rightRungsStatus.get(position))); } - return new Line(points); + return new Line(name, outcome, points); } private static void validateHeight(List leftRungsStatus, List rightRungsStatus) { @@ -28,6 +33,7 @@ private static void validateHeight(List leftRungsStatus, List throw new IllegalArgumentException(Errors.RUNG_STATUS_LENGTH_MUST_MATCH); } } + public List getRightStatus() { List rightStatus = new ArrayList<>(); for (Point point : points) { @@ -40,6 +46,14 @@ public int getHeight() { return this.points.size(); } + public String getName() { + return name; + } + + public String getOutcome() { + return outcome; + } + public boolean isConnectedToLeftLineAt(int position) { final Point nowPoint = this.points.get(position); return nowPoint.isConnectedToLeftLadder(); diff --git a/src/test/java/domain/LineTest.java b/src/test/java/domain/LineTest.java index ba07003f..297d5a69 100644 --- a/src/test/java/domain/LineTest.java +++ b/src/test/java/domain/LineTest.java @@ -10,6 +10,8 @@ import util.Errors; class LineTest { + private static final String name = "이름"; + private static final String outcome = "결과"; @Test @DisplayName("Line의 우측 사다리 유무를 전달받을 수 있다") @@ -19,7 +21,7 @@ void getRungsStatusTest() { final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when - final Line line = Line.of(inputLeftRungStatus, inputRightRungStatus); + final Line line = Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus); // then assertThat(line.getRightStatus()) .containsExactlyElementsOf(inputRightRungStatus); @@ -33,7 +35,7 @@ void invalidLineConstructorTest() { final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then - assertThatThrownBy(() -> Line.of(inputLeftRungStatus, inputRightRungStatus)) + assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.RUNG_STATUS_LENGTH_MUST_MATCH); } @@ -46,7 +48,7 @@ void invalidPointTest() { final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then - assertThatThrownBy(() -> Line.of(inputLeftRungStatus, inputRightRungStatus)) + assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.ADJACENT_LADDERS_CANNOT_HAVE_RUNG_AT_SAME_POSITION); } From dd35e562ce3bf890de5046323a35b04b934601d3 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Mon, 11 Nov 2024 00:15:28 +0900 Subject: [PATCH 3/9] =?UTF-8?q?step4=20:=20Ladder=EC=97=90=20'=EC=9D=B4?= =?UTF-8?q?=EB=A6=84',=20'=EA=B2=B0=EA=B3=BC'=20=EC=83=81=ED=83=9C?= =?UTF-8?q?=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Ladder.java | 22 +++++++++++++++++----- src/test/java/domain/LadderTest.java | 28 ++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/domain/Ladder.java b/src/main/java/domain/Ladder.java index 7517ccca..2396835a 100644 --- a/src/main/java/domain/Ladder.java +++ b/src/main/java/domain/Ladder.java @@ -1,7 +1,9 @@ package domain; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.stream.IntStream; import util.Errors; @@ -30,9 +32,9 @@ private void validateLaddersHeight(List lines) { } private void validatePointStatus(List lines) { - for (int index = 0; index < lines.size()-1; index++) { + for (int index = 0; index < lines.size() - 1; index++) { Line nowLine = lines.get(index); - Line nextLine = lines.get(index+1); + Line nextLine = lines.get(index + 1); validateRungInSamePosition(nowLine, nextLine); } } @@ -60,13 +62,15 @@ public int getHeight() { return this.lines.get(0).getHeight(); } - public List getResult() { - List result = new ArrayList<>(); + public Map getResult() { + Map result = new LinkedHashMap<>(); int height = this.getHeight(); for (int nowIndex = 0; nowIndex < lines.size(); nowIndex++) { int finalIndex = calculateTargetIndex(nowIndex, height); - result.add(finalIndex); + final String name = getNameOfLine(nowIndex); + final String outcome = getOutcomeOfLine(finalIndex); + result.put(name, outcome); } return result; } @@ -89,4 +93,12 @@ private int getNextIndex(int currentIndex, int position) { } return currentIndex; } + + private String getNameOfLine(int index) { + return lines.get(index).getName(); + } + + private String getOutcomeOfLine(int index) { + return lines.get(index).getOutcome(); + } } diff --git a/src/test/java/domain/LadderTest.java b/src/test/java/domain/LadderTest.java index d29fb400..0505b36a 100644 --- a/src/test/java/domain/LadderTest.java +++ b/src/test/java/domain/LadderTest.java @@ -6,12 +6,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import util.Errors; class LadderTest { + private static final String name = "이름"; + private static final String outcome = "결과"; + @Test @DisplayName("Ladder를 통해 각 사다리의 우측 가로줄 유무를 모두 받아올 수 있다.") void constructorTest() { @@ -26,7 +30,7 @@ void constructorTest() { // when List lineCollection = new ArrayList<>(); for (int i = 1; i < inputRungsStatus.size(); i++) { - lineCollection.add(Line.of(inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); + lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } final Ladder ladder = new Ladder(lineCollection); // then @@ -39,8 +43,8 @@ void constructorTest() { @DisplayName("Ladder 내의 모든 Line의 길이가 같지 않다면 예외가 발생한다.") void invalidHeightTest() { // given - final Line line1 = Line.of(Arrays.asList(true, false, true), Arrays.asList(false, false, false)); - final Line line2 = Line.of(Arrays.asList(false, false, false, false, false), + final Line line1 = Line.of(name, outcome, Arrays.asList(true, false, true), Arrays.asList(false, false, false)); + final Line line2 = Line.of(name, outcome, Arrays.asList(false, false, false, false, false), Arrays.asList(true, false, false, true, true)); // when List lineCollection = Arrays.asList(line1, line2); @@ -61,23 +65,31 @@ void resultTest() { Arrays.asList(true, true, false, true), Arrays.asList(false, false, false, false) ); + List names = Arrays.asList("일번", "이번", "삼번", "사번"); + List outcomes = Arrays.asList("100", "200", "300", "400"); List lineCollection = new ArrayList<>(); for (int i = 1; i < inputRungsStatus.size(); i++) { - lineCollection.add(Line.of(inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); + String name = names.get(i-1); + String outcome = outcomes.get(i-1); + lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } final Ladder ladder = new Ladder(lineCollection); // when - final List result = ladder.getResult(); + final Map result = ladder.getResult(); // then - assertThat(result).isEqualTo(List.of(2, 1, 3, 0)); + assertThat(result).isEqualTo(Map.of("일번", "300", + "이번", "200", + "삼번", "400", + "사번", "100")); + } @Test @DisplayName("인접한 line의 경우 좌측 point의 right status 값과 우측 point의 left status 값이 일치하지 않음 예외가 발생한다.") void invalidRungTest() { // given - Line line1 = Line.of(Arrays.asList(false, false, false), Arrays.asList(false, true, true)); - Line line2 = Line.of(Arrays.asList(false, false, true), Arrays.asList(false, false, false)); + Line line1 = Line.of(name, outcome, Arrays.asList(false, false, false), Arrays.asList(false, true, true)); + Line line2 = Line.of(name, outcome, Arrays.asList(false, false, true), Arrays.asList(false, false, false)); List lineCollection = new ArrayList<>(List.of(line1, line2)); // when // then From 4c208414c59d556e779e0ae1b4b54d9c78146736 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Mon, 11 Nov 2024 00:15:42 +0900 Subject: [PATCH 4/9] =?UTF-8?q?step4=20:=20=EC=9D=B4=EB=A6=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B2=B0=EA=B3=BC=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/LadderGameController.java | 32 +++++-- src/main/java/service/LadderService.java | 46 ++++++++-- src/main/java/util/Errors.java | 2 + src/main/java/view/InputView.java | 11 +++ src/main/java/view/OutputView.java | 40 +++++++-- src/test/java/service/LaddersServiceTest.java | 84 ++++++++++++++++++- 6 files changed, 190 insertions(+), 25 deletions(-) diff --git a/src/main/java/controller/LadderGameController.java b/src/main/java/controller/LadderGameController.java index 9c4ba14d..2b5a1c9f 100644 --- a/src/main/java/controller/LadderGameController.java +++ b/src/main/java/controller/LadderGameController.java @@ -4,6 +4,8 @@ import domain.Height; import domain.Ladder; import domain.RungsBuilder; +import java.util.List; +import java.util.Map; import service.LadderService; import view.InputView; import view.OutputView; @@ -21,24 +23,38 @@ public LadderGameController(RungsBuilder rungsBuilder) { } public void start() { - CountOfLine countOfLine = getcountOfLine(); + List names = getNames(); + List outcomes = getOutcomes(); + CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); Height height = getHeight(); - Ladder ladder = laddersService.createLadder(countOfLine, height); - outputView.printStatusOfLadders(ladder.getRightRungStatus(), height.value()); - outputView.printResult(ladder.getResult()); + Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); + outputView.printStatusOfLadders(names, outcomes, ladder.getRightRungStatus(), height.value()); + printResult(ladder.getResult()); } - private CountOfLine getcountOfLine() { - outputView.printInputCountOfLineGuide(); - final int valueOfCountOfLine = inputView.getUserIntegerInput(); - return new CountOfLine(valueOfCountOfLine); + private List getNames() { + outputView.printInputNamesGuide(); + return inputView.getStringList(); } + private List getOutcomes() { + outputView.printInputOutcomesGuid(); + return inputView.getStringList(); + } + + private Height getHeight() { outputView.printInputHeightGuide(); final int valueOfHeight = inputView.getUserIntegerInput(); return new Height(valueOfHeight); } + private void printResult(Map result) { + outputView.printInputTargetName(); + final String targetName = inputView.getString(); + final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + outputView.printResult(resultToPrint); + } + } diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index 6bd08ff8..fa757f4d 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -7,8 +7,10 @@ import domain.RungsBuilder; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; +import util.Errors; public class LadderService { @@ -18,17 +20,19 @@ public LadderService(RungsBuilder rungsBuilder) { this.rungsBuilder = rungsBuilder; } - public Ladder createLadder(CountOfLine countOfLine, Height height) { - final List lineCollection = createLineCollection(countOfLine, height); + public Ladder createLadder(CountOfLine countOfLine, Height height, List names, List outcomes) { + final List lineCollection = createLineCollection(countOfLine, height, names, outcomes); return new Ladder(lineCollection); } - private List createLineCollection(CountOfLine countOfLine, Height height) { + private List createLineCollection(CountOfLine countOfLine, Height height, List names, List outcomes) { final List lineCollection = new ArrayList<>(); for (int index = 0; index < countOfLine.value(); index++) { final List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); - final Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus); + final String name = names.get(index); + final String outcome = outcomes.get(index); + final Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, name, outcome); lineCollection.add(nowLine); } return lineCollection; @@ -43,13 +47,13 @@ private List getPrevLineRightStatus(List lineCollection, int inde } private Line createNowLine(int index, Height height, CountOfLine countOfLine, - List nowLineLeftStatus) { + List nowLineLeftStatus, String name, String outcome) { final List nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height, nowLineLeftStatus); if (index == 0) { nowLineLeftStatus = createEmptyStatus(height); } - return Line.of(nowLineLeftStatus, nowLineRightStatus); + return Line.of(name, outcome, nowLineLeftStatus, nowLineRightStatus); } private List createNowLineRightStatus(int index, CountOfLine countOfLine, Height height, @@ -65,4 +69,34 @@ private List createEmptyStatus(Height height) { .mapToObj(i -> false) .collect(Collectors.toList()); } + + public Map getResultToPrint(Map result, String targetName) { + if (isAllMode(targetName)) { + return result; + } + validateTargetName(result, targetName); + return Map.of(targetName, result.get(targetName)); + } + + private boolean isAllMode(String targetName) { + return targetName.equals("all"); + } + + private void validateTargetName(Map result, String targetName) { + if (!result.containsKey(targetName)) { + throw new IllegalArgumentException(Errors.TARGET_NAME_MUST_BE_IN_NAMES); + } + } + + public CountOfLine getcountOfLine(List names, List outcomes) { + validateCountOfLine(names, outcomes); + final int valueOfCountOfLine = names.size(); + return new CountOfLine(valueOfCountOfLine); + } + + private void validateCountOfLine(List names, List outcomes) { + if (names.size() != outcomes.size()) { + throw new IllegalArgumentException(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME); + } + } } diff --git a/src/main/java/util/Errors.java b/src/main/java/util/Errors.java index f684a91a..029973e1 100644 --- a/src/main/java/util/Errors.java +++ b/src/main/java/util/Errors.java @@ -12,5 +12,7 @@ private Errors() { public static final String INPUT_IS_NOT_INTEGER = "정수가 입력되어야 합니다."; public static final String RUNG_STATUS_LENGTH_MUST_MATCH = "Line의 left rung status의 길이와 right rung status의 길이는 일치해야합니다."; public static final String ADJACENT_POINTER_STATUS_MATCH = "인접한 line의 경우 좌측 line의 pointe의 right status와 우측 line의 left status 값은 일치해야합니다."; + public static final String NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME = "참여할 사람의 수와 결과의 수는 동일해야합니다."; + public static final String TARGET_NAME_MUST_BE_IN_NAMES = "결과를 조회하고 싶은 이름이 참여자 명단에 없습니다."; } diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 34db6b70..7a976a09 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,6 +1,8 @@ package view; +import java.util.Arrays; import java.util.InputMismatchException; +import java.util.List; import java.util.Scanner; import util.Errors; @@ -17,5 +19,14 @@ public int getUserIntegerInput() { } } + public List getStringList() { + final String input = getString(); + return Arrays.asList(input.split(",")); + } + + public String getString() { + scanner = new Scanner(System.in); + return scanner.nextLine(); + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index bd5c705a..bc7c7640 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -2,18 +2,30 @@ import java.util.List; +import java.util.Map; public class OutputView { + private static final String BLANK = " "; private static final String BLANK_LINE = "| "; private static final String RUNG_LINE = "|----"; - public void printStatusOfLadders(List> rungsStatusPerLadder, int height) { + public void printStatusOfLadders(List names, List outcomes, List> rungsStatusPerLadder, int height) { + printFixedWidth(names); for (int nowPosition = height - 1; nowPosition >= 0; nowPosition--) { + printBlank(); printStatusAtLadderPosition(rungsStatusPerLadder, nowPosition); System.out.println(); } + printFixedWidth(outcomes); + } + + public void printFixedWidth(List values) { + for (String value : values) { + System.out.printf(" %s ", value); + } + System.out.println(); } private void printStatusAtLadderPosition(List> rungsStatusPerLadder, int nowPosition) { @@ -29,21 +41,33 @@ private String createOrSkip(List rungPosition, int nowPosition) { return BLANK_LINE; } - private Boolean doesRungExist(List rungPosition, int nowPosition) { - return rungPosition.get(nowPosition); + private void printBlank() { + System.out.print(BLANK); } - public void printInputCountOfLineGuide() { - System.out.println("사다리의 넓이는 몇 개인가요?"); + private Boolean doesRungExist(List rungPosition, int nowPosition) { + return rungPosition.get(nowPosition); } public void printInputHeightGuide() { System.out.println("사다리의 높이는 몇 개인가요?"); } - public void printResult(List result) { - for (int index = 0; index < result.size(); index++) { - System.out.printf("%d -> %d%n", index, result.get(index)); + public void printInputNamesGuide() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + } + + public void printInputOutcomesGuid() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + } + + public void printInputTargetName() { + System.out.println("결과를 보고 싶은 사람은?"); + } + + public void printResult(Map result) { + for (String name : result.keySet()) { + System.out.printf("%s -> %s\n", name, result.get(name)); } } } diff --git a/src/test/java/service/LaddersServiceTest.java b/src/test/java/service/LaddersServiceTest.java index fe629c3e..75aab0f6 100644 --- a/src/test/java/service/LaddersServiceTest.java +++ b/src/test/java/service/LaddersServiceTest.java @@ -1,6 +1,7 @@ package service; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import domain.CountOfLine; import domain.Height; @@ -8,11 +9,18 @@ import domain.RungsBuilder; import java.util.Arrays; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import util.Errors; class LaddersServiceTest { + final static TestRungsBuilder testRungsBuilder = new TestRungsBuilder(); + final static LadderService laddersService = new LadderService(testRungsBuilder); + + + static class TestRungsBuilder implements RungsBuilder { @Override @@ -32,10 +40,10 @@ void test() { // given final Height height = new Height(5); final CountOfLine countOfLine = new CountOfLine(3); - final TestRungsBuilder testRungsBuilder = new TestRungsBuilder(); + List names = Arrays.asList("일번", "이번", "삼번", "사번"); + List outcomes = Arrays.asList("100", "200", "300", "400"); // when - final LadderService laddersService = new LadderService(testRungsBuilder); - final Ladder ladder = laddersService.createLadder(countOfLine, height); + final Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); // then assertThat(ladder.getRightRungStatus()) .isEqualTo( @@ -46,4 +54,74 @@ void test() { ) ); } + + @Test + @DisplayName("이름과 결과의 사이즈가 다르면 예외가 발생한다.") + void invalidCountOfLadderTest() { + // given + List names = List.of("일", "이"); + List outcomes = List.of("1000", "2000", "3000"); + // when + // then + assertThatThrownBy(() -> laddersService.getcountOfLine(names, outcomes)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME); + } + + @Test + @DisplayName("이름과 결과의 사이즈는 곧 사다리의 개수이다.") + void getCountOfLadderTest() { + // given + List names = List.of("일", "이", "삼"); + List outcomes = List.of("1000", "2000", "3000"); + // when + final CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); + // then + assertThat(countOfLine.value()).isEqualTo(names.size()); + } + + @Test + @DisplayName("원하는 참가자의 실행 결과를 조회할 수 있다.") + void selectTargetNameTest() { + // given + Map result = Map.of("일번", "2000", + "이번", "꽝", + "삼번", "1000"); + String targetName = "이번"; + // when + final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + // then + assertThat(resultToPrint) + .isEqualTo(Map.of("이번", "꽝")); + } + + @Test + @DisplayName("결과를 보고 싶은 사람에 all을 입력하면 전체 결과를 조회할 수 있다.") + void selectAllTest() { + // given + Map result = Map.of("일번", "2000", + "이번", "꽝", + "삼번", "1000"); + String targetName = "all"; + // when + final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + // then + assertThat(resultToPrint) + .isEqualTo(result); + } + + @Test + @DisplayName("결과를 보고싶은 사람이 참가자 명단에 없으면 예외가 발생한다.") + void invalidSelectTest() { + // given + Map result = Map.of("일번", "2000", + "이번", "꽝", + "삼번", "1000"); + String targetName = "오번"; + // when + // then + assertThatThrownBy(() -> laddersService.getResultToPrint(result, targetName)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(Errors.TARGET_NAME_MUST_BE_IN_NAMES); + } } From d1acd60c9fbc791f3c6b0f2a14c4da4b537fb3bd Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 24 Nov 2024 16:18:46 +0900 Subject: [PATCH 5/9] =?UTF-8?q?step4=20:=20=EC=BB=A8=EB=B2=A4=EC=85=98=20?= =?UTF-8?q?=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LadderGameApplication.java | 2 +- .../java/controller/LadderGameController.java | 6 +++--- src/main/java/domain/Ladder.java | 6 +++--- src/main/java/domain/Line.java | 4 ++-- src/main/java/domain/RandomRungsBuilder.java | 2 +- src/main/java/service/LadderService.java | 18 +++++++++--------- src/main/java/view/InputView.java | 2 +- src/test/java/domain/LadderTest.java | 10 +++++----- src/test/java/domain/LineTest.java | 14 +++++++------- src/test/java/service/LaddersServiceTest.java | 14 +++++++------- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/main/java/LadderGameApplication.java b/src/main/java/LadderGameApplication.java index af866c5c..1775edec 100644 --- a/src/main/java/LadderGameApplication.java +++ b/src/main/java/LadderGameApplication.java @@ -4,7 +4,7 @@ public class LadderGameApplication { public static void main(String[] args) { - final RandomRungsBuilder randomRungsBuilder = new RandomRungsBuilder(); + RandomRungsBuilder randomRungsBuilder = new RandomRungsBuilder(); LadderGameController ladderGameController = new LadderGameController(randomRungsBuilder); ladderGameController.start(); diff --git a/src/main/java/controller/LadderGameController.java b/src/main/java/controller/LadderGameController.java index 2b5a1c9f..426be93a 100644 --- a/src/main/java/controller/LadderGameController.java +++ b/src/main/java/controller/LadderGameController.java @@ -46,14 +46,14 @@ private List getOutcomes() { private Height getHeight() { outputView.printInputHeightGuide(); - final int valueOfHeight = inputView.getUserIntegerInput(); + int valueOfHeight = inputView.getUserIntegerInput(); return new Height(valueOfHeight); } private void printResult(Map result) { outputView.printInputTargetName(); - final String targetName = inputView.getString(); - final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + String targetName = inputView.getString(); + Map resultToPrint = laddersService.getResultToPrint(result, targetName); outputView.printResult(resultToPrint); } diff --git a/src/main/java/domain/Ladder.java b/src/main/java/domain/Ladder.java index 2396835a..6488a9d9 100644 --- a/src/main/java/domain/Ladder.java +++ b/src/main/java/domain/Ladder.java @@ -46,7 +46,7 @@ private void validateRungInSamePosition(Line nowLine, Line nextLine) { } private boolean isRungInSamePosition(Line nowLine, Line nextLine) { - final int maxPosition = nowLine.getHeight(); + int maxPosition = nowLine.getHeight(); return IntStream.range(0, maxPosition) .allMatch( position -> nowLine.isConnectedToRightLineAt(position) == nextLine.isConnectedToLeftLineAt(position)); @@ -68,8 +68,8 @@ public Map getResult() { for (int nowIndex = 0; nowIndex < lines.size(); nowIndex++) { int finalIndex = calculateTargetIndex(nowIndex, height); - final String name = getNameOfLine(nowIndex); - final String outcome = getOutcomeOfLine(finalIndex); + String name = getNameOfLine(nowIndex); + String outcome = getOutcomeOfLine(finalIndex); result.put(name, outcome); } return result; diff --git a/src/main/java/domain/Line.java b/src/main/java/domain/Line.java index 88134988..eb407051 100644 --- a/src/main/java/domain/Line.java +++ b/src/main/java/domain/Line.java @@ -55,12 +55,12 @@ public String getOutcome() { } public boolean isConnectedToLeftLineAt(int position) { - final Point nowPoint = this.points.get(position); + Point nowPoint = this.points.get(position); return nowPoint.isConnectedToLeftLadder(); } public boolean isConnectedToRightLineAt(int position) { - final Point nowPoint = this.points.get(position); + Point nowPoint = this.points.get(position); return nowPoint.isConnectedToRightLadder(); } diff --git a/src/main/java/domain/RandomRungsBuilder.java b/src/main/java/domain/RandomRungsBuilder.java index 5def0327..3feb3d83 100644 --- a/src/main/java/domain/RandomRungsBuilder.java +++ b/src/main/java/domain/RandomRungsBuilder.java @@ -18,7 +18,7 @@ public RandomRungsBuilder() { public List buildAndGetRungsStatus(List prevRungsStatus) { List rungsStatus = new ArrayList<>(); for (Boolean doesPrevLadderHaveRung : prevRungsStatus) { - final boolean nowRungsStatus = generateRungIfAbsent(doesPrevLadderHaveRung); + boolean nowRungsStatus = generateRungIfAbsent(doesPrevLadderHaveRung); rungsStatus.add(nowRungsStatus); } return rungsStatus; diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index fa757f4d..86a20de9 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -21,18 +21,18 @@ public LadderService(RungsBuilder rungsBuilder) { } public Ladder createLadder(CountOfLine countOfLine, Height height, List names, List outcomes) { - final List lineCollection = createLineCollection(countOfLine, height, names, outcomes); + List lineCollection = createLineCollection(countOfLine, height, names, outcomes); return new Ladder(lineCollection); } private List createLineCollection(CountOfLine countOfLine, Height height, List names, List outcomes) { - final List lineCollection = new ArrayList<>(); + List lineCollection = new ArrayList<>(); for (int index = 0; index < countOfLine.value(); index++) { - final List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); - final String name = names.get(index); - final String outcome = outcomes.get(index); - final Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, name, outcome); + List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); + String name = names.get(index); + String outcome = outcomes.get(index); + Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, name, outcome); lineCollection.add(nowLine); } return lineCollection; @@ -42,13 +42,13 @@ private List getPrevLineRightStatus(List lineCollection, int inde if (index == 0) { return rungsBuilder.buildTemporaryRungsStatus(height.value()); } - final Line prevLine = lineCollection.get(index - 1); + Line prevLine = lineCollection.get(index - 1); return prevLine.getRightStatus(); } private Line createNowLine(int index, Height height, CountOfLine countOfLine, List nowLineLeftStatus, String name, String outcome) { - final List nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height, + List nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height, nowLineLeftStatus); if (index == 0) { nowLineLeftStatus = createEmptyStatus(height); @@ -90,7 +90,7 @@ private void validateTargetName(Map result, String targetName) { public CountOfLine getcountOfLine(List names, List outcomes) { validateCountOfLine(names, outcomes); - final int valueOfCountOfLine = names.size(); + int valueOfCountOfLine = names.size(); return new CountOfLine(valueOfCountOfLine); } diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 7a976a09..6c77669d 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -20,7 +20,7 @@ public int getUserIntegerInput() { } public List getStringList() { - final String input = getString(); + String input = getString(); return Arrays.asList(input.split(",")); } diff --git a/src/test/java/domain/LadderTest.java b/src/test/java/domain/LadderTest.java index 0505b36a..ea5bc75f 100644 --- a/src/test/java/domain/LadderTest.java +++ b/src/test/java/domain/LadderTest.java @@ -32,7 +32,7 @@ void constructorTest() { for (int i = 1; i < inputRungsStatus.size(); i++) { lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } - final Ladder ladder = new Ladder(lineCollection); + Ladder ladder = new Ladder(lineCollection); // then inputRungsStatus = inputRungsStatus.subList(1, inputRungsStatus.size()); assertThat(ladder.getRightRungStatus()) @@ -43,8 +43,8 @@ void constructorTest() { @DisplayName("Ladder 내의 모든 Line의 길이가 같지 않다면 예외가 발생한다.") void invalidHeightTest() { // given - final Line line1 = Line.of(name, outcome, Arrays.asList(true, false, true), Arrays.asList(false, false, false)); - final Line line2 = Line.of(name, outcome, Arrays.asList(false, false, false, false, false), + Line line1 = Line.of(name, outcome, Arrays.asList(true, false, true), Arrays.asList(false, false, false)); + Line line2 = Line.of(name, outcome, Arrays.asList(false, false, false, false, false), Arrays.asList(true, false, false, true, true)); // when List lineCollection = Arrays.asList(line1, line2); @@ -73,9 +73,9 @@ void resultTest() { String outcome = outcomes.get(i-1); lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } - final Ladder ladder = new Ladder(lineCollection); + Ladder ladder = new Ladder(lineCollection); // when - final Map result = ladder.getResult(); + Map result = ladder.getResult(); // then assertThat(result).isEqualTo(Map.of("일번", "300", "이번", "200", diff --git a/src/test/java/domain/LineTest.java b/src/test/java/domain/LineTest.java index 297d5a69..3c4c5dd4 100644 --- a/src/test/java/domain/LineTest.java +++ b/src/test/java/domain/LineTest.java @@ -17,11 +17,11 @@ class LineTest { @DisplayName("Line의 우측 사다리 유무를 전달받을 수 있다") void getRungsStatusTest() { // given - final List inputLeftRungStatus = Arrays.asList(false, false, false, false, false, false); - final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); + List inputLeftRungStatus = Arrays.asList(false, false, false, false, false, false); + List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when - final Line line = Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus); + Line line = Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus); // then assertThat(line.getRightStatus()) .containsExactlyElementsOf(inputRightRungStatus); @@ -31,8 +31,8 @@ void getRungsStatusTest() { @DisplayName("Line을 생성할 때, rightStatus와 leftStatus의 길이가 일치하지 않으면 에외가 발생한다.") void invalidLineConstructorTest() { // given - final List inputLeftRungStatus = Arrays.asList(false, false, false); - final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); + List inputLeftRungStatus = Arrays.asList(false, false, false); + List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) @@ -44,8 +44,8 @@ void invalidLineConstructorTest() { @DisplayName("Line을 생성할 때, 왼쪽 오른쪽이 모두 연결된 point를 생성하려 시도하면 예외가 발생한다.") void invalidPointTest() { // given - final List inputLeftRungStatus = Arrays.asList(false, false, false, false, true, true); - final List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); + List inputLeftRungStatus = Arrays.asList(false, false, false, false, true, true); + List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) diff --git a/src/test/java/service/LaddersServiceTest.java b/src/test/java/service/LaddersServiceTest.java index 75aab0f6..c3bc6f77 100644 --- a/src/test/java/service/LaddersServiceTest.java +++ b/src/test/java/service/LaddersServiceTest.java @@ -17,7 +17,7 @@ class LaddersServiceTest { final static TestRungsBuilder testRungsBuilder = new TestRungsBuilder(); - final static LadderService laddersService = new LadderService(testRungsBuilder); + static LadderService laddersService = new LadderService(testRungsBuilder); @@ -38,12 +38,12 @@ public List buildTemporaryRungsStatus(int height) { @DisplayName("createLadders()에선 RungsBuilder로 각 줄의 오른쪽 rungs 유무를 받고 이를 활용해 Ladders 객체를 만든다.") void test() { // given - final Height height = new Height(5); - final CountOfLine countOfLine = new CountOfLine(3); + Height height = new Height(5); + CountOfLine countOfLine = new CountOfLine(3); List names = Arrays.asList("일번", "이번", "삼번", "사번"); List outcomes = Arrays.asList("100", "200", "300", "400"); // when - final Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); + Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); // then assertThat(ladder.getRightRungStatus()) .isEqualTo( @@ -75,7 +75,7 @@ void getCountOfLadderTest() { List names = List.of("일", "이", "삼"); List outcomes = List.of("1000", "2000", "3000"); // when - final CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); + CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); // then assertThat(countOfLine.value()).isEqualTo(names.size()); } @@ -89,7 +89,7 @@ void selectTargetNameTest() { "삼번", "1000"); String targetName = "이번"; // when - final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + Map resultToPrint = laddersService.getResultToPrint(result, targetName); // then assertThat(resultToPrint) .isEqualTo(Map.of("이번", "꽝")); @@ -104,7 +104,7 @@ void selectAllTest() { "삼번", "1000"); String targetName = "all"; // when - final Map resultToPrint = laddersService.getResultToPrint(result, targetName); + Map resultToPrint = laddersService.getResultToPrint(result, targetName); // then assertThat(resultToPrint) .isEqualTo(result); From cc5c8c4ccc06d9b67528f37e9af3e0133675bede Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 24 Nov 2024 16:19:49 +0900 Subject: [PATCH 6/9] =?UTF-8?q?step4=20:=20null=20safe=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/service/LadderService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index 86a20de9..ead3b5dd 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -79,7 +79,7 @@ public Map getResultToPrint(Map result, String t } private boolean isAllMode(String targetName) { - return targetName.equals("all"); + return "all".equals(targetName); } private void validateTargetName(Map result, String targetName) { From 003dd3c6792236b98cab250f653d00242b7d3307 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 24 Nov 2024 16:23:15 +0900 Subject: [PATCH 7/9] =?UTF-8?q?step4=20:=20=EB=B0=A9=EC=96=B4=EC=A0=81=20?= =?UTF-8?q?=EB=B3=B5=EC=82=AC=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/service/LadderService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index ead3b5dd..6e54bef9 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -6,6 +6,7 @@ import domain.Line; import domain.RungsBuilder; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -72,7 +73,7 @@ private List createEmptyStatus(Height height) { public Map getResultToPrint(Map result, String targetName) { if (isAllMode(targetName)) { - return result; + return Collections.unmodifiableMap(result); } validateTargetName(result, targetName); return Map.of(targetName, result.get(targetName)); From bedb52f31ab92be33d1ef1ea37b173a49dff6b72 Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 24 Nov 2024 18:15:53 +0900 Subject: [PATCH 8/9] =?UTF-8?q?step4=20:=20countOfLine=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=A1=9C=EC=A7=81=20=EC=9C=84=EC=B9=98=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/LadderGameController.java | 3 +-- src/main/java/service/LadderService.java | 7 ++--- src/test/java/service/LaddersServiceTest.java | 27 +++++-------------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/java/controller/LadderGameController.java b/src/main/java/controller/LadderGameController.java index 426be93a..5be2a3df 100644 --- a/src/main/java/controller/LadderGameController.java +++ b/src/main/java/controller/LadderGameController.java @@ -25,10 +25,9 @@ public LadderGameController(RungsBuilder rungsBuilder) { public void start() { List names = getNames(); List outcomes = getOutcomes(); - CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); Height height = getHeight(); - Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); + Ladder ladder = laddersService.createLadder(height, names, outcomes); outputView.printStatusOfLadders(names, outcomes, ladder.getRightRungStatus(), height.value()); printResult(ladder.getResult()); } diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index 6e54bef9..4bd41025 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -21,8 +21,9 @@ public LadderService(RungsBuilder rungsBuilder) { this.rungsBuilder = rungsBuilder; } - public Ladder createLadder(CountOfLine countOfLine, Height height, List names, List outcomes) { - List lineCollection = createLineCollection(countOfLine, height, names, outcomes); + public Ladder createLadder(Height height, List names, List outcomes) { + CountOfLine countOfLine = getcountOfLine(names, outcomes); + List lineCollection = createLineCollection(countOfLine, height, names, outcomes); return new Ladder(lineCollection); } @@ -89,7 +90,7 @@ private void validateTargetName(Map result, String targetName) { } } - public CountOfLine getcountOfLine(List names, List outcomes) { + private CountOfLine getcountOfLine(List names, List outcomes) { validateCountOfLine(names, outcomes); int valueOfCountOfLine = names.size(); return new CountOfLine(valueOfCountOfLine); diff --git a/src/test/java/service/LaddersServiceTest.java b/src/test/java/service/LaddersServiceTest.java index c3bc6f77..27c5227a 100644 --- a/src/test/java/service/LaddersServiceTest.java +++ b/src/test/java/service/LaddersServiceTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import domain.CountOfLine; import domain.Height; import domain.Ladder; import domain.RungsBuilder; @@ -17,8 +16,7 @@ class LaddersServiceTest { final static TestRungsBuilder testRungsBuilder = new TestRungsBuilder(); - static LadderService laddersService = new LadderService(testRungsBuilder); - + static LadderService laddersService = new LadderService(testRungsBuilder); static class TestRungsBuilder implements RungsBuilder { @@ -30,7 +28,7 @@ public List buildAndGetRungsStatus(List prevRungsStatus) { @Override public List buildTemporaryRungsStatus(int height) { - return Arrays.asList(true,false, true, false, true); + return Arrays.asList(true, false, true, false, true); } } @@ -38,16 +36,16 @@ public List buildTemporaryRungsStatus(int height) { @DisplayName("createLadders()에선 RungsBuilder로 각 줄의 오른쪽 rungs 유무를 받고 이를 활용해 Ladders 객체를 만든다.") void test() { // given - Height height = new Height(5); - CountOfLine countOfLine = new CountOfLine(3); + Height height = new Height(5); List names = Arrays.asList("일번", "이번", "삼번", "사번"); List outcomes = Arrays.asList("100", "200", "300", "400"); // when - Ladder ladder = laddersService.createLadder(countOfLine, height, names, outcomes); + Ladder ladder = laddersService.createLadder(height, names, outcomes); // then assertThat(ladder.getRightRungStatus()) .isEqualTo( Arrays.asList( + Arrays.asList(false, false, false, false, false), Arrays.asList(false, false, false, false, false), Arrays.asList(false, false, false, false, false), Arrays.asList(false, false, false, false, false) @@ -59,27 +57,16 @@ void test() { @DisplayName("이름과 결과의 사이즈가 다르면 예외가 발생한다.") void invalidCountOfLadderTest() { // given + Height height = new Height(5); List names = List.of("일", "이"); List outcomes = List.of("1000", "2000", "3000"); // when // then - assertThatThrownBy(() -> laddersService.getcountOfLine(names, outcomes)) + assertThatThrownBy(() -> laddersService.createLadder(height, names, outcomes)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME); } - @Test - @DisplayName("이름과 결과의 사이즈는 곧 사다리의 개수이다.") - void getCountOfLadderTest() { - // given - List names = List.of("일", "이", "삼"); - List outcomes = List.of("1000", "2000", "3000"); - // when - CountOfLine countOfLine = laddersService.getcountOfLine(names, outcomes); - // then - assertThat(countOfLine.value()).isEqualTo(names.size()); - } - @Test @DisplayName("원하는 참가자의 실행 결과를 조회할 수 있다.") void selectTargetNameTest() { From 5f1fa8bc3e9fddd4caa4593610ebd61ab530badb Mon Sep 17 00:00:00 2001 From: JoungMinJu Date: Sun, 24 Nov 2024 18:32:12 +0900 Subject: [PATCH 9/9] =?UTF-8?q?step4=20:=20=EC=9D=B4=EB=A6=84=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Line.java | 12 ++--- src/main/java/domain/Player.java | 25 ++++++++++ src/main/java/service/LadderService.java | 47 +++++++++++-------- src/main/java/util/Errors.java | 3 +- src/test/java/domain/LadderTest.java | 31 ++++++------ src/test/java/domain/LineTest.java | 8 ++-- src/test/java/service/LaddersServiceTest.java | 16 ++++++- 7 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 src/main/java/domain/Player.java diff --git a/src/main/java/domain/Line.java b/src/main/java/domain/Line.java index eb407051..d9e23db3 100644 --- a/src/main/java/domain/Line.java +++ b/src/main/java/domain/Line.java @@ -6,18 +6,18 @@ public class Line { - private final String name; + private final Player player; private final String outcome; private final List points; - private Line(String name, String outcome, List points) { - this.name = name; + private Line(Player player, String outcome, List points) { + this.player = player; this.outcome = outcome; this.points = points; } - public static Line of(String name, String outcome, List leftRungsStatus, List rightRungsStatus) { + public static Line of(Player player, String outcome, List leftRungsStatus, List rightRungsStatus) { validateHeight(leftRungsStatus, rightRungsStatus); int maxPosition = leftRungsStatus.size(); @@ -25,7 +25,7 @@ public static Line of(String name, String outcome, List leftRungsStatus for (int position = 0; position < maxPosition; position++) { points.add(new Point(leftRungsStatus.get(position), rightRungsStatus.get(position))); } - return new Line(name, outcome, points); + return new Line(player, outcome, points); } private static void validateHeight(List leftRungsStatus, List rightRungsStatus) { @@ -47,7 +47,7 @@ public int getHeight() { } public String getName() { - return name; + return player.getName(); } public String getOutcome() { diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java new file mode 100644 index 00000000..0fcef767 --- /dev/null +++ b/src/main/java/domain/Player.java @@ -0,0 +1,25 @@ +package domain; + +import util.Errors; + +public class Player { + + private final int MAX_LENGTH = 5; + + private final String name; + + public Player(String name) { + validate(name); + this.name = name; + } + + public String getName() { + return name; + } + + private void validate(String name) { + if (name.length() > MAX_LENGTH) { + throw new IllegalArgumentException(Errors.NAME_IS_TOO_LONG); + } + } +} diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java index 4bd41025..3694e342 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -4,6 +4,7 @@ import domain.Height; import domain.Ladder; import domain.Line; +import domain.Player; import domain.RungsBuilder; import java.util.ArrayList; import java.util.Collections; @@ -22,19 +23,21 @@ public LadderService(RungsBuilder rungsBuilder) { } public Ladder createLadder(Height height, List names, List outcomes) { - CountOfLine countOfLine = getcountOfLine(names, outcomes); - List lineCollection = createLineCollection(countOfLine, height, names, outcomes); + final List players = getPlayers(names); + CountOfLine countOfLine = getcountOfLine(players, outcomes); + List lineCollection = createLineCollection(countOfLine, height, players, outcomes); return new Ladder(lineCollection); } - private List createLineCollection(CountOfLine countOfLine, Height height, List names, List outcomes) { - List lineCollection = new ArrayList<>(); + private List createLineCollection(CountOfLine countOfLine, Height height, List players, + List outcomes) { + List lineCollection = new ArrayList<>(); for (int index = 0; index < countOfLine.value(); index++) { - List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); - String name = names.get(index); - String outcome = outcomes.get(index); - Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, name, outcome); + List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); + Player player = players.get(index); + String outcome = outcomes.get(index); + Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, player, outcome); lineCollection.add(nowLine); } return lineCollection; @@ -44,18 +47,18 @@ private List getPrevLineRightStatus(List lineCollection, int inde if (index == 0) { return rungsBuilder.buildTemporaryRungsStatus(height.value()); } - Line prevLine = lineCollection.get(index - 1); + Line prevLine = lineCollection.get(index - 1); return prevLine.getRightStatus(); } private Line createNowLine(int index, Height height, CountOfLine countOfLine, - List nowLineLeftStatus, String name, String outcome) { - List nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height, - nowLineLeftStatus); + List nowLineLeftStatus, Player player, String outcome) { + List nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height, + nowLineLeftStatus); if (index == 0) { nowLineLeftStatus = createEmptyStatus(height); } - return Line.of(name, outcome, nowLineLeftStatus, nowLineRightStatus); + return Line.of(player, outcome, nowLineLeftStatus, nowLineRightStatus); } private List createNowLineRightStatus(int index, CountOfLine countOfLine, Height height, @@ -90,15 +93,21 @@ private void validateTargetName(Map result, String targetName) { } } - private CountOfLine getcountOfLine(List names, List outcomes) { - validateCountOfLine(names, outcomes); - int valueOfCountOfLine = names.size(); + private CountOfLine getcountOfLine(List players, List outcomes) { + validateCountOfLine(players, outcomes); + int valueOfCountOfLine = players.size(); return new CountOfLine(valueOfCountOfLine); } - private void validateCountOfLine(List names, List outcomes) { - if (names.size() != outcomes.size()) { - throw new IllegalArgumentException(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME); + private void validateCountOfLine(List players, List outcomes) { + if (players.size() != outcomes.size()) { + throw new IllegalArgumentException(Errors.PLAYERS_AND_OUTCOMES_SIZE_IS_NOT_SAME); } } + + private List getPlayers(List names) { + return names.stream() + .map(Player::new) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/util/Errors.java b/src/main/java/util/Errors.java index 029973e1..c72de082 100644 --- a/src/main/java/util/Errors.java +++ b/src/main/java/util/Errors.java @@ -12,7 +12,8 @@ private Errors() { public static final String INPUT_IS_NOT_INTEGER = "정수가 입력되어야 합니다."; public static final String RUNG_STATUS_LENGTH_MUST_MATCH = "Line의 left rung status의 길이와 right rung status의 길이는 일치해야합니다."; public static final String ADJACENT_POINTER_STATUS_MATCH = "인접한 line의 경우 좌측 line의 pointe의 right status와 우측 line의 left status 값은 일치해야합니다."; - public static final String NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME = "참여할 사람의 수와 결과의 수는 동일해야합니다."; + public static final String PLAYERS_AND_OUTCOMES_SIZE_IS_NOT_SAME = "참여할 사람의 수와 결과의 수는 동일해야합니다."; public static final String TARGET_NAME_MUST_BE_IN_NAMES = "결과를 조회하고 싶은 이름이 참여자 명단에 없습니다."; + public static final String NAME_IS_TOO_LONG = "이름의 길이는 5자를 넘을 수 없습니다."; } diff --git a/src/test/java/domain/LadderTest.java b/src/test/java/domain/LadderTest.java index ea5bc75f..b8fd2853 100644 --- a/src/test/java/domain/LadderTest.java +++ b/src/test/java/domain/LadderTest.java @@ -13,7 +13,7 @@ class LadderTest { - private static final String name = "이름"; + private static final Player player = new Player("이름"); private static final String outcome = "결과"; @Test @@ -30,9 +30,9 @@ void constructorTest() { // when List lineCollection = new ArrayList<>(); for (int i = 1; i < inputRungsStatus.size(); i++) { - lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); + lineCollection.add(Line.of(player, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } - Ladder ladder = new Ladder(lineCollection); + Ladder ladder = new Ladder(lineCollection); // then inputRungsStatus = inputRungsStatus.subList(1, inputRungsStatus.size()); assertThat(ladder.getRightRungStatus()) @@ -43,9 +43,9 @@ void constructorTest() { @DisplayName("Ladder 내의 모든 Line의 길이가 같지 않다면 예외가 발생한다.") void invalidHeightTest() { // given - Line line1 = Line.of(name, outcome, Arrays.asList(true, false, true), Arrays.asList(false, false, false)); - Line line2 = Line.of(name, outcome, Arrays.asList(false, false, false, false, false), - Arrays.asList(true, false, false, true, true)); + Line line1 = Line.of(player, outcome, Arrays.asList(true, false, true), Arrays.asList(false, false, false)); + Line line2 = Line.of(player, outcome, Arrays.asList(false, false, false, false, false), + Arrays.asList(true, false, false, true, true)); // when List lineCollection = Arrays.asList(line1, line2); // then @@ -65,17 +65,20 @@ void resultTest() { Arrays.asList(true, true, false, true), Arrays.asList(false, false, false, false) ); - List names = Arrays.asList("일번", "이번", "삼번", "사번"); + List players = Arrays.asList(new Player("일번"), + new Player("이번"), + new Player("삼번"), + new Player("사번")); List outcomes = Arrays.asList("100", "200", "300", "400"); List lineCollection = new ArrayList<>(); for (int i = 1; i < inputRungsStatus.size(); i++) { - String name = names.get(i-1); - String outcome = outcomes.get(i-1); - lineCollection.add(Line.of(name, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); + Player player = players.get(i - 1); + String outcome = outcomes.get(i - 1); + lineCollection.add(Line.of(player, outcome, inputRungsStatus.get(i - 1), inputRungsStatus.get(i))); } - Ladder ladder = new Ladder(lineCollection); + Ladder ladder = new Ladder(lineCollection); // when - Map result = ladder.getResult(); + Map result = ladder.getResult(); // then assertThat(result).isEqualTo(Map.of("일번", "300", "이번", "200", @@ -88,8 +91,8 @@ void resultTest() { @DisplayName("인접한 line의 경우 좌측 point의 right status 값과 우측 point의 left status 값이 일치하지 않음 예외가 발생한다.") void invalidRungTest() { // given - Line line1 = Line.of(name, outcome, Arrays.asList(false, false, false), Arrays.asList(false, true, true)); - Line line2 = Line.of(name, outcome, Arrays.asList(false, false, true), Arrays.asList(false, false, false)); + Line line1 = Line.of(player, outcome, Arrays.asList(false, false, false), Arrays.asList(false, true, true)); + Line line2 = Line.of(player, outcome, Arrays.asList(false, false, true), Arrays.asList(false, false, false)); List lineCollection = new ArrayList<>(List.of(line1, line2)); // when // then diff --git a/src/test/java/domain/LineTest.java b/src/test/java/domain/LineTest.java index 3c4c5dd4..e270f145 100644 --- a/src/test/java/domain/LineTest.java +++ b/src/test/java/domain/LineTest.java @@ -10,7 +10,7 @@ import util.Errors; class LineTest { - private static final String name = "이름"; + private static final Player player = new Player("이름"); private static final String outcome = "결과"; @Test @@ -21,7 +21,7 @@ void getRungsStatusTest() { List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when - Line line = Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus); + Line line = Line.of(player, outcome, inputLeftRungStatus, inputRightRungStatus); // then assertThat(line.getRightStatus()) .containsExactlyElementsOf(inputRightRungStatus); @@ -35,7 +35,7 @@ void invalidLineConstructorTest() { List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then - assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) + assertThatThrownBy(() -> Line.of(player, outcome, inputLeftRungStatus, inputRightRungStatus)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.RUNG_STATUS_LENGTH_MUST_MATCH); } @@ -48,7 +48,7 @@ void invalidPointTest() { List inputRightRungStatus = Arrays.asList(true, false, false, false, true, true); // when // then - assertThatThrownBy(() -> Line.of(name, outcome, inputLeftRungStatus, inputRightRungStatus)) + assertThatThrownBy(() -> Line.of(player, outcome, inputLeftRungStatus, inputRightRungStatus)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.ADJACENT_LADDERS_CANNOT_HAVE_RUNG_AT_SAME_POSITION); } diff --git a/src/test/java/service/LaddersServiceTest.java b/src/test/java/service/LaddersServiceTest.java index 27c5227a..7596f8fe 100644 --- a/src/test/java/service/LaddersServiceTest.java +++ b/src/test/java/service/LaddersServiceTest.java @@ -64,7 +64,7 @@ void invalidCountOfLadderTest() { // then assertThatThrownBy(() -> laddersService.createLadder(height, names, outcomes)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME); + .hasMessage(Errors.PLAYERS_AND_OUTCOMES_SIZE_IS_NOT_SAME); } @Test @@ -111,4 +111,18 @@ void invalidSelectTest() { .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.TARGET_NAME_MUST_BE_IN_NAMES); } + + @Test + @DisplayName("참가자 이름이 다섯글자가 넘어가면 예외가 발생한다.") + void invalidNameTest() { + // given + Height height = new Height(5); + List names = List.of("일이삼사오육", "이"); + List outcomes = List.of("1000", "2000", "3000"); + // when + // then + assertThatThrownBy(() -> laddersService.createLadder(height, names, outcomes)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(Errors.NAME_IS_TOO_LONG); + } }