示例#1
0
        static void old_Main(string[] args)
        {
            int exeptionCounter = 0;

            try
            {
                // инициализация объектов
                var stone1 = new Stone {
                    Color = "Белый", Price = 20, Status = ProductStatus.Ready, Weight = 100
                };
                var pStone1 = new PreciousStone {
                    Color = "Желтый", Price = 40, Status = ProductStatus.Ready, Weight = 40
                };
                var ruby1 = new Ruby {
                    Price = 57, Status = ProductStatus.None, Weight = 30
                };
                var diamond1 = new Diamond {
                    Price = 89, Status = ProductStatus.Ready, Weight = 28
                };
                //var spStone1 = new SemiPreciousStone { Color = "Зелёный", Price = 30, Status = ProductStatus.None, Weight = 41 };
                //var nefrit1 = new Nefrit { Price = 50, Status = ProductStatus.Ready, Weight = 44 };

                List <Stone> stones = new List <Stone> {
                    stone1,
                    // spStone1,
                    pStone1,
                    //nefrit1,
                    ruby1, diamond1
                };
                var necklace   = new Necklace(stones);
                var controller = new NecklaceController(necklace);


                Console.WriteLine("Тест диапазона (ArgumentRangeException)");
                try { controller.GetStonesByOpacity(3, 1); }
                catch (Exception e)
                {
                    exeptionCounter++;
                    Console.WriteLine($"Обработано исключение: {e}");
                }

                Console.WriteLine("\nТест отрицательного аргумента (ArgumentRangeException)");
                try { diamond1.DamageTest(-10); }
                catch (Exception e)
                {
                    exeptionCounter++;
                    Console.WriteLine($"Обработано исключение: {e}");
                }

                Console.WriteLine("\nТест на пустую или null-строку (StringNullOrEmptyException)");
                try
                {
                    ruby1.Color = null;
                    ruby1.Color = "";
                }
                catch (Exception e)
                {
                    exeptionCounter++;
                    Console.WriteLine($"Обработано исключение: {e}");
                }

                Console.WriteLine("\nТест на вхождение индекса в диапазон (IndexOutOfRangeException  - стандартное исключение)");
                try { necklace[10] = new Stone(); }
                catch (IndexOutOfRangeException e)
                {
                    exeptionCounter++;
                    Console.WriteLine($"Обработано исключение: {e}");
                }

                Console.WriteLine("\nТест на null-аргумент (ArgumentNullException - стандартное исключение)");
                try { necklace.Add(null); }
                catch (ArgumentNullException e)
                {
                    exeptionCounter++;
                    Console.WriteLine($"Обработано исключение: {e}");
                }

                Console.WriteLine("Тест Debug.Assert");
                necklace.AddRange(new List <Stone>());
            }
            catch (Exception e)
            {
                exeptionCounter++;
                Console.WriteLine($"Произошло непредвиденное исключение: {e}");
            }
            finally
            {
                Console.WriteLine($"\nПроизошло {exeptionCounter} исключительных ситуаций");
                Console.WriteLine("Программа завершает работу...");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            // инициализация объектов
            var stone1 = new Stone {
                Color = "Белый", Price = 20, Status = ProductStatus.Ready, Weight = 100
            };
            var pStone1 = new PreciousStone {
                Color = "Желтый", Price = 40, Status = ProductStatus.Ready, Weight = 40
            };
            var ruby1 = new Ruby {
                Price = 57, Status = ProductStatus.None, Weight = 30
            };
            var diamond1 = new Diamond {
                Price = 89, Status = ProductStatus.Ready, Weight = 28
            };
            var spStone1 = new SemiPreciousStone {
                Color = "Зелёный", Price = 30, Status = ProductStatus.None, Weight = 41
            };
            var nefrit1 = new Nefrit {
                Price = 50, Status = ProductStatus.Ready, Weight = 44
            };

            Product[] products = { stone1, pStone1, ruby1, diamond1, spStone1, nefrit1 };

            var printer = new Printer();

            // тест класса Printer
            Console.WriteLine("Printer:");
            foreach (var product in products)
            {
                printer.IamPrinting(product);
            }

            // тест абстрактного класса
            Console.WriteLine("\nТест абстрактного класса:");
            Product pr_stone = stone1, pr_pStone = pStone1, pr_spStone = spStone1;

            Console.WriteLine($"Для камня {pr_stone.DefineMarket()}");
            Console.WriteLine($"Для драгоценного камня {pr_pStone.DefineMarket()}");
            Console.WriteLine($"Для полудрагоценного камня {pr_spStone.DefineMarket()}");

            // тест виртуального метода
            Console.WriteLine("\nТест виртуального метода:");
            Stone s_diamond = diamond1, s_ruby = ruby1;

            s_diamond.DamageTest(2000);
            s_ruby.DamageTest(2000);
            stone1.DamageTest(2000);

            // тест интефейсов
            //1 способ
            Console.WriteLine("\nТест интерфесов:");
            var rubyWorkshop = ruby1 as IJeweleryWorkshop;     //не вызывает исключений
            var rubyReseller = ruby1 as IJeweleryReseller;

            // 2 метода с одинаковым названием (но от разных интефейсов)
            rubyWorkshop.ProcessStone(); // вызываем ProcessStone() от IJeweleryWorkshop
            rubyReseller.ProcessStone(); // вызываем ProcessStone() от IJeweleryReseller

            //2 способ
            ((IJeweleryWorkshop)ruby1).ProcessStone();        //может вызвать исключения
            ((IJeweleryReseller)ruby1).ProcessStone();

            nefrit1.MakeEarrings();  // другие объекты, реализующие IJeweleryWorkshop
            diamond1.MakeRing();


            Console.WriteLine("\nЗаказ товаров:");
            Console.WriteLine($"Алмаз: статус {diamond1.StatusDescription}; Рубин: статус {ruby1.StatusDescription}");
            Console.WriteLine("Осуществление заказа...");
            diamond1.Order();
            ruby1.Order();
            Console.WriteLine($"Алмаз: статус {diamond1.StatusDescription}; Рубин: статус {ruby1.StatusDescription}");


            //Итоги
            Console.WriteLine("\nИтоги:");
            Console.WriteLine($"Всего товаров: {products.Length}");
            Console.WriteLine($"Всего драгоценных камней: {products.OfType<PreciousStone>().Count()}");
            Console.WriteLine($"Всего полудрагоценных камней: {products.OfType<SemiPreciousStone>().Count()}");

            Console.WriteLine($"На складе: {products.Count(p => p.Status == ProductStatus.Ready)} камней");
            Console.WriteLine($"Заказано: {products.Count(p => p.Status == ProductStatus.Ordered)} камней");
            Console.WriteLine($"Повреждено: {products.Count(p => p.Status == ProductStatus.Damaged)} камней");
        }
示例#3
0
        static void Main(string[] args)
        {
            // инициализация объектов
            var stone1 = new Stone {
                Color = "Белый", Price = 20, Status = ProductStatus.Ready, Weight = 100
            };
            var pStone1 = new PreciousStone {
                Color = "Желтый", Price = 40, Status = ProductStatus.Ready, Weight = 40
            };
            var ruby1 = new Ruby {
                Price = 57, Status = ProductStatus.None, Weight = 30
            };
            var diamond1 = new Diamond {
                Price = 89, Status = ProductStatus.Ready, Weight = 28
            };
            var spStone1 = new SemiPreciousStone {
                Color = "Зелёный", Price = 30, Status = ProductStatus.None, Weight = 41
            };
            var nefrit1 = new Nefrit {
                Price = 50, Status = ProductStatus.Ready, Weight = 44
            };

            Product[] products = { stone1, pStone1, ruby1, diamond1, spStone1, nefrit1 };

            var printer = new Printer();

            // тест класса Printer
            Console.WriteLine("Printer:");
            foreach (var product in products)
            {
                printer.IamPrinting(product);
            }

            // тест абстрактного класса
            Console.WriteLine("\nТест абстрактного класса:");
            Product pr_stone = stone1, pr_pStone = pStone1, pr_spStone = spStone1;

            Console.WriteLine($"Для камня {pr_stone.DefineMarket()}");
            Console.WriteLine($"Для драгоценного камня {pr_pStone.DefineMarket()}");
            Console.WriteLine($"Для полудрагоценного камня {pr_spStone.DefineMarket()}");

            // тест виртуального метода
            Console.WriteLine("\nТест виртуального метода:");


            diamond1.DamageTest(2000);
            ruby1.DamageTest(2000);
            stone1.DamageTest(2000);

            // тест интефейсов
            //1 способ
            Console.WriteLine("\nТест интерфесов:");
            var rubyWorkshop = ruby1 as IJeweleryWorkshop;     //проиводит ruby1 к IJeweleryWorkshop
            var rubyReseller = ruby1 as IJeweleryReseller;

            // 2 метода с одинаковым названием (но от разных интефейсов)
            rubyWorkshop.ProcessStone(); // вызываем ProcessStone() от IJeweleryWorkshop
            rubyReseller.ProcessStone(); // вызываем ProcessStone() от IJeweleryReseller


            Console.WriteLine("\nЗаказ товаров:");
            Console.WriteLine($"Алмаз: статус {diamond1.StatusDescription}; Рубин: статус {ruby1.StatusDescription}");


            //Итоги
            Console.WriteLine("\nИтоги:");
            Console.WriteLine($"Всего товаров: {products.Length}");
            Console.WriteLine($"Всего драгоценных камней: {products.OfType<PreciousStone>().Count()}");         //фильтрация элементов объекта products по типу PreciousStone.
            Console.WriteLine($"Всего полудрагоценных камней: {products.OfType<SemiPreciousStone>().Count()}"); //?
        }