Example #1
0
        public void Run()
        {
            while (true)
            {
                string input = Console.ReadLine();

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

                List <string> inputInfo = input
                                          .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                                          .ToList();

                string type      = inputInfo[0];
                int    id        = int.Parse(inputInfo[1]);
                string firstName = inputInfo[2];
                string lastName  = inputInfo[3];

                if (type == "Private")
                {
                    decimal salary = decimal.Parse(inputInfo[4]);

                    Private currentSoldier = new Private(id, firstName, lastName, salary);

                    privets.Add(currentSoldier);

                    Console.WriteLine(currentSoldier);
                }
                else if (type == "LieutenantGeneral")
                {
                    decimal salary = decimal.Parse(inputInfo[4]);

                    int [] paramsPrivate = inputInfo.Skip(5).Select(int.Parse).ToArray();

                    LieutenantGeneral currentSoldier = new LieutenantGeneral(id, firstName, lastName, salary);

                    foreach (int number in paramsPrivate)
                    {
                        Private found = privets.Where(x => x.Id == number).FirstOrDefault();

                        if (found != null)
                        {
                            currentSoldier.AddPrivate(found);
                        }
                    }

                    Console.WriteLine(currentSoldier);
                }
                else if (type == "Engineer")
                {
                    decimal salary = decimal.Parse(inputInfo[4]);

                    if (Enum.TryParse <Corp>(inputInfo[5], out Corp result))
                    {
                        Engineer currentSoldier = new Engineer(id, firstName, lastName, salary, result);

                        string[] repairsInfo = inputInfo.Skip(6).ToArray();

                        for (int i = 0; i < repairsInfo.Count(); i += 2)
                        {
                            string part  = repairsInfo[i];
                            int    hours = int.Parse(repairsInfo[i + 1]);
                            currentSoldier.Repairs.Add(new Repair(part, hours));
                        }

                        Console.WriteLine(currentSoldier);
                    }
                }
                else if (type == "Commando")
                {
                    decimal salary = decimal.Parse(inputInfo[4]);

                    if (Enum.TryParse <Corp>(inputInfo[5], out Corp result))
                    {
                        Commando currentSoldier = new Commando(id, firstName, lastName, salary, result);

                        string[] missionsInfo = inputInfo.Skip(6).ToArray();

                        for (int i = 0; i < missionsInfo.Count(); i += 2)
                        {
                            string name = missionsInfo[i];

                            if (Enum.TryParse <State>(missionsInfo[i + 1], out State resultState))
                            {
                                Mission newMission = new Mission(name, resultState);

                                currentSoldier.Missions.Add(newMission);
                            }
                        }

                        Console.WriteLine(currentSoldier);
                    }
                }
                else if (type == "Spy")
                {
                    int codeNumber = int.Parse(inputInfo[4]);

                    Spy currentSoldier = new Spy(id, firstName, lastName, codeNumber);

                    Console.WriteLine(currentSoldier);
                }
            }
        }
Example #2
0
        public void Run()
        {
            string command;

            while ((command = this.reader.ReadLine()) != "End")
            {
                string[] cmdArgs = command.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray();

                string soldierType = cmdArgs[0];
                int    id          = int.Parse(cmdArgs[1]);
                string firstName   = cmdArgs[2];
                string lastName    = cmdArgs[3];

                ISoldier soldier = null;

                if (soldierType == "Private")
                {
                    decimal salary = decimal.Parse(cmdArgs[4]);
                    soldier = new Private(id, firstName, lastName, salary);
                }
                else if (soldierType == "LieutenantGeneral")
                {
                    soldier = AddGeneral(cmdArgs, id, firstName, lastName);
                }
                else if (soldierType == "Engineer")
                {
                    decimal salary = decimal.Parse(cmdArgs[4]);
                    string  corps  = cmdArgs[5];
                    try
                    {
                        IEngineer engineer = CreateEngineer(cmdArgs, id, firstName, lastName, salary, corps);

                        soldier = engineer;
                    }
                    catch (InvalidCorpsExceptions)
                    {
                        continue;
                    }
                }
                else if (soldierType == "Commando")
                {
                    decimal salary = decimal.Parse(cmdArgs[4]);
                    string  corps  = cmdArgs[5];

                    try
                    {
                        ICommando commando = new Commando(id, firstName, lastName, salary, corps);

                        string[] missionArgs = cmdArgs.Skip(6).ToArray();

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

                                IMission mission = new Mission(missionCodeName, missionState);

                                commando.AddMissions(mission);
                            }
                            catch (InvalidMissionStateException)
                            {
                                continue;
                            }
                        }
                        soldier = commando;
                    }
                    catch (InvalidCorpsExceptions)
                    {
                        continue;
                    }
                }
                else if (soldierType == "Spy")
                {
                    int codeNumber = int.Parse(cmdArgs[4]);
                    soldier = new Spy(id, firstName, lastName, codeNumber);
                }

                if (soldier != null)
                {
                    this.soldiers.Add(soldier);
                }
            }

            foreach (var soldier in this.soldiers)
            {
                this.writer.WriteLine(soldier.ToString());
            }
        }