Пример #1
0
        static void Main(string[] args)
        {
            HashSet <Soldier> soldiers = new HashSet <Soldier>();

            while (true)
            {
                string soldier = Console.ReadLine();

                if (soldier == "End")
                {
                    break;
                }


                string[] soldierArgs = soldier.Split();

                string soldierType = soldierArgs[0];
                string id          = soldierArgs[1];
                string firstName   = soldierArgs[2];
                string lastName    = soldierArgs[3];

                if (soldierType == "Private")
                {
                    decimal salary   = decimal.Parse(soldierArgs[4]);
                    Private @private = new Private(id, firstName, lastName, salary);
                    soldiers.Add(@private);
                }
                else if (soldierType == "Spy")
                {
                    int codeNumber = int.Parse(soldierArgs[4]);
                    Spy spy        = new Spy(id, firstName, lastName, codeNumber);
                    soldiers.Add(spy);
                }
                else if (soldierType == "LieutenantGeneral")
                {
                    decimal salary = decimal.Parse(soldierArgs[4]);

                    List <string>     ids      = soldierArgs.Skip(5).ToList();
                    HashSet <Private> privates = GetPrivates(ids, soldiers);
                    LieutenantGeneral general  = new LieutenantGeneral(id, firstName, lastName, salary, privates);

                    soldiers.Add(general);
                }
                else if (soldierType == "Engineer")
                {
                    decimal salary = decimal.Parse(soldierArgs[4]);
                    string  corps  = soldierArgs[5];

                    List <string>    repairArgs = soldierArgs.Skip(6).ToList();
                    HashSet <Repair> privates   = GetRepairs(repairArgs);
                    try
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, privates);
                        soldiers.Add(engineer);
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (soldierType == "Commando")
                {
                    decimal salary = decimal.Parse(soldierArgs[4]);
                    string  corps  = soldierArgs[5];

                    List <string>     missionArgs = soldierArgs.Skip(6).ToList();
                    HashSet <Mission> missions    = GetMission(missionArgs);
                    try
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, corps, missions);
                        soldiers.Add(commando);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            foreach (var soldierObject in soldiers)
            {
                Console.WriteLine(soldierObject);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var listPeople = new List <ISoldier>();
            var input      = Console.ReadLine().Split();

            while (input[0] != "End")
            {
                var soldierType = input[0];
                var id          = input[1];
                var firstName   = input[2];
                var lastName    = input[3];
                var salary      = decimal.Parse(input[4]);
                try
                {
                    switch (soldierType)
                    {
                    case "Private":
                        listPeople.Add(new Private(firstName, lastName, id, salary));
                        break;

                    case "LieutenantGeneral":
                        var LeutenantGeneral = new LeutenantGeneral(firstName, lastName, id, salary);
                        for (int i = 5; i < input.Count(); i++)
                        {
                            var privateId = input[i];
                            var privat    = (Private)listPeople.FirstOrDefault(x => x.Id == privateId);
                            LeutenantGeneral.AddPrivate(privat);
                        }
                        listPeople.Add(LeutenantGeneral);
                        break;

                    case "Engineer":
                        var engineer = new Engineer(firstName, lastName, id, salary, input[5]);
                        for (int i = 6; i < input.Count(); i++)
                        {
                            var repairPart = input[i];
                            i++;
                            var repairHours = int.Parse(input[i]);
                            var repair      = new Repair(repairPart, repairHours);
                            engineer.AddRepair(repair);
                        }
                        listPeople.Add(engineer);
                        break;

                    case "Commando":
                        var commando = new Commando(firstName, lastName, id, salary, input[5]);
                        for (int i = 6; i < input.Count(); i++)
                        {
                            try
                            {
                                var missionName = input[i];
                                i++;
                                var missionState = input[i];
                                var mission      = new Mission(missionName, missionState);
                                commando.AddMission(mission);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        listPeople.Add(commando);
                        break;

                    case "Spy":
                        var spy = new Spy(firstName, lastName, id, int.Parse(input[4]));
                        listPeople.Add(spy);
                        break;
                    }
                }
                catch (Exception)
                {
                }

                input = Console.ReadLine().Split();
            }

            foreach (var item in listPeople)
            {
                Console.WriteLine(item.ToString());
            }
        }