예제 #1
0
        static void Main(string[] args)
        {
            // creating a leaf gift
            var phone = new SingleGift("Phone", 256);

            // calculating a leaf
            phone.CalculateTotalPrice();
            phone.CalculateTotalPrice();
            Console.WriteLine();

            //composite gift
            var rootBox  = new CompositeGift("RootBox", 0);
            var truckToy = new SingleGift("TruckToy", 289);
            var plainToy = new SingleGift("PlainToy", 587);

            // adding leafs to the composite
            rootBox.Add(truckToy);
            rootBox.Add(plainToy);
            // calculating the branch from our composite
            Console.WriteLine($"Total price of this composite present is: {rootBox.CalculateTotalPrice()}");
            Console.WriteLine($"Total price of this composite present is: {CalculateTotalPriceRecursion(rootBox)}");
            Console.WriteLine();
            var childBox   = new CompositeGift("ChildBox", 0);
            var soldierToy = new SingleGift("SoldierToy", 200);

            childBox.Add(soldierToy);
            // calculating the branch from our composite
            Console.WriteLine($"Total price of this composite present is: {childBox.CalculateTotalPrice()}");
            Console.WriteLine($"Total price of this composite present is: {CalculateTotalPriceRecursion(childBox)}");
            Console.WriteLine();

            // adding composite to our composite
            rootBox.Add(childBox);

            // calculating the tree branch from our composite
            Console.WriteLine($"Total price of this composite present is: {rootBox.CalculateTotalPrice()}");
            Console.WriteLine($"Total price of this composite present is: {CalculateTotalPriceRecursion(rootBox)}");

            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var phone = new SingleGift("Phone", 256);

            phone.CalculateTotalPrice();

            Console.WriteLine();

            var rootBox = new CompositeGift("RootBox", 0);

            var truckToy = new SingleGift("Truck Toy", 289);
            var plainToy = new SingleGift("Plain Toy", 587);

            rootBox.Add(truckToy);
            rootBox.Add(plainToy);

            var childBox   = new CompositeGift("ChildBox", 0);
            var soldierToy = new SingleGift("Soldier Toy", 200);

            childBox.Add(soldierToy);
            rootBox.Add(childBox);

            Console.WriteLine($"Total price of this component present is: {rootBox.CalculateTotalPrice()}");
        }