-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathLadderController.java
108 lines (81 loc) · 3.4 KB
/
LadderController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package controller;
import dto.LadderResultDto;
import dto.LineDto;
import model.*;
import view.LadderInputView;
import view.LadderOutputView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LadderController {
private static final String PRINT_EVERY_RESULT = "all";
private static final LadderController ladderController = new LadderController();
private static final LadderOutputView ladderOutputView = LadderOutputView.getInstance();
private static final LadderInputView ladderInputView = LadderInputView.getInstance();
private LadderController() {
}
public static LadderController getInstance() {
return ladderController;
}
public void run() {
LadderUsers ladderUsers = getLadderUsers();
String[] resultValues = ladderInputView.getResultValues();
int height = ladderInputView.getHeight();
Ladder ladder = new Ladder(ladderUsers.size(), height);
LadderResultCalculator ladderResultCalculator = new LadderResultCalculator(ladderUsers, resultValues);
printLadderShape(ladder, ladderUsers, resultValues);
printLadderResultUntilPrintEveryResult(ladder, ladderResultCalculator, ladderUsers);
}
private LadderUsers getLadderUsers() {
String[] names = ladderInputView.getNames();
List<LadderUser> ladderUsers = Arrays.stream(names)
.map(LadderUser::new)
.toList();
return new LadderUsers(ladderUsers);
}
private void printLadderShape(Ladder ladder, LadderUsers ladderUsers, String[] resultValues) {
ladderOutputView.printLadderResultHeader();
ladderOutputView.printNames(ladderUsers.getNames());
List<Line> lines = ladder.getLines();
for (Line line : lines) {
LineDto lineDto = LineDto.from(line);
ladderOutputView.printLine(lineDto);
}
ladderOutputView.printResultValues(resultValues);
}
private void printLadderResultUntilPrintEveryResult(
Ladder ladder,
LadderResultCalculator ladderResultCalculator,
LadderUsers ladderUsers
) {
String targetName = ladderInputView.getTargetName();
while (shouldPrintSingleResult(targetName)) {
printSingleLadderResult(targetName, ladder, ladderResultCalculator);
targetName = ladderInputView.getTargetName();
}
printEveryLadderResult(ladder, ladderResultCalculator, ladderUsers);
}
private boolean shouldPrintSingleResult(String targetName) {
return !targetName.equals(PRINT_EVERY_RESULT);
}
private void printSingleLadderResult(
String targetName,
Ladder ladder,
LadderResultCalculator ladderResultCalculator
) {
String result = ladderResultCalculator.calculate(targetName, ladder);
ladderOutputView.printLadderResult(result);
}
private void printEveryLadderResult(
Ladder ladder,
LadderResultCalculator ladderResultCalculator,
LadderUsers ladderUsers
) {
List<LadderResultDto> ladderResultDtos = new ArrayList<>();
for (String name : ladderUsers.getNames()) {
String result = ladderResultCalculator.calculate(name, ladder);
ladderResultDtos.add(new LadderResultDto(name, result));
}
ladderOutputView.printLadderResults(ladderResultDtos);
}
}