示例#1
0
        static void BucketScenario()
        {
            Console.Title = "Bucket Scenario";

            Bucket bucket1 = new Bucket();

            Console.WriteLine("Creating default bucket:");
            Console.WriteLine(bucket1);

            WaitAndClear();

            Console.WriteLine("Attempting to create a bucket that is smaller than 10:");
            try
            {
                Bucket wrongBucket = new Bucket(8);
            }
            catch (ContainerException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }

            WaitAndClear();

            Bucket bucket2 = new Bucket(10, 5);

            Console.WriteLine($"Creating a bucket of size {bucket2.Size} that has {bucket2.Volume} units in it:");
            Console.WriteLine(bucket2);

            WaitAndClear();

            Bucket bucket3 = new Bucket(20, 5);

            Console.WriteLine($"Creating a bucket of size {bucket3.Size} that has {bucket3.Volume} units in it:");
            Console.WriteLine(bucket3);

            WaitAndClear();

            Console.WriteLine($"Adding to the first bucket until it is full.");
            Console.WriteLine($"{bucket1}");
            bucket1.Full += Container_Full;
            while (!bucket1.IsFull)
            {
                //Make sure we only write the "current" bucket1
                Console.CursorTop -= 3;
                Console.CursorLeft = 0;
                //Add 1 to the bucket and print.
                bucket1.Fill(1);
                Console.WriteLine($"{bucket1}");
                Thread.Sleep(500);
            }

            WaitAndClear();

            bucket3.Full += Container_Full;
            Console.WriteLine("Filling with another bucket:");
            Console.WriteLine($"Filling\n\n{bucket3}\n\nWith\n\n{bucket1}\n");
            bucket3.FillWith(bucket1);
            Console.WriteLine($"Filled bucket:\n\n{bucket3}\n\nEmptied bucket:\n\n{bucket1}");

            WaitAndClear();

            Console.WriteLine("Filling while not accepting overflow:");
            bucket3.AcceptOverflow = false;
            Console.WriteLine($"Filling\n\n{bucket3}\n\nWith\n\n{bucket2}\n");
            bucket3.FillWith(bucket2);
            Console.WriteLine($"Filled bucket:\n\n{bucket3}\n\nEmptied bucket:\n\n{bucket2}");

            WaitAndClear();

            Console.WriteLine("Checking how much empty units are left in a bucket:");
            Console.WriteLine($"{bucket2}\nHas {bucket2.Remainder} empty units left");

            WaitAndClear();

            Console.WriteLine("Filling while accepting overflow:");
            bucket3.AcceptOverflow = true;
            Console.WriteLine($"Filling\n\n{bucket3}\n\nWith\n\n{bucket2}\n");
            bucket3.FillWith(bucket2);
            Console.WriteLine($"Filled bucket:\n\n{bucket3}\n\nEmptied bucket:\n\n{bucket2}");

            WaitAndClear();

            Console.WriteLine("Emptying a bucket till it is empty");
            Console.WriteLine(bucket3);
            while (!bucket3.IsEmpty)
            {
                //Make sure we only write the "current" bucket1
                if (bucket3.Volume == 10)
                {
                    Console.CursorTop -= 1;
                    Console.CursorLeft = 0;
                    Console.Write(new string(' ', Console.WindowWidth));
                    Console.CursorTop += 1;
                }
                Console.CursorTop -= 3;
                Console.CursorLeft = 0;
                //Remove 1 from the bucket and print.
                bucket3.Empty(1);
                Console.WriteLine($"{bucket3}");
                Thread.Sleep(500);
            }

            WaitAndClear();

            bucket2.Volume = bucket2.Size;
            Console.WriteLine("Refilling one bucket to full and then emptying it with Empty()");
            Console.WriteLine($"Before:\n{bucket2}");
            bucket2.Empty();
            Console.WriteLine($"After:\n{bucket2}");
        }