예제 #1
0
        private string ProcessCommand(string input)
        {
            int    spaceIndex = input.IndexOf(' ');
            string command    = input.Substring(0, spaceIndex);

            string[] data     = input.Substring(spaceIndex + 1).Split(';');
            string   producer = "";
            string   name     = "";
            decimal  price    = 0M;
            string   result   = "";

            switch (command)
            {
            case "AddProduct":
                name     = data[0];
                price    = decimal.Parse(data[1]);
                producer = data[2];
                result   = shoppingCenter.AddProduct(name, price, producer);
                break;

            case "DeleteProducts":
                if (data.Length == 1)
                {
                    producer = data[0];
                    result   = shoppingCenter.DeleteProducts(producer);
                }
                else
                {
                    producer = data[1];
                    name     = data[0];
                    result   = shoppingCenter.DeleteProducts(name, producer);
                }
                break;

            case "FindProductsByName":
                name   = data[0];
                result = shoppingCenter.FindProductsByName(name);
                break;

            case "FindProductsByProducer":
                producer = data[0];
                result   = shoppingCenter.FindProductsByProducer(producer);
                break;

            case "FindProductsByPriceRange":
                decimal startPrice = decimal.Parse(data[0]);
                decimal endPrice   = decimal.Parse(data[1]);
                result = shoppingCenter.FindProductsByPriceRange(startPrice, endPrice);
                break;
            }

            return(result);
        }
        static void Main(string[] args)
        {
            int totalCommands = int.Parse(Console.ReadLine());

            var shoppingCenter = new ShoppingCenter();

            for (int i = 0; i < totalCommands; i++)
            {
                string[] commandLine = Console.ReadLine().Split(new char[] { ' ' }, 2);

                string[] parameters = commandLine[1].Split(';');

                switch (commandLine[0])
                {
                case "AddProduct":
                {
                    shoppingCenter.AddProduct(new Product(
                                                  name: parameters[0],
                                                  price: double.Parse(parameters[1]),
                                                  producer: parameters[2])
                                              );

                    result.AppendLine("Product added");
                    break;
                }

                case "DeleteProducts":
                {
                    int deletedCount = 0;

                    switch (parameters.Length)
                    {
                    case 1:
                    {
                        deletedCount = shoppingCenter.DeleteProducts(parameters[0]);
                        break;
                    }

                    case 2:
                    {
                        deletedCount = shoppingCenter.DeleteProducts(parameters[0], parameters[1]);
                        break;
                    }

                    default:
                        throw new ArgumentException("Invalid Delete Parameters");
                    }

                    if (deletedCount == 0)
                    {
                        result.AppendLine("No products found");
                        break;
                    }

                    result.AppendLine(string.Format("{0} products deleted", deletedCount));
                    break;
                }

                case "FindProductsByName":
                {
                    PrintProducts(shoppingCenter.FindProductsByName(parameters[0]));
                    break;
                }

                case "FindProductsByPriceRange":
                {
                    PrintProducts(shoppingCenter.FindProductsByPriceRange(
                                      min: double.Parse(parameters[0]),
                                      max: double.Parse(parameters[1])));
                    break;
                }

                case "FindProductsByProducer":
                {
                    PrintProducts(shoppingCenter.FindProductsByProducer(parameters[0]));
                    break;
                }

                default:
                    throw new ArgumentException("Invalid command!");
                }
            }

            Console.WriteLine(result.ToString().Trim());
        }