Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            Dictionary <string, ISoldier> soldiersById = new Dictionary <string, ISoldier>();

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

                string[] parts = line.Split();

                string type      = parts[0];
                string id        = parts[1];
                string firstName = parts[2];
                string lastName  = parts[3];

                if (type == nameof(Private))
                {
                    decimal salary = decimal.Parse(parts[4]);
                    soldiersById.Add(id, new Private(id, firstName, lastName, salary));
                }
                else if (type == nameof(LieutenantGeneral))
                {
                    decimal salary = decimal.Parse(parts[4]);

                    ILieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);

                    for (int i = 5; i < parts.Length; i++)
                    {
                        string privateId = parts[i];
                        if (!soldiersById.ContainsKey(privateId))
                        {
                            continue;
                        }

                        lieutenantGeneral.AddPrivate((IPrivate)soldiersById[privateId]);
                    }

                    soldiersById.Add(id, lieutenantGeneral);
                }
                else if (type == nameof(Engineer))
                {
                    decimal salary = decimal.Parse(parts[4]);

                    bool isCorpsValid = Enum.TryParse(parts[5], out Corps corps);

                    if (!isCorpsValid)
                    {
                        continue;
                    }

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

                    for (int i = 6; i < parts.Length; i += 2)
                    {
                        string part        = parts[i];
                        int    hoursWorked = int.Parse(parts[i + 1]);

                        IRepair repair = new Repair(part, hoursWorked);
                        engineer.AddRepair(repair);
                    }

                    soldiersById.Add(id, engineer);
                }
                else if (type == nameof(Commando))
                {
                    decimal salary = decimal.Parse(parts[4]);

                    bool isCorpsValid = Enum.TryParse(parts[5], out Corps corps);

                    if (!isCorpsValid)
                    {
                        continue;
                    }

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



                    for (int i = 6; i < parts.Length; i += 2)
                    {
                        string codeName = parts[i];
                        string state    = parts[i + 1];

                        bool isMissionStateValid = Enum.TryParse(state, out MissionState missionState);

                        if (!isMissionStateValid)
                        {
                            continue;
                        }

                        IMission mission = new Mission(codeName, missionState);

                        commando.AddMission(mission);
                    }

                    soldiersById.Add(id, commando);
                }
                else if (type == nameof(Spy))
                {
                    int codeNumber = int.Parse(parts[4]);

                    ISpy spy = new Spy(id, firstName, lastName, codeNumber);

                    soldiersById.Add(id, spy);
                }
            }

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

            while (line != "End")
            {
                string[] tokens    = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   firstName = string.Empty;
                string   lastname  = string.Empty;
                string   id        = string.Empty;
                decimal  salary    = 0m;
                if (tokens[0] == "Private")
                {
                    SetTokens(tokens, ref firstName, ref lastname, ref id, ref salary);
                    soldiers.Add(new Private(id, firstName, lastname, salary));
                }
                else if (tokens[0] == "LieutenantGeneral")
                {
                    SetTokens(tokens, ref firstName, ref lastname, ref id, ref salary);
                    ILieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastname, salary);
                    for (int i = 5; i < tokens.Length; i++)
                    {
                        IPrivate @private = (IPrivate)soldiers.FirstOrDefault(x => x.Id == tokens[i]);
                        if (@private != null)
                        {
                            lieutenantGeneral.AddPrivate(@private);
                        }
                    }
                    soldiers.Add(lieutenantGeneral);
                }
                else if (tokens[0] == "Engineer")
                {
                    SetTokens(tokens, ref firstName, ref lastname, ref id, ref salary);
                    bool isCorpseValid = Enum.TryParse(tokens[5], out Corps corps);
                    if (!isCorpseValid)
                    {
                        line = Console.ReadLine();
                        continue;
                    }
                    IEngineer engineer = new Engineer(id, firstName, lastname, salary, corps);
                    for (int i = 6; i < tokens.Length; i += 2)
                    {
                        engineer.AddRepair(new Repair(tokens[i], int.Parse(tokens[i + 1])));
                    }
                    soldiers.Add((ISoldier)engineer);
                }
                else if (tokens[0] == "Commando")
                {
                    SetTokens(tokens, ref firstName, ref lastname, ref id, ref salary);
                    bool isCorpseValid = Enum.TryParse(tokens[5], out Corps corps);
                    if (!isCorpseValid)
                    {
                        line = Console.ReadLine();
                        continue;
                    }
                    ICommando commando = new Commando(id, firstName, lastname, salary, corps);
                    for (int i = 6; i < tokens.Length; i += 2)
                    {
                        bool isMissionValid = Enum.TryParse(tokens[i + 1], out MissionState ms);
                        if (!isMissionValid)
                        {
                            continue;
                        }
                        commando.AddMission(new Mission(tokens[i], ms));
                    }
                    soldiers.Add((ISoldier)commando);
                }
                else
                {
                    id        = tokens[1];
                    firstName = tokens[2];
                    lastname  = tokens[3];
                    int codeNumber = int.Parse(tokens[4]);
                    soldiers.Add(new Spy(id, firstName, lastname, codeNumber));
                }
                line = Console.ReadLine();
            }
            soldiers.ForEach(x => Console.WriteLine(x));
        }