Example #1
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);
            }
        }
Example #2
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));
        }
Example #3
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            List <ISoldier> soldiersCollection = new List <ISoldier>();

            while (true)
            {
                string command = Console.ReadLine().Trim();

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

                string[] parameters = command.Split(' ').ToArray();


                if (parameters[0] == "Private")
                {
                    string  id        = parameters[1];
                    string  firstName = parameters[2];
                    string  lastName  = parameters[3];
                    decimal salary    = decimal.Parse(parameters[4]);

                    IPrivate a = new Private(id, firstName, lastName, salary);

                    soldiersCollection.Add(a);
                }
                else if (parameters[0] == "LeutenantGeneral")
                {
                    string  id        = parameters[1];
                    string  firstName = parameters[2];
                    string  lastName  = parameters[3];
                    decimal salary    = decimal.Parse(parameters[4]);

                    ILeutenantGeneral leutenant = new LeutenantGeneral(id, firstName, lastName, salary);


                    for (int i = 5; i < parameters.Length; i++)
                    {
                        ISoldier privateOfficer = soldiersCollection.FirstOrDefault(x => x.Id == parameters[i]);

                        leutenant.PrivatesCollection.Add(privateOfficer);
                    }

                    soldiersCollection.Add(leutenant);
                }
                else if (parameters[0] == "Engineer")
                {
                    string  id        = parameters[1];
                    string  firstName = parameters[2];
                    string  lastName  = parameters[3];
                    decimal salary    = decimal.Parse(parameters[4]);
                    string  corps     = parameters[5];

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


                        for (int i = 6; i < parameters.Length; i += 2)
                        {
                            IRepair repair = new Repair(parameters[i], int.Parse(parameters[i + 1]));
                            engineer.RepairsCollection.Add(repair);
                        }

                        soldiersCollection.Add(engineer);
                    }
                }
                else if (parameters[0] == "Commando")
                {
                    string  id        = parameters[1];
                    string  firstName = parameters[2];
                    string  lastName  = parameters[3];
                    decimal salary    = decimal.Parse(parameters[4]);
                    string  corps     = parameters[5];

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


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

                            if (state == "inProgress" || state == "Finished")
                            {
                                IMission mission = new Mission(parameters[i], parameters[i + 1]);
                                commando.MissionsCollection.Add(mission);
                            }
                        }

                        soldiersCollection.Add(commando);
                    }
                }
                else
                {
                    string id         = parameters[1];
                    string firstName  = parameters[2];
                    string lastName   = parameters[3];
                    string codeNumber = parameters[4];

                    ISoldier a = new Spy(id, firstName, lastName, codeNumber);

                    soldiersCollection.Add(a);
                }
            }

            foreach (var element in soldiersCollection)
            {
                Console.WriteLine(element);
            }
        }
Example #4
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);
            }
        }