summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/Round.java
blob: e4442d36ece36fa8039c650c5bf9725e0c53abea (plain) (blame)
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
package design.model;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class Round {
    private final Course course;
    private final LocalDateTime dateTime;
    private final Hole startingHole;
    private final List<Play> plays;
    private int currentHoleIndex;

    @JsonCreator
    private Round(Course course, LocalDateTime dateTime, Hole startingHole, List<Play> plays) {
        this.course = course;
        this.dateTime = dateTime;
        this.startingHole = startingHole;
        this.plays = plays;
        // Allows the golfer to start anywhere on the course. Helps HolePalyContext be
        // simpler.
        this.currentHoleIndex = Math.max(0, startingHole.getNumber() - 1);
    }

    public Round(Course course, LocalDateTime dateTime, Hole startingHole) {
        this.course = course;
        this.dateTime = dateTime;
        this.startingHole = startingHole;
        plays = new ArrayList<>();
        // Allows the golfer to start anywhere on the course. Helps HolePalyContext be
        // simpler.
        this.currentHoleIndex = Math.max(0, startingHole.getNumber() - 1);
    }

    public int getTotalSwings() {
        return plays.stream()
                .map(Play::getSwingCount)
                .reduce(0, Integer::sum);
    }

    public double getTotalDistance() {
        return plays.stream()
                    .mapToDouble(design.model.Play::getDistance)
                    .sum();
    }

    public Course getCourse() {
        return course;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public Hole getStartingHole() {
        return startingHole;
    }

    public Play[] getPlays() {
        return plays.toArray(Play[]::new);
    }

    public void addPlay(Play play) {
        plays.add(play);
    }

    // Current hole
    public Hole getCurrentHole() {
        return course.getHoles().get(currentHoleIndex);
    }

    // Handles wraparound too
    public void nextHole() {
        currentHoleIndex = (currentHoleIndex + 1) % course.getHoleCount();
    }
}