-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Step3 #1853
base: hoyeoon
Are you sure you want to change the base?
Step3 #1853
Changes from all commits
97f2c06
7e1b66c
0796fcd
7db91d9
56f35bb
c9688d4
fb04575
17e3d2e
02d71a8
d2c1b02
5ce386b
85628d9
9cfc315
c457816
4fd74e1
35046c7
1103791
b91f2b2
74beb37
42f3c43
5aea879
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.ladder.domain; | ||
|
||
public class ExecuteResult { | ||
private static final int NAME_LENGTH_MAX = 5; | ||
|
||
private final String name; | ||
|
||
public ExecuteResult(String name) { | ||
if (name.length() > NAME_LENGTH_MAX) { | ||
throw new IllegalArgumentException("글자수는 최대 5글자까지 부여할 수 있습니다."); | ||
} | ||
this.name = name; | ||
} | ||
|
||
public String name() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ExecuteResult{" + | ||
"name='" + name + '\'' + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExecuteResults { | ||
|
||
private final List<ExecuteResult> executeResults; | ||
|
||
public ExecuteResults(String[] executeResults) { | ||
if (executeResults.length == 0) { | ||
throw new IllegalStateException("실행 결과가 입력되지 않았습니다."); | ||
} | ||
this.executeResults = Arrays.stream(executeResults). | ||
map(ExecuteResult::new). | ||
collect(Collectors.toList()); | ||
} | ||
|
||
public List<ExecuteResult> value() { | ||
return executeResults; | ||
} | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지금과 같은 복잡도와 혼자 작업하는 상황에서라면 상관없겠지만. 이렇게
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.ladder.domain; | ||
|
||
public class InputOutput { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
사다리 게임의 결과가 담겨있다는 의미를 나타낼 수 있는 클래스명으로 변경해보시면 어떨까요? |
||
|
||
private final People people; | ||
private final ExecuteResults executeResults; | ||
|
||
public InputOutput(People people, ExecuteResults executeResults) { | ||
if (people.value().size() != executeResults.value().size()) { | ||
throw new IllegalArgumentException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); | ||
} | ||
this.people = people; | ||
this.executeResults = executeResults; | ||
} | ||
|
||
public People people() { | ||
return people; | ||
} | ||
|
||
public ExecuteResults executeResults() { | ||
return executeResults; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,29 +2,63 @@ | |
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
public class Ladder { | ||
|
||
private final People people; | ||
private static final int BEGIN_INDEX = 0; | ||
|
||
private final int verticalLineCount; | ||
private final Lines lines; | ||
|
||
public Ladder(People people, int height) { | ||
this.people = people; | ||
this.lines = new Lines(generateLadder(height)); | ||
public Ladder(int verticalLineCount, Lines lines) { | ||
this.verticalLineCount = verticalLineCount; | ||
this.lines = lines; | ||
} | ||
|
||
private List<Line> generateLadder(int height) { | ||
return Stream.generate(() -> new Line(people.value().size())) | ||
.limit(height) | ||
.collect(Collectors.toList()); | ||
public Ladder(int verticalLineCount, int height) { | ||
this(verticalLineCount, new Lines(generateLines(verticalLineCount, height))); | ||
} | ||
|
||
public People people() { | ||
return people; | ||
private static List<Line> generateLines(int verticalLineCount, int height) { | ||
return Stream.generate(() -> new Line(verticalLineCount)) | ||
.limit(height) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public Lines lines() { | ||
return lines; | ||
} | ||
|
||
public String[][] result() { | ||
List<Line> lines = this.lines.value(); | ||
|
||
int width = verticalLineCount * 2 - 1; | ||
int height = this.lines.value().size(); | ||
|
||
String[][] ladder = new String[height][width]; | ||
|
||
IntStream.range(BEGIN_INDEX, height) | ||
.forEach(i -> IntStream.range(BEGIN_INDEX, width) | ||
.forEach(j -> { | ||
if (isVerticalLine(j)) { | ||
ladder[i][j] = "v"; | ||
return; | ||
} | ||
if (isHorizontalLine(lines, i, j)) { | ||
ladder[i][j] = "h"; | ||
} | ||
})); | ||
return ladder; | ||
} | ||
Comment on lines
+34
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collection에 여러 편의 메서드들이 존재하는데 다시 이차원 배열을 활용하는게 좋은 선택일지 고민이 필요할 것 같아요. |
||
|
||
private boolean isVerticalLine(int j) { | ||
return j % 2 == 0; | ||
} | ||
|
||
private Boolean isHorizontalLine(List<Line> lines, int i, int j) { | ||
return lines.get(i).value().get((j - 1) / 2); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Match { | ||
|
||
private static final int BEGIN_IDX = 0; | ||
|
||
private final InputOutput inputOutput; | ||
private final Ladder ladder; | ||
|
||
public Match(InputOutput inputOutput, Ladder ladder) { | ||
this.inputOutput = inputOutput; | ||
this.ladder = ladder; | ||
} | ||
|
||
public Match(Ladder ladder) { | ||
this(null, ladder); | ||
} | ||
|
||
public Result makeResult() { | ||
List<Person> people = inputOutput.people().value(); | ||
List<ExecuteResult> executeResults = inputOutput.executeResults().value(); | ||
|
||
return new Result(IntStream.range(BEGIN_IDX, people.size()) | ||
.boxed() | ||
.collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name(), | ||
(v1, v2) -> v1, LinkedHashMap::new))); | ||
} | ||
|
||
int findOutputIdx(int startIdx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기에 접근 제한자가 빠진 이유가 있을까요~? makeResult에서만 활용하는거라면 private 메서드로 만들어주시면 좋겠습니다. |
||
int currentIdx = startIdx * 2; | ||
String[][] ladder = this.ladder.result(); | ||
int rowCount = ladder.length; | ||
int columnLength = ladder[BEGIN_IDX].length; | ||
|
||
currentIdx = IntStream.range(BEGIN_IDX, rowCount) | ||
.reduce(currentIdx, (idx, i) -> { | ||
String[] row = ladder[i]; | ||
if (isMovableToLeft(idx, row)) { | ||
return idx - 2; | ||
} | ||
if (isMovableToRight(idx, row, columnLength)) { | ||
return idx + 2; | ||
} | ||
return idx; | ||
}); | ||
Comment on lines
+41
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 로직이 복잡한데 메서드로 추출해보는건 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전체적으로 클래스도, 책임도 잘 나누어 주셨는데 이 부분의 복잡도가 높은 것 같습니다. 메서드 추출 혹은 리팩토링으로 조금 더 로직을 가독성 좋게 만들어보시면 좋겠습니다. |
||
return currentIdx / 2; | ||
} | ||
|
||
private boolean isMovableToLeft(int idx, String[] row) { | ||
return idx > BEGIN_IDX && "h".equals(row[idx - 1]); | ||
} | ||
|
||
private boolean isMovableToRight(int idx, String[] row, int columnLength) { | ||
return idx < columnLength - 1 && "h".equals(row[idx + 1]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import java.util.Map; | ||
|
||
public class Result { | ||
|
||
private final Map<String, String> result; | ||
|
||
public Result(Map<String, String> result) { | ||
this.result = result; | ||
} | ||
|
||
public String get(String person) { | ||
if (result.get(person) == null) { | ||
throw new IllegalArgumentException("해당하는 사람이 존재하지 않습니다."); | ||
} | ||
return result.get(person); | ||
} | ||
|
||
public Map<String, String> value() { | ||
return this.result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일급 컬렉션이나 클래스 분리, 제약 등을 깔끔하게 잘 정의해주셨네요 👍