Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Events
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Wintellect.PowerCollections;
- class Program
- {
- static SortedDictionary<string, List<Event>> eventByTitle = new SortedDictionary<string, List<Event>>();
- //static SortedDictionary<DateTime, List<Event>> eventByDate = new SortedDictionary<DateTime, List<Event>>();
- static OrderedMultiDictionary<DateTime, Event> eventByDate = new OrderedMultiDictionary<DateTime, Event>(true);
- static StringBuilder sb = new StringBuilder();
- static void Main()
- {
- while (true)
- {
- var line = Console.ReadLine();
- if (line == "End")
- {
- break;
- }
- var commnadEndIndex = line.IndexOf(" ");
- var command = line.Substring(0, commnadEndIndex).Trim();
- var commandParts = line.Substring(commnadEndIndex)
- .Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(x => x.Trim()).ToList();
- if (command == "AddEvent")
- {
- if (commandParts.Count() == 3)
- {
- var newEvent = new Event(commandParts[1], DateTime.Parse(commandParts[0]), commandParts[2]);
- if (!eventByTitle.ContainsKey(newEvent.Title.ToLower()))
- {
- eventByTitle.Add(newEvent.Title.ToLower(), new List<Event>());
- }
- //if (!eventByDate.ContainsKey(newEvent.Date))
- //{
- // eventByDate.Add(newEvent.Date, new List<Event>());
- //}
- eventByTitle[newEvent.Title.ToLower()].Add(newEvent);
- eventByDate[newEvent.Date].Add(newEvent);
- sb.AppendLine("Event added");
- }
- else
- {
- var newEvent = new Event(commandParts[1], DateTime.Parse(commandParts[0]));
- if (!eventByTitle.ContainsKey(newEvent.Title.ToLower()))
- {
- eventByTitle.Add(newEvent.Title.ToLower(), new List<Event>());
- }
- //if (!eventByDate.ContainsKey(newEvent.Date))
- //{
- // eventByDate.Add(newEvent.Date, new List<Event>());
- //}
- eventByTitle[newEvent.Title.ToLower()].Add(newEvent);
- eventByDate[newEvent.Date].Add(newEvent);
- sb.AppendLine("Event added");
- }
- }
- else if (command == "ListEvents")
- {
- var dateFilter = DateTime.Parse(commandParts[0]);
- var count = int.Parse(commandParts[1]);
- var eventToList = eventByDate
- .RangeFrom(dateFilter, true).Values
- .Take(count);
- if (eventToList.Count() == 0)
- {
- sb.AppendLine("No events found");
- continue;
- }
- foreach (var e in eventToList)
- {
- sb.AppendLine(e.ToString());
- }
- }
- else if (command == "DeleteEvents")
- {
- var titleToDelete = commandParts[0].ToLower();
- if (!eventByTitle.ContainsKey(titleToDelete))
- {
- sb.AppendLine("No events found");
- continue;
- }
- var eventsToDelete = eventByTitle[titleToDelete];
- int deletedCount = eventsToDelete.Count;
- foreach (var e in eventsToDelete)
- {
- eventByDate.Remove(e.Date, e);
- }
- eventByTitle.Remove(titleToDelete);
- //foreach (var item in eventsToDelete)
- //{
- // eventByDate[item.Date].Remove(item);
- //}
- sb.AppendLine(string.Format("{0} events deleted", deletedCount));
- }
- }
- Console.WriteLine(sb.ToString().Trim());
- }
- }
- class Event : IComparable<Event>
- {
- public Event(string title, DateTime date)
- {
- this.Title = title;
- this.Date = date;
- }
- public Event(string title, DateTime date, string location)
- : this(title, date)
- {
- this.Location = location;
- }
- public string Title { get; set; }
- public DateTime Date { get; set; }
- public string Location { get; set; }
- public override string ToString()
- {
- if (string.IsNullOrEmpty(this.Location))
- {
- return string.Format("{0} | {1}", this.Date.ToString("yyyy-MM-ddTHH:mm:ss"), this.Title);
- }
- return string.Format("{0} | {1} | {2}", this.Date.ToString("yyyy-MM-ddTHH:mm:ss"), this.Title, this.Location);
- }
- public int CompareTo(Event other)
- {
- int compare = DateTime.Compare(this.Date, other.Date);
- if (compare == 0)
- {
- compare = string.Compare(this.Title, other.Title);
- }
- if (compare == 0)
- {
- compare = string.Compare(this.Location, other.Location);
- }
- return compare;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement