static void CreateCommando(string[] tokens)
        {
            List <IMission> missions = new List <IMission>();

            for (int i = 6; i < tokens.Length; i += 2)
            {
                try
                {
                    missions.Add(new Mission(tokens[i], tokens[i + 1]));
                }
                catch (Exception) { }
            }

            try
            {
                Commando cmd = new Commando(tokens[1],
                                            tokens[2],
                                            tokens[3],
                                            double.Parse(tokens[4]),
                                            tokens[5],
                                            missions);

                Print(cmd);
            }
            catch (Exception) { }
        }
Ejemplo n.º 2
0
        private static void AddCommando(List <ISoldier> allSoldiers, string[] tokens)
        {
            var commando = new Commando(int.Parse(tokens[1]), tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]);

            if (commando.Corps != null)
            {
                for (int i = 6; i < tokens.Length; i += 2)
                {
                    var missions = new Mission(tokens[i], tokens[i + 1]);
                    commando.AddMission(missions);
                }
                allSoldiers.Add(commando);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Private p  = new Private("1", "Pesho", "Peshev", 22.22);
            Private p1 = new Private("222", "Toncho", "Tonchev", 80.08);

            LeutenantGeneral l = new LeutenantGeneral("3", "Joro", "Jorev", 100);

            l.Privates.Add(p);
            l.Privates.Add(p1);

            Commando c = new Commando("13", "Stamat", "Stamov", 13.1, "Airforces");

            Console.WriteLine(p);
            Console.Write(l);
            Console.WriteLine(c);
            c.Misions.Add(new Mision("1", "2"));
        }
Ejemplo n.º 4
0
        private static List <ISoldier> ReadSoldiers()
        {
            List <ISoldier> soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] soldierInfo = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                switch (soldierInfo[0])
                {
                case "Private":
                    soldiers.Add(new Private(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4])));
                    break;

                case "LeutenantGeneral":
                    LeutenantGeneral general = new LeutenantGeneral(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]));
                    for (int index = 5; index < soldierInfo.Length; index++)
                    {
                        var privat = soldiers.First(s => s.Id == int.Parse(soldierInfo[index]));
                        general.Privates.Add(privat);
                    }
                    soldiers.Add(general);
                    break;

                case "Engineer":
                    if (soldierInfo[5] != "Airforces" && soldierInfo[5] != "Marines")
                    {
                        continue;
                    }
                    Engineer engineer = new Engineer(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]), soldierInfo[5]);

                    for (int index = 6; index < soldierInfo.Length; index += 2)
                    {
                        engineer.Repairs.Add(new Repair(soldierInfo[index], soldierInfo[index + 1]));
                    }
                    soldiers.Add(engineer);
                    break;

                case "Commando":
                    if (soldierInfo[5] != "Airforces" && soldierInfo[5] != "Marines")
                    {
                        continue;
                    }
                    Commando commando = new Commando(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]), soldierInfo[5]);
                    for (int index = 6; index < soldierInfo.Length; index += 2)
                    {
                        if (soldierInfo[index + 1] == "inProgress" || soldierInfo[index + 1] == "Finished")
                        {
                            commando.Missions.Add(new Mission(soldierInfo[index], soldierInfo[index + 1]));
                        }
                    }
                    soldiers.Add(commando);

                    break;

                case "Spy":
                    Spy spy = new Spy(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], long.Parse(soldierInfo[4]));
                    soldiers.Add(spy);
                    break;

                default:
                    break;
                }
            }
            return(soldiers);
        }
