Exemplo n.º 1
0
        private static string ExecuteCommand(string commandName, string[] command, ProductsCollection collection)
        {
            string[] cmdValues = new string[command.Length - 1];
            for (int i = 0; i < cmdValues.Length; i++)
            {
                cmdValues[i] = command[i + 1];
            }
            string result = string.Empty;

            switch (commandName)
            {
            case "AddProduct":
                result = collection.AddProduct(cmdValues);
                break;

            case "DeleteProducts":
                int deleted = 0;

                if (cmdValues.Length == 1)
                {
                    deleted = collection.DeleteProductByProducer(cmdValues[0]);
                }
                else
                {
                    deleted = collection.DeleteProductByName(cmdValues[0], cmdValues[1]);
                }

                result = String.Format("{0} products deleted", deleted);
                break;

            case "FindProductsByName":
                List <Product> foundProductsName = collection.FindProductsByName(cmdValues[0]);

                result = ConvertResultsToString(foundProductsName);

                break;

            case "FindProductsByProducer":
                List <Product> foundProductsProducer = collection.FindProductsByProducer(cmdValues[0]);

                result = ConvertResultsToString(foundProductsProducer);

                break;

            case "FindProductsByPriceRange":
                double from = double.Parse(cmdValues[0]);
                double to   = double.Parse(cmdValues[1]);

                List <Product> foundProductsRange = collection.FindProductsByPriceRange(from, to);

                result = ConvertResultsToString(foundProductsRange);

                break;

            default:
                break;
            }

            return(result);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int           commandsNum    = int.Parse(Console.ReadLine());
            int           currCommandNum = 0;
            StringBuilder results        = new StringBuilder();

            ProductsCollection collection = new ProductsCollection();
            StringBuilder      input      = new StringBuilder();

            while (currCommandNum < commandsNum)
            {
                string commandInput = Console.ReadLine();
                input.Append(commandInput);
                string[] command = ParseCommand(commandInput);
                string   result  = ExecuteCommand(command[0], command, collection);

                results.Append(String.Format("{0}\n", result));
                currCommandNum++;
            }

            Console.WriteLine(results.ToString());
        }