Exemple #1
0
        private static Engineer CreateEngineer(
            string id,
            string firstName,
            string lastName,
            decimal salary,
            string corps,
            List <string> commandParts)
        {
            var engineer = new Engineer(id, firstName, lastName, salary, corps);

            for (int i = 6; i < commandParts.Count - 1; i += 2)
            {
                try
                {
                    var repairName  = commandParts[i];
                    var repairHours = int.Parse(commandParts[i + 1]);

                    engineer.AddRepair(new Repair(repairName, repairHours));
                }
                catch
                {
                    continue;
                }
            }

            return(engineer);
        }
        private static IEngineer CreateEngineer(string[] cmdArgs, int id, string firstName, string lastName, decimal salary, string corps)
        {
            IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps);

            var repairArgs = cmdArgs
                             .Skip(6)
                             .ToArray();

            for (int i = 0; i < repairArgs.Length; i += 2)
            {
                string  partName    = repairArgs[i];
                int     hoursWorked = int.Parse(repairArgs[i + 1]);
                IRepair repair      = new Repair(partName, hoursWorked);
                engineer.AddRepair(repair);
            }

            return(engineer);
        }
Exemple #3
0
        private static void AddEngineer(string[] tokens)
        {
            try
            {
                Engineer engineer = new Engineer(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]);

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

                soldiers.Add(engineer);
            }
            catch (ArgumentException)
            {
            }
        }
Exemple #4
0
        private static void AddEngineer(string[] tokens)
        {
            try
            {
                Engineer engineer = new Engineer(tokens[1], tokens[2],
                                                 tokens[3], decimal.Parse(tokens[4]), tokens[5]);

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

                soldiers.Add(engineer);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #5
0
        private static ISoldier CreateEngineer(string[] commandArgs, int ID, string firstName, string lastName, decimal salary, string corps)
        {
            ISoldier  soldier;
            IEngineer engineer = new Engineer(ID, firstName, lastName, salary, corps);

            string[] repairArgs = commandArgs.Skip(6).ToArray();

            for (int i = 0; i < repairArgs.Length; i += 2)
            {
                string partName    = repairArgs[i];
                int    hoursWorked = int.Parse(repairArgs[i + 1]);

                IRepair repair = new Repair(partName, hoursWorked);

                engineer.AddRepair(repair);
            }

            soldier = engineer;
            return(soldier);
        }
        private Engineer CreateEngineer(string[] soldierParams)
        {
            //Engineer<id> < firstName > < lastName > < salary > < corps > < repair1Part > < repair1Hours > … < repairNPart > < repairNHours >
            string  id            = soldierParams[0];
            string  firstName     = soldierParams[1];
            string  lastName      = soldierParams[2];
            decimal salary        = decimal.Parse(soldierParams[3]);
            string  corpsAsString = soldierParams[4];

            CorpsEnum corps;

            if (corpsAsString == "Airforces")
            {
                corps = CorpsEnum.Airforces;
            }
            else if (corpsAsString == "Marines")
            {
                corps = CorpsEnum.Marines;
            }
            else
            {
                throw new InvalidCastException(INVALID_CORPS_MSG);
            }

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

            for (int i = 5; i < soldierParams.Length; i += 2)
            {
                string repairName  = soldierParams[i];
                int    workerHours = int.Parse(soldierParams[i + 1]);

                engineer.AddRepair(repairName, workerHours);
            }

            return(engineer);
        }
Exemple #7
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[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[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 codeName = parts[i];
                        string state    = parts[i + 1];

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

                        if (!isMissionValid)
                        {
                            continue;
                        }

                        IMission mission = new Mission(codeName, 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);
            }
        }
Exemple #8
0
        static void Main(string[] args)
        {
            List <ISoldier> soldiers = new List <ISoldier>();
            List <Private>  privates = new List <Private>();
            string          text     = Console.ReadLine();

            while (text != "End")
            {
                string[] split     = text.Split();
                string   type      = split[0];
                string   id        = split[1];
                string   firstName = split[2];
                string   lastName  = split[3];
                decimal  salary    = decimal.Parse(split[4]);

                switch (type)
                {
                case "Private":
                    Private @private = new Private(id, firstName, lastName, salary);
                    soldiers.Add(@private);
                    privates.Add(@private);
                    break;

                case "LieutenantGeneral":
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);

                    for (int i = 5; i < split.Length; i++)
                    {
                        if (privates.Any(s => s.Id == split[i]))
                        {
                            lieutenantGeneral.AddPrivate(privates.FirstOrDefault(s => s.Id == split[i]));
                        }
                    }

                    soldiers.Add(lieutenantGeneral);
                    break;

                case "Engineer":
                    string corp = split[5];

                    if (Enum.IsDefined(typeof(Enums.Corp), corp))
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, Enum.Parse <Enums.Corp>(corp));

                        for (int i = 6; i < split.Length; i += 2)
                        {
                            engineer.AddRepair(new Repair(split[i], int.Parse(split[i + 1])));
                        }

                        soldiers.Add(engineer);
                    }
                    break;

                case "Commando":
                    corp = split[5];

                    if (Enum.IsDefined(typeof(Enums.Corp), corp))
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, Enum.Parse <Enums.Corp>(corp));

                        for (int i = 6; i < split.Length; i += 2)
                        {
                            if (Enum.IsDefined(typeof(Enums.State), split[i + 1]))
                            {
                                commando.AddMission(new Mission(split[i], Enum.Parse <Enums.State>(split[i + 1])));
                            }
                        }

                        soldiers.Add(commando);
                    }
                    break;

                case "Spy":
                    int codeNumber = (int)salary;
                    soldiers.Add(new Spy(id, firstName, lastName, codeNumber));
                    break;
                }

                text = Console.ReadLine();
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
Exemple #9
0
        static void Main(string[] args)
        {
            string input = Console.ReadLine();

            while (input != "End")
            {
                string[] splitted = input.Split();

                string  soldierType = splitted[0];
                int     id          = int.Parse(splitted[1]);
                string  firstName   = splitted[2];
                string  lastName    = splitted[3];
                decimal salary      = decimal.Parse(splitted[4]);

                ISoldier soldier = null;

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

                    case "Spy":
                        soldier = new Spy(id, firstName, lastName, int.Parse(splitted[4]));
                        break;

                    case "LieutenantGeneral":
                        LieutenantGeneral lieutenant = new LieutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 5; i < splitted.Length; i++)
                        {
                            int      privateId      = int.Parse(splitted[i]);
                            ISoldier currentPrivate = soldiers
                                                      .FirstOrDefault(s => s.Id == privateId);
                            lieutenant.AddPrivate(currentPrivate);
                        }

                        soldier = lieutenant;
                        break;

                    case "Engineer":
                        string   engineerCorps = splitted[5];
                        Engineer engineer      = new Engineer(id, firstName, lastName, salary, engineerCorps);

                        for (int i = 6; i < splitted.Length; i += 2)
                        {
                            string  repairPart  = splitted[i];
                            int     repairHours = int.Parse(splitted[i + 1]);
                            IRepair repair      = new Repair(repairPart, repairHours);
                            engineer.AddRepair(repair);
                        }

                        soldier = engineer;
                        break;

                    case "Commando":
                        string   commandoCorps = splitted[5];
                        Commando commando      = new Commando(id, firstName, lastName, salary, commandoCorps);

                        for (int i = 6; i < splitted.Length; i += 2)
                        {
                            string missionCodeName = splitted[i];
                            string missionState    = splitted[i + 1];

                            try
                            {
                                IMission mission = new Mission(missionCodeName, missionState);
                                commando.AddMission(mission);
                            }
                            catch (ArgumentException) { }
                        }

                        soldier = commando;
                        break;

                    default:
                        throw new ArgumentException("Invalid soldier type!");
                    }
                    soldiers.Add(soldier);
                }
                catch (ArgumentException) { }

                input = Console.ReadLine();
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            var soldiers = new Dictionary <int, ISoldier>();

            while (true)
            {
                string[] input = Console.ReadLine().Split();
                if (input[0] == "End")
                {
                    break;
                }

                int    id        = int.Parse(input[1]);
                string firstName = input[2];
                string lastName  = input[3];


                if (input[0] == "Private")
                {
                    decimal salary = decimal.Parse(input[4]);
                    soldiers.Add(id, new Private(id, firstName, lastName, salary));
                }
                else if (input[0] == "LieutenantGeneral")
                {
                    decimal salary  = decimal.Parse(input[4]);
                    var     general = new LieutenantGeneral(id, firstName, lastName, salary);

                    for (int i = 5; i < input.Length; i++)
                    {
                        Private @private = (Private)soldiers[int.Parse(input[i])];
                        general.AddPrivate(@private);
                    }

                    soldiers.Add(id, general);
                }
                else if (input[0] == "Commando")
                {
                    decimal salary = decimal.Parse(input[4]);
                    string  corps  = input[5];
                    if (corps != "Airforces" && corps != "Marines")
                    {
                        continue;
                    }

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

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        var mission = new Mission(input[i], input[i + 1]);
                        commando.AddMission(mission);
                    }

                    soldiers.Add(id, commando);
                }
                else if (input[0] == "Engineer")
                {
                    decimal salary = decimal.Parse(input[4]);
                    string  corps  = input[5];
                    if (corps != "Airforces" && corps != "Marines")
                    {
                        continue;
                    }

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

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        var repair = new Repair(input[i], int.Parse(input[i + 1]));
                        engineer.AddRepair(repair);
                    }

                    soldiers.Add(id, engineer);
                }
                else if (input[0] == "Spy")
                {
                    int codeNumber = int.Parse(input[4]);
                    soldiers.Add(id, new Spy(id, firstName, lastName, codeNumber));
                }
            }

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

            while (true)
            {
                string[] input = Console.ReadLine().Split();

                if (input[0] == "End")
                {
                    break;
                }

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

                if (type == nameof(Private))
                {
                    decimal salary = decimal.Parse(input[4]);

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

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

                    for (int i = 5; i < input.Length; i++)
                    {
                        if (soldiersById.ContainsKey(input[5]))
                        {
                            lieutenant.AddPrivate((IPrivate)soldiersById[input[i]]);
                        }
                    }

                    soldiersById[id] = lieutenant;
                }
                else if (type == nameof(Engineer))
                {
                    decimal salary = decimal.Parse(input[4]);

                    if (!Enum.TryParse(input[5], out Corps corps))
                    {
                        continue;
                    }

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

                    for (int i = 6; i < input.Length; i++)
                    {
                        string part  = input[i++];
                        int    hours = int.Parse(input[i]);

                        engineer.AddRepair(new Repair(part, hours));
                    }

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

                    if (!Enum.TryParse(input[5], out Corps corps))
                    {
                        continue;
                    }

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

                    for (int i = 6; i < input.Length; i++)
                    {
                        string codeName = input[i++];

                        if (!Enum.TryParse(input[i], out MissionState missionState))
                        {
                            continue;
                        }

                        commando.AddMission(new Mission(codeName, missionState));
                    }

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

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

                    soldiersById[id] = spy;
                }
            }

            foreach (var soldier in soldiersById)
            {
                Console.WriteLine(soldier.Value);
            }
        }
