Example #1
0
        static void Main(string[] args)
        {
            var items = new[]
                {
                    new After.ConcreteItem {Name = "one", Price = (decimal) 3.5, Category = "fun"},
                    new After.ConcreteItem {Name = "two", Price = (decimal) 10.1, Category = "notfun"},
                    new After.ConcreteItem {Name = "three", Price = (decimal) 1.1, Category = "chłam"}
                };

            var register = new After.CashRegister(new After.TaxCalculator());
            register.PrintBill(items);

            Console.WriteLine();

            var comparer =
                Comparer<After.ConcreteItem>.Create((ix, iy) => Comparer.Default.Compare(ix.Category, iy.Category));
            register.PrintBill(items, comparer);
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Code which does not respect OCP");
            Before.Item[] beforeItems = new Before.Item[5];
            beforeItems[0] = new Before.Item("Bread", 3.49m, "Baked goods");
            beforeItems[1] = new Before.Item("Panini", 4.49m, "Baked goods");
            beforeItems[2] = new Before.Item("Tiger", 3.99m, "Other");
            beforeItems[3] = new Before.Item("Cheese", 2.49m, "Milk products");
            beforeItems[4] = new Before.Item("Milk", 1.79m, "Milk products");

            Before.CashRegister cashRegister = new Before.CashRegister();
            Console.WriteLine(cashRegister.PrintBill(beforeItems));

            Console.WriteLine("\n\nCode which respects OCP");
            After.TaxBakedGoods   bakedGoods   = new After.TaxBakedGoods();
            After.TaxMilkProducts milkProducts = new After.TaxMilkProducts();
            After.TaxOther        other        = new After.TaxOther();

            After.AlphabeticalBillPrinter alphabeticalBillPrinter = new After.AlphabeticalBillPrinter();
            After.NormalBillPrinter       normalBillPrinter       = new After.NormalBillPrinter();

            After.Item[] afterItems = new After.Item[5];
            afterItems[0] = new After.Item("Bread", 3.49m, bakedGoods);
            afterItems[1] = new After.Item("Panini", 4.49m, bakedGoods);
            afterItems[2] = new After.Item("Tiger", 3.99m, other);
            afterItems[3] = new After.Item("Cheese", 2.49m, milkProducts);
            afterItems[4] = new After.Item("Milk", 1.79m, milkProducts);

            After.CashRegister cashRegisterAlphabetical = new After.CashRegister(alphabeticalBillPrinter);
            After.CashRegister cashRegisterNormal       = new After.CashRegister(normalBillPrinter);

            Console.WriteLine("NORMAL BILL");
            Console.WriteLine(cashRegisterNormal.PrintBill(afterItems));
            Console.WriteLine("ALPHABETICALLY SORTED BILL");
            Console.WriteLine(cashRegisterAlphabetical.PrintBill(afterItems));
        }