-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLines.java
More file actions
47 lines (39 loc) · 1.38 KB
/
Lines.java
File metadata and controls
47 lines (39 loc) · 1.38 KB
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
package nextstep.ladder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Lines {
private final static String LINES_DELIMITER = "\n";
private List<Line> lines = new ArrayList<>();
public Lines(int height, int countOfPerson) {
if (height <= 0) {
throw new IllegalArgumentException("사다리 높이는 0 이상 입력해주세요.");
}
generate(height, countOfPerson);
}
public Lines(List<Line> lines) {
this.lines = lines;
}
private void generate(int height, int countOfPerson) {
this.lines = Stream.generate(() -> new Line(countOfPerson))
.limit(height)
.collect(Collectors.toList());
}
public List<Line> getValues() {
return this.lines;
}
public String toString(String lineTrueSymbol, String lineFalseSymbol) {
return String.join(
LINES_DELIMITER,
this.lines
.stream()
.map((line) -> line.toString(lineTrueSymbol, lineFalseSymbol))
.collect(Collectors.toList())
);
}
public int getPersonFinalResultIndex(int personIndex) {
return lines.stream()
.reduce(personIndex, (index, line) -> line.getResultIndex(index), (a, b) -> b);
}
}