Ejemplo n.º 5
0
        private static List <ISoldier> GetSoldiers()
        {
            var soldiers = new List <ISoldier>();

            while (true)
            {
                var input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                var    soldierTokens = ParseInput(input);
                var    type          = soldierTokens[0];
                var    id            = soldierTokens[1];
                var    firstName     = soldierTokens[2];
                var    lastName      = soldierTokens[3];
                double salary        = 0;

                switch (type)
                {
                case "Private":
                    salary = double.Parse(soldierTokens[4]);
                    var privateSoldier = new Private(id, firstName, lastName, salary);
                    soldiers.Add(privateSoldier);
                    break;

                case "LeutenantGeneral":
                    salary = double.Parse(soldierTokens[4]);
                    var leutenantGeneral = new LeutenantGeneral(id, firstName, lastName, salary);
                    var privatesIds      = soldierTokens.Skip(5);
                    foreach (var privateId in privatesIds)
                    {
                        var priv = (Private)soldiers.FirstOrDefault(s => s.Id == privateId);
                        leutenantGeneral.Privates.Add(priv);
                    }
                    soldiers.Add(leutenantGeneral);
                    break;

                case "Engineer":
                    salary = double.Parse(soldierTokens[4]);
                    var corps = soldierTokens[5];
                    try
                    {
                        var engineer = new Engineer(id, firstName, lastName, salary, corps);
                        var repairs  = soldierTokens.Skip(6).ToList();
                        for (int i = 0; i < repairs.Count; i += 2)
                        {
                            var repair = new Repair(repairs[i], int.Parse(repairs[i + 1]));
                            engineer.Repairs.Add(repair);
                        }
                        soldiers.Add(engineer);
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    break;

                case "Commando":
                    salary = double.Parse(soldierTokens[4]);
                    corps  = soldierTokens[5];
                    try
                    {
                        var commando = new Commando(id, firstName, lastName, salary, corps);
                        var missions = soldierTokens.Skip(6).ToList();
                        for (int i = 0; i < missions.Count; i += 2)
                        {
                            try
                            {
                                var mission = new Mission(missions[i], missions[i + 1]);
                                commando.Missions.Add(mission);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                        soldiers.Add(commando);
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    break;

                case "Spy":
                    var codeNumber = int.Parse(soldierTokens[4]);
                    var spy        = new Spy(id, firstName, lastName, codeNumber);
                    soldiers.Add(spy);
                    break;

                default: break;
                }
            }

            return(soldiers);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            string input = Console.ReadLine();

            Data data = new Data();

            while (!input.Equals("End"))
            {
                string[] inputParameters = input.Split();

                string type      = inputParameters[0];
                string id        = inputParameters[1];
                string firstName = inputParameters[2];
                string lastName  = inputParameters[3];

                switch (type)
                {
                case "Private":
                {
                    double salary = double.Parse(inputParameters[4]);
                    data.Privates.Add(id, new Private(id, firstName, lastName, salary));
                    data.Army.Add(new Private(id, firstName, lastName, salary));
                }
                break;

                case "LeutenantGeneral":
                {
                    double   salary = double.Parse(inputParameters[4]);
                    string[] privs  = inputParameters.Skip(5).ToArray();
                    data.Army.Add(new LeutenantGeneral(id, firstName, lastName, salary, privs, data));
                }
                break;

                case "Commando":
                {
                    double   salary   = double.Parse(inputParameters[4]);
                    string   corpus   = inputParameters[5];
                    string[] missions = inputParameters.Skip(6).ToArray();
                    var      commando = new Commando(id, firstName, lastName, salary, corpus, missions);
                    if (commando.ValidateCorpus())
                    {
                        data.Army.Add(commando);
                    }
                }

                break;

                case "Engineer":
                {
                    double   salary   = double.Parse(inputParameters[4]);
                    string   corpus   = inputParameters[5];
                    string[] parts    = inputParameters.Skip(6).ToArray();
                    var      engineer = new Engineer(id, firstName, lastName, salary, corpus, parts);
                    if (engineer.ValidateCorpus())
                    {
                        data.Army.Add(engineer);
                    }
                }
                break;

                case "Spy":
                {
                    int code = int.Parse(inputParameters[4]);
                    data.Army.Add(new Spy(id, firstName, lastName, code));
                }
                break;

                default: throw new ArgumentException(type);
                }
                input = Console.ReadLine();
            }


            foreach (var soldier in data.Army)
            {
                Console.WriteLine(soldier);
            }
            var a = 0;
        }
Ejemplo n.º 7
0
        public static void Main()
        {
            var input = Console.ReadLine();

            IList <Soldier> soldiers = new List <Soldier>();

            while (input != "End")
            {
                var command = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                switch (command[0])
                {
                case "Private":
                    string id             = command[1];
                    string firstName      = command[2];
                    string lastName       = command[3];
                    double salary         = double.Parse(command[4]);
                    var    currentPrivate = new Private(id, firstName, lastName, salary);
                    soldiers.Add(currentPrivate);
                    break;

                case "LeutenantGeneral":
                    id        = command[1];
                    firstName = command[2];
                    lastName  = command[3];
                    salary    = double.Parse(command[4]);
                    var privateNumbers = new List <Private>();
                    for (int i = 5; i < command.Length; i++)
                    {
                        string privateId = command[i];

                        var searchedPrivate = soldiers.SingleOrDefault(s => s.Id == privateId);

                        if (searchedPrivate != null)
                        {
                            privateNumbers.Add(searchedPrivate as Private);
                        }
                    }

                    var currentLeutenant = new LeutenantGeneral(id, firstName, lastName, salary, privateNumbers);

                    soldiers.Add(currentLeutenant);

                    break;

                case "Engineer":
                    id        = command[1];
                    firstName = command[2];
                    lastName  = command[3];
                    salary    = double.Parse(command[4]);
                    string         corps   = command[5];
                    IList <Repair> repairs = new List <Repair>();

                    for (int i = 6; i < command.Length - 1; i += 2)
                    {
                        string repairName    = command[i];
                        int    repairHours   = int.Parse(command[i + 1]);
                        var    currentRepair = new Repair(repairName, repairHours);
                        repairs.Add(currentRepair);
                    }

                    try
                    {
                        var currentEngineer = new Engineer(id, firstName, lastName, salary, corps, repairs);

                        soldiers.Add(currentEngineer);
                    }
                    catch (ArgumentException)
                    {
                    }

                    break;

                case "Commando":
                    id        = command[1];
                    firstName = command[2];
                    lastName  = command[3];
                    salary    = double.Parse(command[4]);
                    corps     = command[5];
                    IList <Mission> missions = new List <Mission>();

                    for (int i = 6; i < command.Length - 1; i += 2)
                    {
                        string missionCodeName = command[i];
                        string missionState    = command[i + 1];
                        try
                        {
                            var currentMission = new Mission(missionCodeName, missionState);
                            missions.Add(currentMission);
                        }
                        catch (ArgumentException)
                        {
                        }
                    }

                    try
                    {
                        var currentCommando = new Commando(id, firstName, lastName, salary, corps, missions);
                        soldiers.Add(currentCommando);
                    }
                    catch (ArgumentException)
                    {
                    }
                    break;

                case "Spy":
                    id        = command[1];
                    firstName = command[2];
                    lastName  = command[3];
                    string codeNumbers = command[4];

                    var currentSpy = new Spy(id, firstName, lastName, codeNumbers);

                    soldiers.Add(currentSpy);
                    break;
                }

                input = Console.ReadLine();
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
Ejemplo n.º 8
0
        public void Run()
        {
            string command = Console.ReadLine();

            while (command != "End")
            {
                string[] commandArgs = command.Split();

                string  type      = commandArgs[0];
                int     id        = int.Parse(commandArgs[1]);
                string  firstName = commandArgs[2];
                string  lastName  = commandArgs[3];
                decimal salary    = decimal.Parse(commandArgs[4]);

                if (type == "Private")
                {
                    Private @private = new Private(id, firstName, lastName, salary);

                    this.army.Add(@private);
                }
                else if (type == "LieutenantGeneral")
                {
                    int[] privatesId = commandArgs
                                       .Skip(5)
                                       .Select(int.Parse)
                                       .ToArray();

                    LieutenantGeneral lieutenant = new LieutenantGeneral(id, firstName, lastName, salary);

                    foreach (var pid in privatesId)
                    {
                        ISoldier soldierToAdd = this.army
                                                .First(s => s.Id == pid);

                        lieutenant.AddPrivate((Private)soldierToAdd);
                    }

                    this.army.Add(lieutenant);
                }
                else if (type == "Engineer")
                {
                    try
                    {
                        string corps = commandArgs[5];

                        string[] repairArgs = commandArgs
                                              .Skip(6)
                                              .ToArray();

                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps);

                        for (int i = 0; i < repairArgs.Length; i += 2)
                        {
                            string partName    = repairArgs[i];
                            int    hoursWorked = int.Parse(repairArgs[i + 1]);

                            IRepair repair = new Repair(partName, hoursWorked);

                            engineer.AddRepair(repair);
                        }

                        this.army.Add(engineer);
                    }
                    catch (InvalidCorpsExceptions ice)
                    {
                    }
                }
                else if (type == "Commando")
                {
                    try
                    {
                        string corps = commandArgs[5];

                        ICommando comando = new Commando(id, firstName, lastName, salary, corps);

                        string[] missionsArgs = commandArgs
                                                .Skip(6)
                                                .ToArray();

                        for (int i = 0; i < missionsArgs.Length; i += 2)
                        {
                            try
                            {
                                string codeName = missionsArgs[i];
                                string state    = missionsArgs[i + 1];

                                IMission mission = new Mission(codeName, state);

                                comando.AddMission(mission);
                            }
                            catch (InvalidStatesExceptions ise)
                            {
                            }
                        }

                        this.army.Add(comando);
                    }
                    catch (InvalidCorpsExceptions ice)
                    {
                        continue;
                    }
                }
                else if (type == "Spy")
                {
                    int codeNumber = (int)salary;

                    ISpy spy = new Spy(id, firstName, lastName, codeNumber);

                    this.army.Add(spy);
                }

                command = Console.ReadLine();
            }

            foreach (var soldier in this.army)
            {
                Type type = soldier.GetType();

                Object actual = Convert.ChangeType(soldier, type);

                Console.WriteLine(soldier.ToString());
            }
        }
        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.º 10
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);
        }
Ejemplo n.º 11
0
        public static void Main()
        {
            var            input    = Console.ReadLine().Split();
            var            privates = new Dictionary <int, Soldier>();
            List <Soldier> soldiers = new List <Soldier>();

            while (!input[0].Equals("End"))
            {
                int    id        = int.Parse(input[1]);
                string firstName = input[2];
                string lastName  = input[3];
                string corps;

                switch (input[0])
                {
                case "Private":
                    double  salary  = double.Parse(input[4]);
                    Soldier soldier = new Private(id, firstName, lastName, salary);
                    privates[id] = soldier;
                    soldiers.Add(soldier);
                    break;

                case "LeutenantGeneral":
                    double         generalSalary   = double.Parse(input[4]);
                    List <Private> generalPrivates = new List <Private>();

                    for (int i = 5; i < input.Length; i++)
                    {
                        int     keyId          = int.Parse(input[i]);
                        Soldier currentSoldier = privates[keyId];
                        generalPrivates.Add((Private)currentSoldier);
                    }
                    Soldier leutenant = new LeutenantGeneral(id, firstName, lastName, generalSalary, generalPrivates);
                    soldiers.Add(leutenant);
                    break;

                case "Engineer":
                    double engineerSalary = double.Parse(input[4]);
                    corps = input[5];
                    List <Repairs> repairs = new List <Repairs>();

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        var partName    = input[i];
                        var workedHours = int.Parse(input[i + 1]);
                        repairs.Add(new Repairs(partName, workedHours));
                    }

                    try
                    {
                        Soldier engineer = new Engineer(id, firstName, lastName, engineerSalary, corps, repairs);
                        soldiers.Add(engineer);
                    }
                    catch (ArgumentException arg)
                    {
                    }
                    break;

                case "Commando":
                    double commandoSalary = double.Parse(input[4]);
                    corps = input[5];
                    List <Mission> missions = new List <Mission>();

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        try
                        {
                            var codeName = input[i];
                            var state    = input[i + 1];
                            missions.Add(new Mission(codeName, state));
                        }
                        catch (ArgumentException arg)
                        {
                        }
                    }
                    try
                    {
                        Soldier commando = new Commando(id, firstName, lastName, commandoSalary, corps, missions);
                        soldiers.Add(commando);
                    }
                    catch (ArgumentException arg)
                    {
                    }
                    break;

                case "Spy":
                    var     codeNumber = input[4];
                    Soldier spy        = new Spy(id, firstName, lastName, codeNumber);
                    soldiers.Add(spy);
                    break;
                }
                input = Console.ReadLine().Split();
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            HashSet <Soldier> soldiers = new HashSet <Soldier>();
            string            input    = Console.ReadLine();

            while (input != "End")
            {
                string[] soldierTokens = input
                                         .Split();

                string  typeOfSoldier = soldierTokens[0];
                int     id            = int.Parse(soldierTokens[1]);
                string  firstName     = soldierTokens[2];
                string  lastName      = soldierTokens[3];
                decimal salary        = decimal.Parse(soldierTokens[4]);
                try
                {
                    if (typeOfSoldier == "Private")
                    {
                        var privateSoldier = new Private(id, firstName, lastName, salary);
                        soldiers.Add(privateSoldier);
                    }
                    else if (typeOfSoldier == "LieutenantGeneral")
                    {
                        List <int> privatesIds = soldierTokens.Skip(5).Select(int.Parse).ToList();

                        var lieutenant = new LieutanantGeneral(id, firstName, lastName, salary);

                        foreach (var currentId in privatesIds)
                        {
                            Private currentPrivate = (Private)soldiers.FirstOrDefault(x => x.Id == currentId);

                            lieutenant.Privates.Add(currentPrivate);
                        }

                        soldiers.Add(lieutenant);
                    }
                    else if (typeOfSoldier == "Engineer")
                    {
                        string corps    = soldierTokens[5];
                        var    engineer = new Engineer(id, firstName, lastName, salary, corps);

                        List <string> repairs = soldierTokens.Skip(6).ToList();

                        for (int i = 0; i < repairs.Count; i += 2)
                        {
                            string partName = repairs[i];
                            int    hours    = int.Parse(repairs[i + 1]);

                            Repair repair = new Repair(partName, hours);
                            engineer.Repairs.Add(repair);
                        }

                        soldiers.Add(engineer);
                    }
                    else if (typeOfSoldier == "Commando")
                    {
                        string corps    = soldierTokens[5];
                        var    commando = new Commando(id, firstName, lastName, salary, corps);

                        List <string> missions = soldierTokens.Skip(6).ToList();

                        for (int i = 0; i < missions.Count; i += 2)
                        {
                            try
                            {
                                string codeName = missions[i];
                                string state    = missions[i + 1];

                                Mission mission = new Mission(codeName, state);
                                commando.Missions.Add(mission);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }

                        soldiers.Add(commando);
                    }
                    else if (typeOfSoldier == "Spy")
                    {
                        int code = Convert.ToInt32(salary);
                        var spy  = new Spy(id, firstName, lastName, code);
                        soldiers.Add(spy);
                    }

                    input = Console.ReadLine();
                }
                catch (Exception ex)
                {
                    if (ex.Message == "invalid corps")
                    {
                        input = Console.ReadLine();
                    }
                }
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            HashSet <Soldier> soldiers = new HashSet <Soldier>();

            while (true)
            {
                string[] inputArgs = Console.ReadLine().Split();

                string type = inputArgs[0];

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

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

                if (type == "Private")
                {
                    decimal salary = decimal.Parse(inputArgs[4]);

                    Private @private = new Private(id, firstName, lastName, salary);
                    soldiers.Add(@private);
                }
                else if (type == "Spy")
                {
                    int code = int.Parse(inputArgs[4]);
                    Spy spy  = new Spy(id, firstName, lastName, code);

                    soldiers.Add(spy);
                }
                else if (type == "LieutenantGeneral")
                {
                    decimal           salary   = decimal.Parse(inputArgs[4]);
                    HashSet <string>  ids      = inputArgs.Skip(5).ToHashSet();
                    HashSet <Private> privates = new HashSet <Private>();
                    GetSoldiers(ids, soldiers, privates);

                    LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary, privates);

                    soldiers.Add(general);
                }
                else if (type == "Engineer")
                {
                    decimal       salary     = decimal.Parse(inputArgs[4]);
                    string        corps      = inputArgs[5];
                    List <string> repairArgs = inputArgs.Skip(6).ToList();
                    var           repairs    = new HashSet <Repair>();
                    GetRepairs(repairArgs, repairs);
                    try
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs);
                        soldiers.Add(engineer);
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (type == "Commando")
                {
                    decimal       salary   = decimal.Parse(inputArgs[4]);
                    string        corps    = inputArgs[5];
                    List <string> mission  = inputArgs.Skip(6).ToList();
                    var           missions = new HashSet <Mission>();
                    GetMissions(mission, missions);
                    try
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, corps, missions);
                        soldiers.Add(commando);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            foreach (var item in soldiers)
            {
                Console.WriteLine(item);
            }
        }