Exemple #1
0
        static void Main()
        {
            Console.WriteLine("To exit press E or Press any key to START decorating");
            char command = Console.ReadKey().KeyChar;

            Console.WriteLine("START RUNNING DECORATOR.");
            WormerBaseInterface entityToDecorate;
            Client client = new Client();

            Console.WriteLine("App: created a PERSON.");
            ConcretePerson person = new ConcretePerson();

            Console.WriteLine("App: Aply client code for the Person component.");


            Console.WriteLine("Enter the temperature of the enviroment. The temperature is quantified as bellow zero becouse now is winter");
            double temperatureOutside = double.Parse(Console.ReadLine());

            person.BodyTemperature -= temperatureOutside;
            Console.WriteLine($"The current BodyTemperature of the person is: {person.BodyTemperature:F}");
            client.ClientCode(person, person.BodyTemperature);
            entityToDecorate = person;
            while (command != 'E')
            {
                Console.WriteLine("To decorate the PERSON with the PANTS press 1");
                Console.WriteLine("To decorate the PERSON with the TSHORT press 2");
                Console.WriteLine("To udecorate the PERSON with the JUMPER press 3");
                Console.WriteLine("To decorate the PERSON with the COAT press 4");
                Console.WriteLine("To decorate the PERSON with the HOUSE press 5");
                Console.WriteLine("To exit press E");
                command = Console.ReadKey().KeyChar;

                switch (command)
                {
                case '1':
                    Console.WriteLine("\n App: The Person is decorated with Pants.");
                    entityToDecorate = new PantsDecorator(entityToDecorate);
                    break;

                case '2':
                    Console.WriteLine("\n App: The Person is decorated with Tshirt.");
                    entityToDecorate = new TshirtDecorator(entityToDecorate);
                    break;

                case '3':
                    Console.WriteLine("\n App: The Person is decorated with Jumper.");
                    entityToDecorate = new JumperDecorator(entityToDecorate);

                    break;

                case '4':
                    Console.WriteLine("\n App: The Person is decorated with Coat.");
                    entityToDecorate = new CoatDecorator(entityToDecorate);

                    break;

                case '5':
                    Console.WriteLine("\n App: The Person is decorated with House.");
                    entityToDecorate = new HouseDecorator(entityToDecorate);
                    break;

                default:
                    Console.WriteLine("\n Something went wrong. Please try again.");
                    break;
                }
            }
            Console.WriteLine("\n App: Aply client code for the Decorated component.");
            client.ClientCode(entityToDecorate, entityToDecorate.BaseTemperature);
            Console.WriteLine("END RUNNING ADAPTER.");
            Console.ReadKey();
        }
        public static void Main(string[] args)
        {
            string input;
            var    abstractPlayers = new Dictionary <string, AbstractPlayer>()
            {
                { "Player", null },
                { "CommonBoots", null },
                { "EpicHelmet", null },
                { "LegendarySword", null },
                { "Pants", null },
                { "RareGloves", null },
            };

            do
            {
                Console.WriteLine("1. Aby stworzyć nowego bohatera naciśnij 1" +
                                  "\n2. W celu dodania/usunięcia rękawic do/z aktualnego bohatera naciśnij 2." +
                                  "\n3. W celu dodania/usunięcia butów do/z aktualnego bohatera naciśnij 3." +
                                  "\n4. W celu dodania/usunięcia spodni do/z aktualnego bohatera naciśnij 4." +
                                  "\n5. W celu dodania/usunięcia hełmu do/z aktualnego bohatera naciśnij 5." +
                                  "\n6. W celu dodania/usunięcia miecza do/z aktualnego bohatera naciśnij 6." +
                                  "\n7. Aby zatrzymać działanie programu naciśnij 0");
                input = Console.ReadLine();
                switch (input)
                {
                case "1":
                    if (DictionaryHasValue(abstractPlayers, "Player"))
                    {
                        Console.WriteLine("Bohater już istnieje.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"] = new Player();
                        Console.WriteLine("Stworzono nowego bohatera.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "2":
                    if (DictionaryHasValue(abstractPlayers, "Player") == false)
                    {
                        Console.WriteLine("Najpierw stwórz bohatera.");
                        break;
                    }

                    if (DictionaryHasValue(abstractPlayers, "RareGloves"))
                    {
                        abstractPlayers["Player"] =
                            new RareGlovesDecorator().RemoveAttribute(abstractPlayers["Player"]);
                        abstractPlayers["RareGloves"] = null;
                        Console.WriteLine("Usunięto Rzadkie Rękawice.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"]     = new RareGlovesDecorator(abstractPlayers["Player"]);
                        abstractPlayers["RareGloves"] = abstractPlayers["Player"];
                        Console.WriteLine("Dodano Rzadkie Rękawice.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "3":
                    if (DictionaryHasValue(abstractPlayers, "Player") == false)
                    {
                        Console.WriteLine("Najpierw stwórz bohatera.");
                        break;
                    }

                    if (DictionaryHasValue(abstractPlayers, "CommonBoots"))
                    {
                        abstractPlayers["Player"] =
                            new CommonBootsDecorator().RemoveAttribute(abstractPlayers["Player"]);
                        abstractPlayers["CommonBoots"] = null;
                        Console.WriteLine("Usunięto Zwyczajne Buty.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"]      = new CommonBootsDecorator(abstractPlayers["Player"]);
                        abstractPlayers["CommonBoots"] = abstractPlayers["Player"];
                        Console.WriteLine("Dodano Zwyczajne Buty.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "4":
                    if (DictionaryHasValue(abstractPlayers, "Player") == false)
                    {
                        Console.WriteLine("Najpierw stwórz bohatera.");
                        break;
                    }

                    if (DictionaryHasValue(abstractPlayers, "Pants"))
                    {
                        abstractPlayers["Player"] =
                            new PantsDecorator().RemoveAttribute(abstractPlayers["Player"]);
                        abstractPlayers["Pants"] = null;
                        Console.WriteLine("Usunięto Spodnie.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"] = new PantsDecorator(abstractPlayers["Player"]);
                        abstractPlayers["Pants"]  = abstractPlayers["Player"];
                        Console.WriteLine("Dodano Spodnie.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "5":
                    if (DictionaryHasValue(abstractPlayers, "Player") == false)
                    {
                        Console.WriteLine("Najpierw stwórz bohatera.");
                        break;
                    }

                    if (DictionaryHasValue(abstractPlayers, "EpicHelmet"))
                    {
                        abstractPlayers["Player"] =
                            new EpicHelmetDecorator().RemoveAttribute(abstractPlayers["Player"]);
                        abstractPlayers["EpicHelmet"] = null;
                        Console.WriteLine("Usunięto Epicki Hełm.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"]     = new EpicHelmetDecorator(abstractPlayers["Player"]);
                        abstractPlayers["EpicHelmet"] = abstractPlayers["Player"];
                        Console.WriteLine("Dodano Epicki Hełm.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "6":
                    if (DictionaryHasValue(abstractPlayers, "Player") == false)
                    {
                        Console.WriteLine("Najpierw stwórz bohatera.");
                        break;
                    }

                    if (DictionaryHasValue(abstractPlayers, "LegendarySword"))
                    {
                        abstractPlayers["Player"] =
                            new LegendarySwordDecorator().RemoveAttribute(abstractPlayers["Player"]);
                        abstractPlayers["LegendarySword"] = null;
                        Console.WriteLine("Usunięto Legendarny Miecz.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }
                    else
                    {
                        abstractPlayers["Player"]         = new LegendarySwordDecorator(abstractPlayers["Player"]);
                        abstractPlayers["LegendarySword"] = abstractPlayers["Player"];
                        Console.WriteLine("Dodano Legendarny Miecz.");
                        Console.WriteLine(abstractPlayers["Player"].CalculateStatistics());
                    }

                    break;

                case "0":
                    break;

                default:
                    Console.WriteLine("Nie rozpoznano polecenia.");
                    break;
                }
            } while (input != "0");
        }