Skip to content

Commit e954912

Browse files
committed
feat: 실행결과 출력하기
1 parent 16783e9 commit e954912

File tree

12 files changed

+89
-5
lines changed

12 files changed

+89
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* [X] 참여할 사람 명단 입력받기
3030
* [X] 쉼표 구분자를 파싱하기
3131
* [X] 최대 사다리 높이 입력받기
32-
* [ ] 실행결과 출력하기
32+
* [X] 실행결과 출력하기
3333
* [X] 사다리 만들기
3434
* [X] 사다리 한 줄(Line) 만들기
3535
* [X] 사다리 다리 구성하기 (Bridge)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package nextstep.ladder;
2+
3+
import nextstep.ladder.controller.LadderController;
4+
5+
public class LadderApplication {
6+
7+
public static void main(String[] args) {
8+
LadderController ladderController = new LadderController();
9+
ladderController.game();
10+
}
11+
}

src/main/java/nextstep/ladder/controller/LadderController.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import nextstep.ladder.model.LinesGenerator;
66
import nextstep.ladder.model.Participants;
77
import nextstep.ladder.view.InputView;
8+
import nextstep.ladder.view.OutputView;
89

910
public class LadderController {
1011

@@ -16,5 +17,6 @@ public void game() {
1617
Lines lines = new Lines(linesGenerator.generatorLines(participants.getNumbersOfParticipants(), height));
1718

1819
Ladder ladder = new Ladder(participants, lines, height);
20+
OutputView.printLadder(ladder);
1921
}
2022
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static Bridge of(boolean value) {
2222
return cacheBridge.get(value);
2323
}
2424

25-
boolean canCrossBridge() {
25+
public boolean canCrossBridge() {
2626
return value;
2727
}
2828

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

+6
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ private void validateHeight(Integer height) {
2121
}
2222
}
2323

24+
public Participants getParticipants() {
25+
return participants;
26+
}
2427

28+
public Lines getLines() {
29+
return lines;
30+
}
2531
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ private void validateNumbersOfPeople(Integer numbersOfPeople) {
3838
throw new IllegalArgumentException("사람은 최소 2명 이상이어야 합니다.");
3939
}
4040
}
41+
42+
public List<Bridge> getBridges() {
43+
return bridges;
44+
}
4145
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public class Lines {
99
public Lines(List<Line> lines) {
1010
this.lines = lines;
1111
}
12+
13+
public List<Line> getLines() {
14+
return lines;
15+
}
1216
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ private void validateNameLength(String name) {
1717
throw new IllegalArgumentException("0글자 이하이거나 5글자를 초과하는 참가자 이름이 포함되어 있습니다.");
1818
}
1919
}
20+
21+
public String getName() {
22+
return name;
23+
}
2024
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ private List<Participant> createParticipants(List<String> inputParticipants) {
2626
public Integer getNumbersOfParticipants() {
2727
return participants.size();
2828
}
29+
30+
public List<Participant> getParticipants() {
31+
return participants;
32+
}
2933
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package nextstep.ladder.view;
2+
3+
import java.util.stream.Collectors;
4+
import nextstep.ladder.model.Ladder;
5+
import nextstep.ladder.model.Line;
6+
import nextstep.ladder.model.Lines;
7+
import nextstep.ladder.model.Participant;
8+
import nextstep.ladder.model.Participants;
9+
10+
public class OutputView {
11+
private static final StringBuffer buffer = new StringBuffer();
12+
private static final String INITIAL_BRIDGE = " |";
13+
private static final String PASSABLE_BRIDGE = "-----|";
14+
private static final String IMPASSABLE_BRIDGE = " |";
15+
16+
public static void printLadder(Ladder ladder) {
17+
18+
buffer.append("\n실행결과\n");
19+
printParticipants(ladder.getParticipants());
20+
printLadderLines(ladder.getLines());
21+
System.out.println(buffer);
22+
}
23+
24+
public static void printParticipants(Participants participants) {
25+
for (Participant participant : participants.getParticipants()) {
26+
buffer.append(String.format ("%6s", participant.getName()));
27+
}
28+
buffer.append("\n");
29+
}
30+
31+
private static void printLadderLines(Lines lines) {
32+
for (Line line : lines.getLines()) {
33+
buffer.append(INITIAL_BRIDGE);
34+
printLine(line);
35+
buffer.append("\n");
36+
}
37+
}
38+
39+
private static void printLine(Line line) {
40+
buffer.append(
41+
line.getBridges().stream()
42+
.map(bridge -> bridge.canCrossBridge() ? PASSABLE_BRIDGE : IMPASSABLE_BRIDGE)
43+
.collect(Collectors.joining()));
44+
}
45+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public class BridgeTest {
2222
assertThat(bridge.canCrossBridge()).isFalse();
2323
}
2424

25+
@Test
26+
public void next_현재_다리_value가_TRUE_일때_무조건_False반환() {
27+
Bridge now = Bridge.of(true);
28+
29+
assertThat(now.next()).isEqualTo(Bridge.of(false));
30+
}
31+
2532
@Test
2633
public void compareToNextBridge_연속된_두개가_True일시_에러_반환() {
2734
Bridge prev = Bridge.of(true);

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

-3
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,6 +12,4 @@ public class SequentialRandomBridgesGeneratorTest {
1312
public void generateBridges_다리_개수_확인() {
1413
assertThat(bridgesGenerator.generateBridges(5)).hasSize(4);
1514
}
16-
17-
1815
}

0 commit comments

Comments
 (0)