Example #1
0
        public static void Main()
        {
            List <ISoldier> soldiers = new List <ISoldier>();

            while (true)
            {
                string cmd = Console.ReadLine();
                if (cmd == "End")
                {
                    break;
                }

                string[] cmdArgs = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                switch (cmdArgs[0])
                {
                case "Private":
                    var @private = new Private(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]));
                    soldiers.Add(@private);
                    break;

                case "LieutenantGeneral":
                    var ltGeneral = new LieutenantGeneral(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]));
                    for (int i = 5; i < cmdArgs.Length; i++)
                    {
                        int privateId = int.Parse(cmdArgs[i]);
                        ltGeneral.AddPrivate((Private)soldiers.Find(s => s.GetType().Name == "Private" && s.Id == privateId));
                    }

                    soldiers.Add(ltGeneral);

                    break;

                case "Engineer":
                    try
                    {
                        var engineer = new Engineer(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]), cmdArgs[5]);
                        for (int i = 6; i < cmdArgs.Length; i += 2)
                        {
                            var repair = new Repair(cmdArgs[i], int.Parse(cmdArgs[i + 1]));
                            engineer.AddRepair(repair);
                        }

                        soldiers.Add(engineer);
                    }
                    catch (ArgumentException)
                    {
                        continue;
                    }

                    break;

                case "Commando":
                    try
                    {
                        var comando = new Comando(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]), cmdArgs[5]);
                        for (int i = 6; i < cmdArgs.Length; i += 2)
                        {
                            try
                            {
                                var mission = new Mission(cmdArgs[i], cmdArgs[i + 1]);
                                comando.AddMission(mission);
                            }
                            catch (ArgumentException)
                            {
                                continue;
                            }
                        }

                        soldiers.Add(comando);
                    }
                    catch (ArgumentException)
                    {
                        continue;
                    }

                    break;

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

            soldiers.ForEach(s => Console.WriteLine(s));
        }
        public void Run()
        {
            while (true)
            {
                string command = Console.ReadLine().TrimEnd();

                if (command.Equals("End"))
                {
                    break;
                }

                string[] input = command.Split().ToArray();

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

                if (type.Equals("Private"))
                {
                    Private privateSoldier = new Private(id, firstName, lastName, salary);

                    army.Add(privateSoldier);
                }
                else if (type.Equals("LieutenantGeneral"))
                {
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);

                    string[] privates = input.Skip(5).ToArray();

                    for (int i = 0; i < privates.Length; i++)
                    {
                        ISoldier current = army.First(x => x.Id == privates[i]);

                        lieutenantGeneral.AddSoldier(current);
                    }

                    army.Add(lieutenantGeneral);
                }
                else if (type.Equals("Engineer"))
                {
                    string corp = input[5];

                    try
                    {
                        Engineer engineer = null;

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

                        string[] repairs = input.Skip(6).ToArray();

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

                            Repair repair = new Repair(partName, hoursWorked);

                            engineer.AddRepair(repair);
                        }

                        army.Add(engineer);
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex.Message);
                    }
                }
                else if (type.Equals("Commando"))
                {
                    string corp = input[5];

                    try
                    {
                        Comando comando = new Comando(id, firstName, lastName, salary, corp);

                        string[] missions = input.Skip(6).ToArray();

                        for (int i = 0; i < missions.Length; i += 2)
                        {
                            try
                            {
                                string missionCodeName = missions[i];
                                string missionState    = missions[i + 1];

                                Mission mission = new Mission(missionCodeName, missionState);

                                comando.AddMission(mission);
                            }
                            catch (Exception ex)
                            {
                                //Console.WriteLine(ex.Message);
                            }
                        }

                        army.Add(comando);
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex.Message);
                    }
                }
                else if (type.Equals("Spy"))
                {
                    Spy spy = new Spy(id, firstName, lastName, (int)salary);

                    army.Add(spy);
                }
            }

            foreach (var soldier in army)
            {
                Console.WriteLine(soldier);
            }
        }