Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            List <Private> allPrivates = new List <Private>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                try
                {
                    string[] tokens = input.Split();

                    string soldierType = tokens[0];

                    switch (soldierType)
                    {
                    case "Private":
                        Private nextPrivate = new Private(int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]));
                        allPrivates.Add(nextPrivate);
                        Console.WriteLine(nextPrivate);
                        break;

                    case "LieutenantGeneral":
                        LieutenantGeneral nextGeneral = new LieutenantGeneral(int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]));
                        for (int i = 5; i < tokens.Length; i++)
                        {
                            int nextPrivateId = int.Parse(tokens[i]);
                            nextGeneral.Privates.Add(allPrivates.Find(x => x.Id == nextPrivateId));
                        }

                        Console.WriteLine(nextGeneral);
                        break;

                    case "Engineer":
                        Engineer nextEngineer = new Engineer(int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]), tokens[5]);

                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            string nextPart  = tokens[i];
                            int    nextHours = int.Parse(tokens[i + 1]);

                            Tuple <string, int> repair = new Tuple <string, int>(nextPart, nextHours);
                            nextEngineer.Repairs.Add(repair);
                        }

                        Console.WriteLine(nextEngineer);
                        break;

                    case "Commando":
                        Commando nextCommando = new Commando(int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]), tokens[5]);

                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            string missionName  = tokens[i];
                            string missionState = tokens[i + 1];
                            try
                            {
                                Mission nextMission = new Mission(missionName, missionState);
                                nextCommando.Missions.Add(nextMission);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                        Console.WriteLine(nextCommando);
                        break;

                    case "Spy":
                        Spy nextSpy = new Spy(int.Parse(tokens[1]), tokens[2], tokens[3], int.Parse(tokens[4]));
                        Console.WriteLine(nextSpy);
                        break;
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            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[id] = new Private(id, firstName, lastName, salary);
                }
                else if (type == nameof(LieutenantGeneral))
                {
                    decimal salary = decimal.Parse(parts[4]);

                    LieutenantGeneral 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[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[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 missionName = parts[i];
                        string state       = parts[i + 1];

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

                        if (!isMissionValid)
                        {
                            continue;
                        }

                        IMission mission = new Mission(missionName, missionState);
                        commando.AddMission(mission);
                    }

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

                    ISpy spy = new Spy(id, firstName, lastName, codeNumber);
                    soldiersById[id] = spy;
                }
            }

            foreach (var soldier in soldiersById.Values)
            {
                Console.WriteLine(soldier.ToString());
            }
        }