-
Notifications
You must be signed in to change notification settings - Fork 745
Step2 #2324
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
base: bb-worm
Are you sure you want to change the base?
Step2 #2324
Changes from all commits
0421b95
b1014ad
1702140
3e3dfdc
dd877b8
400b0a9
76cac9c
348199e
7c642d4
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,21 @@ | ||||||||
package ladder; | ||||||||
|
||||||||
import ladder.domain.Ladder; | ||||||||
import ladder.domain.People; | ||||||||
import ladder.view.InputView; | ||||||||
import ladder.view.ResultView; | ||||||||
|
||||||||
import java.util.Random; | ||||||||
|
||||||||
public class Main { | ||||||||
private static void makeLadder() { | ||||||||
People people = InputView.inputPeople(); | ||||||||
int ladderHeight = InputView.inputLadderHeight(); | ||||||||
Ladder ladder = new Ladder(ladderHeight, people, (idx, preConnected) -> !preConnected && new Random().nextBoolean()); | ||||||||
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. 일부러 람다 사용해보려고 ConnectStrategy를 람다로 표현했습니다. 그런데 오히려 코드 읽기가 어려워지는 거 같아서.. 이런 경우엔 따로 클래스로 빼는 게 나을까요? 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. 클래스도 좋고 변수로 선언해도 좋을 것 같아요
Suggested change
null을 활용한 설계가 아니라면 억지로 사용하지 않으셔도 됩니다 😃 |
||||||||
ResultView.printLadder(people, ladder); | ||||||||
} | ||||||||
|
||||||||
public static void main(String[] args) { | ||||||||
makeLadder(); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ladder.domain; | ||
|
||
public interface ConnectStrategy { | ||
boolean connect(int idx, boolean preConnected); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Ladder { | ||
private final List<Line> lines; | ||
|
||
public Ladder(int height, People people, ConnectStrategy connectStrategy) { | ||
this.lines = IntStream.range(0, height) | ||
.mapToObj(i -> new Line(people.size(), connectStrategy)) | ||
.collect(Collectors.toList()); | ||
} | ||
Comment on lines
+10
to
+14
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 Ladder(List<Line> lines) {
// validate
this.lines = lines;
}
public static Ladder create(int height, People people, ConnectStrategy connectStrategy) {
final List<Line> lines = ...;
return new Ladder(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. 매번 생성자가 복잡해질 때마다 고민이 있었는데 말씀하신 것처럼 분리해보겠습니다 |
||
|
||
public List<Line> lines() { | ||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class Line { | ||
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. Line 객체는 특별한 기능이 없는 것은 같아요 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 Points points; | ||
|
||
public Line(List<Point> points) { | ||
this.points = new Points(points); | ||
} | ||
|
||
public Line(int size, ConnectStrategy connectStrategy) { | ||
this.points = new Points(size, connectStrategy); | ||
} | ||
Comment on lines
+9
to
+15
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 Line(Points points) {
this.points = points;
}
public Line(List<Points> points) {
this(new Points(points));
} 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 Points points() { | ||
return points; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Line line = (Line) o; | ||
return Objects.equals(points, line.points); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(points); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class People { | ||
private final static String DELIMITER = ","; | ||
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 또는 controller로 이동하면 어떨까요? 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. 말씀하신 것처럼 People이 구분자를 가지고 있을 필요가 없네요. 변경해두겠습니다. |
||
|
||
private final List<Person> people; | ||
|
||
public People(String input) { | ||
this.people = Arrays.stream(input.split(DELIMITER)) | ||
.map(Person::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public int size() { | ||
return people.size(); | ||
} | ||
|
||
public List<Person> values() { | ||
return people; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
package ladder.domain; | ||||||
|
||||||
public class Person { | ||||||
private static final int NAME_MAX_LENGTH = 5; | ||||||
|
||||||
private final String name; | ||||||
|
||||||
public Person(String name) { | ||||||
if (name.length() > 5) { | ||||||
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.
Suggested change
|
||||||
throw new IllegalArgumentException("이름 길이는 최대 " + NAME_MAX_LENGTH + "자입니다."); | ||||||
} | ||||||
this.name = name; | ||||||
} | ||||||
|
||||||
public String name() { | ||||||
return name; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Point { | ||
private final boolean rightConnected; | ||
Comment on lines
+5
to
+6
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.
좌우연결을 가진 상태로 끝까지 구현해 보셨어도 좋았을 것 같아요. 마지막 질문에 대한 답변을 드리자면 생성 단계에서 막아주는게 좋지 않을까 생각이 드는데요 😃 Point first = Point.first();
Point second = first.next();
Point third = second.next();
Point forth = third.last(); Point가 처음, 중간, 마지막에 대한 값을 스스로 결정할 수 있다면 어떨까요? 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 Point(boolean rightConnected) { | ||
this.rightConnected = rightConnected; | ||
} | ||
|
||
public boolean connected() { | ||
return rightConnected; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Point point = (Point) o; | ||
return rightConnected == point.rightConnected; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(rightConnected); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
|
||
public class Points { | ||
private final List<Point> points; | ||
|
||
public Points(List<Point> points) { | ||
this.points = points; | ||
validate(); | ||
} | ||
|
||
public Points(int size, ConnectStrategy connectStrategy) { | ||
this(generatePoints(size, connectStrategy)); | ||
} | ||
|
||
public List<Point> values() { | ||
return points; | ||
} | ||
|
||
private static List<Point> generatePoints(int size, ConnectStrategy connectStrategy) { | ||
List<Point> points = new ArrayList<>(size); | ||
IntStream.range(0, size) | ||
.forEach(i -> points.add(new Point(connectStrategy.connect(i, i != 0 && points.get(i - 1).connected())))); | ||
return points; | ||
} | ||
|
||
public void validate() { | ||
IntStream.range(0, points.size()-1) | ||
.filter(this::continuousConnected) | ||
.findFirst() | ||
.ifPresent(i -> { | ||
throw new IllegalArgumentException("각 포인트는 연속적으로 연결될 수 없음"); | ||
}); | ||
} | ||
Comment on lines
+31
to
+38
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. Point가 좌우연결을 가진 구조라면 유효성 검증도 단순화할 수 있을 것 같아요 new Point(true, true); // 생성 불가 |
||
|
||
private boolean continuousConnected(int idx) { | ||
return points.get(idx).connected() && points.get(idx+1).connected(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Points points1 = (Points) o; | ||
return Objects.equals(points, points1.points); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(points); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ladder.view; | ||
|
||
import ladder.domain.People; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private final static Scanner scanner = new Scanner(System.in); | ||
|
||
public static People inputPeople() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
return new People(scanner.nextLine()); | ||
} | ||
|
||
public static int inputLadderHeight() { | ||
System.out.println("최대 사다리 높이는 몇인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ladder.view; | ||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.People; | ||
|
||
public class ResultView { | ||
public static void printLadder(People people, Ladder ladder) { | ||
System.out.println("실행결과"); | ||
printPeople(people); | ||
printLadder(ladder); | ||
} | ||
|
||
private static void printPeople(People people) { | ||
people.values().forEach(v -> System.out.printf("%-6s", v.name())); | ||
System.out.println(); | ||
} | ||
|
||
private static void printLadder(Ladder ladder) { | ||
ladder.lines().forEach(l -> { | ||
l.points().values().forEach(p -> { | ||
System.out.print("|"); | ||
System.out.printf("%5s", p.connected() ? "-----" : ""); | ||
}); | ||
System.out.println(); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ladder; | ||
|
||
import ladder.domain.Line; | ||
import ladder.domain.Point; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class LineTest { | ||
Point notConnected = new Point(false); | ||
Point connected = new Point(true); | ||
|
||
@Test | ||
void 라인_생성() { | ||
Line line = new Line(4, (idx, preConnected) -> false); | ||
assertThat(line).isEqualTo(new Line(List.of(notConnected, notConnected, notConnected, notConnected))); | ||
} | ||
|
||
@Test | ||
void 처음에만_연결() { | ||
Line line = new Line(4, (idx, preConnected) -> idx == 0); | ||
assertThat(line).isEqualTo(new Line(List.of(connected, notConnected, notConnected, notConnected))); | ||
} | ||
|
||
@Test | ||
void 홀수번째만_연결() { | ||
Line line = new Line(4, (idx, preConnected) -> idx % 2 != 0); | ||
assertThat(line).isEqualTo(new Line(List.of(notConnected, connected, notConnected, connected))); | ||
} | ||
|
||
@Test | ||
void 연속적으로_연결되면_예외() { | ||
assertThatThrownBy(() -> new Line(4, (idx, preConnected) -> true)).isInstanceOf(IllegalArgumentException.class); | ||
Comment on lines
+34
to
+36
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. 예외 테스트 👍 Point, Points, Ladder, Person 에 대한 단위 테스트도 추가하면 어떨까요? 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,13 @@ | ||
package ladder; | ||
|
||
import ladder.domain.People; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
public class PeopleTest { | ||
@Test | ||
void 이름_길이_예외() { | ||
assertThatThrownBy(() -> new People("finnnnnn,foo")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.