Beispiel #1
0
        static void Main(string[] args)
        {
            var soldiers = new List <Soldier>();
            var privates = new List <Private>();

            while (true)
            {
                var command = Console.ReadLine();

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

                var partsOfCommand     = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                var typeOfSoldier      = partsOfCommand[0];
                var id                 = int.Parse(partsOfCommand[1]);
                var firstName          = partsOfCommand[2];
                var lastName           = partsOfCommand[3];
                var salaryOrCodeNumber = partsOfCommand[4];

                switch (typeOfSoldier)
                {
                case "Private":
                    var privateToAdd = new Private(id, firstName, lastName, decimal.Parse(salaryOrCodeNumber));
                    soldiers.Add(privateToAdd);
                    privates.Add(privateToAdd);
                    break;

                case "LieutenantGeneral":
                    var lieutenantToAdd = new LieutenantGeneral(id, firstName, lastName, decimal.Parse(salaryOrCodeNumber));
                    for (int i = 5; i < partsOfCommand.Length; i++)
                    {
                        var privateId = int.Parse(partsOfCommand[i]);
                        lieutenantToAdd.PrivatesUnderCommand
                        .Add(privates.Where(x => x.Id == privateId).FirstOrDefault());
                    }
                    soldiers.Add(lieutenantToAdd);
                    break;

                case "Engineer":
                    var corps = partsOfCommand[5];
                    if (corps == "Airforces" || corps == "Marines")
                    {
                        var engineerToAdd = new Engineer(id, firstName, lastName, decimal.Parse(salaryOrCodeNumber), corps);
                        for (int i = 6; i < partsOfCommand.Length - 1; i += 2)
                        {
                            var partName    = partsOfCommand[i];
                            var hoursWorked = int.Parse(partsOfCommand[i + 1]);
                            engineerToAdd.Repairs.Add(new Repair(partName, hoursWorked));
                        }
                        soldiers.Add(engineerToAdd);
                    }
                    break;

                case "Commando":
                    var corpsOfCommando = partsOfCommand[5];
                    if (corpsOfCommando == "Airforces" || corpsOfCommando == "Marines")
                    {
                        var commandoToAdd = new Commando(id, firstName, lastName, decimal.Parse(salaryOrCodeNumber), corpsOfCommando);
                        for (int i = 6; i < partsOfCommand.Length - 1; i += 2)
                        {
                            var codeName = partsOfCommand[i];
                            var state    = partsOfCommand[i + 1];
                            if (state == "inProgress" || state == "Finished")
                            {
                                commandoToAdd.Missions.Add(new Mission(codeName, state));
                            }
                        }
                        soldiers.Add(commandoToAdd);
                    }
                    break;

                case "Spy":
                    var spyToAdd = new Spy(id, firstName, lastName, int.Parse(salaryOrCodeNumber));
                    soldiers.Add(spyToAdd);
                    break;
                }
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
        private static void GetSoldiers(List <ISoldier> militaryForces)
        {
            string soldierInput = String.Empty;

            while ((soldierInput = Console.ReadLine()) != "End")
            {
                try
                {
                    string[] tokens    = soldierInput.Split();
                    int      id        = int.Parse(tokens[1]);
                    string   firstName = tokens[2];
                    string   lastName  = tokens[3];
                    ISoldier soldier   = null;

                    switch (tokens[0])
                    {
                    case "Private":
                        soldier = new Private(id, firstName, lastName, decimal.Parse(tokens[4]));
                        break;

                    case "LeutenantGeneral":
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, decimal.Parse(tokens[4]));

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

                        break;

                    case "Engineer":
                        var engineer = new Engineer(id, firstName, lastName, decimal.Parse(tokens[4]), tokens[5]);

                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            IRepairable repair = new Repair(tokens[i], int.Parse(tokens[i + 1]));
                            engineer.AddRepair(repair);
                        }
                        soldier = engineer;

                        break;

                    case "Commando":
                        var commando = new Commando(id, firstName, lastName, decimal.Parse(tokens[4]), tokens[5]);

                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            try
                            {
                                IMissionable mission = new Mission(tokens[i], tokens[i + 1]);
                                commando.AddMission(mission);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        soldier = commando;

                        break;

                    case "Spy":
                        soldier = new Spy(id, firstName, lastName, int.Parse(tokens[4]));
                        break;

                    default: throw new Exception();
                    }

                    militaryForces.Add(soldier);
                }
                catch (Exception)
                {
                }
            }
        }