Example #1
0
        private static void AddLeutenantGeneral(string[] tokens)
        {
            LeutenantGeneral general = new LeutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]));

            for (int i = 5; i < tokens.Length; i++)
            {
                Private privat = (Private)soldiers.Single(s => s.Id == tokens[i]);
                general.AddPrivate(privat);
            }

            soldiers.Add(general);
        }
Example #2
0
        static void Main(string[] args)
        {
            var    soldiers = new List <ISoldier>();
            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                try
                {
                    var soldierInput = input.Split();
                    var id           = int.Parse(soldierInput[1]);
                    var firstName    = soldierInput[2];
                    var lastName     = soldierInput[3];
                    var sodierType   = soldierInput[0];
                    var salary       = decimal.Parse(soldierInput[4]);

                    ISoldier sodier = null;
                    switch (sodierType)
                    {
                    case "Private":
                        sodier = new Private(id, firstName, lastName, salary);
                        ;
                        break;

                    case "Spy":
                        sodier = new Spy(id, firstName, lastName, int.Parse(soldierInput[4]));

                        break;

                    case "LeutenantGeneral":
                        var general = new LeutenantGeneral(id, firstName, lastName, salary);
                        for (int i = 5; i < soldierInput.Length; i++)
                        {
                            int      iid       = int.Parse(soldierInput[i]);
                            ISoldier bbSoldier = soldiers.First(x => x.Id == iid);
                            general.AddPrivate(bbSoldier);
                        }

                        sodier = general;
                        break;

                    case "Engineer":
                        var eng = new Engineer(id, firstName, lastName, salary, soldierInput[5]);
                        for (int i = 6; i < soldierInput.Length; i++)
                        {
                            try
                            {
                                string  partName   = soldierInput[i];
                                int     hourWorked = int.Parse(soldierInput[++i]);
                                IRepair repeir     = new Repair(partName, hourWorked);
                                eng.AddRepiar(repeir);
                            }
                            catch { }
                        }
                        sodier = eng;
                        break;

                    case "Commando":

                        var commando = new Commando(id, firstName, lastName, salary, soldierInput[5]);
                        for (int i = 6; i < soldierInput.Length; i++)
                        {
                            try
                            {
                                string   codeName = soldierInput[i];
                                string   state    = soldierInput[++i];
                                IMission mission  = new Mission(codeName, state);
                                commando.AddMision(mission);
                            }
                            catch
                            {
                            }
                        }
                        sodier = commando;
                        break;

                    default: throw new ArgumentException();
                    }
                    soldiers.Add(sodier);
                }
                catch { }
            }
            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var tokens = input
                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);

                var soldireType = tokens[0];
                var id          = int.Parse(tokens[1]);
                var firstName   = tokens[2];
                var lastName    = tokens[3];
                var sallary     = double.Parse(tokens[4]);

                ISoldier soldier = null;

                try
                {
                    switch (soldireType)
                    {
                    case "Private":
                        soldier = new Private(id, firstName, lastName, sallary);
                        break;

                    case "LeutenantGeneral":
                        var lueitenant = new LeutenantGeneral(id, firstName, lastName, sallary);

                        for (int i = 5; i < tokens.Length; i++)
                        {
                            var privateId = int.Parse(tokens[i]);

                            ISoldier @private = soldiers.FirstOrDefault(p => p.Id == privateId);
                            lueitenant.AddPrivate(@private);
                        }
                        soldier = lueitenant;

                        break;

                    case "Engineer":
                        var engineerCorps = tokens[5];
                        var engineer      = new Engineer(id, firstName, lastName, sallary, engineerCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var partName    = tokens[i];
                            var hoursWorked = int.Parse(tokens[++i]);

                            IRepair repair = new Repair(partName, hoursWorked);
                            engineer.AddRepair(repair);
                        }

                        soldier = engineer;
                        break;

                    case "Commando":
                        var commandoCorps = tokens[5];
                        var commando      = new Commando(id, firstName, lastName, sallary, commandoCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var codeName     = tokens[i];
                            var missionState = tokens[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionState);
                                commando.AddMission(mission);
                            }
                            catch { }
                        }

                        soldier = commando;
                        break;

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

                    default:
                        throw new ArgumentException("Invalid soldier type!");
                    }

                    soldiers.Add(soldier);
                }
                catch { }
            }

            foreach (var s in soldiers)
            {
                Console.WriteLine(s);
            }
        }