public void Process(Command command, StringBuilder output) { switch (command.Name) { case "AddProduct": { this.AddProduct(command.Arguments, output); break; } case "DeleteProducts": { this.DeleteProducts(command.Arguments, output); break; } case "FindProductsByName": { this.ListProductsByName(command.Arguments, output); break; } case "FindProductsByProducer": { this.ListProductsByProducer(command.Arguments, output); break; } case "FindProductsByPriceRange": { this.ListProductsByPriceRange(command.Arguments, output); break; } default: { throw new ArgumentException("Invalid command: " + command.Name, "command"); } } }
private static void AddProductCommand(Catalog catalog, Command command, StringBuilder output) { catalog.Add(new Content() { Name = command.Parameters[(int) ContentItemTypes.Name], Price = double.Parse(command.Parameters[(int) ContentItemTypes.Price]), Producer = command.Parameters[(int) ContentItemTypes.Producer] }); output.AppendLine("Product added"); }
private void FindProductsByPriceRangeCommand(Catalog catalog, Command command, StringBuilder output) { var contentList = catalog.GetContentByPriceRange(double.Parse(command.Parameters[0]), double.Parse(command.Parameters[1])); if (contentList != null && contentList.Count() > 0) { foreach (var content in contentList) { output.AppendLine(content.ToString()); } } else { output.AppendLine("No products found"); } }
public static Command Parse(string value) { if (value == null) { throw new ArgumentNullException("value", "value cannot be null."); } int firstSpaceIndex = value.IndexOf(" "); string name = value.Substring(0, firstSpaceIndex); string argumentsList = value.Substring(firstSpaceIndex + 1).Trim(); string[] arguments = argumentsList.Split(new char[] { ';' }); Command command = new Command(name, arguments); return command; }
private static void DeleteProductsCommand(Catalog catalog, Command command, StringBuilder output) { int updatedCount = 0; if (command.Parameters.Length > 1) { updatedCount = catalog.Delete(command.Parameters[0], command.Parameters[1]); } else { updatedCount = catalog.Delete(string.Empty, command.Parameters[0]); } if (updatedCount > 0) { output.AppendLine(String.Format("{0} products deleted", updatedCount)); } else { output.AppendLine("No products found"); } }
public void ExecuteCommand(Catalog catalog, Command command, StringBuilder output) { switch (command.Type) { case CommandTypes.AddProduct: AddProductCommand(catalog, command, output); break; case CommandTypes.DeleteProducts: DeleteProductsCommand(catalog, command, output); break; case CommandTypes.FindProductsByName: FindProductsByNameCommand(catalog, command, output); break; case CommandTypes.FindProductsByPriceRange: FindProductsByPriceRangeCommand(catalog, command, output); break; case CommandTypes.FindProductsByProducer: FindProductsByProducerCommand(catalog, command, output); break; default: throw new ArgumentException("Unknown command: " + command.Type.ToString()); } }