Advertisement
Guest User

message

a guest
Jul 8th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. namespace _06.Messages
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class User
  8.     {
  9.         public string Name { get; set; }
  10.  
  11.         public List<Message> ReceivedMessages { get; set; }
  12.     }
  13.  
  14.     public class Message
  15.     {
  16.         public string Content { get; set; }
  17.  
  18.         public string Sender { get; set; }
  19.     }
  20.  
  21.     public class Messages
  22.     {
  23.         public static void Main()
  24.         {
  25.  
  26.             // 60/100 judge
  27.  
  28.             List<User> registeredUsers = new List<User>();
  29.             List<Message> registeretdMeseges = new List<Message>();
  30.  
  31.             string[] input = Console.ReadLine()
  32.                 .Split(new char[] { ' ' },
  33.                 StringSplitOptions.RemoveEmptyEntries);
  34.  
  35.  
  36.             while (input[0] != "exit")
  37.             {
  38.                 #region Register the usersrs and take the messeges
  39.                 if (input[0] == "register")
  40.                 {
  41.                     User newUser = new User()
  42.                     {
  43.                         Name = input[1],
  44.                         ReceivedMessages = new List<Message>()
  45.                     };
  46.                     registeredUsers.Add(newUser);
  47.                 }
  48.                 else
  49.                 {
  50.                     //Chek did we have the recipient for the messeges
  51.                     for (int i = 0; i < registeredUsers.Count; i++)
  52.                     {
  53.                         if (registeredUsers[i].Name.Contains(input[2]))
  54.                         {
  55.                             Message newMessage = new Message()
  56.                             {
  57.                                 Sender = input[0],
  58.  
  59.                                 Content = input[3]
  60.                             };
  61.                             registeretdMeseges.Add(newMessage);
  62.                         }
  63.                     }
  64.                 }
  65.                 #endregion
  66.  
  67.                 input = Console.ReadLine()
  68.                 .Split(new char[] { ' ' },
  69.                 StringSplitOptions.RemoveEmptyEntries);
  70.             }
  71.             #region Add the sender and the message
  72.             //Add the Sender and the message send to the registeredUser.ReceivedMessages
  73.             foreach (var user in registeredUsers)
  74.             {
  75.                 foreach (var msg in registeretdMeseges)
  76.                 {
  77.                     if (user.Name.Contains(msg.Sender))
  78.                     {
  79.                         user.ReceivedMessages.Add(msg);
  80.                     }
  81.                 }
  82.             }
  83.             #endregion
  84.  
  85.             input = Console.ReadLine()
  86.                .Split(new char[] { ' ' },
  87.                StringSplitOptions.RemoveEmptyEntries);
  88.  
  89.             string firstUserName = input[0];
  90.             string secondUserName = input[1];
  91.  
  92.             //List<string> names = new List<string>();
  93.  
  94.             //bool areUsersRegistered = false;
  95.  
  96.             //#region Get the registeret name and find if we have machest with the input
  97.             ////Get all names that we have in list
  98.             //foreach (var user in registeredUsers)
  99.             //{
  100.             //    names.Add(user.Name);
  101.             //}
  102.  
  103.             ////if (input.Length >= 1)
  104.             ////{
  105.             //    //Chek if we have the name to start the chat
  106.             //    if (names.Contains(firstUserName) && names.Contains(secondUserName))
  107.             //    {
  108.             //        areUsersRegistered = true;
  109.             //    }
  110.             //}
  111.  
  112.             //#endregion
  113.  
  114.  
  115.  
  116.             //if (areUsersRegistered)
  117.             //{
  118.  
  119.             //    //Find if there are messeges between the both of them
  120.             //    bool haveMeseges = false;
  121.  
  122.             //    if (registeredUsers.Find(u => u.Name == firstUserName)
  123.             //        .ReceivedMessages.Count() > 0 &&
  124.             //        registeredUsers.Find(u => u.Name == secondUserName)
  125.             //        .ReceivedMessages.Count() > 0)
  126.             //    {
  127.             //        haveMeseges = true;
  128.             //    }
  129.  
  130.                 //if (haveMeseges)
  131.                 //{
  132.  
  133.  
  134.                     List<Message> nameOneChat = registeredUsers
  135.                         .Find(u => u.Name == firstUserName)
  136.                         .ReceivedMessages
  137.                         .Where(m => m.Sender == firstUserName)
  138.                         .ToList();
  139.                     List<Message> nameTwoChat = registeredUsers
  140.                         .Find(u => u.Name == secondUserName)
  141.                         .ReceivedMessages
  142.                         .Where(m => m.Sender == secondUserName)
  143.                         .ToList();
  144.  
  145.             if (nameOneChat.Count == 0 && nameTwoChat.Count == 0)
  146.             {
  147.                 Console.WriteLine("No messages"); return;
  148.             }
  149.  
  150.  
  151.             ////Get the chat in separeted lists
  152.             //foreach (var person in registeredUsers)
  153.             //{
  154.             //    if(person.Name == firstUserName)
  155.             //    {
  156.             //        nameOneChat = person.ReceivedMessages;
  157.             //    }
  158.  
  159.             //    if(person.Name == secondUserName)
  160.             //    {
  161.             //        nameTwoChat = person.ReceivedMessages;
  162.             //    }
  163.             //}
  164.  
  165.             ////Get the max lenght and print all messeges in order
  166.             int lenghtOfTheChat = Math.Max(nameOneChat.Count, nameTwoChat.Count);
  167.  
  168.                     for (int i = 0; i < lenghtOfTheChat; i++)
  169.                     {
  170.                         if (i < nameOneChat.Count)
  171.                         {
  172.                             Console.WriteLine($"{firstUserName}: {nameOneChat[i].Content}");
  173.                         }
  174.                         if (i < nameTwoChat.Count)
  175.                         {
  176.                             Console.WriteLine($"{nameTwoChat[i].Content} :{secondUserName}");
  177.                         }
  178.                     }
  179.  
  180.                 //}
  181.                 //else
  182.                 //{
  183.                 //    Console.WriteLine("No messages");
  184.                 //}
  185.  
  186.             }
  187.             //else
  188.             //{
  189.             //    return;
  190.             //}
  191.         }
  192.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement