Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var allSoldiers = new List <ISoldier>();
            var privates    = new List <IPrivate>();

            string input = Console.ReadLine();

            while (input != "End")
            {
                string[] data      = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   title     = data[0];
                string   id        = data[1];
                string   firstName = data[2];
                string   lastName  = data[3];
                decimal  salary    = decimal.Parse(data[4]);

                switch (title)
                {
                case "Private":
                    var currentPrivate = new Private(id, firstName, lastName, salary);
                    privates.Add(currentPrivate);
                    allSoldiers.Add(currentPrivate);
                    break;

                case "LieutenantGeneral":
                    var currentLeutenant = new LieutenantGeneral(id, firstName, lastName, salary);
                    for (int i = 5; i < data.Length; i++)
                    {
                        currentLeutenant.AddPrivate(privates, data[i]);
                    }
                    allSoldiers.Add(currentLeutenant);
                    break;

                case "Engineer":
                    string engCorps = data[5];
                    if (engCorps == "Airforces" || engCorps == "Marines")
                    {
                        var currentEngineer = new Engineer(id, firstName, lastName, salary, engCorps);
                        for (int i = 6; i < data.Length; i += 2)
                        {
                            var repair = new Repair(data[i], int.Parse(data[i + 1]));
                            currentEngineer.AddRepair(repair);
                        }
                        if (currentEngineer.Corps != null)
                        {
                            allSoldiers.Add(currentEngineer);
                        }
                    }

                    break;

                case "Commando":
                    string commandoCorps = data[5];

                    if (commandoCorps == "Airforces" || commandoCorps == "Marines")
                    {
                        var currentCommando = new Commando(id, firstName, lastName, salary, commandoCorps);
                        for (int i = 6; i < data.Length; i += 2)
                        {
                            if (data[i + 1] == "inProgress" || data[i + 1] == "Finished")
                            {
                                var mission = new Mission(data[i], data[i + 1]);
                                currentCommando.AddMission(mission);
                            }
                        }
                        allSoldiers.Add(currentCommando);
                    }
                    break;

                case "Spy":
                    var currentSpy = new Spy(id, firstName, lastName, (int)salary);
                    allSoldiers.Add(currentSpy);
                    break;

                default:
                    break;
                }
                input = Console.ReadLine();
            }

            foreach (var soldier in allSoldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var tokens      = input.Split();
                var soldierType = tokens[0];
                int id          = int.Parse(tokens[1]);
                var firstName   = tokens[2];
                var lastName    = tokens[3];
                var 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++)
                        {
                            var      privateId = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.FirstOrDefault(p => p.Id == privateId);
                            leutenant.AddPivate(@private);
                        }

                        soldier = leutenant;
                        break;

                    case "Engineer":
                        var engineerCorps = tokens[5];
                        var enginner      = new Engineer(id, firstName, lastName, salary, 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);
                            enginner.AddRepair(repair);
                        }

                        soldier = enginner;
                        break;

                    case "Commando":
                        var commandoCorps = tokens[5];
                        var commando      = new Commando(id, firstName, lastName, salary, 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)salary;
                        soldier = new Spy(id, firstName, lastName, codeNumber);
                        break;

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

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