-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLineBuilder.java
More file actions
40 lines (31 loc) · 1.05 KB
/
LineBuilder.java
File metadata and controls
40 lines (31 loc) · 1.05 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
package nextstep.ladder.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class LineBuilder {
private static final Random RANDOM = new Random();
private final List<Point> points = new ArrayList<>();
public static Line buildWithRandomPoints(int count) {
return new LineBuilder().initRandomMoveablePoint()
.addRandomMoveablePoints(count - 2)
.build();
}
private Line build() {
points.add(lastPoint().createRightmost());
return new Line(points);
}
private LineBuilder addRandomMoveablePoints(int numberOfMiddlePoints) {
for (int i = 0; i < numberOfMiddlePoints; i++) {
Point point = lastPoint().createNext(RANDOM.nextBoolean());
points.add(point);
}
return this;
}
private LineBuilder initRandomMoveablePoint() {
points.add(Point.createLeftmost(RANDOM.nextBoolean()));
return this;
}
private Point lastPoint() {
return points.get(points.size() - 1);
}
}