Exemple #12
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];


                //•	Private: "Private <id> <firstName> <lastName> <salary>"
                if (type == nameof(Private))
                {
                    decimal salary = decimal.Parse(parts[4]);
                    soldiersById[id] = new Private(id, firstName, lastName, salary);
                }

                //•	LeutenantGeneral: "LieutenantGeneral <id> <firstName> <lastName> <salary>
                //<private1Id> <private2Id> … <privateNId>" where privateXId will always be an Id of a Private already received through the input.
                else if (type == nameof(LieutenantGeneral))
                {
                    decimal            salary           = decimal.Parse(parts[4]);
                    ILieutenantGeneral leutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);
                    for (int i = 5; i < parts.Length; i++)
                    {
                        string privateId = parts[i];
                        if (!soldiersById.ContainsKey(privateId))
                        {
                            continue;
                        }
                        leutenantGeneral.AddPrivate((IPrivate)soldiersById[privateId]); // иска IPrivate  и го кастваме
                    }



                    soldiersById[id] = leutenantGeneral;
                }

                //•	Engineer: "Engineer <id> <firstName> <lastName> <salary> <corps> <repair1Part> <repair1Hours> … <repairNPart> <repairNHours>" where repairXPart is the name of a repaired part and repairXHours the hours it took to repair it(the two parameters will always come paired).
                else if (type == nameof(Engineer))
                {
                    decimal salary       = decimal.Parse(parts[4]);
                    bool    isCorpsValid = Enum.TryParse(parts[5], out Corps corps); // после ползваме директно corps v инит на Енгинера

                    if (!isCorpsValid)
                    {
                        continue;
                    }
                    IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); // corps go vzema ot enum 3 reda po nagore

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

                        IRepair repair = new Repair(repairPart, hoursWorked);

                        engineer.AddRepair(repair);
                    }
                    soldiersById[id] = engineer;
                }
                //•	Commando: "Commando <id> <firstName> <lastName> <salary> <corps>
                //<mission1CodeName>  <mission1state> … <missionNCodeName> <missionNstate>" a missions code name, description and state will always come together.
                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 missionCodeName       = parts[i];
                        string missionState          = parts[i + 1];
                        bool   isMissIonStateIsValid = Enum.TryParse(missionState, out MissionState missState);
                        if (!isMissIonStateIsValid)
                        {
                            continue;
                        }

                        IMission mission = new Mission(missionCodeName, missState);
                        commando.AddMission(mission);
                    }
                    soldiersById[id] = commando;
                }
                //•	Spy: "Spy <id> <firstName> <lastName> <codeNumber>"
                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);
            }
        }
Exemple #13
0
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var tokens = input
                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);

                var soldireType = tokens[0];
                var id          = int.Parse(tokens[1]);
                var firstName   = tokens[2];
                var lastName    = tokens[3];
                var sallary     = double.Parse(tokens[4]);

                ISoldier soldier = null;

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

                    case "LeutenantGeneral":
                        var lueitenant = new LeutenantGeneral(id, firstName, lastName, sallary);

                        for (int i = 5; i < tokens.Length; i++)
                        {
                            var privateId = int.Parse(tokens[i]);

                            ISoldier @private = soldiers.FirstOrDefault(p => p.Id == privateId);
                            lueitenant.AddPrivate(@private);
                        }
                        soldier = lueitenant;

                        break;

                    case "Engineer":
                        var engineerCorps = tokens[5];
                        var engineer      = new Engineer(id, firstName, lastName, sallary, engineerCorps);

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

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

                        soldier = engineer;
                        break;

                    case "Commando":
                        var commandoCorps = tokens[5];
                        var commando      = new Commando(id, firstName, lastName, sallary, commandoCorps);

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

                        soldier = commando;
                        break;

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

                    default:
                        throw new ArgumentException("Invalid soldier type!");
                    }

                    soldiers.Add(soldier);
                }
                catch { }
            }

            foreach (var s in soldiers)
            {
                Console.WriteLine(s);
            }
        }
Exemple #14
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

            Dictionary <string, ISoldier> soldiersByID = new Dictionary <string, ISoldier>();

            string command;

            while ((command = Console.ReadLine()) != "End")
            {
                string[] commandArgs = command.Split();

                string soldierType = commandArgs[0];
                string id          = commandArgs[1];
                string firstName   = commandArgs[2];
                string lastName    = commandArgs[3];

                if (soldierType == typeof(Private).Name)
                {
                    decimal salary = decimal.Parse(commandArgs[4]);
                    soldiersByID[id] = new Private(id, firstName, lastName, salary);
                }
                else if (soldierType == typeof(LieutenantGeneral).Name)
                {
                    decimal salary = decimal.Parse(commandArgs[4]);

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


                    for (int i = 5; i < commandArgs.Length; i++)
                    {
                        string privateId = commandArgs[i];

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

                    soldiersByID[id] = lieutenantGeneral;
                }
                else if (soldierType == typeof(Engineer).Name)
                {
                    //•	Engineer:
                    // Name: < firstName > < lastName > Id: < id > Salary: < salary > Corps: < corps >Repairs:
                    // < repair1 ToString() >
                    // < repair2 ToString() >
                    //< repairN ToString() >

                    decimal salary = decimal.Parse(commandArgs[4]);

                    bool isCorpsValid = Enum.TryParse(commandArgs[5], out SoldierCorpEnum corps);

                    if (!isCorpsValid)
                    {
                        continue;
                    }

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

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

                        IRepair repair = new Repair(part, hoursWorked);

                        engineer.AddRepair(repair);
                    }

                    soldiersByID[id] = engineer;
                }
                else if (soldierType == typeof(Commando).Name)
                {
                    decimal salary = decimal.Parse(commandArgs[4]);

                    bool isCorpsValid = Enum.TryParse(commandArgs[5], out SoldierCorpEnum corps);

                    if (!isCorpsValid)
                    {
                        continue;
                    }

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

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

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

                        if (!isMissionValid)
                        {
                            continue;
                        }

                        IMission mission = new Mission(codeName, missionState);

                        commando.AddMision(mission);
                    }
                    soldiersByID[id] = commando;
                }
                else if (soldierType == typeof(Spy).Name)
                {
                    // •	Spy:
                    //Name: < firstName > < lastName > Id: < id >
                    //Code Number: < codeNumber >

                    int codeNumber = int.Parse(commandArgs[4]);

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

                    soldiersByID[id] = spy;
                }
            }

            foreach (var soldierr in soldiersByID.Values)
            {
                Console.WriteLine(soldierr);
            }
        }
