コード例 #1
0
        static void Main(string[] args)
        {
            List <ISoldier> soldiers = new List <ISoldier>();

            string input = Console.ReadLine();

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

                if (soldierType == "Private")
                {
                    Private privateSoldier = new Private(id, firstName, lastName, salary);
                    soldiers.Add(privateSoldier);
                }
                else if (soldierType == "LieutenantGeneral")
                {
                    LieutenantGeneral ltnGeneral = new LieutenantGeneral(id, firstName, lastName, salary);

                    for (int i = 5; i < soldierInfo.Length; i++)
                    {
                        var currPrivateId = soldiers.FirstOrDefault(n => n.Id == soldierInfo[i]);
                        ltnGeneral.Privates.Add((IPrivate)currPrivateId);
                    }

                    soldiers.Add(ltnGeneral);
                }
                else if (soldierType == "Engineer")
                {
                    string corp = soldierInfo[5];

                    if (corp == "Airforces" || corp == "Marines")
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corp);

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

                            Repair repair = new Repair(part, hours);
                            engineer.Repairs.Add(repair);
                        }

                        soldiers.Add(engineer);
                    }
                }
                else if (soldierType == "Commando")
                {
                    string corp = soldierInfo[5];

                    if (corp == "Airforces" || corp == "Marines")
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, corp);

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

                            if (state == "Finished" || state == "inProgress")
                            {
                                Mission mission = new Mission(codeName, state);
                                commando.Missions.Add(mission);
                            }
                        }

                        soldiers.Add(commando);
                    }
                }
                else if (soldierType == "Spy")
                {
                    int codeNumber = int.Parse(soldierInfo[4]);
                    Spy spy        = new Spy(id, firstName, lastName, codeNumber);
                    soldiers.Add(spy);
                }

                input = Console.ReadLine();
            }

            Console.WriteLine(string.Join(Environment.NewLine, soldiers));
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string[]        input    = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
            List <ISoldier> soldiers = new List <ISoldier>();

            while (input[0] != "End")
            {
                string type      = input[0];
                string id        = input[1];
                string firstName = input[2];
                string lastName  = input[3];

                if (type == "Private")
                {
                    decimal salary     = decimal.Parse(input[4]);
                    Private curPrivate = new Private(id, firstName, lastName, salary);
                    soldiers.Add(curPrivate);
                }
                else if (type == "LieutenantGeneral")
                {
                    decimal        salary   = decimal.Parse(input[4]);
                    List <Private> privates = new List <Private>();

                    for (int i = 5; i < input.Length; i++)
                    {
                        ISoldier findPrivate = soldiers.FirstOrDefault(x => x.Id == input[i]);

                        if (findPrivate != null)
                        {
                            privates.Add((Private)findPrivate);
                        }
                    }

                    LieutenantGeneral lg = new LieutenantGeneral(id, firstName, lastName, salary, privates);
                    soldiers.Add(lg);
                }
                else if (type == "Engineer")
                {
                    decimal salary = decimal.Parse(input[4]);
                    string  corps  = input[5];

                    if (corps != "Marines" && corps != "Airforces")
                    {
                        input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                        continue;
                    }

                    List <Repairs> repairs = new List <Repairs>();

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        string  repairPart = input[i];
                        int     hours      = int.Parse(input[i + 1]);
                        Repairs repair     = new Repairs(repairPart, hours);
                        repairs.Add(repair);
                    }
                    Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs);
                    soldiers.Add(engineer);
                }
                else if (type == "Commando")
                {
                    decimal salary = decimal.Parse(input[4]);
                    string  corps  = input[5];

                    if (corps != "Marines" && corps != "Airforces")
                    {
                        input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                        continue;
                    }

                    List <Missions> missions = new List <Missions>();

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        string missName  = input[i];
                        string missState = input[i + 1];

                        if (missState != "inProgress" && missState != "Finished")
                        {
                            continue;
                        }
                        Missions mission = new Missions(missName, missState);
                        missions.Add(mission);
                    }

                    Commando commando = new Commando(id, firstName, lastName, salary, corps, missions);
                    soldiers.Add(commando);
                }
                else if (type == "Spy")
                {
                    int codeNum = int.Parse(input[4]);
                    Spy spy     = new Spy(id, firstName, lastName, codeNum);
                    soldiers.Add(spy);
                }

                input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
コード例 #3
0
        public static void Main()
        {
            Dictionary <string, ISoldier> soldiers = 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]);

                    soldiers[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 (!soldiers.ContainsKey(privateId))
                        {
                            continue;
                        }

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

                    soldiers[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 partName    = parts[i];
                        int    hoursWorked = int.Parse(parts[i + 1]);

                        IRepair repair = new Repair(partName, hoursWorked);

                        engineer.AddRepair(repair);
                    }

                    soldiers[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);
                    }

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

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

                    soldiers[id] = spy;
                }
            }

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