Advertisement
shubhampal07

Untitled

Dec 4th, 2021
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define PB push_back
  5. typedef vector<int> VI;
  6. typedef vector<VI> VVI;
  7.  
  8.  
  9.  
  10. class User {
  11.     public:
  12.     string userID,userName,description;
  13.     int noOFPosts,noOfFollowers,noOfFollowing;
  14.     User(){}
  15.     User(string userId,string userName,string desc)
  16.     {
  17.         this->userID = userId;
  18.         this->userName = userName;
  19.         this->description = desc;
  20.         noOFPosts=0;noOfFollowers=0;noOfFollowing=0;
  21.     }
  22. };
  23.  
  24. class Post {
  25.     public:
  26.         string postId, postContents;
  27.         int timestamp;
  28.         Post(){};
  29.         Post(string id, string cont, int currTime) {
  30.             postId = id;postContents = cont;timestamp = currTime;
  31.         }
  32. };
  33.  
  34. bool onTime(Post a, Post b) {
  35.     return a.timestamp<b.timestamp;
  36. }
  37.  
  38. class SocialNetwork{
  39.     public:
  40.         map<string, vector<string> > network;
  41.         map<string, vector<Post>> PostsMap;
  42.         map<string, vector<string> > LikedPosts;
  43.         map<string, User> UserDetails;
  44.         map<string, Post> PostDetails;
  45.         void RegisterUser(string, string, string);
  46.         void FollowUser(string, string);
  47.         void unFollowUser(string, string);
  48.         void createPost(string, string, string, int);
  49.         void getFeed(string);
  50.         void getAllUserInfo();
  51.         void likePost(string, string);
  52.         void getAllLikedPosts(string);
  53.  
  54. };
  55. void SocialNetwork::likePost(string uid, string pid) {
  56.     LikedPosts[uid].push_back(pid);
  57. }
  58.  
  59. void SocialNetwork::getAllLikedPosts(string userId) {
  60.     for(auto x: LikedPosts[userId]) {
  61.         cout<<PostDetails[x].postId<<"   "<<PostDetails[x].postContents<<endl;
  62.     }
  63. }
  64.  
  65. void SocialNetwork::getAllUserInfo() {
  66.     for(auto x:UserDetails)
  67.     {
  68.         cout<<"UserId: "<<x.first<<"  "<<"name: "<<x.second.userName<<"  "<<" description: "<<x.second.description<<" posts: "<<PostsMap[x.first].size()<<" followers: "<<x.second.noOfFollowers<<" following:"<<x.second.noOfFollowing<<endl;
  69.  
  70.     }
  71. }
  72.  
  73. void SocialNetwork::getFeed(string userId) {
  74.     if(UserDetails.find(userId)==UserDetails.end()) {
  75.         cout<<"userId NOT exist!\n";
  76.         return;
  77.     }
  78.     vector<Post> FeedPosts;
  79.     for(auto following:network[userId]) {
  80.         for(auto posts: PostsMap[following]) {
  81.             FeedPosts.push_back(posts);
  82.         }
  83.     }
  84.  
  85.     sort(FeedPosts.begin(), FeedPosts.end(), onTime);
  86.     for(auto x: FeedPosts) {
  87.         cout<<x.postId<<" ---> "<<x.postContents<<" ---> "<<x.timestamp<<endl;
  88.     }
  89.  
  90. }
  91.  
  92. void SocialNetwork::createPost(string creatorUserId, string postId, string Contents, int currTime) {
  93.     if(PostDetails.find(postId)!=PostDetails.end()) {
  94.         cout<<postId<<" already exixts\n";
  95.         return;
  96.     }
  97.    
  98.     Post p(postId, Contents, currTime);
  99.     PostDetails[postId] = p;
  100.     PostsMap[creatorUserId].push_back(p);
  101.     cout<<"Post "<<postId<<" created!\n";
  102.  
  103. }
  104.  
  105. void SocialNetwork::unFollowUser(string followerUserId, string followingUserId) {
  106.     if(UserDetails.find(followerUserId)==UserDetails.end()) {
  107.         cout<<"followerUserId does not exist!\n";
  108.         return;
  109.     }
  110.     auto it = find(network[followerUserId].begin(),network[followerUserId].end(),followingUserId);
  111.     if(it==network[followerUserId].end()){
  112.         cout<<followerUserId<<" already DO NOT follows "<<followingUserId<<endl;
  113.         return;
  114.     }
  115.     network[followerUserId].erase(it);
  116.     UserDetails[followerUserId].noOfFollowing--;
  117.     UserDetails[followingUserId].noOfFollowers--;
  118. }
  119.  
  120. void SocialNetwork::FollowUser(string followerUserId, string followingUserId) {
  121.     if(UserDetails.find(followerUserId)==UserDetails.end()) {
  122.         cout<<"followerUserId does not exist!\n";
  123.         return;
  124.     }
  125.     auto it = find(network[followerUserId].begin(),network[followerUserId].end(),followingUserId);
  126.     if(it!=network[followerUserId].end()){
  127.         cout<<followerUserId<<" already follows "<<followingUserId<<endl;
  128.         return;
  129.     }
  130.     network[followerUserId].push_back(followingUserId);
  131.     UserDetails[followerUserId].noOfFollowing++;
  132.     UserDetails[followingUserId].noOfFollowers++;
  133. }
  134.  
  135. void SocialNetwork::RegisterUser(string userId, string userName, string desc) {
  136.     if(UserDetails.find(userId)!=UserDetails.end()) {
  137.         cout<<"userId has already been taken!\n";
  138.         return;
  139.     }
  140.     User u(userId,userName,desc);
  141.     UserDetails[userId] = u;
  142.     cout<<userName<<" is registered successfully!\n";
  143. }
  144.  
  145.  
  146.  
  147. int32_t main()
  148. {
  149.     ios_base::sync_with_stdio(false);
  150.     cin.tie(NULL);
  151.     #ifndef ONLINE_JUDGE
  152.     freopen("../input.txt","r",stdin);
  153.     freopen("../output.txt","w",stdout);
  154.     #endif
  155.     SocialNetwork s;
  156.     s.RegisterUser("shub", "shubham", "I am a coder");
  157.     s.RegisterUser("avni","Avani", "Hi, I love books");
  158.     s.RegisterUser("gvr", "Gaurav", "My name is Gaurav");
  159.     s.FollowUser("shub","avni");
  160.     s.FollowUser("shub", "gvr");
  161.     s.createPost("avni","p1","avani started her schooling", 1);
  162.     s.createPost("gvr", "p2", "gaurav started his schooling", 2);
  163.     s.getFeed("shub");
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement