Exemple #1
0
        public static void Main()
        {
            var market = new OnlineMarket();
            var result = new StringBuilder();

            while (true)
            {
                var line = Console.ReadLine().Split(' ');
                if (line[0] == "end")
                {
                    return;
                }

                if (line[0] == "add")
                {
                    Console.WriteLine(market.Add(line[1], double.Parse(line[2]), line[3]));
                }
                else if (line[0] == "filter")
                {
                    if (line[2] == "type")
                    {
                        Console.WriteLine(market.FilterByType(line[3]));
                    }
                    else if (line.Length == 7)
                    {
                        Console.WriteLine(market.FilterByPriceFromTo(double.Parse(line[4]), double.Parse(line[6])));
                    }
                    else if (line[3] == "from")
                    {
                        Console.WriteLine(market.FilterByPriceFromTo(double.Parse(line[4]), double.MaxValue));
                    }
                    else
                    {
                        Console.WriteLine(market.FilterByPriceFromTo(double.MinValue, double.Parse(line[4])));
                    }
                }
            }
        }
Exemple #2
0
        static void Main()
        {
            var market = new OnlineMarket();

            while (true)
            {
                string[] command = Console.ReadLine().Split(' ');

                if (command[0] == "end")
                {
                    break;
                }

                switch (command[0])
                {
                case "add":
                {
                    var product = new Product(command[1], double.Parse(command[2]), command[3]);

                    var withThisName = market.FindProductsByName(command[1]);
                    if (withThisName.Count() != 0)
                    {
                        result.AppendLine(string.Format("Error: Product {0} already exists", command[1]));
                        break;
                    }

                    market.AddProduct(product);

                    result.AppendLine(string.Format("Ok: Product {0} added successfully", command[1]));
                    break;
                }

                case "filter":
                {
                    switch (command[2])
                    {
                    case "type":
                    {
                        // filter by type PRODUCT_TYPE
                        var products = market.FindProductsByType(command[3]);
                        PrintProductsByType(products, command[3]);
                        break;
                    }

                    case "price":
                    {
                        if (command.Length == 7)
                        {
                            // filter by price from MIN_PRICE to MAX_PRICE
                            double min      = double.Parse(command[4]);
                            double max      = double.Parse(command[6]);
                            var    products = market.FindProductsByPriceRange(min, max);
                            PrintProducts(products);
                            break;
                        }
                        else
                        {
                            if (command[3] == "from")
                            {
                                // filter by price from MIN_PRICE
                                double min      = double.Parse(command[4]);
                                var    products = market.FindProductsByMinPrice(min);
                                PrintProducts(products);
                                break;
                            }
                            else
                            {
                                // filter by price to MAX_PRICE
                                double max      = double.Parse(command[4]);
                                var    products = market.FindProductsByMaxPrice(max);
                                PrintProductsToMaxPrice(products);
                                break;
                            }
                        }
                    }

                    default:
                        break;
                    }
                    break;
                }

                default:
                    break;
                }
            }

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