-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
User.java
66 lines (57 loc) · 2.25 KB
/
User.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
package stackoverflow;
import java.util.ArrayList;
import java.util.List;
public class User {
private final int id;
private final String username;
private final String email;
private int reputation;
private final List<Question> questions;
private final List<Answer> answers;
private final List<Comment> comments;
private static final int QUESTION_REPUTATION = 5;
private static final int ANSWER_REPUTATION = 10;
private static final int COMMENT_REPUTATION = 2;
public User(int id, String username, String email) {
this.id = id;
this.username = username;
this.email = email;
this.reputation = 0;
this.questions = new ArrayList<>();
this.answers = new ArrayList<>();
this.comments = new ArrayList<>();
}
public Question askQuestion(String title, String content, List<String> tags) {
Question question = new Question(this, title, content, tags);
questions.add(question);
updateReputation(QUESTION_REPUTATION); // Gain 5 reputation for asking a question
return question;
}
public Answer answerQuestion(Question question, String content) {
Answer answer = new Answer(this, question, content);
answers.add(answer);
question.addAnswer(answer);
updateReputation(ANSWER_REPUTATION); // Gain 10 reputation for answering
return answer;
}
public Comment addComment(Commentable commentable, String content) {
Comment comment = new Comment(this, content);
comments.add(comment);
commentable.addComment(comment);
updateReputation(COMMENT_REPUTATION); // Gain 2 reputation for commenting
return comment;
}
public synchronized void updateReputation(int value) {
this.reputation += value;
if (this.reputation < 0) {
this.reputation = 0;
}
}
// Getters
public int getId() { return id; }
public String getUsername() { return username; }
public int getReputation() { return reputation; }
public List<Question> getQuestions() { return new ArrayList<>(questions); }
public List<Answer> getAnswers() { return new ArrayList<>(answers); }
public List<Comment> getComments() { return new ArrayList<>(comments); }
}