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

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var inputParams = input.Split();
                var soldierType = inputParams[0];
                var id          = int.Parse(inputParams[1]);
                var firstName   = inputParams[2];
                var lastName    = inputParams[3];

                switch (soldierType)
                {
                case "Private":
                    var salary         = double.Parse(inputParams[4]);
                    var soldierPrivate = new Private(id, firstName, lastName, salary);
                    soldiers.Add(soldierPrivate);
                    break;

                case "Spy":
                    var codeNumber = int.Parse(inputParams[4]);
                    var spy        = new Spy(id, firstName, lastName, codeNumber);
                    soldiers.Add(spy);
                    break;

                case "LeutenantGeneral":
                    var generalSalary = double.Parse(inputParams[4]);
                    var inputSoldiers = inputParams.Skip(5).ToList();
                    var privates      = GetCommandedPrivates(inputSoldiers, soldiers);

                    var general = new LeutenantGeneral(id, firstName, lastName, generalSalary, privates);
                    soldiers.Add(general);
                    break;

                case "Commando":
                    if (!(inputParams[5] == "Marines" || inputParams[5] == "Airforces"))
                    {
                        break;
                    }

                    var commandoSalary = double.Parse(inputParams[4]);
                    var commandoCorps  = inputParams[5];
                    var missions       = GetValidMissions(inputParams.Skip(6).ToList());
                    var commando       = new Commando(id, firstName, lastName, commandoSalary, commandoCorps, missions);
                    soldiers.Add(commando);
                    break;

                case "Engineer":
                    if (!(inputParams[5] == "Marines" || inputParams[5] == "Airforces"))
                    {
                        break;
                    }
                    var eSalary  = double.Parse(inputParams[4]);
                    var eCorps   = inputParams[5];
                    var parts    = GetRepairs(inputParams.Skip(6).ToList());
                    var engineer = new Engineer(id, firstName, lastName, eSalary, eCorps, parts);
                    soldiers.Add(new Engineer(int.Parse(inputParams[1]), inputParams[2], inputParams[3], double.Parse(inputParams[4]), inputParams[5], parts));
                    break;
                }
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] tokens      = input.Split();
                string   soldierType = tokens[0];
                int      id          = int.Parse(tokens[1]);
                string   firstName   = tokens[2];
                string   lastName    = tokens[3];
                decimal  salary      = decimal.Parse(tokens[4]);

                ISoldier soldier = null;

                try
                {
                    switch (soldierType)
                    {
                    case "Private":
                        soldier = new Private(id, firstName, lastName, salary);
                        break;

                    case "LeutenantGeneral":
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 5; i < tokens.Length; i++)
                        {
                            int      privateId = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.First(p => p.Id == privateId);
                            leutenant.AddPrivate(@private);
                        }

                        soldier = leutenant;
                        break;

                    case "Engineer":
                        string engineerCorps = tokens[5];
                        var    engiineer     = new Engineer(id, firstName, lastName, salary, engineerCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            string partName    = tokens[i];
                            int    hoursWorked = int.Parse(tokens[++i]);

                            IRepair repair = new Repair(partName, hoursWorked);
                            engiineer.AddRepair(repair);
                        }
                        soldier = engiineer;
                        break;

                    case "Commando":

                        string commandoCorps = tokens[5];
                        var    commando      = new Commando(id, firstName, lastName, salary, commandoCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            string codeName     = tokens[i];
                            string missionState = tokens[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionState);
                                commando.AddMission(mission);
                            }
                            catch { }
                        }
                        soldier = commando;
                        break;

                    case "Spy":
                        int codeNumber = (int)salary;
                        soldier = new Spy(id, firstName, lastName, codeNumber);
                        break;

                    default: throw new ArgumentException("Invalid soldier type");
                    }
                    soldiers.Add(soldier);
                }
                catch (Exception e)
                {
                    // Console.WriteLine(e.Message);
                }
            }

            foreach (var s in soldiers)
            {
                Console.WriteLine(s);

                //Type soldierType = s.GetType();
                //var actualSoldier = Convert.ChangeType(s, soldierType);
                //Console.WriteLine(actualSoldier);
            }
        }
Example #3
0
        static void Main()
        {
            var soldiers = new List <ISoldier>();

            string input = "";

            while ((input = Console.ReadLine()) != "End")
            {
                var      tokens    = input.Split();
                var      id        = int.Parse(tokens[1]);
                var      firstName = tokens[2];
                var      lastName  = tokens[3];
                var      salary    = double.Parse(tokens[4]);
                ISoldier soldier   = null;
                try
                {
                    if (tokens[0] == "Private")
                    {
                        soldier = new Private(id, firstName, lastName, salary);
                    }
                    else if (tokens[0] == "LeutenantGeneral")
                    {
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, salary);
                        for (int i = 5; i < tokens.Length; i++)
                        {
                            int      privateID = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.First(x => x.Id == privateID);
                            leutenant.AddPrivate(@private);
                        }
                        soldier = leutenant;
                    }
                    else if (tokens[0] == "Engineer")
                    {
                        var engCorp  = tokens[5];
                        var engineer = new Engineer(id, firstName, lastName, salary, engCorp);
                        for (int i = 6; i < tokens.Length; i++)
                        {
                            string partNmae  = tokens[i];
                            int    partHours = int.Parse(tokens[++i]);
                            try
                            {
                                IRepair repair = new Repair(partNmae, partHours);
                                engineer.AddRepairItem(repair);
                            }
                            catch
                            {
                            }
                        }
                        soldier = engineer;
                    }
                    else if (tokens[0] == "Commando")
                    {
                        var comandoCorp = tokens[5];
                        var commando    = new Commando(id, firstName, lastName, salary, comandoCorp);
                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var codeName      = tokens[i];
                            var missionStatus = tokens[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionStatus);
                                commando.AddMission(mission);
                            }
                            catch
                            {
                            }
                        }
                        soldier = commando;
                    }
                    else if (tokens[0] == "Spy")
                    {
                        int codeNumber = (int)salary;
                        soldier = new Spy(id, firstName, lastName, codeNumber);
                    }
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.Message);
                }
                soldiers.Add(soldier);
            }

            foreach (var s in soldiers)
            {
                ////TAST SOLVED
                //Console.WriteLine(s);

                // to get the correct ToString if not working from Interface
                Type soldiertype   = s.GetType();
                var  actualSoldier = Convert.ChangeType(s, soldiertype);
                Console.WriteLine(actualSoldier);
            }
        }
Example #4
0
        public static void Main()
        {
            var inputLine = string.Empty;

            var listOfAllSolders = new List <ISoldier>();

            var listOfPrivates = new List <Private>();

            while ((inputLine = Console.ReadLine()) != "End")
            {
                var tokens = inputLine.Split(' ');

                var typeOfSolder = tokens[0];
                var id           = int.Parse(tokens[1]);
                var firstname    = tokens[2];
                var lastname     = tokens[3];

                Private currentSolder = null;

                switch (typeOfSolder)
                {
                case "Private":
                    var salary = decimal.Parse(tokens[4]);
                    currentSolder = new Private(id, firstname, lastname, salary);
                    listOfPrivates.Add(currentSolder);
                    listOfAllSolders.Add(currentSolder);
                    break;

                case "LeutenantGeneral":
                    salary = decimal.Parse(tokens[4]);
                    var privates = ParsePrivates(tokens, listOfPrivates);
                    currentSolder = new LeutenantGeneral(id, firstname, lastname, salary, privates);
                    listOfPrivates.Add(currentSolder);
                    listOfAllSolders.Add(currentSolder);
                    break;

                case "Engineer":
                    salary = decimal.Parse(tokens[4]);
                    var parsed = Enum.TryParse(tokens[5], out Corps corps);
                    if (parsed)
                    {
                        var repaires = ParseRepairs(tokens);
                        currentSolder = new Engineer(id, firstname, lastname, salary, corps, repaires);
                        listOfPrivates.Add(currentSolder);
                        listOfAllSolders.Add(currentSolder);
                    }
                    break;

                case "Commando":
                    salary = decimal.Parse(tokens[4]);
                    parsed = Enum.TryParse(tokens[5], out corps);
                    if (parsed)
                    {
                        var missions = ParseMissions(tokens);
                        currentSolder = new Commando(id, firstname, lastname, salary, corps, missions);
                        listOfPrivates.Add(currentSolder);
                        listOfAllSolders.Add(currentSolder);
                    }
                    break;

                case "Spy":
                    var codeNumber = int.Parse(tokens[4]);
                    var currentSpy = new Spy(id, firstname, lastname, codeNumber);
                    listOfAllSolders.Add(currentSpy);
                    break;
                }
            }

            listOfAllSolders.ForEach(Console.WriteLine);
        }