|
| 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 | +} |
0 commit comments