-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
StackOverflowDemo.java
87 lines (70 loc) · 3.9 KB
/
StackOverflowDemo.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
78
79
80
81
82
83
84
85
86
87
package stackoverflow;
import java.util.Arrays;
import java.util.List;
public class StackOverflowDemo {
public static void run() {
StackOverflow system = new StackOverflow();
// Create users
User alice = system.createUser("Alice", "[email protected]");
User bob = system.createUser("Bob", "[email protected]");
User charlie = system.createUser("Charlie", "[email protected]");
// Alice asks a question
Question javaQuestion = system.askQuestion(alice, "What is polymorphism in Java?",
"Can someone explain polymorphism in Java with an example?",
Arrays.asList("java", "oop"));
// Bob answers Alice's question
Answer bobAnswer = system.answerQuestion(bob, javaQuestion,
"Polymorphism in Java is the ability of an object to take on many forms...");
// Charlie comments on the question
system.addComment(charlie, javaQuestion, "Great question! I'm also interested in learning about this.");
// Alice comments on Bob's answer
system.addComment(alice, bobAnswer, "Thanks for the explanation! Could you provide a code example?");
// Charlie votes on the question and answer
system.voteQuestion(charlie, javaQuestion, 1); // Upvote
system.voteAnswer(charlie, bobAnswer, 1); // Upvote
// Alice accepts Bob's answer
system.acceptAnswer(bobAnswer);
// Bob asks another question
Question pythonQuestion = system.askQuestion(bob, "How to use list comprehensions in Python?",
"I'm new to Python and I've heard about list comprehensions. Can someone explain how to use them?",
Arrays.asList("python", "list-comprehension"));
// Alice answers Bob's question
Answer aliceAnswer = system.answerQuestion(alice, pythonQuestion,
"List comprehensions in Python provide a concise way to create lists...");
// Charlie votes on Bob's question and Alice's answer
system.voteQuestion(charlie, pythonQuestion, 1); // Upvote
system.voteAnswer(charlie, aliceAnswer, 1); // Upvote
// Print out the current state
System.out.println("Question: " + javaQuestion.getTitle());
System.out.println("Asked by: " + javaQuestion.getAuthor().getUsername());
System.out.println("Tags: " + javaQuestion.getTags().stream().map(Tag::getName).reduce((a, b) -> a + ", " + b).orElse(""));
System.out.println("Votes: " + javaQuestion.getVoteCount());
System.out.println("Comments: " + javaQuestion.getComments().size());
System.out.println("\nAnswer by " + bobAnswer.getAuthor().getUsername() + ":");
System.out.println(bobAnswer.getContent());
System.out.println("Votes: " + bobAnswer.getVoteCount());
System.out.println("Accepted: " + bobAnswer.isAccepted());
System.out.println("Comments: " + bobAnswer.getComments().size());
System.out.println("\nUser Reputations:");
System.out.println("Alice: " + alice.getReputation());
System.out.println("Bob: " + bob.getReputation());
System.out.println("Charlie: " + charlie.getReputation());
// Demonstrate search functionality
System.out.println("\nSearch Results for 'java':");
List<Question> searchResults = system.searchQuestions("java");
for (Question q : searchResults) {
System.out.println(q.getTitle());
}
System.out.println("\nSearch Results for 'python':");
searchResults = system.searchQuestions("python");
for (Question q : searchResults) {
System.out.println(q.getTitle());
}
// Demonstrate getting questions by user
System.out.println("\nBob's Questions:");
List<Question> bobQuestions = system.getQuestionsByUser(bob);
for (Question q : bobQuestions) {
System.out.println(q.getTitle());
}
}
}