Exemplo n.º 1
0
        public static Command Parse(string inputCommand)
        {
            if (inputCommand == null)
            {
                throw new ArgumentNullException("Input command can't be null.");
            }

            int commandNameEndIndex = inputCommand.IndexOf(' ');
            if (commandNameEndIndex == -1)
            {
                throw new ArgumentException("Invalid command: " + inputCommand);
            }

            string commandName = inputCommand.Substring(0, commandNameEndIndex);
            string parametters = inputCommand.Substring(commandNameEndIndex + 1);

            string[] commandArguments = parametters.Split('|');

            for (int i = 0; i < commandArguments.Length; i++)
            {
                parametters = commandArguments[i];
                commandArguments[i] = parametters.Trim();
            }

            Command parsedCommand = new Command { CommandName = commandName, Params = commandArguments };
            return parsedCommand;
        }
Exemplo n.º 2
0
        private string ProcessCommandAddEvent(Command command)
        {
            if (command.Params.Length != 2 && command.Params.Length != 3)
            {
                throw new ArgumentException("Invalid number of command parameters.");
            }

            DateTime date = DateTime.ParseExact(command.Params[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
            string title = command.Params[1];
            string location;

            if (command.Params.Length == 2) // there isn't location
            {
                location = null;
            }
            else
            {
                location = command.Params[2];
            }

            Event newEvent = new Event()
            {
                Date = date,
                Title = title,
                Location = location
            };

            this.eventsManager.AddEvent(newEvent);

            return "Event added";
        }
Exemplo n.º 3
0
        public static Command Parse(string inputCommand)
        {
            int sepatator = inputCommand.IndexOf(' ');

            if (sepatator == -1)
            {
                throw new IndexOutOfRangeException("Invalid command order: " + inputCommand);
            }

            string name = inputCommand.Substring(0, sepatator);
            string parameters = inputCommand.Substring(sepatator + 1);

            var commandArguments = parameters.Split('|');

            for (int i = 0; i < commandArguments.Length; i++)
            {
                parameters = commandArguments[i];
                commandArguments[i] = parameters.Trim();
            }

            var command = new Command(name, commandArguments);
            //var command = new Command {CommandName = name, Arguments = commandArguments};

            return command;
        }
Exemplo n.º 4
0
        public void Parse_InvalidCommandWithNewLineIn()
        {
            string commandStr = "AddEvent 2012-01-21T20:00:00" + Environment.NewLine + " | party Viki | home";
            Command command = new Command();

            command = Command.Parse(commandStr);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Converts string to an instance of command
        /// </summary>
        /// <param name="commandText">A string representing the command</param>
        /// <returns>Parsed command</returns>
        public static Command Parse(string commandText)
        {
            if (commandText == null)
            {
                throw new ArgumentNullException("commandText");
            }

            int spaceIndex = commandText.IndexOf(' ');
            if (spaceIndex == -1)
            {
                throw new ArgumentException("Invalid command: " + commandText);
            }

            string commandName = commandText.Substring(0, spaceIndex);
            string argumentsString = commandText.Substring(spaceIndex + 1);

            var commandArguments = argumentsString.Split('|');
            for (int i = 0; i < commandArguments.Length; i++)
            {
                commandArguments[i] = commandArguments[i].Trim();
            }

            var command = new Command { Name = commandName, Arguments = commandArguments };

            return command;
        }
Exemplo n.º 6
0
        public void Parse_InvalidCommandType()
        {
            string commandStr = "Add 2012-01-21T20:00:00 | party Viki | home";
            Command command = new Command();

            command = Command.Parse(commandStr);
        }
Exemplo n.º 7
0
        public void Parse_InvalidCommandWithoutSpace()
        {
            string commandStr = "AddEvent2012-01-21T20:00:00|party|home";
            Command command = new Command();

            command = Command.Parse(commandStr);
        }
Exemplo n.º 8
0
        public void Parse_InvalidCommandWithoutVerticalLineBetweenParams()
        {
            string commandStr = "AddEvent 2012-01-21T20:00:00 party Viki home";
            Command command = new Command();

            command = Command.Parse(commandStr);
        }
Exemplo n.º 9
0
        public string ProcessCommand(Command command)
        {
            if ((command.CommandName == "AddEvent") && (command.Parameters.Length == 2))
            {
                return ProcessCommandAddWithTwoParams(command);
            }

            if ((command.CommandName == "AddEvent") && (command.Parameters.Length == 3))
            {
                return ProcessCommandAddWithThreeParams(command);
            }

            if ((command.CommandName == "DeleteEvents") && (command.Parameters.Length == 1))
            {
                return ProcessCommandDeleteEvents(command);
            }

            if ((command.CommandName == "ListEvents") && (command.Parameters.Length == 2))
            {
                return ProcessCommandListEvents(command);
            }
            else
            {
                throw new ArgumentException("Invalid command!No such command {0}", command.CommandName);
            }
        }
Exemplo n.º 10
0
        private string ExecuteAddEvent(Command command)
        {
            var date = DateTime.ParseExact(
                command.Parameters[0],
                "yyyy-MM-ddTHH:mm:ss",
                CultureInfo.InvariantCulture);

            var eventItem = new Event
            {
                Date = date,
                Title = command.Parameters[1],
            };

            if (command.Parameters.Length == 2)
            {
                eventItem.Location = null;
            }
            else if (command.Parameters.Length == 3)
            {
                eventItem.Location = command.Parameters[2];
            }
            else
            {
                throw new ArgumentException("WTF " + command.Name + " is?", "command.CommandName");
            }

            this.eventsManager.AddEvent(eventItem);

            return "Event added";
        }
        private string AddEvent(Command command)
        {
            var date = DateTime.ParseExact(
                command.paramms[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
            if (command.paramms.Length == 2)
            {
                var ev = new Event
                {
                    date = date,
                    Title = command.paramms[1],
                    Location = null,
                };

                this.eventsManager.AddEvent(ev);
            }

            if (command.paramms.Length == 3)
            {
                var ev = new Event
                {
                    date = date,
                    Title = command.paramms[1],
                    Location = command.paramms[2],
                };

                this.eventsManager.AddEvent(ev);

            }
                return "Event added";
        }
        /// <summary>
        /// Process a command using an event processor
        /// </summary>
        /// <param name="command">The command to process</param>
        /// <returns>String representing the result of command processing</returns>
        public string ProcessCommand(Command command)
        {
            if ((command.Name == AddEventCommandName) && (command.Arguments.Length == 2))
            {
                var date = DateTime.ParseExact(command.Arguments[0], Event.DateFormat, CultureInfo.InvariantCulture);
                return this.ProcessAddEventCommand(date, command.Arguments[1]);
            }

            if ((command.Name == AddEventCommandName) && (command.Arguments.Length == 3))
            {
                var date = DateTime.ParseExact(command.Arguments[0], Event.DateFormat, CultureInfo.InvariantCulture);
                return this.ProcessAddEventCommand(date, command.Arguments[1], command.Arguments[2]);
            }

            if ((command.Name == DeleteEventCommandName) && (command.Arguments.Length == 1))
            {
                return this.ProcessDeleteEventsCommand(command.Arguments[0]);
            }

            if ((command.Name == ListEventCommandName) && (command.Arguments.Length == 2))
            {
                var date = DateTime.ParseExact(command.Arguments[0], Event.DateFormat, CultureInfo.InvariantCulture);
                var count = int.Parse(command.Arguments[1]);
                return this.ProcessListEventsCommand(date, count);
            }

            throw new ArgumentException("Unknown command: " + command.Name);
        }
        public void TestProcessDeleteExistingEvents()
        {
            CommandExecutor cmdExecutor = new CommandExecutor(new EventsManagerFast());
            Command addCommand = new Command
            {
                CommandName = "AddEvent",
                Parameters = new string[]
                {
                    "2001-01-01T10:30:03",
                    "party Lora"
                }
            };
            cmdExecutor.ProcessCommand(addCommand);

            Command deleteCommand = new Command
            {
                CommandName = "DeleteEvents",
                Parameters = new string[]
                {
                    "party Lora",
                }
            };
            string result = cmdExecutor.ProcessCommand(deleteCommand);
            Assert.AreEqual("1 events deleted", result);
        }
Exemplo n.º 14
0
        public string ProcessCommand(Command command)
        {
            if (command.CommandName == "AddEvent")
            {
                string location;
                if (command.Parameters.Length == 2)
                {
                    location = null;
                }
                else
                {
                    location = command.Parameters[2];
                }

                var date = DateTime.ParseExact(command.Parameters[0], DateFormat, CultureInfo.InvariantCulture);
                var title = command.Parameters[1];

                Event newEvent = new Event(date, title, location);

                this.eventsManagerList.AddEvent(newEvent);

                return "Event added";
            }

            if ((command.CommandName == "DeleteEvents") && (command.Parameters.Length == 1))
            {
                int numberOfAddedEvents = this.eventsManagerList.DeleteEventsByTitle(command.Parameters[0]);

                if (numberOfAddedEvents == 0)
                {
                    return "No events found";
                }

                return numberOfAddedEvents + " events deleted";
            }

            if ((command.CommandName == "ListEvents") && (command.Parameters.Length == 2))
            {
                var date = DateTime.ParseExact(command.Parameters[0], DateFormat, CultureInfo.InvariantCulture);
                var numberOfElementsToList = int.Parse(command.Parameters[1]);
                var eventsList = this.eventsManagerList.ListEvents(date, numberOfElementsToList).ToList();

                if (!eventsList.Any())
                {
                    return "No events found";
                }

                StringBuilder result = new StringBuilder();
                foreach (var ev in eventsList)
                {
                    result.AppendLine(ev.ToString());
                }

                return result.ToString().Trim();
            }

            throw new ArgumentException("Invalid command name: " + command.CommandName);
        }
 public string ProcessCommand(Command command)
 {
     switch (command.CommandType)
     {
         case "AddEvent": return AddEvent(command);
         case "DeleteEvents": return DeleteEvents(command);
         case "ListEvents": return ListEvents(command);
         default: throw new ArgumentException("This " + command.CommandType + " is not valid!");
     }
     
 }
        private string DeleteEvents(Command command)
        {
                int count = this.eventsManager.DeleteEventsByTitle(command.paramms[0]);

                if (count == 0)
                {
                    return "No events found";
                }

                return count + " events deleted";
        }
        private string DeleteEvents(Command cmd)
        {
            int deletedEventsCount = this.eventsManager.DeleteEventsByTitle(cmd.CommandParams[0]);

            if (deletedEventsCount == 0)
            {
                return "No events found";
            }

            return deletedEventsCount + " events deleted";
        }
Exemplo n.º 18
0
        private string DeleteEvents(Command command)
        {
            string eventTitle = command.Parameters[0];
            int eventsDeleted = this.eventsManager.DeleteEventsByTitle(eventTitle);

            if (eventsDeleted == 0)
            {
                return "No events found";
            }

            return string.Format("{0} events deleted", eventsDeleted);
        }
Exemplo n.º 19
0
        public void TestParseAddWithFourParameters()
        {
            string inputCommand = "AddEvent 2011-11-11T11:11:22 | party Viki 22 | home ";
            Command actualCommand = Command.Parse(inputCommand);

            Command expectedCommand = new Command
            {
                CommandName = "AddEvent",
                Parameters = new string[] { "2011-11-11T11:11:22", "party Viki 22", "home" }
            };

            Assert.AreEqual<Command>(expectedCommand, actualCommand);
        }    
Exemplo n.º 20
0
        public void TestParseAddWithTwoParametersWithHour()
        {
            string inputCommand = "AddEvent 2011-01-09T03:00:59 | party Viki (11:33)";
            Command actualCommand = Command.Parse(inputCommand);

            Command expectedCommand = new Command
            {
                CommandName = "AddEvent",
                Parameters = new string[] { "2011-01-09T03:00:59", "party Viki (11:33)" }
            };

            Assert.AreEqual(expectedCommand, actualCommand);
        }
Exemplo n.º 21
0
        public void TestParseWithTwoParameters()
        {
            string inputCommand = "AddEvent 2012-03-26T08:00:00 | C# exam";
            Command actualCommand = Command.Parse(inputCommand);

            Command expectedCommand = new Command
            {
                CommandName = "AddEvent",
                Parameters = new string[] { "2012-03-26T08:00:00", "C# exam" }
            };

            Assert.AreEqual(expectedCommand, actualCommand);
        }
 public void TestProcessDeleteEventsCommandWithTwoParameters()
 {
     CommandExecutor cmdExecutor = new CommandExecutor(new EventsManagerFast());
     Command cmd = new Command
     {
         CommandName = "DeleteEvents",
         Parameters = new string[]
         {
             "Rock party",
         }
     };
     string result = cmdExecutor.ProcessCommand(cmd);
     Assert.AreEqual("No events found", result);
 }
Exemplo n.º 23
0
 public string ProcessCommand(Command command)
 {
     switch (command.CommandName)
     {
         case AddEventCommand:
             return ProcessCommandAddEvent(command);
         case DeleteEventsCommand:
             return ProcessDeleteEventsCommand(command);
         case ListEventsCommand:
             return ProcessListEventsCommand(command);
         default:
             throw new ArgumentException("Invalid command name " + command.CommandName);
     }
 }
Exemplo n.º 24
0
        public static Command Parse(string command)
        {
            if (command.Contains(Environment.NewLine))
            {
                throw new FormatException("Command cannot containes symbol for new line.");
            }

            int endOfCommandName = command.IndexOf(' ');
            if (endOfCommandName == -1)
            {
                string exceptionMessage = string.Format("Invalid command: {0}", command);
                throw new FormatException(exceptionMessage);
            }

            string commandName = command.Substring(0, endOfCommandName);
            string commandParams = command.Substring(endOfCommandName + 1);

            if (!commandParams.Contains(" | ") && (commandName == "AddEvent" || commandName == "ListEvents"))
            {
                throw new FormatException("Invalid command parametars. Parametars should be separeted with ' | '.");
            }

            var commandArguments = commandParams.Split('|');
            for (int i = 0; i < commandArguments.Length; i++)
            {
                commandParams = commandArguments[i];
                commandArguments[i] = commandParams.Trim();
            }

            CommandType currandCommandType;
            switch (commandName)
            {
                case "AddEvent":
                    currandCommandType = CommandType.AddEvent;
                    break;
                case "DeleteEvents":
                    currandCommandType = CommandType.DeleteEvents;
                    break;
                case "ListEvents":
                    currandCommandType = CommandType.ListEvents;
                    break;
                default:
                    throw new ArgumentException("Wrong command type!");
            }

            var commandFull = new Command(currandCommandType, commandArguments);

            return commandFull;
        }
Exemplo n.º 25
0
        private string ProcessAddEvent(Command command)
        {
            if (!(command.Parameters.Count == 2 || command.Parameters.Count == 3))
            {
                throw new FormatException("Invalid number of parameters: " + command.Parameters.Count);
            }

            DateTime date = ParseDate(command.Parameters[0]);
            string title = command.Parameters[1];
            string location = (command.Parameters.Count == 3) ? command.Parameters[2] : null;

            Event @event = new Event(date, title, location);
            this.eventsManager.AddEvent(@event);
            return "Event added";
        }
 public void TestProcessAddEventCommandWithTwoParameters()
 {
     CommandExecutor cmdExecutor = new CommandExecutor(new EventsManagerFast());
     Command cmd = new Command
     {
         CommandName = "AddEvent",
         Parameters = new string[]
         {
             "2001-01-01T10:30:03",
             "party Lora"
         }
     };
     string result = cmdExecutor.ProcessCommand(cmd);
     Assert.AreEqual("Event added", result);
 }
Exemplo n.º 27
0
        private string ProcessCommandAddWithTwoParams(Command command)
        {
            DateTime date = DateTime.ParseExact(command.Parameters[0], "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
            Event ev = new Event
            {
                DateTime = date,
                Title = command.Parameters[1],
                Location = null,
            };

            this.eventsProcessor.AddEvent(ev);
            string message = "Event added";

            return message;
        }
Exemplo n.º 28
0
        public void Parse_ValidCommandDeleteEvents()
        {
            string commandDeleteStr = "DeleteEvents c# exam";
            Command command = new Command();

            command = Command.Parse(commandDeleteStr);

            bool isCommandNameValid = command.CommandName == CommandType.DeleteEvents;
            string[] parametars = command.Parametars;
            string title = parametars[0];
            bool isTitleCorrect = title == "c# exam";

            bool isParseValid = isCommandNameValid && isTitleCorrect;

            Assert.IsTrue(isParseValid, "Parse with valid input does not work correct.");
        }
Exemplo n.º 29
0
        private string ProcessDeleteEvents(Command command)
        {
            if (!(command.Parameters.Count == 1))
            {
                throw new FormatException("Invalid number of parameters: " + command.Parameters.Count);
            }

            int numberOfDeletedEvents = this.eventsManager.DeleteEventsByTitle(command.Parameters[0]);

            if (numberOfDeletedEvents == 0)
            {
                return "No events found";
            }

            return numberOfDeletedEvents + " events deleted";
        }
        /// <summary>
        /// Processes the delete events command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns>Output result</returns>
        private string ProcessDeleteEventsCommand(Command command)
        {
            if (command.Parameters.Length != 1)
            {
                throw new ArgumentException("The number of parameters in DeleteEvents command must be 1");
            }

            int deletedEventsCount = this.eventManager.DeleteEventsByTitle(command.Parameters[0]);

            if (deletedEventsCount == 0)
            {
                return "No events found";
            }

            return deletedEventsCount + " events deleted";
        }