-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Question.java
76 lines (66 loc) · 2.27 KB
/
Question.java
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
package stackoverflow;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Question implements Votable, Commentable {
private final int id;
private final String title;
private final String content;
private final User author;
private final Date creationDate;
private final List<Answer> answers;
private final List<Comment> comments;
private final List<Tag> tags;
private final List<Vote> votes;
public Question(User author, String title, String content, List<String> tagNames) {
this.id = generateId();
this.author = author;
this.title = title;
this.content = content;
this.creationDate = new Date();
this.answers = new ArrayList<>();
this.tags = new ArrayList<>();
this.votes = new ArrayList<>();
this.comments = new ArrayList<>();
for (String tagName : tagNames) {
this.tags.add(new Tag(tagName));
}
}
public void addAnswer(Answer answer) {
if (!answers.contains(answer)) {
answers.add(answer);
}
}
@Override
public void vote(User user, int value) {
if (value != 1 && value != -1) {
throw new IllegalArgumentException("Vote value must be either 1 or -1");
}
votes.removeIf(v -> v.getUser().equals(user));
votes.add(new Vote(user, value));
author.updateReputation(value * 5); // +5 for upvote, -5 for downvote
}
@Override
public int getVoteCount() {
return votes.stream().mapToInt(Vote::getValue).sum();
}
@Override
public void addComment(Comment comment) {
comments.add(comment);
}
@Override
public List<Comment> getComments() {
return new ArrayList<>(comments);
}
private int generateId() {
return (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
}
// Getters
public int getId() { return id; }
public User getAuthor() { return author; }
public String getTitle() { return title; }
public String getContent() { return content; }
public Date getCreationDate() { return creationDate; }
public List<Answer> getAnswers() { return new ArrayList<>(answers); }
public List<Tag> getTags() { return new ArrayList<>(tags); }
}