public void Run()
        {
            string             input;
            HashSet <ISoldier> soldiers = new HashSet <ISoldier>();

            while ((input = reader.ReadLine()) != END_INPUT)
            {
                string[] args = input.Split();
                string   type = args[0];

                Soldier soldier = null;
                try
                {
                    if (type == typeof(Private).Name)
                    {
                        soldier = (Soldier)privateFactory.CreatePrivate(args);
                    }
                    else if (type == typeof(LieutenantGeneral).Name)
                    {
                        soldier = (Soldier)generalFactory.CreateLieutenantGeneral(args, soldiers);
                    }
                    else if (type == typeof(Engineer).Name)
                    {
                        soldier = (Soldier)engineerFactory.CreateEngineer(args);
                    }
                    else if (type == typeof(Commando).Name)
                    {
                        soldier = (Soldier)commandoFactory.CreateCommando(args);
                    }
                    else if (type == typeof(Spy).Name)
                    {
                        soldier = (Soldier)spyFactory.CreateSpy(args);
                    }
                    soldiers.Add(soldier);
                }
                catch (Exception)
                {
                }
            }

            foreach (var soldier in soldiers)
            {
                writer.WriteLine(soldier.ToString());
            }
        }
예제 #2
0
        public void Run()
        {
            string input;

            while ((input = reader.ReadLine()) != STOP_WHILE)
            {
                string[] arg  = input.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
                string   type = arg[0];
                arg = arg.Skip(1).ToArray();

                try
                {
                    switch (type)
                    {
                    case PRIVATE:
                        soldiers.Add(PrivateFactory.CreatePrivate(arg));
                        break;

                    case LEUTENANT:
                        soldiers.Add(LeutenantGeneralFactory.CreateLeutenantGeneral(arg, this.soldiers));
                        break;

                    case ENGINEER:
                        soldiers.Add(EngineerFactory.CreateEngineer(arg));
                        break;

                    case COMMANDO:
                        soldiers.Add(CommandoFactory.CreateCommando(arg));
                        break;

                    case SPY:
                        soldiers.Add(SpyFactory.CreateSpy(arg));
                        break;
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

            this.soldiers.ToList().ForEach(x => writer.WriteLine(x.ToString()));
        }