summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/Golfer.java
blob: 870b4609c36a39f8a7c96f17eedd44b06569388d (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package design.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.ArrayList;
import java.util.List;

import design.model.undo.Memento;
import design.model.undo.Originator;

@JsonPropertyOrder({ "clubs", "nextClubId" })
public class Golfer implements Originator {
    private String username;
    private int passwordHash;
    private String fullName;
    private final List<Course> courses;
    private final List<Round> rounds;
    private final List<Club> clubs; // Keep track of golfer's clubs
    private int nextClubId;

    @JsonCreator
    private Golfer(String username, int passwordHash, String fullName, List<Course> courses, List<Round> rounds,
            List<Club> clubs) {
        this.username = username;
        this.passwordHash = passwordHash;
        this.fullName = fullName;
        this.courses = courses;
        this.rounds = rounds;
        this.clubs = clubs;
        this.nextClubId = this.clubs.stream().mapToInt(Club::getId).max().orElse(0) + 1;
    }

    public Golfer(String fullName, String username, String password) {
        this.fullName = fullName;
        this.username = username;
        this.passwordHash = password.hashCode();
        this.courses = new ArrayList<>();
        this.rounds = new ArrayList<>();
        this.clubs = new ArrayList<>();
        this.nextClubId = 1;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void setPassword(String password) {
        this.passwordHash = password.hashCode();
    }

    public boolean checkPassword(String password) {
        return passwordHash == password.hashCode();
    }

    public Course[] getCourses() {
        return courses.toArray(Course[]::new);
    }

    public void addCourse(Course course) {
        courses.add(course);
    }

    public void removeCourse(Course course) {
        courses.remove(course);
    }

    public boolean hasCourses() {
        return !courses.isEmpty();
    }

    public Round[] getRounds() {
        return rounds.toArray(Round[]::new);
    }

    public void addRound(Round round) {
        rounds.add(round);
    }

    public Club addClub(Club c) {
        clubs.add(c);
        c.setId(nextClubId++);
        return c;
    }

    public boolean hasClubs() {
        return !clubs.isEmpty();
    }

    public boolean hasClub(Club c) {
        return clubs.contains(c);
    }

    public Club[] getClubs() {
        return clubs.toArray(Club[]::new);
    }

    @Override
    public String toString() {
        return String.format("%s (@%s)", fullName, username);
    }

    public void removeClub(Club c) {
        clubs.remove(c);
    }

    // Takes a snapshot of the golfer
    private static class GolferMemento implements Memento {
        private final String username;
        private final int passwordHash;
        private final String fullName;
        private final List<Course> courses;
        private final List<Round> rounds;
        private final List<Club> clubs;
        private final int nextClubId;

        GolferMemento(Golfer g) {
            this.username = g.username;
            this.passwordHash = g.passwordHash;
            this.fullName = g.fullName;
            this.courses = new ArrayList<>(g.courses);
            this.rounds = new ArrayList<>(g.rounds);
            this.clubs = new ArrayList<>(g.clubs);
            this.nextClubId = g.nextClubId;
        }
    }

    @Override
    public Memento createMemento() {
        return new GolferMemento(this);
    }

    @Override
    public void restore(Memento memento) {
        GolferMemento gm = (GolferMemento) memento;

        this.username = gm.username;
        this.passwordHash = gm.passwordHash;
        this.fullName = gm.fullName;

        this.courses.clear();
        this.courses.addAll(gm.courses);

        this.rounds.clear();
        this.rounds.addAll(gm.rounds);

        this.clubs.clear();
        this.clubs.addAll(gm.clubs);

        this.nextClubId = gm.nextClubId;
    }
}