Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Product[] products = new List <Product>
            {
                new Product("Apple", Color.Green, Size.Small),
                new Product("Tree", Color.Green, Size.Large),
                new Product("House", Color.Blue, Size.Large)
            }.ToArray();

            var filter = new ProductFilter();

            Console.WriteLine("Green products:");
            foreach (Product product in filter.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine(product);
            }
            Console.WriteLine();
            Console.WriteLine("Large products:");
            foreach (Product product in filter.Filter(products, new SizeSpecification(Size.Large)))
            {
                Console.WriteLine(product);
            }
            Console.WriteLine();
            Console.WriteLine("Large green products:");
            foreach (Product product in filter.Filter(products,
                                                      new AndSpecification <Product>(
                                                          new ColorSpecification(Color.Green),
                                                          new SizeSpecification(Size.Large))))
            {
                Console.WriteLine(product);
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <Product> GetSizeSmall()
        {
            var filterSize       = new ProductFilter();
            var productSizeSmall = filterSize.Filter(productList, new SizeSpecification(Size.Small))
                                   .GetListProduct();

            return(productSizeSmall);
        }
Ejemplo n.º 3
0
        public IEnumerable <Product> GetColorBlue()
        {
            var filterColor      = new ProductFilter();
            var productColorBlue = filterColor.Filter(productList, new ColorSpecification(Color.Blue))
                                   .GetListProduct();

            return(productColorBlue);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var prodA = new Product("A", Color.Blue, Size.Small);
            var prodB = new Product("B", Color.Red, Size.Medium);
            var prodC = new Product("C", Color.Red, Size.Large);
            var prodD = new Product("D", Color.Blue, Size.Small);

            var products = new Product[] { prodA, prodB, prodC, prodD };

            var filter = new ProductFilter();

            WriteLine("Red products:");
            var redProdFilter = filter.Filter(products, new ColorSpecification(Color.Red));

            foreach (var item in redProdFilter)
            {
                WriteLine($"{item.GetName()} {item.GetColor()} {item.GetSize()}");
            }

            WriteLine("Blue and Small products:");
            var blueAndSmallProdFilter = filter.Filter(products,
                                                       new AndSpecification <Product>(
                                                           new ColorSpecification(Color.Blue),
                                                           new SizeSpecification(Size.Small)
                                                           )
                                                       );

            foreach (var item in blueAndSmallProdFilter)
            {
                WriteLine($"{item.GetName()} {item.GetColor()} {item.GetSize()}");
            }

            // Result
            // -----------------------
            // Red products:
            // B Red Medium
            // C Red Large
            //
            // Blue and Small products:
            // A Blue Small
            // D Blue Small
        }
Ejemplo n.º 5
0
        public IEnumerable <Product> GetSizeMediumAndColorBlack()
        {
            var filterSmallAndBlue   = new ProductFilter();
            var productMediumAndBlue = filterSmallAndBlue.Filter(productList, new AndSpecification(
                                                                     new List <ISpecification <ProductManage> > {
                new ColorSpecification(Color.Black), new SizeSpecification(Size.Midium)
            })
                                                                 )
                                       .GetListProduct();

            return(productMediumAndBlue);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            var desk = new Product("Desk", Size.Medium, Color.Yellow);
            var car  = new Product("Car", Size.Large, Color.Yellow);
            var tv   = new Product("Tv", Size.Medium, Color.Green);

            Product[] products = { desk, car, tv };

            var productFilter = new ProductFilter();

            foreach (var product in productFilter.Filter(products, new ColorSpecification(Color.Yellow)))
            {
                Console.WriteLine($" {product.Name} color is Yellow");
            }

            foreach (var product in productFilter.Filter(products, new SizeSpecification(Size.Medium)))
            {
                Console.WriteLine($" {product.Name} size is Medium");
            }

            foreach (var product in productFilter.Filter(products, new AndSpecification(Size.Medium, Color.Green)))
            {
                Console.WriteLine($" {product.Name} color is Green and size is Medium");
            }

            #region PreventClose

            if (!Debugger.IsAttached)
            {
                return;
            }
            Console.WriteLine("Press any key to continue . . .");
            Console.ReadLine();

            #endregion
        }
Ejemplo n.º 7
0
        public void TestFilter_ByName()
        {
            var products = new[] { new Product {
                                       Name = "test1"
                                   }, new Product {
                                       Name = "test2"
                                   }, }.AsQueryable();
            var filter        = new ProductFilter();
            var filteredUsers = filter.Filter(products, new ProductFilterOptions {
                Name = "2"
            }).ToArray();

            Assert.AreEqual(1, filteredUsers.Length);
            Assert.AreEqual("test2", filteredUsers.Single().Name);
        }
Ejemplo n.º 8
0
        public void ProductFilterColorBlue_IsSuccess()
        {
            var productFilter = new ProductFilter();
            var colorSpecify  = new ColorSpecification(Color.Blue);
            var data          = new List <ProductManage>()
            {
                new ProductManage("P1", Color.Blue, Size.Large, 1200),
                new ProductManage("P2", Color.Yellow, Size.Large, 1200)
            };

            var listDatas = productFilter.Filter(data, colorSpecify);

            Assert.True(listDatas.Count() > 0);
            foreach (var checkData in listDatas)
            {
                Assert.Equal(Color.Blue, checkData.color);
            }
        }
Ejemplo n.º 9
0
        private static void FilterList(IEnumerable <Product> products, string searchBy, string searchKey)
        {
            ISpecification <Product> spec = null;
            var productFilter             = new ProductFilter();

            switch (searchBy.ToLower())
            {
            case "category":
                spec = new CategorySpecification(searchKey);
                break;

            case "color":
                spec = new ColorSpecification(searchKey);
                break;
            }
            Console.WriteLine("\n---------Filtered List------------\n");

            foreach (var item in productFilter.Filter(products, spec))
            {
                Console.WriteLine($"Product: {item.Name}. Category: {item.Category}. Color: {item.Color}");
            }

            Console.WriteLine("\n---------End of List--------------");
        }