-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
StackOverflow.java
77 lines (64 loc) · 2.57 KB
/
StackOverflow.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
77
package stackoverflow;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class StackOverflow {
private final Map<Integer, User> users;
private final Map<Integer, Question> questions;
private final Map<Integer, Answer> answers;
private final Map<String, Tag> tags;
public StackOverflow() {
users = new ConcurrentHashMap<>();
questions = new ConcurrentHashMap<>();
answers = new ConcurrentHashMap<>();
tags = new ConcurrentHashMap<>();
}
public User createUser(String username, String email) {
int id = users.size() + 1;
User user = new User(id, username, email);
users.put(id, user);
return user;
}
public Question askQuestion(User user, String title, String content, List<String> tags) {
Question question = user.askQuestion(title, content, tags);
questions.put(question.getId(), question);
for (Tag tag : question.getTags()) {
this.tags.putIfAbsent(tag.getName(), tag);
}
return question;
}
public Answer answerQuestion(User user, Question question, String content) {
Answer answer = user.answerQuestion(question, content);
answers.put(answer.getId(), answer);
return answer;
}
public Comment addComment(User user, Commentable commentable, String content) {
return user.addComment(commentable, content);
}
public void voteQuestion(User user, Question question, int value) {
question.vote(user, value);
}
public void voteAnswer(User user, Answer answer, int value) {
answer.vote(user, value);
}
public void acceptAnswer(Answer answer) {
answer.markAsAccepted();
}
public List<Question> searchQuestions(String query) {
return questions.values().stream()
.filter(q -> q.getTitle().toLowerCase().contains(query.toLowerCase()) ||
q.getContent().toLowerCase().contains(query.toLowerCase()) ||
q.getTags().stream().anyMatch(t -> t.getName().equalsIgnoreCase(query)))
.collect(Collectors.toList());
}
public List<Question> getQuestionsByUser(User user) {
return user.getQuestions();
}
// Getters
public User getUser(int id) { return users.get(id); }
public Question getQuestion(int id) { return questions.get(id); }
public Answer getAnswer(int id) { return answers.get(id); }
public Tag getTag(String name) { return tags.get(name); }
}