-
Notifications
You must be signed in to change notification settings - Fork 33
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
[LBP] 김준 사다리 5단계 제출합니다. #43
base: kjoon418
Are you sure you want to change the base?
Changes from all commits
d02f1bd
f072a93
00f6903
35edfcf
7059223
2fde758
63d9750
f61159e
857cfc9
74d4322
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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import controller.LadderController; | ||
|
||
public class LadderApplication { | ||
public static void main(String[] args) { | ||
LadderController ladderController = LadderController.getInstance(); | ||
|
||
ladderController.run(); | ||
public static void main(String[] args) { | ||
try { | ||
LadderController ladderController = LadderController.getInstance(); | ||
ladderController.run(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dto; | ||
|
||
public record LadderResultDto(String name, String resultValue) { | ||
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. record 사용 👍🏻 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import static model.LinkStatus.PRESENT; | ||
|
||
public class DetachedRandomLinksGenerator implements LinksGenerator { | ||
|
||
private static final Random random = new Random(); | ||
|
||
private final int linksSize; | ||
|
||
public DetachedRandomLinksGenerator(int linksSize) { | ||
this.linksSize = linksSize; | ||
} | ||
|
||
public List<Link> generate() { | ||
List<Link> links = getUndefinedLinks(linksSize); | ||
resolveUndefinedLinks(links); | ||
|
||
return Collections.unmodifiableList(links); | ||
} | ||
|
||
private List<Link> getUndefinedLinks(int linksSize) { | ||
List<Link> links = new ArrayList<>(); | ||
for (int i = 0; i < linksSize; i++) { | ||
links.add(Link.getUndefinedLink()); | ||
} | ||
|
||
return links; | ||
} | ||
|
||
private void resolveUndefinedLinks(List<Link> links) { | ||
int index = getRandomStartIndex(links); | ||
|
||
while (containsUndefined(links)) { | ||
boolean connectDecider = getConnectDecider(); | ||
boolean connectable = isConnectable(index, links); | ||
|
||
links.set(index, Link.getDefinedLink(connectDecider, connectable)); | ||
|
||
index = getNextIndex(index, links); | ||
} | ||
} | ||
|
||
private int getRandomStartIndex(List<Link> links) { | ||
return random.nextInt(links.size()); | ||
} | ||
|
||
private boolean containsUndefined(List<Link> links) { | ||
return links.stream() | ||
.anyMatch(Link::isUndefined); | ||
} | ||
|
||
private boolean getConnectDecider() { | ||
return random.nextBoolean(); | ||
} | ||
|
||
private boolean isConnectable(int index, List<Link> links) { | ||
return isLeftNotPresent(index, links) && isRightNotPresent(index, links); | ||
} | ||
|
||
private boolean isLeftNotPresent(int index, List<Link> links) { | ||
if (isFirstIndex(index)) { | ||
return true; | ||
} | ||
|
||
Link leftLink = links.get(index - 1); | ||
|
||
return leftLink.getLinkstatus() != PRESENT; | ||
} | ||
|
||
private boolean isRightNotPresent(int index, List<Link> links) { | ||
if (isLastIndex(index, links)) { | ||
return true; | ||
} | ||
|
||
Link rightLink = links.get(index + 1); | ||
|
||
return rightLink.getLinkstatus() != PRESENT; | ||
} | ||
|
||
private boolean isFirstIndex(int index) { | ||
return index == 0; | ||
} | ||
|
||
private boolean isLastIndex(int index, List<Link> links) { | ||
return index == links.size() - 1; | ||
} | ||
|
||
private int getNextIndex(int index, List<Link> linkStatuses) { | ||
return (index + 1) % linkStatuses.size(); | ||
} | ||
Comment on lines
+66
to
+96
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. 맞습니다...! 유사한 로직이
이 점 때문에 메서드 이름을 전략 패턴을 처음 사용해보면서 놓친 것이 많은 것 같은데, 오래 고민한 후 해결해 보도록 하겠습니다! |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,52 @@ | |
import java.util.List; | ||
|
||
public class Ladder { | ||
|
||
private static final int MINIMUM_WIDTH = 2; | ||
private static final int MINIMUM_HEIGHT = 1; | ||
|
||
private final List<Line> lines; | ||
|
||
public Ladder(int width, int height) { | ||
validateSize(width, height); | ||
|
||
this.lines = createLines(width, height); | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
|
||
public int getEndPoint(int startPoint) { | ||
int point = startPoint; | ||
|
||
for (Line line : lines) { | ||
point = line.getNextPoint(point); | ||
} | ||
|
||
return point; | ||
} | ||
|
||
private List<Line> createLines(int width, int height) { | ||
int linksSize = width - 1; | ||
DetachedRandomLinksGenerator linksGenerator = new DetachedRandomLinksGenerator(linksSize); | ||
|
||
List<Line> lines = new ArrayList<>(); | ||
for (int i = 0; i < height; i++) { | ||
lines.add(new Line(width)); | ||
Line line = new Line(linksGenerator); | ||
lines.add(line); | ||
} | ||
|
||
this.lines = Collections.unmodifiableList(lines); | ||
return Collections.unmodifiableList(lines); | ||
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. 정말 부끄럽게도 아래는 저의 기준입니다...! 저는 둘의 차이점을 공부하며, unmodifiable...()는 기존 컬렉션과 똑같은 참조를 반환한다라는 점에 주목했습니다. |
||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
private void validateSize(int width, int height) { | ||
if (width < MINIMUM_WIDTH) { | ||
throw new IllegalArgumentException("사다리의 너비는 " + MINIMUM_WIDTH + "보다 짧을 수 없습니다. 전달된 값: " + width); | ||
} | ||
if (height < MINIMUM_HEIGHT) { | ||
throw new IllegalArgumentException("사다리의 높이는 " + MINIMUM_HEIGHT + "보다 짧을 수 없습니다. 전달된 값: " + height); | ||
} | ||
Comment on lines
+51
to
+53
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. 높이 입력값이 1일 때도 사다리가 생성이 되나요? 🧐 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,37 @@ | ||
package model; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class LadderResultCalculator { | ||
|
||
private final LadderUsers ladderUsers; | ||
private final List<String> resultValues; | ||
|
||
public LadderResultCalculator(LadderUsers ladderUsers, String[] resultValues) { | ||
validateSize(ladderUsers, resultValues); | ||
|
||
this.ladderUsers = ladderUsers; | ||
this.resultValues = Arrays.stream(resultValues) | ||
.toList(); | ||
} | ||
|
||
public String calculate(String name, Ladder ladder) { | ||
int index = ladderUsers.findIndexOfUserByName(name) | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 이름입니다.")); | ||
int endPoint = ladder.getEndPoint(index); | ||
|
||
return findResultByIndex(endPoint); | ||
} | ||
|
||
private void validateSize(LadderUsers ladderUsers, String[] resultValues) { | ||
if (ladderUsers.size() != resultValues.length) { | ||
throw new IllegalArgumentException("참여자의 수와 실행 결과의 수가 일치하지 않습니다."); | ||
} | ||
} | ||
|
||
private String findResultByIndex(int index) { | ||
return resultValues.get(index); | ||
} | ||
|
||
} |
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.
*
을 사용하신 이유가 있나요?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.
IntelliJ의 자동 임포트 기능을 활용하다보니
*
가 사용된 것 같습니다.IntelliJ가 제공해준
import
구문을 굳이 바꿀 필요 없다 생각해서 의식하지 않았는데, 이에 관한 리뷰어님의 의견이 궁금합니다.리뷰어님은
*
의 사용을 지양하시나요?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.
저는 어떤 클래스가
import
되는지 명시를 해주는 게 다른 개발자가 코드를 읽을 때 "아! 이 도메인이 사용되었구나." 하고 빠르게 이해하는 데 도움이 된다고 생각해요.그리고 대부분의 컴파일러에서 사용되지 않는
import
는 자동으로 최적화되지만 일부 경우에는 불필요한 클래스가 로드될 수 있기 때문에*
사용을 지양하는 편이에요!하지만 무조건
*
을 사용하지 않는 것은 아니고, 상수나Math
같은 유틸 클래스의 메서드를 여러 개 사용할 경우에는 사용해도 된다고 생각합니다 😄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.
조언 감사합니다!
앞으로 코드를 작성할 때
*
가 과하게 사용되진 않았는지 확인하도록 하겠습니다 😄