Advertisement
Adrian_Apostolov

Events

Dec 2nd, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.75 KB | None | 0 0
  1. namespace Events
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     using Wintellect.PowerCollections;
  9.  
  10.     class Program
  11.     {
  12.         static SortedDictionary<string, List<Event>> eventByTitle = new SortedDictionary<string, List<Event>>();
  13.         //static SortedDictionary<DateTime, List<Event>> eventByDate = new SortedDictionary<DateTime, List<Event>>();
  14.  
  15.         static OrderedMultiDictionary<DateTime, Event> eventByDate = new OrderedMultiDictionary<DateTime, Event>(true);
  16.         static StringBuilder sb = new StringBuilder();
  17.         static void Main()
  18.         {
  19.             while (true)
  20.             {
  21.                 var line = Console.ReadLine();
  22.  
  23.                 if (line == "End")
  24.                 {
  25.                     break;
  26.                 }
  27.  
  28.                 var commnadEndIndex = line.IndexOf(" ");
  29.                 var command = line.Substring(0, commnadEndIndex).Trim();
  30.  
  31.                 var commandParts = line.Substring(commnadEndIndex)
  32.                     .Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  33.                     .Select(x => x.Trim()).ToList();
  34.  
  35.                 if (command == "AddEvent")
  36.                 {
  37.                     if (commandParts.Count() == 3)
  38.                     {
  39.                         var newEvent = new Event(commandParts[1], DateTime.Parse(commandParts[0]), commandParts[2]);
  40.  
  41.                         if (!eventByTitle.ContainsKey(newEvent.Title.ToLower()))
  42.                         {
  43.                             eventByTitle.Add(newEvent.Title.ToLower(), new List<Event>());
  44.                         }
  45.  
  46.                         //if (!eventByDate.ContainsKey(newEvent.Date))
  47.                         //{
  48.                         //    eventByDate.Add(newEvent.Date, new List<Event>());
  49.                         //}
  50.  
  51.                         eventByTitle[newEvent.Title.ToLower()].Add(newEvent);
  52.                         eventByDate[newEvent.Date].Add(newEvent);
  53.  
  54.                         sb.AppendLine("Event added");
  55.                     }
  56.                     else
  57.                     {
  58.                         var newEvent = new Event(commandParts[1], DateTime.Parse(commandParts[0]));
  59.  
  60.                         if (!eventByTitle.ContainsKey(newEvent.Title.ToLower()))
  61.                         {
  62.                             eventByTitle.Add(newEvent.Title.ToLower(), new List<Event>());
  63.                         }
  64.  
  65.                         //if (!eventByDate.ContainsKey(newEvent.Date))
  66.                         //{
  67.                         //    eventByDate.Add(newEvent.Date, new List<Event>());
  68.                         //}
  69.  
  70.                         eventByTitle[newEvent.Title.ToLower()].Add(newEvent);
  71.                         eventByDate[newEvent.Date].Add(newEvent);
  72.  
  73.                         sb.AppendLine("Event added");
  74.                     }
  75.                 }
  76.                 else if (command == "ListEvents")
  77.                 {
  78.                     var dateFilter = DateTime.Parse(commandParts[0]);
  79.                     var count = int.Parse(commandParts[1]);
  80.  
  81.                     var eventToList = eventByDate
  82.                         .RangeFrom(dateFilter, true).Values
  83.                         .Take(count);
  84.  
  85.  
  86.                     if (eventToList.Count() == 0)
  87.                     {
  88.                         sb.AppendLine("No events found");
  89.                         continue;
  90.                     }
  91.  
  92.                     foreach (var e in eventToList)
  93.                     {
  94.                         sb.AppendLine(e.ToString());
  95.                     }
  96.  
  97.                 }
  98.                 else if (command == "DeleteEvents")
  99.                 {
  100.                     var titleToDelete = commandParts[0].ToLower();
  101.  
  102.                     if (!eventByTitle.ContainsKey(titleToDelete))
  103.                     {
  104.                         sb.AppendLine("No events found");
  105.                         continue;
  106.                     }
  107.  
  108.                     var eventsToDelete = eventByTitle[titleToDelete];
  109.                     int deletedCount = eventsToDelete.Count;
  110.  
  111.                     foreach (var e in eventsToDelete)
  112.                     {
  113.                         eventByDate.Remove(e.Date, e);
  114.                     }
  115.  
  116.                     eventByTitle.Remove(titleToDelete);
  117.  
  118.                     //foreach (var item in eventsToDelete)
  119.                     //{
  120.                     //    eventByDate[item.Date].Remove(item);
  121.                     //}
  122.  
  123.                     sb.AppendLine(string.Format("{0} events deleted", deletedCount));
  124.                 }
  125.             }
  126.  
  127.             Console.WriteLine(sb.ToString().Trim());
  128.         }
  129.     }
  130.  
  131.     class Event : IComparable<Event>
  132.     {
  133.         public Event(string title, DateTime date)
  134.         {
  135.             this.Title = title;
  136.             this.Date = date;
  137.         }
  138.  
  139.         public Event(string title, DateTime date, string location)
  140.             : this(title, date)
  141.         {
  142.             this.Location = location;
  143.         }
  144.  
  145.         public string Title { get; set; }
  146.  
  147.         public DateTime Date { get; set; }
  148.  
  149.         public string Location { get; set; }
  150.  
  151.         public override string ToString()
  152.         {
  153.             if (string.IsNullOrEmpty(this.Location))
  154.             {
  155.                 return string.Format("{0} | {1}", this.Date.ToString("yyyy-MM-ddTHH:mm:ss"), this.Title);
  156.             }
  157.  
  158.             return string.Format("{0} | {1} | {2}", this.Date.ToString("yyyy-MM-ddTHH:mm:ss"), this.Title, this.Location);
  159.         }
  160.  
  161.         public int CompareTo(Event other)
  162.         {
  163.             int compare = DateTime.Compare(this.Date, other.Date);
  164.  
  165.             if (compare == 0)
  166.             {
  167.                 compare = string.Compare(this.Title, other.Title);
  168.             }
  169.             if (compare == 0)
  170.             {
  171.                 compare = string.Compare(this.Location, other.Location);
  172.             }
  173.  
  174.             return compare;
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement