Example #1
0
        private static void AddProduct(string name, string producer, string stringPrice)
        {
            var price = decimal.Parse(stringPrice);
            var currentProduct = new Product(name, producer, price);

            if (!productsByName.ContainsKey(name))
            {
                productsByName[name] = new OrderedBag<Product>();
            }
            productsByName[name].Add(currentProduct);

            if (!productsByProducer.ContainsKey(producer))
            {
                productsByProducer[producer] = new OrderedBag<Product>();
            }
            productsByProducer[producer].Add(currentProduct);

            if (!productsByPrice.ContainsKey(price))
            {
                productsByPrice[price] = new OrderedBag<Product>();
            }
            productsByPrice[price].Add(currentProduct);

            Console.WriteLine(AddedMessage);
        }
Example #2
0
 private static void ExecuteAddCommand(string token)
 {
     string[] current = token.Split(';');
     var name = current[0];
     decimal price = Math.Round(decimal.Parse(current[1], CultureInfo.InvariantCulture), 2);
     var producer = current[2];
     var newProduct = new Product(name, price, producer);
     priceDict.Add(price, newProduct);
     nameDict.Add(name, newProduct);
     prodDict.Add(producer, newProduct);
     output.Append("Product added");
     output.Append(Environment.NewLine);
 }
Example #3
0
        public static void Main()
        {
            var numberOfLines = int.Parse(Console.ReadLine());

            for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
            {
                var currentLine = Console.ReadLine();
                var indexOfFirstSpace = currentLine.IndexOf(' ');
                var currentComand = currentLine.Substring(0, indexOfFirstSpace);
                var currentParameters = currentLine.Substring(indexOfFirstSpace + 1).Split(';');

                if (currentComand == "AddProduct")
                {
                    var productForAdd = new Product()
                    {
                        Name = currentParameters[0],
                        Price = decimal.Parse(currentParameters[1]),
                        Producer = currentParameters[2]
                    };

                    if (!productsByName.ContainsKey(productForAdd.Name))
                    {
                        productsByName.Add(productForAdd.Name, new List<Product>());
                    }
                    productsByName[productForAdd.Name].Add(productForAdd);

                    if (!productsByProducer.ContainsKey(productForAdd.Producer))
                    {
                        productsByProducer.Add(productForAdd.Producer, new List<Product>());
                    }
                    productsByProducer[productForAdd.Producer].Add(productForAdd);

                    var nameProducerKey = productForAdd.Name + productForAdd.Producer;
                    if (!productsByNameAndProducer.ContainsKey(nameProducerKey))
                    {
                        productsByNameAndProducer.Add(nameProducerKey, new List<Product>());
                    }
                    productsByNameAndProducer[nameProducerKey].Add(productForAdd);

                    if (!productsByPrice.ContainsKey(productForAdd.Price))
                    {
                        productsByPrice.Add(productForAdd.Price, new List<Product>());
                    }
                    productsByPrice[productForAdd.Price].Add(productForAdd);

                    sb.AppendLine("Product added");
                }
                else if (currentComand == "DeleteProducts")
                {
                    if (currentParameters.Length == 1)
                    {
                        var producerName = currentParameters[0];
                        if (productsByProducer.ContainsKey(producerName))
                        {
                            var deletedProducts = productsByProducer[producerName];

                            foreach (var deletedProduct in deletedProducts)
                            {
                                productsByName[deletedProduct.Name].Remove(deletedProduct);
                                productsByNameAndProducer[deletedProduct.Name + deletedProduct.Producer].Remove(deletedProduct);
                                productsByPrice[deletedProduct.Price].Remove(deletedProduct);
                            }

                            if (deletedProducts.Count > 0)
                            {
                                sb.AppendLine(string.Format("{0} products deleted", deletedProducts.Count));
                                productsByProducer.Remove(producerName);
                            }
                            else
                            {
                                sb.AppendLine(NotFoundMessage);
                            }
                        }
                        else
                        {
                            sb.AppendLine(NotFoundMessage);
                        }
                    }
                    else
                    {
                        var productName = currentParameters[0];
                        var producerName = currentParameters[1];

                        var nameProducerKey = productName + producerName;
                        if (productsByNameAndProducer.ContainsKey(nameProducerKey))
                        {
                            var deletedProducts = productsByNameAndProducer[nameProducerKey];

                            foreach (var deletedProduct in deletedProducts)
                            {
                                productsByName[deletedProduct.Name].Remove(deletedProduct);
                                productsByProducer[deletedProduct.Producer].Remove(deletedProduct);
                                productsByPrice[deletedProduct.Price].Remove(deletedProduct);
                            }

                            if (deletedProducts.Count > 0)
                            {
                                sb.AppendLine(string.Format("{0} products deleted", deletedProducts.Count));

                                productsByNameAndProducer.Remove(productName + producerName);
                            }
                            else
                            {
                                sb.AppendLine(NotFoundMessage);
                            }
                        }
                        else
                        {
                            sb.AppendLine(NotFoundMessage);
                        }
                    }
                }
                else if (currentComand == "FindProductsByName")
                {
                    if (productsByName.ContainsKey(currentParameters[0]))
                    {
                        var result = productsByName[currentParameters[0]];
                        if (result.Count() > 0)
                        {
                            PrintProducts(result);
                        }
                        else
                        {
                            sb.AppendLine(NotFoundMessage);
                        }
                    }
                    else
                    {
                        sb.AppendLine(NotFoundMessage);
                    }
                }
                else if (currentComand == "FindProductsByPriceRange")
                {
                    decimal minPrice = decimal.Parse(currentParameters[0]);
                    decimal maxPrice = decimal.Parse(currentParameters[1]);

                    var keys = productsByPrice.Keys.Where(k => minPrice <= k && k <= maxPrice);
                    List<Product> result = new List<Product>();

                    foreach (var key in keys)
                    {
                        foreach (var product in productsByPrice[key])
                        {
                            result.Add(product);
                        }
                    }

                    if (result.Count > 0)
                    {
                        PrintProducts(result);
                    }
                    else
                    {
                        sb.AppendLine(NotFoundMessage);
                    }
                }
                else if (currentComand == "FindProductsByProducer")
                {
                    if (productsByProducer.ContainsKey(currentParameters[0]))
                    {
                        var result = productsByProducer[currentParameters[0]];
                        if (result.Count > 0)
                        {
                            PrintProducts(result);
                        }
                        else
                        {
                            sb.AppendLine(NotFoundMessage);
                        }
                    }
                    else
                    {
                        sb.AppendLine(NotFoundMessage);
                    }
                }
            }

            Console.WriteLine(sb.ToString());
        }
Example #4
0
        public static void Main()
        {
            var numberOfLines = int.Parse(Console.ReadLine());

            for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
            {
                var currentLine = Console.ReadLine();
                var indexOfFirstSpace = currentLine.IndexOf(' ');
                var currentComand = currentLine.Substring(0, indexOfFirstSpace);
                var currentParameters = currentLine.Substring(indexOfFirstSpace + 1).Split(';');

                if (currentComand == "AddProduct")
                {
                    var productForAdd = new Product()
                    {
                        Name = currentParameters[0],
                        Price = decimal.Parse(currentParameters[1]),
                        Producer = currentParameters[2]
                    };

                    productsByName.Add(productForAdd.Name, productForAdd);
                    productsByProducer.Add(productForAdd.Producer, productForAdd);
                    productsByNameAndProducer.Add(productForAdd.Name + productForAdd.Producer, productForAdd);
                    productsByPrice.Add(productForAdd.Price, productForAdd);

                    sb.AppendLine("Product added");
                    //Console.WriteLine("Product added");
                }
                else if (currentComand == "DeleteProducts")
                {
                    if (currentParameters.Length == 1)
                    {
                        DeleteByProducer(currentParameters[0]);
                    }
                    else
                    {
                        DeleteByNameAndProducer(currentParameters[0], currentParameters[1]);
                    }
                }
                else if (currentComand == "FindProductsByName")
                {
                    var result = productsByName[currentParameters[0]];
                    PrintProducts(result);
                }
                else if (currentComand == "FindProductsByPriceRange")
                {
                    decimal minPrice = decimal.Parse(currentParameters[0]);
                    decimal maxPrice = decimal.Parse(currentParameters[1]);

                    var keys = productsByPrice.Keys.Where(k => minPrice <= k && k <= maxPrice);
                    List<Product> result = new List<Product>();

                    foreach (var key in keys)
                    {
                        foreach (var product in productsByPrice[key])
                        {
                            result.Add(product);
                        }
                    }

                    PrintProducts(result);

                    //PrintProducts(productsByPrice
                    //    .Where(p => minPrice <= p.Key && p.Key <= maxPrice)
                    //    .SelectMany(p => p.Value));
                }
                else if (currentComand == "FindProductsByProducer")
                {
                    var result = productsByProducer[currentParameters[0]];
                    PrintProducts(result);
                }
            }

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