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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package design.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = ScrambleLeague.class, name = "scramble"),
@JsonSubTypes.Type(value = StrokeLeague.class, name = "stroke")
})
public abstract class League {
private int id;
private final String name;
private final Date registrationDate;
private final Date startDate;
private final Date endDate;
private final Golfer owner;
private final List<Match> schedule;
private final List<Match> completedMatches;
private boolean completed;
@JsonCreator
protected League(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Match> schedule) {
this.id = id;
this.name = name;
this.registrationDate = registrationDate;
this.startDate = startDate;
this.endDate = endDate;
this.owner = owner;
this.schedule = schedule != null ? schedule : new ArrayList<>();
this.completed = false;
this.completedMatches = new ArrayList<>();
for(Match m : this.schedule){
if(m.checkCompletion()){
completedMatches.add(m);
}
}
}
public League(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) {
this.id = -1;
this.name = name;
this.registrationDate = registrationDate;
this.startDate = startDate;
this.endDate = endDate;
this.owner = owner;
this.schedule = new ArrayList<>();
this.completed = false;
this.completedMatches = new ArrayList<>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Date getRegistrationDate() {
return registrationDate;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public Golfer getOwner() {
return owner;
}
public Match[] getSchedule() {
return schedule.toArray(Match[]::new);
}
public boolean isCompleted() {
return completed;
}
public List<Match> getCompletedMatches() {
return completedMatches;
}
public void addMatchToSchedule(Match match) {
if(match.getDateScheduled().after(endDate)){
throw new IllegalArgumentException("Cannot create match after league has ended");
}
schedule.add(match);
}
public void setId(int id) {
assert this.id == -1;
this.id = id;
}
public abstract String getType();
@Override
public String toString() {
return String.format("%s - %s", name, getType());
}
public boolean isPlayable() {
Date now = new Date();
return now.after(startDate) && now.before(endDate);
}
public void markCompleted(){
this.completed = true;
}
public abstract void recordPlay(Golfer player, Match match, Round round);
public abstract void finalizeLeague();
public abstract Map<?, Integer> getResults();
public abstract void finalizeMatch(Match match);
}
|