示例#1
0
        static void Main()
        {
            var soldiers = new List <ISoldier>();

            string input = "";

            while ((input = Console.ReadLine()) != "End")
            {
                var      tokens    = input.Split();
                var      id        = int.Parse(tokens[1]);
                var      firstName = tokens[2];
                var      lastName  = tokens[3];
                var      salary    = double.Parse(tokens[4]);
                ISoldier soldier   = null;
                try
                {
                    if (tokens[0] == "Private")
                    {
                        soldier = new Private(id, firstName, lastName, salary);
                    }
                    else if (tokens[0] == "LeutenantGeneral")
                    {
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, salary);
                        for (int i = 5; i < tokens.Length; i++)
                        {
                            int      privateID = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.First(x => x.Id == privateID);
                            leutenant.AddPrivate(@private);
                        }
                        soldier = leutenant;
                    }
                    else if (tokens[0] == "Engineer")
                    {
                        var engCorp  = tokens[5];
                        var engineer = new Engineer(id, firstName, lastName, salary, engCorp);
                        for (int i = 6; i < tokens.Length; i++)
                        {
                            string partNmae  = tokens[i];
                            int    partHours = int.Parse(tokens[++i]);
                            try
                            {
                                IRepair repair = new Repair(partNmae, partHours);
                                engineer.AddRepairItem(repair);
                            }
                            catch
                            {
                            }
                        }
                        soldier = engineer;
                    }
                    else if (tokens[0] == "Commando")
                    {
                        var comandoCorp = tokens[5];
                        var commando    = new Commando(id, firstName, lastName, salary, comandoCorp);
                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var codeName      = tokens[i];
                            var missionStatus = tokens[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionStatus);
                                commando.AddMission(mission);
                            }
                            catch
                            {
                            }
                        }
                        soldier = commando;
                    }
                    else if (tokens[0] == "Spy")
                    {
                        int codeNumber = (int)salary;
                        soldier = new Spy(id, firstName, lastName, codeNumber);
                    }
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.Message);
                }
                soldiers.Add(soldier);
            }

            foreach (var s in soldiers)
            {
                ////TAST SOLVED
                //Console.WriteLine(s);

                // to get the correct ToString if not working from Interface
                Type soldiertype   = s.GetType();
                var  actualSoldier = Convert.ChangeType(s, soldiertype);
                Console.WriteLine(actualSoldier);
            }
        }
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

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

                ISoldier soldier = null;

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

                    case "LeutenantGeneral":
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 5; i < tokens.Length; i++)
                        {
                            int      privateId = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.First(p => p.Id == privateId);
                            leutenant.AddPrivate(@private);
                        }

                        soldier = leutenant;
                        break;

                    case "Engineer":
                        string engineerCorps = tokens[5];
                        var    engiineer     = new Engineer(id, firstName, lastName, salary, engineerCorps);

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

                            IRepair repair = new Repair(partName, hoursWorked);
                            engiineer.AddRepair(repair);
                        }
                        soldier = engiineer;
                        break;

                    case "Commando":

                        string commandoCorps = tokens[5];
                        var    commando      = new Commando(id, firstName, lastName, salary, commandoCorps);

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

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

                    default: throw new ArgumentException("Invalid soldier type");
                    }
                    soldiers.Add(soldier);
                }
                catch (Exception e)
                {
                    // Console.WriteLine(e.Message);
                }
            }

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

                //Type soldierType = s.GetType();
                //var actualSoldier = Convert.ChangeType(s, soldierType);
                //Console.WriteLine(actualSoldier);
            }
        }