-
Notifications
You must be signed in to change notification settings - Fork 708
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
Step2, 3 #1813
base: 201411167
Are you sure you want to change the base?
Step2, 3 #1813
Changes from all commits
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,5 @@ | ||
package nextstep.fp; | ||
|
||
public interface SumStrategy { | ||
boolean option(int number); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nextstep.ladder; | ||
|
||
import nextstep.ladder.domain.Ladder; | ||
import nextstep.ladder.domain.LadderResults; | ||
import nextstep.ladder.domain.User; | ||
import nextstep.ladder.domain.Users; | ||
import nextstep.ladder.view.InputView; | ||
import nextstep.ladder.view.OutputView; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
|
||
public class LadderApplication { | ||
|
||
public static void main(String[] args) { | ||
|
||
List<String> names = InputView.inputUserNames(); | ||
Users users = new Users(names); | ||
|
||
List<String> results = InputView.inputLadderResult(); | ||
LadderResults ladderResults = new LadderResults(results); | ||
|
||
int height = InputView.inputLadderHeight(); | ||
|
||
Ladder ladder = Ladder.createLadder(users.getUsers().size(), height, () -> new Random().nextBoolean()); | ||
OutputView.display(users, ladder, ladderResults); | ||
|
||
List<User> usersAfter = users.getUsers().stream() | ||
.map(user -> user.rideLadder(ladder)) | ||
.collect(Collectors.toList()); | ||
Comment on lines
+29
to
+31
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. getter로 값을 꺼내서 처리하는 대신에, 메시지를 보내는 방식으로 처리해보면 어떨까요? |
||
|
||
String name = InputView.inputUserName(); | ||
|
||
if (name.equals("all")) { | ||
OutputView.announceAllUsers(usersAfter, ladderResults); | ||
return; | ||
} | ||
OutputView.announceUser(name, usersAfter, ladderResults); | ||
Comment on lines
+35
to
+39
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. 해당 로직은 OutputView 내부로 옮겨보면 어떨까요? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package nextstep.ladder.domain; | ||
|
||
@FunctionalInterface | ||
public interface BridgeStrategy { | ||
Boolean bridgeBuild(); | ||
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,44 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
public class Ladder { | ||
private List<Line> lines = new ArrayList<>(); | ||
|
||
public static Ladder createLadder(int countOfUsers, int height, BridgeStrategy strategy) { | ||
return new Ladder(countOfUsers, height, strategy); | ||
} | ||
|
||
private Ladder(int countOfUsers, int height, BridgeStrategy strategy) { | ||
for (int i = 0; i < height; i++) { | ||
Line line = Line.createLine(countOfUsers, strategy); | ||
lines.add(line); | ||
} | ||
} | ||
Comment on lines
+12
to
+21
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. 다음 클래스 작성 순서 가이드를 참고해보시면 좋을 것 같습니다. |
||
|
||
public String status() { | ||
String result = ""; | ||
int height = lines.size(); | ||
for (int i = 0; i < height; i++) { | ||
Line line = lines.get(i); | ||
result += line.status(); | ||
} | ||
return result; | ||
} | ||
|
||
public int findLastPosition(int position) { | ||
if (lines.isEmpty()) { | ||
return position; | ||
} | ||
|
||
int currentPosition = position; | ||
for (Line line : lines) { | ||
currentPosition = line.getNextPosition(currentPosition); | ||
} | ||
return currentPosition; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class LadderResult { | ||
private String result; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
public class LadderResults { | ||
private List<LadderResult> ladderResultList = new ArrayList<>(); | ||
|
||
public LadderResults(List<String> results) { | ||
for (String result : results) { | ||
ladderResultList.add(new LadderResult(result)); | ||
} | ||
} | ||
|
||
public String status() { | ||
String result = ""; | ||
for (LadderResult ladderResult : ladderResultList) { | ||
result += ladderResult.getResult() + " "; | ||
} | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Getter | ||
public class Line { | ||
|
||
private List<Boolean> bridges = new ArrayList<>(); | ||
|
||
public static Line createLine(int countOfPerson, BridgeStrategy strategy) { | ||
Line line = new Line(countOfPerson, strategy); | ||
validate(line); | ||
return line; | ||
} | ||
|
||
private static void validate(Line line) { | ||
if (line.isInvalidBridge()) { | ||
throw new RuntimeException("올바르지 않은 라인입니다."); | ||
} | ||
} | ||
|
||
private Line(int countOfPerson, BridgeStrategy strategy) { | ||
for (int i = 0; i < countOfPerson - 1; i++) { | ||
Optional<Integer> prevIndex = Optional.ofNullable(i) | ||
.map(o -> o - 1); | ||
|
||
Boolean expected = build(strategy); | ||
bridges.add(decide(prevIndex, expected)); | ||
} | ||
} | ||
|
||
private static Boolean build(BridgeStrategy strategy) { | ||
return strategy.bridgeBuild(); | ||
} | ||
|
||
private Boolean decide(Optional<Integer> prevIndex, Boolean expected) { | ||
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 prevIndex.filter(i -> i >= 0) | ||
.map(i -> bridges.get(i)) | ||
.map(b -> Boolean.TRUE.equals(b) ? Boolean.valueOf(false) : expected) | ||
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. 이번 교육과정에서는 삼항 연산자도 |
||
.orElse(expected); | ||
} | ||
|
||
public int getNextPosition(int position) { | ||
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. 해당 메서드의 라인 수도 줄여보면 좋을 것 같습니다. |
||
// bridges[position - 1] == T 이면 왼쪽으로 | ||
Boolean toLeft = ableToLeft(position); | ||
|
||
// bridges[position] == T 이면 오른쪽으로 | ||
Boolean toRight = ableToRight(position); | ||
|
||
if (toLeft && toRight) { | ||
throw new RuntimeException("사다리 상태를 다시 확인해주세요."); | ||
} | ||
|
||
if (toLeft) { | ||
return position - 1; | ||
} | ||
|
||
if (toRight) { | ||
return position + 1; | ||
} | ||
|
||
return position; | ||
} | ||
|
||
public Boolean ableToRight(int position) { | ||
return Optional.ofNullable(position) | ||
.filter(o -> o < bridges.size()) | ||
.map(o -> bridges.get(o)) | ||
.filter(b -> b) | ||
.orElse(false); | ||
} | ||
|
||
public Boolean ableToLeft(int position) { | ||
return Optional.ofNullable(position) | ||
.map(o -> o - 1) | ||
.filter(o -> o >= 0) | ||
.map(o -> bridges.get(o)) | ||
.filter(b -> b) | ||
.orElse(false); | ||
} | ||
|
||
public boolean isValidBridge() { | ||
boolean valid = true; | ||
for (int i = 0; i < bridges.size(); i++) { | ||
if (ableToLeft(i) && ableToRight(i)) { | ||
valid = false; | ||
} | ||
} | ||
Comment on lines
+88
to
+92
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 valid; | ||
} | ||
public boolean isInvalidBridge() { | ||
return !isValidBridge(); | ||
} | ||
|
||
|
||
public String status() { | ||
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. 도메인 객체 내부에 view와 관련된 로직이 포함되어 있어서, 도메인이 view에 의존하는 관계라고 볼 수 있습니다. |
||
String result = ""; | ||
for (Boolean bridge : bridges) { | ||
result += "|"; | ||
result += bridge ? "-----" : " "; | ||
} | ||
result += "|\n"; | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class User { | ||
private String name; | ||
private int position; | ||
|
||
|
||
public User(String name, int position) { | ||
validateName(name); | ||
|
||
this.name = name; | ||
this.position = position; | ||
} | ||
|
||
private static void validateName(String name) { | ||
if (name.length() > 5) { | ||
throw new IllegalArgumentException("이름은 5글자까지 가능합니다."); | ||
} | ||
if (name.equals("all")) { | ||
throw new IllegalArgumentException("all은 부적절한 이름입니다."); | ||
} | ||
Comment on lines
+19
to
+24
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.
|
||
} | ||
|
||
public User rideLadder(Ladder ladder) { | ||
int lastPosition = ladder.findLastPosition(position); | ||
return new User(name, lastPosition); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
public class Users { | ||
private List<User> users = new ArrayList<>(); | ||
|
||
public Users(List<String> names) { | ||
for (int i = 0; i < names.size(); i++) { | ||
User user = new User(names.get(i), i); | ||
users.add(user); | ||
} | ||
} | ||
|
||
public String status() { | ||
String result = ""; | ||
for (User user : users) { | ||
result += user.getName() + " "; | ||
} | ||
return 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.
메서드 라인 수가 길어 보이는데, 자동차 경주 미션 때처럼 메서드의 라인 수를 줄여보면 어떨까요?
이 같은 원칙 아래에서 메소드의 라인 수를 15라인이 넘지 않도록 구현한다.