Exemple #15
0
        public void Run()
        {
            string line = Console.ReadLine();

            while (line != "End")
            {
                string[] args = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string   type = args[0];

                try
                {
                    switch (type)
                    {
                    case "Private":
                        Private privateSoldier = this.privateFactory.MakePrivateSoldier(args);
                        this.soldiers.Add(privateSoldier);
                        break;

                    case "LieutenantGeneral":
                        LieutenantGeneral lieutenantGeneral =
                            this.lieutenantGeneralFactory.MakeLieutenantGeneral(args);

                        for (int i = 5; i < args.Length; i++)
                        {
                            Private currentPrivate =
                                (Private)this.soldiers.FirstOrDefault(p => p.Id == int.Parse(args[i]));

                            lieutenantGeneral.AddPrivate(currentPrivate);
                        }

                        this.soldiers.Add(lieutenantGeneral);

                        break;

                    case "Engineer":
                        Engineer engineer = this.engineerFactory.MakeEngineer(args);

                        for (int i = 6; i < args.Length; i += 2)
                        {
                            string name   = args[i];
                            int    hours  = int.Parse(args[i + 1]);
                            Repair repair = this.repairFactory.MakeRepair(name, hours);
                            engineer.AddRepair(repair);
                        }

                        this.soldiers.Add(engineer);
                        break;

                    case "Commando":
                        Commando commando = this.commandoFactory.MakeCommando(args);

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

                            Mission mission = this.missionFactory.MakeMission(name, state);
                            commando.AddMission(mission);
                        }

                        this.soldiers.Add(commando);
                        break;

                    case "Spy":
                        Spy spy = this.spyFactory.MakeSpy(args);
                        this.soldiers.Add(spy);
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception)
                {
                }

                line = Console.ReadLine();
            }

            foreach (var soldier in this.soldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
Exemple #16
0
        public static void Main()
        {
            List <ISoldier> soldiers = new List <ISoldier>();

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

                string[] cmdArgs = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                switch (cmdArgs[0])
                {
                case "Private":
                    var @private = new Private(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]));
                    soldiers.Add(@private);
                    break;

                case "LieutenantGeneral":
                    var ltGeneral = new LieutenantGeneral(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]));
                    for (int i = 5; i < cmdArgs.Length; i++)
                    {
                        int privateId = int.Parse(cmdArgs[i]);
                        ltGeneral.AddPrivate((Private)soldiers.Find(s => s.GetType().Name == "Private" && s.Id == privateId));
                    }

                    soldiers.Add(ltGeneral);

                    break;

                case "Engineer":
                    try
                    {
                        var engineer = new Engineer(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]), cmdArgs[5]);
                        for (int i = 6; i < cmdArgs.Length; i += 2)
                        {
                            var repair = new Repair(cmdArgs[i], int.Parse(cmdArgs[i + 1]));
                            engineer.AddRepair(repair);
                        }

                        soldiers.Add(engineer);
                    }
                    catch (ArgumentException)
                    {
                        continue;
                    }

                    break;

                case "Commando":
                    try
                    {
                        var comando = new Comando(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4]), cmdArgs[5]);
                        for (int i = 6; i < cmdArgs.Length; i += 2)
                        {
                            try
                            {
                                var mission = new Mission(cmdArgs[i], cmdArgs[i + 1]);
                                comando.AddMission(mission);
                            }
                            catch (ArgumentException)
                            {
                                continue;
                            }
                        }

                        soldiers.Add(comando);
                    }
                    catch (ArgumentException)
                    {
                        continue;
                    }

                    break;

                case "Spy":
                    var spy = new Spy(int.Parse(cmdArgs[1]), cmdArgs[2], cmdArgs[3], int.Parse(cmdArgs[4]));
                    soldiers.Add(spy);
                    break;
                }
            }

            soldiers.ForEach(s => Console.WriteLine(s));
        }
        public static void Main(string[] args)
        {
            List <ISoldier> soldiers = new List <ISoldier>();
            string          command  = string.Empty;

            while ((command = Console.ReadLine()) != "End")
            {
                string[] input = command
                                 .Split(' ', StringSplitOptions.RemoveEmptyEntries);
                int    id        = int.Parse(input[1]);
                string firstName = input[2];
                string lastName  = input[3];


                switch (input[0])
                {
                case "Private":
                    Private @private = new Private(id, firstName, lastName, decimal.Parse(input[4]));
                    soldiers.Add(@private);
                    break;

                case "LieutenantGeneral":
                    decimal           salary            = decimal.Parse(input[4]);
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);
                    for (int i = 5; i < input.Length; i++)
                    {
                        ISoldier currentPrivate = soldiers.FirstOrDefault(s => s.Id.Equals(int.Parse(input[i])));
                        if (currentPrivate != null)
                        {
                            if (currentPrivate is Private currentSoldier)
                            {
                                lieutenantGeneral.AddPrivate(currentSoldier);
                            }
                        }
                    }
                    soldiers.Add(lieutenantGeneral);
                    break;

                case "Engineer":
                    salary = decimal.Parse(input[4]);
                    string corps = input[5];
                    if (!(corps != "Airforces" || corps != "Marines"))
                    {
                        continue;
                    }
                    Engineer engineer = new Engineer(id, firstName, lastName, salary, corps);

                    for (int i = 6; i < input.Length; i++)
                    {
                        if (i + 1 < input.Length)
                        {
                            if (int.TryParse(input[i + 1], out int repairHours))
                            {
                                Repair repair = new Repair(input[i], repairHours);
                                engineer.AddRepair(repair);
                            }
                        }
                    }

                    soldiers.Add(engineer);
                    break;

                case "Commando":
                    salary = decimal.Parse(input[4]);
                    corps  = input[5];
                    if (!(corps != "Airforces" || corps != "Marines"))
                    {
                        continue;
                    }
                    Commando commando = new Commando(id, firstName, lastName, salary, corps);
                    for (int i = 6; i < input.Length; i++)
                    {
                        if (i + 1 < input.Length)
                        {
                            if (input[i + 1] != "inProgress")
                            {
                                i++;
                                continue;
                            }
                            Mission mission = new Mission(input[i], input[i + 1]);
                            commando.AddMissin(mission);
                            i++;
                        }
                    }
                    soldiers.Add(commando);
                    break;

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

                default:
                    break;
                }
            }
            foreach (var soldir in soldiers)
            {
                Console.WriteLine(soldir);
            }
        }
