Beispiel #1
0
        private static void AddCommando(string[] tokens)
        {
            try
            {
                Commando commando = new Commando(tokens[1], tokens[2],
                                                 tokens[3], decimal.Parse(tokens[4]), tokens[5]);

                for (int i = 6; i < tokens.Length; i += 2)
                {
                    try
                    {
                        Mission mission = new Mission(tokens[i], tokens[i + 1]);
                        commando.AddMission(mission);
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                soldiers.Add(commando);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        private static Commando CreateCommando(
            string id,
            string firstName,
            string lastName,
            decimal salary,
            string corps,
            List <string> commandParts)
        {
            var commando = new Commando(id, firstName, lastName, salary, corps);

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

                    commando.AddMission(new Mission(missionName, missionState));
                }
                catch (ArgumentException)
                {
                    continue;
                }
            }

            return(commando);
        }
        private static ICommando AddCommando(string[] cmdArgs, int id, string firstName, string lastName, decimal salary, string corps)
        {
            ICommando commando = new Commando(id, firstName, lastName, salary, corps);

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

            for (int i = 0; i < missinArgs.Length; i += 2)
            {
                try
                {
                    string missionCodeName = missinArgs[i];
                    string missionState    = missinArgs[i + 1];

                    IMisson misson = new Mission(missionCodeName, missionState);

                    commando.AddMission(misson);
                }
                catch (InvalidStateException ise)
                {
                    continue;
                }
            }

            return(commando);
        }
        private Commando CreateCommando(string[] soldierParams)
        {
            //Commando <id> <firstName> <lastName> <salary> <corps> <mission1CodeName>  <mission1state> … <missionNCodeName> <missionNstate>"
            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);
            }

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

            for (int i = 5; i < soldierParams.Length; i += 2)
            {
                string missionName          = soldierParams[i];
                string missionStateAsString = soldierParams[i + 1];

                MissionStateEnum missionState;

                if (missionStateAsString == "inProgress")
                {
                    missionState = MissionStateEnum.inProgress;
                }
                else if (missionStateAsString == "Finished")
                {
                    missionState = MissionStateEnum.Finished;
                }
                else
                {
                    missionState = MissionStateEnum.Invalid;
                }

                if (missionState == MissionStateEnum.inProgress || missionState == MissionStateEnum.Finished)
                {
                    commando.AddMission(missionName, missionState);
                }
            }

            return(commando);
        }
Beispiel #5
0
        private static void AddCommando(string[] tokens)
        {
            try
            {
                Commando commando = new Commando(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]);

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

                soldiers.Add(commando);
            }
            catch (ArgumentException)
            {
            }
        }
Beispiel #6
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);
            }
        }
Beispiel #7
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());
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        public static void Main(string[] args)
        {
            List <ISoldier> soldiers = new List <ISoldier>();

            do
            {
                var inputCommand = Console.ReadLine();
                if (inputCommand == "End")
                {
                    break;
                }
                var inputData = inputCommand
                                .Split(new[] { ' ' });
                var     id        = inputData[1];
                var     firstName = inputData[2];
                var     lastName  = inputData[3];
                decimal salary;
                ICorp   corps;
                bool    solderPressent = soldiers.Any(x => x.Id == id);
                switch (inputData[0])
                {
                case "Private":
                    salary = decimal.Parse(inputData[4]);
                    if (solderPressent)
                    {
                        var index       = soldiers.FindIndex(x => x.Id == id);
                        var tempPrivate = (IPrivate)soldiers[index];
                        tempPrivate.FirstName = firstName;
                        tempPrivate.LastName  = lastName;
                        tempPrivate.FirstName = firstName;
                        tempPrivate.Salary    = salary;
                        soldiers[index]       = tempPrivate;
                        //Console.WriteLine(tempPrivate);
                    }
                    else
                    {
                        IPrivate privateSolder = new Private(id, firstName, lastName, salary);
                        soldiers.Add(privateSolder);
                        //Console.WriteLine(privateSolder);
                    }


                    break;

                case "LieutenantGeneral":
                    salary = decimal.Parse(inputData[4]);
                    if (solderPressent)
                    {
                        var index = soldiers.FindIndex(x => x.Id == id);
                        var tempLieutenantGeneral = (ILieutenantGeneral)soldiers[index];
                        tempLieutenantGeneral.FirstName = firstName;
                        tempLieutenantGeneral.LastName  = lastName;
                        tempLieutenantGeneral.FirstName = firstName;
                        tempLieutenantGeneral.Salary    = salary;
                        for (int i = 4; i < inputData.Length; i++)
                        {
                            var currentId = inputData[i];

                            if (soldiers.Any(x => x.Id == currentId))
                            {
                                var indexPrivate = soldiers.FindIndex(x => x.Id == currentId);
                                tempLieutenantGeneral.AddPrivate((IPrivate)soldiers[indexPrivate]);
                            }
                        }
                        soldiers[index] = tempLieutenantGeneral;

                        //Console.WriteLine(tempLieutenantGeneral);
                    }
                    else
                    {
                        ILieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 4; i < inputData.Length; i++)
                        {
                            var currentId = inputData[i];

                            if (soldiers.Any(x => x.Id == currentId))
                            {
                                //var result = soldiers.ToList();
                                var index = soldiers.FindIndex(x => x.Id == currentId);
                                lieutenantGeneral.AddPrivate((IPrivate)soldiers[index]);
                            }
                        }
                        //Console.WriteLine(lieutenantGeneral);
                        soldiers.Add(lieutenantGeneral);
                    }
                    break;

                case "Engineer":
                    salary = decimal.Parse(inputData[4]);
                    try
                    {
                        corps = new Corp(inputData[5]);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    IEngineer         engineer   = new Engineer(id, firstName, lastName, salary, corps);
                    HashSet <IRepair> repairList = new HashSet <IRepair>();
                    for (int i = 6; i < inputData.Length; i += 2)
                    {
                        IRepair repair = new Repair(inputData[i], int.Parse(inputData[i + 1]));
                        engineer.AddRepairs(repair);
                    }

                    //Console.WriteLine(engineer);
                    soldiers.Add(engineer);
                    break;

                case "Commando":
                    salary = decimal.Parse(inputData[4]);
                    try
                    {
                        corps = new Corp(inputData[5]);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    ICommando       commando    = new Commando(id, firstName, lastName, salary, corps);
                    List <IMission> missionList = new List <IMission>();
                    IMission        mission;
                    for (int i = 6; i < inputData.Length; i += 2)
                    {
                        try
                        {
                            mission = new Mission(inputData[i], inputData[i + 1]);
                            commando.AddMission(mission);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }

                    //Console.WriteLine(commando);
                    soldiers.Add(commando);
                    break;

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

                default:
                    break;
                }
            } while (true);

            foreach (var item in soldiers)
            {
                Console.WriteLine(item);
            }
        }
Beispiel #11
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);
            }
        }
Beispiel #12
0
        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);
            }
        }
Beispiel #13
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);
            }
        }
Beispiel #14
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);
            }
        }
Beispiel #15
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);
            }
        }