forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackOverflowDemo.cs
More file actions
65 lines (57 loc) · 2.64 KB
/
StackOverflowDemo.cs
File metadata and controls
65 lines (57 loc) · 2.64 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
public class StackOverflowDemo
{
public static void Main(string[] args)
{
StackOverflowService service = new StackOverflowService();
// 1. Create Users
User alice = service.CreateUser("Alice");
User bob = service.CreateUser("Bob");
User charlie = service.CreateUser("Charlie");
// 2. Alice posts a question
Console.WriteLine("--- Alice posts a question ---");
Tag javaTag = new Tag("java");
Tag designPatternsTag = new Tag("design-patterns");
HashSet<Tag> tags = new HashSet<Tag> { javaTag, designPatternsTag };
Question question = service.PostQuestion(alice.GetId(), "How to implement Observer Pattern?", "Details about Observer Pattern...", tags);
PrintReputations(alice, bob, charlie);
// 3. Bob and Charlie post answers
Console.WriteLine("\n--- Bob and Charlie post answers ---");
Answer bobAnswer = service.PostAnswer(bob.GetId(), question.GetId(), "You can use the java.util.Observer interface.");
Answer charlieAnswer = service.PostAnswer(charlie.GetId(), question.GetId(), "A better way is to create your own Observer interface.");
PrintReputations(alice, bob, charlie);
// 4. Voting happens
Console.WriteLine("\n--- Voting Occurs ---");
service.VoteOnPost(alice.GetId(), question.GetId(), VoteType.UPVOTE);
service.VoteOnPost(bob.GetId(), charlieAnswer.GetId(), VoteType.UPVOTE);
service.VoteOnPost(alice.GetId(), bobAnswer.GetId(), VoteType.DOWNVOTE);
PrintReputations(alice, bob, charlie);
// 5. Alice accepts Charlie's answer
Console.WriteLine("\n--- Alice accepts Charlie's answer ---");
service.AcceptAnswer(question.GetId(), charlieAnswer.GetId());
PrintReputations(alice, bob, charlie);
// 6. Search for questions
Console.WriteLine("\n--- (C) Combined Search: Questions by 'Alice' with tag 'java' ---");
List<ISearchStrategy> filtersC = new List<ISearchStrategy>
{
new UserSearchStrategy(alice),
new TagSearchStrategy(javaTag)
};
List<Question> searchResults = service.SearchQuestions(filtersC);
foreach (var q in searchResults)
{
Console.WriteLine($" - Found: {q.GetTitle()}");
}
}
private static void PrintReputations(params User[] users)
{
Console.WriteLine("--- Current Reputations ---");
foreach (User user in users)
{
Console.WriteLine($"{user.GetName()}: {user.GetReputation()}");
}
}
}