Exemple #18
0
        static void Main(string[] args)
        {
            List <ISoldier> list     = new List <ISoldier>();
            List <Private>  privates = new List <Private>();

            string[] input = Console.ReadLine().Split();

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

                switch (soldier)
                {
                case "Private":
                    decimal salary = decimal.Parse(input[4]);
                    Private privat = new Private(id, firstName, lastName, salary);
                    privates.Add(privat);
                    list.Add(privat);
                    break;

                case "LieutenantGeneral":
                    decimal           salaryLieut       = decimal.Parse(input[4]);
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salaryLieut);
                    string[]          privatesIds       = input.Skip(5).ToArray();

                    for (int i = 0; i < privatesIds.Length; i++)
                    {
                        foreach (var privateSoldier in privates)
                        {
                            if (privateSoldier.Id == privatesIds[i])
                            {
                                lieutenantGeneral.AddPrivate(privateSoldier);
                            }
                        }
                    }
                    list.Add(lieutenantGeneral);
                    break;

                case "Engineer":
                    string corps = input[5];
                    if (corps == "Airforces" || corps == "Marines")
                    {
                        decimal  salaryEngi = decimal.Parse(input[4]);
                        Engineer engineer   = new Engineer(id, firstName, lastName, salaryEngi, corps);
                        string[] repairs    = input.Skip(6).ToArray();

                        for (int i = 0; i < repairs.Length; i += 2)
                        {
                            string partName    = repairs[i];
                            int    workedHours = int.Parse(repairs[i + 1]);

                            Repair repair = new Repair(partName, workedHours);
                            engineer.AddRepair(repair);
                        }
                        list.Add(engineer);
                    }

                    break;

                case "Commando":
                    string corpsComm = input[5];
                    if (corpsComm == "Airforces" || corpsComm == "Marines")
                    {
                        decimal  salaryComm = decimal.Parse(input[4]);
                        Commando commando   = new Commando(id, firstName, lastName, salaryComm, corpsComm);
                        string[] missions   = input.Skip(6).ToArray();

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

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

                case "Spy":
                    int codeNumber = int.Parse(input[4]);
                    Spy spy        = new Spy(id, firstName, lastName, codeNumber);
                    list.Add(spy);
                    break;
                }


                input = Console.ReadLine().Split();
            }

            foreach (var soldier in list)
            {
                Console.WriteLine(soldier);
            }
        }
