예제 #1
0
        static void Main(string[] args)
        {
            var coffee = Breakfast.PourCoffee();

            while (!coffee.Temperature.Equals("Cold"))
            {
                Console.WriteLine("Coffee temperature: " + coffee.Temperature);
            }
            Console.WriteLine("Coffee temperature: " + coffee.Temperature);

            var bacon = Breakfast.FryBacon(5);

            Breakfast.EatBacon(ref bacon, 2);

            var eggs = Breakfast.FryEggs(4);

            eggs.Print();

            var toasts = Breakfast.ToastBread(6);

            Breakfast.ApplyButter(toasts[1]);
            Breakfast.ApplyButter(toasts[4]);

            for (int i = 0; i < toasts.Length; i++)
            {
                var buttered = toasts[i].ButterApplied;
                var jamed    = toasts[i].JamApplied;

                Console.WriteLine("Butter on Toast #" + i + ": " + buttered);
                Console.WriteLine("Jam on Toast #" + i + ": " + jamed);
            }
        }
        public void TestApplyingToToast()
        {
            var toasts = Breakfast.ToastBread(2);

            Breakfast.ApplyButter(toasts[0]);
            Breakfast.ApplyJam(toasts[1]);

            Assert.False(toasts[0].JamApplied);
            Assert.False(toasts[1].ButterApplied);
            Assert.True(toasts[1].JamApplied);
            Assert.True(toasts[0].ButterApplied);
        }
        public void TestToastingBreadLength()
        {
            var toasts = Breakfast.ToastBread(2);

            Assert.Equal(2, toasts.Count);
        }
        static async Task Main(string[] args)
        {
            DateTime startTime;
            DateTime endTime;

            Console.WriteLine("Aufgabe Breakfast");

            if (true)
            {
                Console.WriteLine();
                Console.WriteLine("-----------------------------");
                Console.WriteLine("--- Make normal Breakfast ---");
                Console.WriteLine("-----------------------------");

                startTime = System.DateTime.Now;

                var coffee      = Breakfast.PourCoffee();
                var eggs        = Breakfast.FryEggs(4);
                var bacon       = Breakfast.FryBacon(10);
                var toastBreads = Breakfast.ToastBread(3);

                foreach (var toastBread in toastBreads)
                {
                    Breakfast.ApplyButter(toastBread);
                }

                foreach (var toastBread in toastBreads)
                {
                    Breakfast.ApplyJam(toastBread);
                }

                var juice = Breakfast.PourJuice();

                endTime = System.DateTime.Now;

                var cookingTime = endTime - startTime;

                Console.WriteLine("*** Breakfast is finish!!! It took " + cookingTime + " (hh:mm:ss.ms)");
            }

            if (true)
            {
                Console.WriteLine();
                Console.WriteLine("----------------------------");
                Console.WriteLine("--- Make async Breakfast ---");
                Console.WriteLine("----------------------------");

                startTime = System.DateTime.Now;

                var coffee2   = Breakfast.PourCoffee();// PureCoffe();
                var eggsTask  = Breakfast.FryEggsAsync(4);
                var baconTask = Breakfast.FryBaconAsync(10);
                var toastTask = MakeToastWithButterAndJamAsync(3);

                var breakfastTasks = new List <Task> {
                    eggsTask, baconTask, toastTask
                };
                while (breakfastTasks.Count > 0)
                {
                    Task finishedTask = await Task.WhenAny(breakfastTasks);

                    if (finishedTask == eggsTask)
                    {
                        Console.WriteLine("eggs are ready");
                    }
                    else if (finishedTask == baconTask)
                    {
                        Console.WriteLine("bacon is ready");
                    }
                    else if (finishedTask == toastTask)
                    {
                        Console.WriteLine("toast is ready");
                    }
                    breakfastTasks.Remove(finishedTask);
                }

                Juice juice = Breakfast.PourJuice();

                endTime = System.DateTime.Now;
                var cookingTime = endTime - startTime;

                Console.WriteLine("*** Async Breakfast is finish!!! It took " + cookingTime + " (hh:mm:ss.ms)");
            }

            Console.WriteLine();
            Console.WriteLine("press enter to start Parallel ForEach");
            Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("------------------------");
            Console.WriteLine("--- Parallel ForEach ---");
            Console.WriteLine("------------------------");

            int length = 1000;

            int[] numbers = new int[length];

            for (int i = 0; i < length; i++)
            {
                numbers[i] = i;
            }

            int counter = 0;

            Parallel.ForEach(numbers, (x) =>
            {
                counter++;
                Console.WriteLine("number: " + x + " - counter: " + counter);
            });

            Console.ReadLine();
        }