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 ``` 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 9c4ba14d..5be2a3df 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,37 @@ public LadderGameController(RungsBuilder rungsBuilder) { } public void start() { - CountOfLine countOfLine = getcountOfLine(); + List names = getNames(); + List outcomes = getOutcomes(); Height height = getHeight(); - Ladder ladder = laddersService.createLadder(countOfLine, height); - outputView.printStatusOfLadders(ladder.getRightRungStatus(), height.value()); - outputView.printResult(ladder.getResult()); + Ladder ladder = laddersService.createLadder(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(); + int valueOfHeight = inputView.getUserIntegerInput(); return new Height(valueOfHeight); } + private void printResult(Map result) { + outputView.printInputTargetName(); + 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 7517ccca..6488a9d9 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); } } @@ -44,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)); @@ -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); + String name = getNameOfLine(nowIndex); + 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/main/java/domain/Line.java b/src/main/java/domain/Line.java index 36a6f0fa..d9e23db3 100644 --- a/src/main/java/domain/Line.java +++ b/src/main/java/domain/Line.java @@ -6,13 +6,18 @@ public class Line { + private final Player player; + private final String outcome; + private final List points; - private Line(List points) { + private Line(Player player, String outcome, List points) { + this.player = player; + this.outcome = outcome; this.points = points; } - public static Line of(List leftRungsStatus, List rightRungsStatus) { + public static Line of(Player player, 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(player, 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,13 +46,21 @@ public int getHeight() { return this.points.size(); } + public String getName() { + return player.getName(); + } + + public String getOutcome() { + return outcome; + } + 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/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/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 6bd08ff8..3694e342 100644 --- a/src/main/java/service/LadderService.java +++ b/src/main/java/service/LadderService.java @@ -4,11 +4,15 @@ import domain.Height; import domain.Ladder; import domain.Line; +import domain.Player; import domain.RungsBuilder; import java.util.ArrayList; +import java.util.Collections; 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 +22,22 @@ public LadderService(RungsBuilder rungsBuilder) { this.rungsBuilder = rungsBuilder; } - public Ladder createLadder(CountOfLine countOfLine, Height height) { - final List lineCollection = createLineCollection(countOfLine, height); + public Ladder createLadder(Height height, List names, List 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) { - final 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++) { - final List prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height); - final Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus); + 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; @@ -38,18 +47,18 @@ 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) { - final 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(nowLineLeftStatus, nowLineRightStatus); + return Line.of(player, outcome, nowLineLeftStatus, nowLineRightStatus); } private List createNowLineRightStatus(int index, CountOfLine countOfLine, Height height, @@ -65,4 +74,40 @@ private List createEmptyStatus(Height height) { .mapToObj(i -> false) .collect(Collectors.toList()); } + + public Map getResultToPrint(Map result, String targetName) { + if (isAllMode(targetName)) { + return Collections.unmodifiableMap(result); + } + validateTargetName(result, targetName); + return Map.of(targetName, result.get(targetName)); + } + + private boolean isAllMode(String targetName) { + return "all".equals(targetName); + } + + private void validateTargetName(Map result, String targetName) { + if (!result.containsKey(targetName)) { + throw new IllegalArgumentException(Errors.TARGET_NAME_MUST_BE_IN_NAMES); + } + } + + private CountOfLine getcountOfLine(List players, List outcomes) { + validateCountOfLine(players, outcomes); + int valueOfCountOfLine = players.size(); + return new CountOfLine(valueOfCountOfLine); + } + + 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 f684a91a..c72de082 100644 --- a/src/main/java/util/Errors.java +++ b/src/main/java/util/Errors.java @@ -12,5 +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 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/main/java/view/InputView.java b/src/main/java/view/InputView.java index 34db6b70..6c77669d 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() { + 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/domain/LadderTest.java b/src/test/java/domain/LadderTest.java index d29fb400..b8fd2853 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 Player player = new Player("이름"); + private static final String outcome = "결과"; + @Test @DisplayName("Ladder를 통해 각 사다리의 우측 가로줄 유무를 모두 받아올 수 있다.") void constructorTest() { @@ -26,9 +30,9 @@ 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(player, 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()) @@ -39,9 +43,9 @@ 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), - 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 @@ -61,23 +65,34 @@ void resultTest() { Arrays.asList(true, true, false, true), Arrays.asList(false, false, false, false) ); + 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++) { - lineCollection.add(Line.of(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))); } - final Ladder ladder = new Ladder(lineCollection); + Ladder ladder = new Ladder(lineCollection); // when - final List result = ladder.getResult(); + 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(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 ba07003f..e270f145 100644 --- a/src/test/java/domain/LineTest.java +++ b/src/test/java/domain/LineTest.java @@ -10,16 +10,18 @@ import util.Errors; class LineTest { + private static final Player player = new Player("이름"); + private static final String outcome = "결과"; @Test @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(inputLeftRungStatus, inputRightRungStatus); + Line line = Line.of(player, outcome, inputLeftRungStatus, inputRightRungStatus); // then assertThat(line.getRightStatus()) .containsExactlyElementsOf(inputRightRungStatus); @@ -29,11 +31,11 @@ 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(inputLeftRungStatus, inputRightRungStatus)) + assertThatThrownBy(() -> Line.of(player, outcome, inputLeftRungStatus, inputRightRungStatus)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(Errors.RUNG_STATUS_LENGTH_MUST_MATCH); } @@ -42,11 +44,11 @@ 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(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 fe629c3e..7596f8fe 100644 --- a/src/test/java/service/LaddersServiceTest.java +++ b/src/test/java/service/LaddersServiceTest.java @@ -1,18 +1,24 @@ package service; 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; 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(); + static LadderService laddersService = new LadderService(testRungsBuilder); + + static class TestRungsBuilder implements RungsBuilder { @Override @@ -22,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); } } @@ -30,20 +36,93 @@ 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); - final TestRungsBuilder testRungsBuilder = new TestRungsBuilder(); + Height height = new Height(5); + 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); + 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) ) ); } + + @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.createLadder(height, names, outcomes)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(Errors.PLAYERS_AND_OUTCOMES_SIZE_IS_NOT_SAME); + } + + @Test + @DisplayName("원하는 참가자의 실행 결과를 조회할 수 있다.") + void selectTargetNameTest() { + // given + Map result = Map.of("일번", "2000", + "이번", "꽝", + "삼번", "1000"); + String targetName = "이번"; + // when + 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 + 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); + } + + @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); + } }