-
Notifications
You must be signed in to change notification settings - Fork 60
[사다리 - 4단계] 정민주 미션 제출합니다. #25
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
Changes from 4 commits
179a889
24846dd
dd35e56
4c20841
d1acd60
cc5c8c4
003dd3c
bedb52f
5f1fa8b
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 |
|---|---|---|
|
|
@@ -6,28 +6,34 @@ | |
|
|
||
| public class Line { | ||
|
|
||
| private final String name; | ||
| private final String outcome; | ||
|
|
||
|
||
| 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) { | ||
|
|
@@ -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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<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; | ||
|
|
@@ -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, | ||
|
|
@@ -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; | ||
| } | ||
|
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. 해당 result를 View에 제공할 때 방어적 복사를 해주는 것이 안전할 것 같습니다. (외부의 변화가 내부의 변화로 이어지지 않습니다) |
||
| validateTargetName(result, targetName); | ||
| return Map.of(targetName, result.get(targetName)); | ||
| } | ||
|
|
||
| private boolean isAllMode(String targetName) { | ||
| return targetName.equals("all"); | ||
| } | ||
|
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. targetName에 null이 들어오는 경우 NullPointerException이 발생하기에 |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
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.
현재 사다리를 생성하는 과정에서 countOfLine, heigth, names과 outcomes를 인자로 받고 있는 상태입니다.
countOfLine은 names과 outcomes에서 파생되는 결과로 보입니다. (이는 사다리를 생성하는 과정에서 필요한 과정으로 보입니다)
countOfLine을 생성하는 과정을 굳이 controller라는 외부에서 드러낼 필요 없이 createLadder의 내부 과정으로 넣는 것이 더 사다리 생성이라는 기능관점에서 볼 때에서 응집도가 높을 것 같습니다.