Skip to content

Commit 16783e9

Browse files
committed
feat: 사람 명단, 높이 입력받기 완료
- refactoring 사항 - validation은 generator에서 객체로 이동 - 생성자에서 필드를 초기화하는 것이 아닌, 컨트롤러에서 필드 값들을 만들고 넘겨주는 것으로 변경
1 parent 4e7450f commit 16783e9

16 files changed

+195
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
* [X] Optional 실습 3 : Optional에서 exception 처리
2727

2828
# STEP 2
29-
* [ ] 참여할 사람 명단 입력받기
30-
* [ ] 쉼표 구분자를 파싱하기
31-
* [ ] 최대 사다리 높이 입력받기
29+
* [X] 참여할 사람 명단 입력받기
30+
* [X] 쉼표 구분자를 파싱하기
31+
* [X] 최대 사다리 높이 입력받기
3232
* [ ] 실행결과 출력하기
3333
* [X] 사다리 만들기
3434
* [X] 사다리 한 줄(Line) 만들기
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nextstep.ladder.controller;
2+
3+
import nextstep.ladder.model.Ladder;
4+
import nextstep.ladder.model.Lines;
5+
import nextstep.ladder.model.LinesGenerator;
6+
import nextstep.ladder.model.Participants;
7+
import nextstep.ladder.view.InputView;
8+
9+
public class LadderController {
10+
11+
private final LinesGenerator linesGenerator = new LinesGenerator();
12+
13+
public void game() {
14+
Participants participants = new Participants(InputView.inputGameParticipants());
15+
int height = InputView.inputLadderHeight();
16+
Lines lines = new Lines(linesGenerator.generatorLines(participants.getNumbersOfParticipants(), height));
17+
18+
Ladder ladder = new Ladder(participants, lines, height);
19+
}
20+
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package nextstep.ladder.model;
22

3-
import java.util.List;
4-
53
public class Ladder {
64

7-
private final List<Line> lines;
5+
private static final int MIN_HEIGHT = 1;
6+
7+
private final Participants participants;
8+
private final Lines lines;
89
private final Integer height;
910

10-
public Ladder(List<Line> lines, Integer height) {
11+
public Ladder(Participants participants, Lines lines, Integer height) {
12+
validateHeight(height);
13+
this.participants = participants;
1114
this.lines = lines;
1215
this.height = height;
1316
}
17+
18+
private void validateHeight(Integer height) {
19+
if (height < MIN_HEIGHT) {
20+
throw new IllegalArgumentException("높이는 최소 1 이상이어야 합니다.");
21+
}
22+
}
23+
24+
1425
}

src/main/java/nextstep/ladder/model/Line.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public class Line {
66

7+
private static final Integer MIN_NUMBERS_OF_PEOPLE = 2;
8+
79
private final List<Bridge> bridges;
810

911
public Line(Integer numbersOfPeople, List<Bridge> bridges) {
@@ -14,6 +16,7 @@ public Line(Integer numbersOfPeople, List<Bridge> bridges) {
1416
private void validateLine(Integer numbersOfPeople, List<Bridge> bridges) {
1517
validateBridgeCount(numbersOfPeople, bridges);
1618
validateContinuousTrueBridge(bridges);
19+
validateNumbersOfPeople(numbersOfPeople);
1720
}
1821

1922
private void validateBridgeCount(Integer numbersOfPeople, List<Bridge> bridges) {
@@ -29,4 +32,10 @@ private void validateContinuousTrueBridge(List<Bridge> bridges) {
2932
return currentBridge;
3033
});
3134
}
35+
36+
private void validateNumbersOfPeople(Integer numbersOfPeople) {
37+
if (numbersOfPeople < MIN_NUMBERS_OF_PEOPLE) {
38+
throw new IllegalArgumentException("사람은 최소 2명 이상이어야 합니다.");
39+
}
40+
}
3241
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package nextstep.ladder.model;
2+
3+
import java.util.List;
4+
5+
public class Lines {
6+
7+
private final List<Line> lines;
8+
9+
public Lines(List<Line> lines) {
10+
this.lines = lines;
11+
}
12+
}

src/main/java/nextstep/ladder/model/LinesGenerator.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,11 @@
66

77
public class LinesGenerator {
88

9-
private static final Integer MIN_HEIGHT = 1;
10-
119
private final BridgesGenerator bridgesGenerator = new SequentialRandomBridgesGenerator();
1210

1311
public List<Line> generatorLines(Integer numbersOfPeople, Integer height) {
14-
validateHeight(height);
1512
return IntStream.range(0, height)
1613
.mapToObj(index -> new Line(numbersOfPeople, bridgesGenerator.generateBridges(numbersOfPeople)))
1714
.collect(Collectors.toList());
1815
}
19-
20-
private void validateHeight(Integer height) {
21-
if (height < MIN_HEIGHT) {
22-
throw new IllegalArgumentException("높이는 최소 1 이상이어야 합니다.");
23-
}
24-
}
2516
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nextstep.ladder.model;
2+
3+
public class Participant {
4+
5+
private static final Integer MIN_NAME_LENGTH = 1;
6+
private static final Integer MAX_NAME_LENGTH = 5;
7+
8+
private final String name;
9+
10+
public Participant(String name) {
11+
validateNameLength(name);
12+
this.name = name;
13+
}
14+
15+
private void validateNameLength(String name) {
16+
if (name.length() < MIN_NAME_LENGTH || name.length() > MAX_NAME_LENGTH) {
17+
throw new IllegalArgumentException("0글자 이하이거나 5글자를 초과하는 참가자 이름이 포함되어 있습니다.");
18+
}
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package nextstep.ladder.model;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class Participants {
7+
8+
private static final String DELIMITER = ",";
9+
10+
private final List<Participant> participants;
11+
12+
public Participants(String inputParticipants) {
13+
this.participants = createParticipants(parseInputParticipants(inputParticipants));
14+
}
15+
16+
private List<String> parseInputParticipants(String inputParticipants) {
17+
return List.of(inputParticipants.split(DELIMITER));
18+
}
19+
20+
private List<Participant> createParticipants(List<String> inputParticipants) {
21+
return inputParticipants.stream()
22+
.map(Participant::new)
23+
.collect(Collectors.toList());
24+
}
25+
26+
public Integer getNumbersOfParticipants() {
27+
return participants.size();
28+
}
29+
}

src/main/java/nextstep/ladder/model/SequentialRandomBridgesGenerator.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
public class SequentialRandomBridgesGenerator implements BridgesGenerator {
88

9-
private static final Integer MIN_NUMBERS_OF_PEOPLE = 2;
10-
119
@Override
1210
public List<Bridge> generateBridges(Integer numbersOfPeople) {
13-
validateNumbersOfPeople(numbersOfPeople);
1411
List<Bridge> bridges = new LinkedList<>();
1512
Bridge currentBridge = Bridge.of(ThreadLocalRandom.current().nextBoolean());
1613
for (int i = 0; i < numbersOfPeople - 1; i++) {
@@ -19,10 +16,4 @@ public List<Bridge> generateBridges(Integer numbersOfPeople) {
1916
}
2017
return bridges;
2118
}
22-
23-
private void validateNumbersOfPeople(Integer numbersOfPeople) {
24-
if (numbersOfPeople < MIN_NUMBERS_OF_PEOPLE) {
25-
throw new IllegalArgumentException("사람은 최소 2명 이상이어야 합니다.");
26-
}
27-
}
2819
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package nextstep.ladder.view;
2+
3+
import java.util.Scanner;
4+
5+
public class InputView {
6+
private static final Scanner scanner = new Scanner(System.in);
7+
8+
public static String inputGameParticipants() {
9+
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
10+
return scanner.nextLine();
11+
}
12+
13+
public static int inputLadderHeight() {
14+
System.out.println("최대 사다리 높이는 몇 개인가요?");
15+
return Integer.parseInt(scanner.nextLine());
16+
}
17+
}

src/test/java/nextstep/ladder/model/BridgeTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package nextstep.ladder.model;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatCode;
5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
46

57
import org.junit.jupiter.api.Test;
68

@@ -19,4 +21,22 @@ public class BridgeTest {
1921

2022
assertThat(bridge.canCrossBridge()).isFalse();
2123
}
24+
25+
@Test
26+
public void compareToNextBridge_연속된_두개가_True일시_에러_반환() {
27+
Bridge prev = Bridge.of(true);
28+
Bridge next = Bridge.of(true);
29+
30+
assertThatThrownBy(() -> prev.compareToNextBridge(next))
31+
.isInstanceOf(IllegalArgumentException.class);
32+
}
33+
34+
@Test
35+
public void compareToNextBridge_연속된_두개가_True가_아닐시_정상동작_테스트() {
36+
Bridge prev = Bridge.of(false);
37+
Bridge next = Bridge.of(true);
38+
39+
assertThatCode(() -> prev.compareToNextBridge(next))
40+
.doesNotThrowAnyException(); // 예외가 발생하지 않아야 함
41+
}
2242
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package nextstep.ladder.model;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class LadderTest {
10+
11+
private Participants participants;
12+
private Lines lines;
13+
14+
@BeforeEach
15+
public void setup() {
16+
List<Bridge> bridges = List.of(Bridge.of(true));
17+
List<Line> line = List.of(new Line(2, bridges));
18+
19+
participants = new Participants("a,b");
20+
lines = new Lines(line);
21+
}
22+
23+
@Test
24+
public void generatorLines_높이가_0일때_에러_테스트() {
25+
assertThatThrownBy( () -> new Ladder(participants, lines, 0))
26+
.isInstanceOf(IllegalArgumentException.class);
27+
}
28+
}

src/test/java/nextstep/ladder/model/LineTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThatThrownBy;
44

5+
import java.util.LinkedList;
56
import java.util.List;
67
import org.junit.jupiter.api.Test;
78

@@ -20,4 +21,10 @@ public class LineTest {
2021
assertThatThrownBy(() -> new Line(5, bridges))
2122
.isInstanceOf(IllegalArgumentException.class);
2223
}
24+
25+
@Test
26+
public void 사람수_1명일때_에러_테스트() {
27+
assertThatThrownBy(() -> new Line(1, new LinkedList<>()))
28+
.isInstanceOf(IllegalArgumentException.class);
29+
}
2330
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package nextstep.ladder.model;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
54

65
import org.junit.jupiter.api.Test;
76

@@ -13,10 +12,4 @@ public class LinesGeneratorTest {
1312
public void generatorLines_높이에_맞게_생성하는지_테스트() {
1413
assertThat(linesGenerator.generatorLines(5, 6)).hasSize(6);
1514
}
16-
17-
@Test
18-
public void generatorLines_높이가_0일때_에러_테스트() {
19-
assertThatThrownBy( () -> linesGenerator.generatorLines(5, 0))
20-
.isInstanceOf(IllegalArgumentException.class);
21-
}
2215
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package nextstep.ladder.model;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class ParticipantTest {
8+
9+
@Test
10+
public void 이름_5글자_이상일때_에러_반환_테스트() {
11+
assertThatThrownBy(() -> new Participant("aaaaaa"))
12+
.isInstanceOf(IllegalArgumentException.class);
13+
}
14+
}

src/test/java/nextstep/ladder/model/SequentialRandomBridgesGeneratorTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,5 @@ public class SequentialRandomBridgesGeneratorTest {
1414
assertThat(bridgesGenerator.generateBridges(5)).hasSize(4);
1515
}
1616

17-
@Test
18-
public void generateBridges_사람수_1명일때_에러_테스트() {
19-
assertThatThrownBy(() -> bridgesGenerator.generateBridges(1))
20-
.isInstanceOf(IllegalArgumentException.class);
21-
}
17+
2218
}

0 commit comments

Comments
 (0)