Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- 사다리의 크기를 입력받아 생성할 수 있다.
### step3
- 사다리를 보여준 후 결과를 출력한다.
### step4
- 사다리 게임에 참여하는 사람에 이름을 최대 5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다.
- 사람 이름은 쉼표(,)를 기준으로 구분한다.
- 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한

**📠 도메인 분석 내용**
- 사용자는 "참여 인원 수"를 입력한다 (네이버엔 구현되어있지만 실행예시엔 해당 없음)
Expand Down Expand Up @@ -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

```
32 changes: 24 additions & 8 deletions src/main/java/controller/LadderGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,24 +23,38 @@ public LadderGameController(RungsBuilder rungsBuilder) {
}

public void start() {
CountOfLine countOfLine = getcountOfLine();
List<String> names = getNames();
List<String> 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 사다리를 생성하는 과정에서 countOfLine, heigth, names과 outcomes를 인자로 받고 있는 상태입니다.
countOfLine은 names과 outcomes에서 파생되는 결과로 보입니다. (이는 사다리를 생성하는 과정에서 필요한 과정으로 보입니다)
countOfLine을 생성하는 과정을 굳이 controller라는 외부에서 드러낼 필요 없이 createLadder의 내부 과정으로 넣는 것이 더 사다리 생성이라는 기능관점에서 볼 때에서 응집도가 높을 것 같습니다.

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<String> getNames() {
outputView.printInputNamesGuide();
return inputView.getStringList();
}

private List<String> getOutcomes() {
outputView.printInputOutcomesGuid();
return inputView.getStringList();
}


private Height getHeight() {
outputView.printInputHeightGuide();
final int valueOfHeight = inputView.getUserIntegerInput();
return new Height(valueOfHeight);
}

private void printResult(Map<String, String> result) {
outputView.printInputTargetName();
final String targetName = inputView.getString();
final Map<String, String> resultToPrint = laddersService.getResultToPrint(result, targetName);
outputView.printResult(resultToPrint);
}

}
22 changes: 17 additions & 5 deletions src/main/java/domain/Ladder.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -30,9 +32,9 @@ private void validateLaddersHeight(List<Line> lines) {
}

private void validatePointStatus(List<Line> 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);
}
}
Expand Down Expand Up @@ -60,13 +62,15 @@ public int getHeight() {
return this.lines.get(0).getHeight();
}

public List<Integer> getResult() {
List<Integer> result = new ArrayList<>();
public Map<String, String> getResult() {
Map<String, String> 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;
}
Expand All @@ -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();
}
}
20 changes: 17 additions & 3 deletions src/main/java/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@

public class Line {

private final String name;
private final String outcome;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분은 player 객체를 따로 만들어서 관리를 해야할 것 같습니다!
현재 line의 책임이 큰 것 같습니다! (현재 요구사항 중 이름 글자수 검증은 따로 코드에 구현이 안된 것 같은데 이는 사다리의 요소인 line이 책임져야할 부분은 아닌 것 같습니다)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이름 조건을 확인못했습니다! 구현하도록 하겠습니다

private final List<Point> points;

private Line(List<Point> points) {
private Line(String name, String outcome, List<Point> points) {
this.name = name;
this.outcome = outcome;
this.points = points;
}

public static Line of(List<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
public static Line of(String name, String outcome, List<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
validateHeight(leftRungsStatus, rightRungsStatus);

int maxPosition = leftRungsStatus.size();
List<Point> points = new ArrayList<>();
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<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
if (leftRungsStatus.size() != rightRungsStatus.size()) {
throw new IllegalArgumentException(Errors.RUNG_STATUS_LENGTH_MUST_MATCH);
}
}

public List<Boolean> getRightStatus() {
List<Boolean> rightStatus = new ArrayList<>();
for (Point point : points) {
Expand All @@ -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();
Expand Down
46 changes: 40 additions & 6 deletions src/main/java/service/LadderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -18,17 +20,19 @@ public LadderService(RungsBuilder rungsBuilder) {
this.rungsBuilder = rungsBuilder;
}

public Ladder createLadder(CountOfLine countOfLine, Height height) {
final List<Line> lineCollection = createLineCollection(countOfLine, height);
public Ladder createLadder(CountOfLine countOfLine, Height height, List<String> names, List<String> outcomes) {
final List<Line> lineCollection = createLineCollection(countOfLine, height, names, outcomes);
return new Ladder(lineCollection);
}

private List<Line> createLineCollection(CountOfLine countOfLine, Height height) {
private List<Line> createLineCollection(CountOfLine countOfLine, Height height, List<String> names, List<String> outcomes) {
final List<Line> lineCollection = new ArrayList<>();

for (int index = 0; index < countOfLine.value(); index++) {
final List<Boolean> 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;
Expand All @@ -43,13 +47,13 @@ private List<Boolean> getPrevLineRightStatus(List<Line> lineCollection, int inde
}

private Line createNowLine(int index, Height height, CountOfLine countOfLine,
List<Boolean> nowLineLeftStatus) {
List<Boolean> nowLineLeftStatus, String name, String outcome) {
final List<Boolean> 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<Boolean> createNowLineRightStatus(int index, CountOfLine countOfLine, Height height,
Expand All @@ -65,4 +69,34 @@ private List<Boolean> createEmptyStatus(Height height) {
.mapToObj(i -> false)
.collect(Collectors.toList());
}

public Map<String, String> getResultToPrint(Map<String, String> result, String targetName) {
if (isAllMode(targetName)) {
return result;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 result를 View에 제공할 때 방어적 복사를 해주는 것이 안전할 것 같습니다. (외부의 변화가 내부의 변화로 이어지지 않습니다)

validateTargetName(result, targetName);
return Map.of(targetName, result.get(targetName));
}

private boolean isAllMode(String targetName) {
return targetName.equals("all");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetName에 null이 들어오는 경우 NullPointerException이 발생하기에 "all".equals(targetName)으로 하는 것이 null safe합니다.


private void validateTargetName(Map<String, String> result, String targetName) {
if (!result.containsKey(targetName)) {
throw new IllegalArgumentException(Errors.TARGET_NAME_MUST_BE_IN_NAMES);
}
}

public CountOfLine getcountOfLine(List<String> names, List<String> outcomes) {
validateCountOfLine(names, outcomes);
final int valueOfCountOfLine = names.size();
return new CountOfLine(valueOfCountOfLine);
}

private void validateCountOfLine(List<String> names, List<String> outcomes) {
if (names.size() != outcomes.size()) {
throw new IllegalArgumentException(Errors.NAMES_AND_OUTCOMES_SIZE_IS_NOT_SAME);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/util/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "결과를 조회하고 싶은 이름이 참여자 명단에 없습니다.";

}
11 changes: 11 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,5 +19,14 @@ public int getUserIntegerInput() {
}
}

public List<String> getStringList() {
final String input = getString();
return Arrays.asList(input.split(","));
}

public String getString() {
scanner = new Scanner(System.in);
return scanner.nextLine();
}

}
Loading