private string ProcessListEventsCommand(Command command) { DateTime eventDate = DateTime.ParseExact( command.Arguments[0], DateTimeFormat, CultureInfo.InvariantCulture); int numberOfElementsToList = int.Parse(command.Arguments[1]); // BOTTLENECK: Converting an IEnumerable<T> collection to List collection can be slow // when the elements in the IEnumerable<T> collection are not stored // in a collection that implements IList<T>. // FIX: In this case no convertion is neeed because IEnumrabl<T> collection can be // traversed with a foreach loop. IEnumerable<Event> events = this.eventsManager.ListEvents(eventDate, numberOfElementsToList); if (events.Count() != 0) { StringBuilder output = new StringBuilder(); foreach (Event currentEvent in events) { output.AppendLine(currentEvent.ToString()); } return output.ToString().Trim(); } else { return NoEventsFoundMessage; } }
public string ProcessCommand(Command command) { switch (command.Name) { case AddEventCommandName: if (command.Arguments.Length < 2 || command.Arguments.Length > 3) { throw new ArgumentException( "'AddEvent' command has invalid number of arguments! " + "Only two or three arguments can be passed!"); } return this.ProcessAddCommand(command); case DeleteEventsCommandName: if (command.Arguments.Length != 1) { throw new ArgumentException( "'DeleteEvents' command has invalid number of arguments! " + "Only one argument can be passed!"); } return this.ProcessDeleteCommand(command); case ListEventsCommandName: if (command.Arguments.Length != 2) { throw new ArgumentException( "'ListEvents' command has invalid number of arguments! " + "Only two arguments can be passed!"); } return this.ProcessListEventsCommand(command); default: throw new ArgumentException( string.Format("Invalid input command: {0}!", command.Name), "inputCommand"); } }
public static Command Parse(string inputCommand) { int nameEndIndex = inputCommand.IndexOf(CommandNameSeparator); if (nameEndIndex == -1) { throw new FormatException( string.Format("Invalid input command: {0}! Command cannot be 1 word!", inputCommand)); } string commandName = inputCommand.Substring(0, nameEndIndex); string argumentsSubstring = inputCommand.Substring(nameEndIndex + 1); string[] commandArguments = argumentsSubstring.Split( CommandArgumentsSeparators, StringSplitOptions.RemoveEmptyEntries); TrimArguments(commandArguments); Command command = new Command(); command.Name = commandName; command.Arguments = commandArguments; return command; }
private string ProcessAddCommand(Command command) { DateTime eventDate = DateTime.ParseExact( command.Arguments[0], DateTimeFormat, CultureInfo.InvariantCulture); Event newEvent = new Event(); newEvent.Date = eventDate; newEvent.Title = command.Arguments[1]; if (command.Arguments.Length == 3) { newEvent.Location = command.Arguments[2]; } else { newEvent.Location = null; } this.eventsManager.AddEvent(newEvent); return EventAddedMessage; }
private string ProcessDeleteCommand(Command command) { int deletedEventsCount = this.eventsManager.DeleteEventsByTitle(command.Arguments[0]); if (deletedEventsCount == 0) { return NoEventsFoundMessage; } else { return deletedEventsCount + DeletedEventsMessage; } }