public bool AddProduct(string productName, string price, string type)
        {
            if (this.productsMapByName.Contains(productName))
            {
                return false;
            }

            var product = new Product
            {
                Name = productName,
                Price = decimal.Parse(price),
                Type = type
            };

            if (!this.mapByType.ContainsKey(type))
            {
                this.mapByType[type] = new List<Product>();
            }

            this.productsMapByName.Add(product.Name);
            this.mapByType[type].Add(product);
            this.productsByPrice.Add(product);

            return true;
        }
        public IEnumerable<Product> FilterByPrice(
            decimal minPrice = Product.MinimmPrice,
            decimal maxPrice = Product.MaximumPrice,
            int count = 10)
        {
            var minProduct = new Product
            {
                Name = "testProduct",
                Type = "testType",
                Price = minPrice
            };

            var maxProduct = new Product
            {
                Name = "testProduct",
                Type = "testType",
                Price = maxPrice
            };

            var result = this.productsByPrice.Range(
                minProduct, true, maxProduct, true)
                .Take(count);

            return this.ApplyOrdering(result, count);
        }
        public string Add(Product product)
        {
            // check unique names
            if (productNames.Contains(product.Name))
            {
                return string.Format("Error: Product {0} already exists", product.Name);
            }

            productNames.Add(product.Name);

            // add by type
            if (!productsByType.ContainsKey(product.Type))
            {
                productsByType[product.Type] = new SortedSet<Product>();
            }

            this.productsByType[product.Type].Add(product);

            // add by price
            this.allPrices.Add(product.Price);

            if (!this.productsByPrice.ContainsKey(product.Price))
            {
                this.productsByPrice[product.Price] = new SortedSet<Product>();
            }

            this.productsByPrice[product.Price].Add(product);

            return string.Format("Ok: Product {0} added successfully", product.Name);
        }
Beispiel #4
0
        private static bool AddProduct(
            Product product,
            OrderedBag<Product> byPrice,
            Dictionary<string, OrderedBag<Product>> byType)
        {
            if (productNames.Contains(product.Name))
            {
                return false;
            }

            productNames.Add(product.Name);
            types.Add(product.Type);

            byPrice.Add(product);

            if (byType.ContainsKey(product.Type))
            {
                byType[product.Type].Add(product);
            }
            else
            {
                byType.Add(product.Type, new OrderedBag<Product> { product });
            }

            return true;
        }