Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using Wintellect.PowerCollections;
- namespace Posts
- {
- class Program
- {
- static Dictionary<string, User> usersByName = new Dictionary<string, User>();
- static void Main(string[] args)
- {
- var postNumber = new PostCreationNumberCount();
- var sb = new StringBuilder();
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- var line = Console.ReadLine().Split();
- var command = line[0];
- if (command == "add")
- {
- var name = line[1];
- AddUser(name);
- }
- else if (command == "subscribe")
- {
- var firstName = line[1];
- var secondName = line[2];
- var output = SubscribeUser(firstName, secondName);
- sb.AppendLine(output);
- }
- else if (command == "post")
- {
- string name = line[1];
- string text = line[2];
- var output = CreatePost(name, text, postNumber);
- sb.AppendLine(output);
- }
- else if (command == "listposts")
- {
- string name = line[1];
- var ouput = ListPostsByUser(name);
- sb.AppendLine(ouput);
- }
- }
- Console.WriteLine(sb.ToString().TrimEnd());
- }
- static void AddUser(string name)
- {
- var user = new User(name);
- usersByName.Add(name, user);
- }
- static string SubscribeUser(string firstName, string secondName)
- {
- var secondUser = usersByName[secondName];
- var firstUser = usersByName[firstName];
- firstUser.FollowedUsers.Add(secondUser);
- return $"{firstName} subscribed to {secondName}";
- }
- static string CreatePost(string name, string text, PostCreationNumberCount number)
- {
- var user = usersByName[name];
- var post = new Post(text, user, number);
- user.AddPost(post);
- return $"{name} created post {post.Order}: {text}";
- }
- static string ListPostsByUser(string name)
- {
- var sb = new StringBuilder();
- var user = usersByName[name];
- var postByUser = user.PostsByUser.ToList();
- var postsByUserFollowers = user.FollowedUsers.SelectMany(x => x.PostsByUser).ToList();
- var allPosts = postByUser.Concat(postsByUserFollowers).ToList();
- allPosts.Sort();
- var result = allPosts.Take(10);
- sb.AppendLine( $"{name}: {user.FollowedUsers.Count} subscriptions");
- foreach (var item in result)
- {
- sb.AppendLine(item.ToString());
- }
- return sb.ToString().TrimEnd();
- }
- }
- public class PostCreationNumberCount
- {
- private int counter;
- public PostCreationNumberCount()
- {
- counter = 0;
- }
- public int PostNumberCount()
- {
- counter++;
- return counter;
- }
- }
- public class User : IComparable<User>
- {
- public OrderedSet<Post> PostsByUser { get; set; }
- public List<User> FollowedUsers { get; set; }
- public User(string name)
- {
- this.Name = name;
- PostsByUser = new OrderedSet<Post>();
- FollowedUsers = new List<User>();
- }
- public string Name { get; set; }
- public void AddPost(Post post)
- {
- if (PostsByUser.Count >= 10)
- {
- var lastUser = PostsByUser[9];
- PostsByUser.RemoveLast();
- PostsByUser.Add(post);
- }
- else
- {
- PostsByUser.Add(post);
- }
- }
- public int CompareTo(User other)
- {
- int compariosn = this.Name.CompareTo(other.Name);
- return compariosn;
- }
- }
- public class Post : IComparable<Post>
- {
- public Post(string text, User userCreator, PostCreationNumberCount number )
- {
- this.Text = text;
- this.UserCreator = userCreator;
- this.Order = number.PostNumberCount();
- }
- public User UserCreator { get; set; }
- public string Text { get; set; }
- public int Order { get; set; }
- public override string ToString()
- {
- return $"- Post {Order}: {Text}";
- }
- public int CompareTo(Post other)
- {
- int comparison = this.Order.CompareTo(other.Order) * -1;
- return comparison;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement