public virtual ICommand CreateCommand(string commandName, IPerformanceDatabase performanceDatabase, IRenderer renderer) { ICommand command = null; switch (commandName.ToLower()) { case "addperformance": command = new AddPerformanceCommand(performanceDatabase, renderer); break; case "addtheatre": command = new AddTheatreCommand(performanceDatabase, renderer); break; case "printallperformances": command = new PrintAllPerformancesCommand(performanceDatabase, renderer); break; case "printalltheatres": command = new PrintAllTheatresCommand(performanceDatabase, renderer); break; case "printperformances": command = new PrintPerformancesCommand(performanceDatabase, renderer); break; default: throw new ArgumentException("Invalid command!"); } return(command); }
public IExecutable ExecuteCommand(string line) { string[] inputArgs = line.Split(new[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries); string command = inputArgs[0]; string[] parameters = inputArgs.Skip(1).Select(p => p.Trim()).ToArray(); IExecutable commandResult = null; switch (command) { case "AddTheatre": commandResult = new AddTheatreCommand(parameters, this.performanceDatabase); break; case "PrintAllTheatres": commandResult = new PrintAllTheatresCommand(this.performanceDatabase); break; case "AddPerformance": commandResult = new AddPerformanceCommand(parameters, this.performanceDatabase); break; case "PrintAllPerformances": commandResult = new PrintAllPerformancesCommand(this.performanceDatabase); break; case "PrintPerformances": commandResult = new PrintPerformancesCommand(parameters, this.performanceDatabase); break; default: commandResult = new InvalidResultCommand(this.performanceDatabase); break; } return commandResult; }
public static string ExecuteAddPerformanceCommand(IPerformanceDatabase dataBase, AddPerformanceCommand command) { dataBase.AddPerformance( command.Theatre, command.Performance, command.Date, command.Duration, command.Price); return "Performance added"; }
public void ExecuteCommand(string inputLine) { string[] commandLine = inputLine.Split('('); string commandName = commandLine[0]; string[] commandArgumentsArray = commandLine[1] .Split( new[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries) .Select(p => p.Trim()) .ToArray(); ICommand command = null; switch (commandName) { case "AddTheatre": command = new AddTheatreCommand(commandArgumentsArray, this.performanceDatabase); break; case "PrintAllTheatres": command = new PrintAllTheatersCommand(commandArgumentsArray, this.performanceDatabase); break; case "AddPerformance": command = new AddPerformanceCommand(commandArgumentsArray, this.performanceDatabase); break; case "PrintAllPerformances": command = new PrintAllPerformancesCommand(commandArgumentsArray, this.performanceDatabase); break; case "PrintPerformances": command = new PrintPerformancesCommand(commandArgumentsArray, this.performanceDatabase); break; default: throw new NotImplementedException("The command with name " + commandName + " is not defined/implemented."); } string commandResult; try { commandResult = command.Execute(); } catch (Exception ex) { commandResult = "Error: " + ex.Message; } Console.WriteLine(commandResult); }
private void ExecuteCommand(string commandLine) { if (!string.IsNullOrWhiteSpace(commandLine)) { var command = new Command(commandLine); string commandResult; try { switch (command.CommandName) { case "AddTheatre": var addTheatreCmd = new AddThratreCommand(commandLine); commandResult = CommandExecuter.ExecuteAddTheatreCommand(this.DataBase, addTheatreCmd); break; case "PrintAllTheatres": commandResult = CommandExecuter.ExecutePrintAllTheatresCommand(this.DataBase); break; case "AddPerformance": var addPerformanceCmd = new AddPerformanceCommand(commandLine); commandResult = CommandExecuter.ExecuteAddPerformanceCommand(this.DataBase, addPerformanceCmd); break; case "PrintAllPerformances": commandResult = CommandExecuter.ExecutePrintAllPerformancesCommand(this.DataBase); break; case "PrintPerformances": var printPerformances = new PrintPerformancesCommand(commandLine); commandResult = CommandExecuter.ExecutePrintPerformancesCommand(this.DataBase, printPerformances); break; default: commandResult = "Invalid command!"; break; } this.OutputMethod.Output(commandResult); } catch (Exception ex) { OutputMethod.Output("Error: "+ ex.Message); } } }