summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/StrokeLeague.java
blob: 487a2b58e5f7b4d56a1b7cc9ecad71601b94eb79 (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
package design.model;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StrokeLeague extends League {
    private List<Golfer> participants;
    private Map<Golfer, Integer> totalStrokes;

    @JsonCreator
    private StrokeLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Golfer> participants, List<Match> schedule) {
        super(id, name, registrationDate, startDate, endDate, owner, schedule);
        this.participants = participants;
        this.totalStrokes = new HashMap<>();
        participants.forEach(p -> totalStrokes.putIfAbsent(p, 0));
    }

    public StrokeLeague(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) {
        super(name, registrationDate, startDate, endDate, owner);
        this.participants = new ArrayList<>();
        this.totalStrokes = new HashMap<>();
    }

    public boolean addParticipants(Golfer g) {
        boolean added = participants.add(g);
        if(added) totalStrokes.putIfAbsent(g, 0);
        return added;
    }

    public boolean removeParticipants(Golfer g) {
        totalStrokes.remove(g);
        return participants.remove(g);
    }

    public Golfer[] getParticipants() {
        return participants.toArray(Golfer[]::new);
    }

    @Override
    public void recordPlay(Golfer player, Match match, Round round){
        if(!isPlayable() || !participants.contains(player)) return;
        int strokes = round.getTotalSwings();
        totalStrokes.merge(player, strokes, Integer::sum);
        match.addRound(round);
    }

    @Override
    public void finalizeLeague(){
        markCompleted();
        participants.sort(Comparator.comparingInt(totalStrokes::get));
    }
    
    public Map<Golfer, Integer> getTotalStrokes(){
        return Collections.unmodifiableMap(totalStrokes);
    }
}