Exemple #19
0
        static void Main(string[] args)
        {
            ISoldier soldier = null;

            ICollection <ISoldier> soldiers = new List <ISoldier>();

            string input = Console.ReadLine();

            while (input != "End")
            {
                string[] token = input.Split();

                switch (token[0])
                {
                case "Private":
                    soldier = new Private(int.Parse(token[1]), token[2], token[3], decimal.Parse(token[4]));
                    soldiers.Add(soldier);
                    break;

                case "LieutenantGeneral":
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(int.Parse(token[1]), token[2], token[3], decimal.Parse(token[4]));
                    for (int i = 5; i < token.Length; i++)
                    {
                        if (soldiers.FirstOrDefault(s => s.Id == int.Parse(token[i])) is IPrivate @private)
                        {
                            lieutenantGeneral.AddPrivate(@private);
                        }
                    }
                    soldier = lieutenantGeneral as ILieutenantGeneral;
                    soldiers.Add(soldier);
                    break;

                case "Engineer":
                    IEngineer engineer;
                    try
                    {
                        engineer = new Engineer(int.Parse(token[1]), token[2], token[3], decimal.Parse(token[4]), token[5]);
                        soldier  = engineer as IEngineer;
                        for (int i = 6; i < token.Length - 1; i += 2)
                        {
                            IRepair repair = new Repair(token[i], int.Parse(token[i + 1])) as IRepair;
                            engineer.AddRepair(repair);
                        }
                        soldiers.Add(soldier);
                    }
                    catch (ArgumentException)
                    {
                        //Console.WriteLine(e.Message);
                        //throw;
                    }
                    break;

                case "Commando":
                    try
                    {
                        ICommando commando = new Commando(int.Parse(token[1]), token[2], token[3], decimal.Parse(token[4]), token[5]);
                        soldier = commando as ISoldier;
                        for (int i = 6; i < token.Length - 1; i += 2)
                        {
                            IMission mission = new Mission(token[i], token[i + 1]) as IMission;
                            if (mission.State == "inProgress" || mission.State == "Finished")
                            {
                                commando.AddMission(mission);
                            }
                        }
                        soldiers.Add(soldier);
                    }
                    catch (System.ArgumentException)
                    {
                        //throw;
                    }

                    break;

                case "Spy":
                    ISpy spy = new Spy(int.Parse(token[1]), token[2], token[3], int.Parse(token[4].ToLower()));
                    soldier = spy as ISoldier;
                    soldiers.Add(soldier);
                    break;

                default:
                    break;
                }
                input = Console.ReadLine();
            }

            foreach (var @item in soldiers)
            {
                Console.WriteLine(@item);
            }
        }