static void Main(string[] args)
        {
            List <ISoldier> soldiers = new List <ISoldier>();

            string input = "";

            while ((input = Console.ReadLine()) != "End")
            {
                string[] splitInput = input.Split();

                string  soldierType = splitInput[0];
                int     id          = int.Parse(splitInput[1]);
                string  firstName   = splitInput[2];
                string  lastNames   = splitInput[3];
                decimal salary      = decimal.Parse(splitInput[4]);

                ISoldier soldier = null;
                try
                {
                    switch (splitInput[0])
                    {
                    case "Private":
                        soldier = new Private(id, firstName, lastNames, salary);
                        break;

                    case "Spy":
                        int codeNumber = (int)salary;
                        soldier = new Spy(id, firstName, lastNames, salary, codeNumber);
                        break;

                    case "LeutenantGeneral":
                        LeutenantGeneral leutenant = new LeutenantGeneral(id, firstName, lastNames, salary);
                        for (int i = 5; i < splitInput.Length; i++)
                        {
                            int      privateId = int.Parse(splitInput[i]);
                            ISoldier @private  = soldiers.First(x => x.Id == privateId);
                            leutenant.AddPrivate(@private);
                        }
                        soldier = leutenant;
                        break;

                    case "Engineer":
                        string   engineerCorps = splitInput[5];
                        Engineer engineer      = new Engineer(id, firstName, lastNames, salary, engineerCorps);

                        for (int i = 6; i < splitInput.Length; i++)
                        {
                            string partName    = splitInput[i];
                            int    hoursWorked = int.Parse(splitInput[++i]);
                            try
                            {
                                IRepair repair = new Repair(partName, hoursWorked);
                                engineer.AddRepairs(repair);
                            }
                            catch { }
                        }
                        soldier = engineer;
                        break;

                    case "Commando":
                        string   commandoCorps = splitInput[5];
                        Commando commando      = new Commando(id, firstName, lastNames, salary, commandoCorps);

                        for (int i = 6; i < splitInput.Length; i++)
                        {
                            string codeName     = splitInput[i];
                            string missionState = splitInput[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionState);
                                commando.AddMissios(mission);
                            }
                            catch { }
                        }
                        soldier = commando;
                        break;

                    default: throw new ArgumentException("Invalid soldier type!");
                    }
                    soldiers.Add(soldier);
                }
                catch {  }
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
Ejemplo n.º 2
0
        static void Main()
        {
            var somting = Assembly.GetExecutingAssembly();



            var soldiersList = new List <Soldier>();

            string soldierInfo = Console.ReadLine();

            while (soldierInfo != "End")
            {
                var info = soldierInfo.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                string id        = info[1];
                string firstName = info[2];
                string lastName  = info[3];

                decimal salary = 0;
                string  corps  = string.Empty;

                switch (info[0])
                {
                case "Private":
                    salary = decimal.Parse(info[4]);
                    Private @private = new Private(id, firstName, lastName, salary);
                    soldiersList.Add(@private);
                    break;

                case "LieutenantGeneral":
                    salary = decimal.Parse(info[4]);
                    LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary);

                    var targetId = info.Skip(5);

                    foreach (var currentID in targetId)
                    {
                        var currentSoldier = soldiersList.Where(x => x.Id == currentID).FirstOrDefault();
                        if (currentSoldier != null)
                        {
                            var type = currentSoldier.GetType().Name;

                            if (type == "Private")
                            {
                                general.AddSoldier((Private)currentSoldier);
                            }
                        }
                    }
                    soldiersList.Add(general);
                    break;

                case "Engineer":
                    salary = decimal.Parse(info[4]);
                    corps  = info[5];


                    if (corps == "Airforces" || corps == "Marines")
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps);

                        var repaiers = info.Skip(6).ToArray();
                        while (repaiers.Length != 0)
                        {
                            var     repaierInfo = repaiers.Take(2).ToArray();
                            Repairs repairs     = new Repairs(repaierInfo[0], int.Parse(repaierInfo[1]));

                            engineer.AddRepairs(repairs);

                            repaiers = repaiers.Skip(2).ToArray();
                        }
                        soldiersList.Add(engineer);
                    }



                    break;

                case "Commando":
                    salary = decimal.Parse(info[4]);
                    corps  = info[5];



                    if (corps == "Airforces" || corps == "Marines")
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, corps);

                        var missions = info.Skip(6).ToArray();
                        while (missions.Length != 0)
                        {
                            var      missionInfo = missions.Take(2).ToArray();
                            Missions missions1   = new Missions(missionInfo[0], missionInfo[1]);


                            if (missions1.State == "Finished" || missions1.State == "inProgress")
                            {
                                commando.AddMission(missions1);
                            }


                            missions = missions.Skip(2).ToArray();
                        }
                        soldiersList.Add(commando);
                    }


                    break;

                case "Spy":
                    string codeNumber = info[4];
                    Spy    spy        = new Spy(id, firstName, lastName, int.Parse(codeNumber));
                    soldiersList.Add(spy);
                    break;

                default:
                    break;
                }


                soldierInfo = Console.ReadLine();
            }

            soldiersList.ForEach(Console.WriteLine);
        }