示例#1
0
        private static List <Repair> GetRepairs(string[] soldierInfo)
        {
            string[]      repairsInfo = soldierInfo.Skip(6).ToArray();
            List <Repair> repairs     = new List <Repair>();

            for (int i = 0; i < repairsInfo.Length; i += 2)
            {
                string repairPart  = repairsInfo[i];
                int    repairHours = int.Parse(repairsInfo[i + 1]);

                Repair newRepair = new Repair(repairPart, repairHours);
                repairs.Add(newRepair);
            }

            return(repairs);
        }
        private static List <Repair> GetRepairs(List <string> repairArgs)
        {
            List <Repair> repairs = new List <Repair>();

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

                Repair repair = new Repair(partName, hoursWorked);

                repairs.Add(repair);
            }

            return(repairs);
        }
        private static List <IRepair> ExtractRepairs(string[] splitedInput)
        {
            var repairs = new List <IRepair>();

            var repairsInfo = splitedInput.Skip(6).ToArray();

            for (int i = 0; i < repairsInfo.Length; i += 2)
            {
                var partName    = repairsInfo[i];
                var hoursWorked = repairsInfo[i + 1];
                var inputRepair = new Repair(hoursWorked, partName);

                repairs.Add(inputRepair);
            }

            return(repairs);
        }
示例#4
0
        private static void CreateEngeneer(string[] tokens, int id, string firstName, string lastName, decimal salary, string corp)
        {
            List <IRepair> repairs = new List <IRepair>();

            if (IsValidCorp(corp))
            {
                for (int i = 6; i < tokens.Length; i += 2)
                {
                    string repairPart  = tokens[i];
                    int    repairHours = int.Parse(tokens[i + 1]);

                    Repair repair = new Repair(repairPart, repairHours);
                    repairs.Add(repair);
                }
                Engineer engineer = new Engineer(id, firstName, lastName, salary, corp, repairs);
                Console.WriteLine(engineer.ToString());
            }
        }
        public static void AddEngineer(List <Soldier> soldiers, string[] commandArray)
        {
            try
            {
                Engineer engineer = new Engineer(commandArray[1], commandArray[2], commandArray[3], decimal.Parse(commandArray[4]), commandArray[5]);

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

                soldiers.Add(engineer);
            }
            catch (ArgumentException)
            {
            }
        }
示例#6
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)
            {
            }
        }
        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);
        }
示例#8
0
        private static Engineer CreateEngineer(string[] inputArgs, int id, string firstName, string lastName)
        {
            if (VerifyCorps(inputArgs, out Corps corps))
            {
                var      salary    = decimal.Parse(inputArgs[4]);
                var      partsArgs = inputArgs.Skip(6).ToArray();
                Engineer engineer  = new Engineer(id, firstName, lastName, salary, corps);

                for (int i = 0; i < partsArgs.Length; i += 2)
                {
                    var    partName  = partsArgs[i];
                    var    partHours = int.Parse(partsArgs[i + 1]);
                    Repair repair    = new Repair(partName, partHours);
                    engineer.Repairs.Add(repair);
                }
                return(engineer);
            }
            return(null);
        }
示例#9
0
        private static void AddEngineerToList(List <Soldier> soldiers, string[] soldierData)
        {
            GetIdNameAndSalary(soldierData, out int id, out string firstName, out string lastName, out double salary);
            string corps = soldierData[5];

            if (IsValidCorps(corps))
            {
                var repairs = new List <Repair>();
                for (int i = 6; i < soldierData.Length; i += 2)
                {
                    var partName    = soldierData[i];
                    var hoursWorked = int.Parse(soldierData[i + 1]);
                    var repair      = new Repair(partName, hoursWorked);
                    repairs.Add(repair);
                }
                var engineer = new Engineer(id, firstName, lastName, salary, corps, repairs);
                soldiers.Add(engineer);
            }
        }
示例#10
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);
            }
        }
示例#11
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);
        }
示例#12
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));
        }
示例#13
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());
            }
        }
示例#14
0
        static void Main()
        {
            var    privates = new Dictionary <string, Private>();
            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var     cmdArgs     = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var     soldierType = cmdArgs[0];
                string  id;
                string  firstName;
                string  lastName;
                decimal salary;
                Corps   corps;

                switch (soldierType)
                {
                case "Private":
                    id        = cmdArgs[1];
                    firstName = cmdArgs[2];
                    lastName  = cmdArgs[3];
                    salary    = decimal.Parse(cmdArgs[4]);

                    var privateSoldier = new Private(firstName, lastName, id, salary);
                    privates.Add(id, privateSoldier);
                    Console.WriteLine(privateSoldier);
                    break;

                case "LieutenantGeneral":
                    id        = cmdArgs[1];
                    firstName = cmdArgs[2];
                    lastName  = cmdArgs[3];
                    salary    = decimal.Parse(cmdArgs[4]);
                    var leutenantGeneral = new LieutenantGeneral(firstName, lastName, id, salary);

                    if (cmdArgs.Length >= 5)
                    {
                        for (var i = 5; i < cmdArgs.Length; i++)
                        {
                            string privateId = cmdArgs[i];
                            privateSoldier = privates[privateId];

                            leutenantGeneral.Privates.Add(privateSoldier);
                        }
                    }
                    Console.WriteLine(leutenantGeneral);
                    break;

                case "Engineer":
                    id        = cmdArgs[1];
                    firstName = cmdArgs[2];
                    lastName  = cmdArgs[3];
                    salary    = decimal.Parse(cmdArgs[4]);

                    if (Enum.TryParse(cmdArgs[5], out corps))
                    {
                        var engineer = new Engineer(firstName, lastName, id);

                        if (cmdArgs.Length >= 6)
                        {
                            for (var i = 6; i < cmdArgs.Length; i += 2)
                            {
                                var repairPartName = cmdArgs[i];
                                var repairHours    = int.Parse(cmdArgs[i + 1]);

                                var repair = new Repair(repairPartName, repairHours);
                                engineer.Repairs.Add(repair);
                            }
                        }
                        Console.WriteLine(engineer);
                    }
                    break;

                case "Commando":
                    id        = cmdArgs[1];
                    firstName = cmdArgs[2];
                    lastName  = cmdArgs[3];
                    salary    = decimal.Parse(cmdArgs[4]);

                    if (Enum.TryParse(cmdArgs[5], out corps))
                    {
                        Commando commando = new Commando(firstName, lastName, id, salary, corps);

                        if (cmdArgs.Length > 6)
                        {
                            for (var i = 6; i < cmdArgs.Length; i += 2)
                            {
                                if (Enum.TryParse(cmdArgs[i + 1], out MissionState missionState))
                                {
                                    var missionName = cmdArgs[i];
                                    var mission     = new Mission(missionName, missionState);
                                    commando.Missions.Add(mission);
                                }
                            }
                            Console.WriteLine(commando.ToString());
                        }
                    }
                    break;

                case "Spy":
                    id        = cmdArgs[1];
                    firstName = cmdArgs[2];
                    lastName  = cmdArgs[3];
                    var codeNumber = int.Parse(cmdArgs[4]);

                    var spy = new Spy(id, firstName, lastName, codeNumber);
                    Console.WriteLine(spy);
                    break;

                default:
                    throw new ArgumentException("Invalid Type of Soldier!");
                }
            }
        }
示例#15
0
 public void AddRepair(Repair repair)
 {
     this.repairs.Add(repair);
 }
示例#16
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);
            }
        }
示例#17
0
        static void Main(string[] args)
        {
            string[] line = Console.ReadLine().Split();

            var dictOfPrivates = new Dictionary <int, Private>();
            var soldiers       = new List <ISoldier>();

            while (line[0] != "End")
            {
                if (line[0] == "Private")
                {
                    var @private = new Private(int.Parse(line[1]), line[2], line[3], double.Parse(line[4]));
                    dictOfPrivates.Add(int.Parse(line[1]), @private);
                    soldiers.Add(@private);
                }
                else if (line[0] == "LeutenantGeneral")
                {
                    var leutenantGeneral = new LeutenantGeneral(int.Parse(line[1]), line[2], line[3], double.Parse(line[4]));
                    leutenantGeneral.Privates = new List <Private>();
                    for (int i = 5; i < line.Length; i++)
                    {
                        leutenantGeneral.Privates.Add(dictOfPrivates[int.Parse(line[i])]);
                    }

                    soldiers.Add(leutenantGeneral);
                }
                else if (line[0] == "Engineer")
                {
                    var engineer = new Engineer(int.Parse(line[1]), line[2], line[3], double.Parse(line[4]), line[5]);
                    for (int i = 6; i < line.Length; i += 2)
                    {
                        var repair = new Repair(int.Parse(line[i]), line[i + 1]);
                        engineer.Repairs.Add(repair);
                    }

                    soldiers.Add(engineer);
                }
                else if (line[0] == "Commando")
                {
                    var commando = new Commando(int.Parse(line[1]), line[2], line[3], double.Parse(line[4]), line[5]);
                    commando.Missions = new List <Mission>();
                    for (int i = 6; i < line.Length; i += 2)
                    {
                        var mission = new Mission(line[i], line[i + 1]);
                        commando.Missions.Add(mission);
                    }

                    soldiers.Add(commando);
                }
                else if (line[0] == "Spy")
                {
                    var spy = new Spy(int.Parse(line[1]), line[2], line[3], double.Parse(line[4]), int.Parse(line[5]));

                    soldiers.Add(spy);
                }

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

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier);
            }
        }
示例#18
0
        static void Main(string[] args)
        {
            var privates = new List <Private>();

            while (true)
            {
                string[] command = Console.ReadLine().Split();
                string   member  = command[0];
                if (member == "End")
                {
                    break;
                }
                string id        = command[1];
                string firstName = command[2];
                string lastName  = command[3];
                if (member == "Private")
                {
                    decimal salary        = decimal.Parse(command[4]);
                    var     privateMember = new Private(id, firstName, lastName, salary);
                    Console.WriteLine(privateMember);
                    privates.Add(privateMember);
                }
                else if (member == "LieutenantGeneral")
                {
                    var     privateIds = new List <string>();
                    decimal salary     = decimal.Parse(command[4]);
                    foreach (var privateId in command.Skip(4))
                    {
                        privateIds.Add(privateId);
                    }

                    var lieutenantGeneral =
                        new LieutenantGeneral(id, firstName, lastName, salary, privateIds, privates);
                    Console.WriteLine(lieutenantGeneral);
                }
                else if (member == "Engineer")
                {
                    var     repairs = new List <Repair>();
                    decimal salary  = decimal.Parse(command[4]);
                    string  corps   = command[5];
                    if (corps != "Airforces" && corps != "Marines")
                    {
                        continue;
                    }
                    for (int i = 6; i < command.Length - 1; i += 2)
                    {
                        var repair = new Repair(command[i], int.Parse(command[i + 1]));
                        repairs.Add(repair);
                    }
                    var engeener = new Engineer(id, firstName, lastName, salary, corps, repairs);
                    Console.WriteLine(engeener);
                }
                else if (member == "Commando")
                {
                    var     missions = new List <Mission>();
                    decimal salary   = decimal.Parse(command[4]);
                    string  corps    = command[5];
                    if (corps != "Airforces" && corps != "Marines")
                    {
                        continue;
                    }
                    for (int i = 6; i < command.Length - 1; i += 2)
                    {
                        string codeName = command[i];
                        string state    = command[i + 1];
                        if (state == "Finished" || state == "inProgress")
                        {
                            var mission = new Mission(codeName, state);
                            missions.Add(mission);
                        }
                    }
                    var commando = new Commando(id, firstName, lastName, salary, corps, missions);
                    Console.WriteLine(commando);
                }
                else if (member == "Spy")
                {
                    int codeNumber = int.Parse(command[4]);
                    var spy        = new Spy(id, firstName, lastName, codeNumber);
                    Console.WriteLine(spy);
                }
            }
        }
示例#19
0
        static void Main(string[] args)
        {
            List <Soldier> army = new List <Soldier>();
            string         command;

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

                switch (soldierType)
                {
                case "Private":
                    Private curPrivate = new Private(Id, firstName, lastName, decimal.Parse(curCommand[4]));
                    army.Add(curPrivate);
                    break;

                case "LieutenantGeneral":
                    LieutenantGeneral curLieutenantGeneral = new LieutenantGeneral(Id, firstName, lastName, decimal.Parse(curCommand[4]));


                    for (int i = 5; i < curCommand.Length; i++)
                    {
                        Soldier searchPrivate = army.First(el => el.Id == int.Parse(curCommand[i]) && el.GetType().Name == "Private");
                        curLieutenantGeneral.Add((Private)searchPrivate);
                    }

                    army.Add(curLieutenantGeneral);
                    break;

                case "Engineer":
                    try
                    {
                        Engineer curEngineer = new Engineer(Id, firstName, lastName, decimal.Parse(curCommand[4]), curCommand[5]);
                        string[] repairs     = curCommand.Skip(6).ToArray();
                        for (int i = 0; i < repairs.Length; i += 2)
                        {
                            Repair curRepair = new Repair(repairs[i], int.Parse(repairs[i + 1]));
                            curEngineer.Add(curRepair);
                        }

                        army.Add(curEngineer);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    break;

                case "Commando":
                    try
                    {
                        Commando curCommando = new Commando(Id, firstName, lastName, decimal.Parse(curCommand[4]), curCommand[5]);
                        string[] missions    = curCommand.Skip(6).ToArray();
                        for (int i = 0; i < missions.Length; i += 2)
                        {
                            try
                            {
                                Mission curMission = new Mission(missions[i], missions[i + 1]);
                                curCommando.Add(curMission);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }

                        army.Add(curCommando);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    break;

                case "Spy":
                    Spy curSpy = new Spy(Id, firstName, lastName, int.Parse(curCommand[4]));
                    army.Add(curSpy);
                    break;
                }
            }

            foreach (var item in army)
            {
                Console.WriteLine(item);
            }
        }
示例#20
0
文件: Program.cs 项目: AndrewTheM/OOP
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

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

                try
                {
                    var inputs = line.Split(' ');

                    string command   = inputs[0],
                           id        = inputs[1],
                           firstName = inputs[2],
                           lastName  = inputs[3];
                    double salary    = double.Parse(inputs[4].Replace('.', ','));

                    ISoldier soldier;

                    switch (command)
                    {
                    case nameof(Private):
                    {
                        soldier = new Private(id, firstName, lastName, salary);
                        break;
                    }

                    case nameof(Spy):
                    {
                        soldier = new Spy(id, firstName, lastName, int.Parse(inputs[5]));
                        break;
                    }

                    case nameof(LeutenantGeneral):
                    {
                        soldier = new LeutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 5; i < inputs.Length; ++i)
                        {
                            var priv = soldiers.OfType <Private>()
                                       .SingleOrDefault(pr => pr.Id == inputs[i]);

                            if (priv != null)
                            {
                                (soldier as LeutenantGeneral).Privates.Add(priv);
                            }
                        }
                        break;
                    }

                    case nameof(Engineer):
                    {
                        soldier = new Engineer(id, firstName, lastName, salary, inputs[5]);

                        for (int i = 6; i < inputs.Length - 1; i += 2)
                        {
                            var repair = new Repair(inputs[i], int.Parse(inputs[i + 1]));
                            (soldier as Engineer).Repairs.Add(repair);
                        }
                        break;
                    }

                    case nameof(Commando):
                    {
                        soldier = new Commando(id, firstName, lastName, salary, inputs[5]);

                        for (int i = 6; i < inputs.Length - 1; i += 2)
                        {
                            try
                            {
                                var mission = new Mission(inputs[i], inputs[i + 1]);
                                (soldier as Commando).Missions.Add(mission);
                            }
                            catch (ArgumentException)
                            {
                            }
                        }
                        break;
                    }

                    default: throw new Exception("Invalid command.");
                    }
                    ;

                    soldiers.Add(soldier);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

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

            Console.ReadKey();
        }
示例#21
0
        public static void Main()
        {
            List <Private> privates = new List <Private>();
            Corps          corps;

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

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

                if (token[0] == "Private")
                {
                    Private privatee = new Private(token[1], token[2], token[3], decimal.Parse(token[4]));
                    privates.Add(privatee);
                    Console.WriteLine(privatee.ToString());
                }
                else if (token[0] == "LieutenantGeneral")
                {
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(token[1], token[2], token[3], decimal.Parse(token[4]));

                    if (token.Length >= 5)
                    {
                        for (int i = 5; i < token.Length; i++)
                        {
                            string  privateId = token[i];
                            Private privatee  = privates.FirstOrDefault(x => x.Id == privateId);
                            lieutenantGeneral.Privates.Add(privatee);
                        }
                    }

                    Console.WriteLine(lieutenantGeneral.ToString());
                }
                else if (token[0] == "Engineer")
                {
                    if (Enum.TryParse(token[5], out corps))
                    {
                        Engineer engineer = new Engineer(token[1], token[2], token[3], decimal.Parse(token[4]), corps);

                        if (token.Length >= 6)
                        {
                            for (int i = 6; i < token.Length; i += 2)
                            {
                                string repairPartName  = token[i];
                                int    repairPartHours = int.Parse(token[i + 1]);

                                Repair repair = new Repair(repairPartName, repairPartHours);
                                engineer.Repairs.Add(repair);
                            }
                        }

                        Console.WriteLine(engineer.ToString());
                    }
                }
                else if (token[0] == "Commando")
                {
                    if (Enum.TryParse(token[5], out corps))
                    {
                        Commando commando = new Commando(token[1], token[2], token[3], decimal.Parse(token[4]), corps);

                        if (token.Length >= 6)
                        {
                            for (int i = 6; i < token.Length; i += 2)
                            {
                                string missionName     = token[i];
                                string missionProgress = token[i + 1];

                                if (Enum.TryParse(missionProgress, out MissionState missionState))
                                {
                                    Mission mission = new Mission(missionName, missionState);
                                    commando.Missions.Add(mission);
                                }
                            }

                            Console.WriteLine(commando.ToString());
                        }
                    }
                }
                else if (token[0] == "Spy")
                {
                    Spy spy = new Spy(token[1], token[2], token[3], int.Parse(token[4]));
                    Console.WriteLine(spy);
                }
                else
                {
                    throw new ArgumentException("Invalid Type of Soldier!");
                }
            }
        }
        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);
            }
        }
示例#23
0
        static void Main(string[] args)
        {
            var privateList = new List <Private>();

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

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

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

                    var privateSoldie = new Private(id, firstName, lastName, salary);
                    privateList.Add(privateSoldie);
                    Console.WriteLine(privateSoldie);
                }
                else if (input[0] == "Spy")
                {
                    id        = int.Parse(input[1]);
                    firstName = input[2];
                    lastName  = input[3];

                    int code = int.Parse(input[4]);


                    var spy = new Spy(id, firstName, lastName, code);
                    Console.WriteLine(spy);
                }
                else if (input[0] == "Engineer")
                {
                    id        = int.Parse(input[1]);
                    firstName = input[2];
                    lastName  = input[3];
                    salary    = decimal.Parse(input[4]);
                    corp      = input[5];
                    try
                    {
                        var engineer = new Engineer(id, firstName, lastName, salary, corp);
                        //string[] result = input.Skip(6).ToArray();
                        for (int i = 6; i < input.Length; i += 2)
                        {
                            string partname = input[i];
                            int    hours    = int.Parse(input[i + 1]);
                            var    repair   = new Repair(partname, hours);
                            engineer.Add(repair);
                        }
                        Console.WriteLine(engineer);
                    }
                    catch (ArgumentException)
                    {
                        //continue;
                    }
                }
                else if (input[0] == "Commando")
                {
                    id        = int.Parse(input[1]);
                    firstName = input[2];
                    lastName  = input[3];
                    salary    = decimal.Parse(input[4]);
                    corp      = input[5];

                    try
                    {
                        var commando = new Commando(id, firstName, lastName, salary, corp);
                        //string[] result = input.Skip(6).ToArray();

                        for (int i = 6; i < input.Length; i += 2)
                        {
                            string partname = input[i];
                            string state    = input[i + 1];
                            try
                            {
                                var repair = new Mission(partname, state);
                                commando.Add(repair);
                            }
                            catch (ArgumentException)
                            {
                                //continue;
                            }
                        }
                        Console.WriteLine(commando);
                    }
                    catch (ArgumentException)
                    {
                        //continue;
                    }
                }
                else if (input[0] == "LieutenantGeneral")
                {
                    id        = int.Parse(input[1]);
                    firstName = input[2];
                    lastName  = input[3];
                    salary    = decimal.Parse(input[4]);



                    var leutenant = new LieutenantGeneral(id, firstName, lastName, salary);
                    //string[] result = input.Skip(5).ToArray();

                    for (int i = 5; i < input.Length; i++)
                    {
                        int num = int.Parse(input[i]);
                        var currentPrivateSoldier = privateList.FirstOrDefault(x => x.Id == num);
                        leutenant.Add(currentPrivateSoldier);
                    }
                    Console.WriteLine(leutenant);
                }
            }
        }
示例#24
0
        public void Run()
        {

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

                string[] tokens = input.Split();
                string type = tokens[0];



                if (type == "Private")
                {
                    string id = tokens[1];
                    string firstName = tokens[2];
                    string lastName = tokens[3];
                    double salary = double.Parse(tokens[4]);
                    Private priva = new Private(id, firstName, lastName, salary);
                    privates.Add(priva);
                    Console.WriteLine(priva);


                }
                else if (type == "Spy")
                {
                    Spy spy = new Spy(tokens[1], tokens[2], tokens[3],int.Parse(tokens[4]));
                    Console.WriteLine(spy);
                }
                else if (type == "LieutenantGeneral")
                {
                    LeutenantGeneral lg = new LeutenantGeneral(tokens[1], tokens[2],
                        tokens[3], double.Parse(tokens[4]));
                    if (tokens.Length > 5)
                    {
                        for (int i = 5; i < tokens.Length; i++)
                        {
                            foreach (var item in privates)
                            {
                                if (item.Id == tokens[i])
                                {
                                    lg.Privates.Add(item);
                                }
                            }
                        }
                    }
                    Console.WriteLine(lg);

                }
                else if (type == "Engineer")
                {
                    Engineer engineer = new Engineer(tokens[1], tokens[2],tokens[3], double.Parse(tokens[4]), tokens[5]);

                    if (tokens.Length > 6)
                    {
                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            string part = tokens[i];
                            int hours = int.Parse(tokens[i + 1]);
                            Repair repair = new Repair(part, hours);
                            engineer.Repairs.Add(repair);
                        }
                    }
                    Console.WriteLine(engineer);
                }
                else if (type == "Commando")
                {
                    Commando commando = new Commando(tokens[1], tokens[2],
                        tokens[3], double.Parse(tokens[4]), tokens[5]);
                    if (tokens.Length > 6)
                    {
                        for (int i = 6; i < tokens.Length; i += 2)
                        {
                            string codeName = tokens[i];
                            string state = tokens[i + 1];
                            Mission mission = new Mission(codeName, state);
                            commando.Missions.Add(mission);
                        }
                    }
                    Console.WriteLine(commando);



                }


            }
        }
示例#25
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);
            }
        }
示例#26
0
文件: StartUp.cs 项目: stilyssk/C-OOP
        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);
            }
        }
示例#27
0
        static void Main(string[] args)
        {
            List <Private> privates = new List <Private>();

            while (true)
            {
                try
                {
                    string[] soldierArgs = Console.ReadLine().Split();
                    string   type        = soldierArgs[0];

                    if (type == "End")
                    {
                        break;
                    }
                    string id        = soldierArgs[1];
                    string firstName = soldierArgs[2];
                    string lastName  = soldierArgs[3];

                    if (type == "Spy")
                    {
                        int codeNumber = int.Parse(soldierArgs[4]);
                        var spy        = new Spy(id, firstName, lastName, codeNumber);
                        Console.WriteLine(spy);
                    }
                    else
                    {
                        decimal salary = decimal.Parse(soldierArgs[4]);
                        if (type == "Private")
                        {
                            var @private = new Private(id, firstName, lastName, salary);
                            privates.Add(@private);
                            Console.WriteLine(@private);
                        }
                        else if (type == "LieutenantGeneral")
                        {
                            var privatesGeneral = new List <Private>();
                            for (int i = 5; i < soldierArgs.Length; i++)
                            {
                                var currentPrivate = privates.FirstOrDefault(x => x.Id == soldierArgs[i]);
                                privatesGeneral.Add(currentPrivate);
                            }
                            var leutenant = new LieutenantGeneral(id, firstName, lastName, salary, privatesGeneral);
                            Console.WriteLine(leutenant);
                        }
                        else if (type == "Engineer")
                        {
                            var    repairs = new List <Repair>();
                            string corps   = soldierArgs[5];
                            for (int i = 6; i < soldierArgs.Length; i += 2)
                            {
                                string name   = soldierArgs[i];
                                int    hours  = int.Parse(soldierArgs[i + 1]);
                                var    repair = new Repair(name, hours);
                                repairs.Add(repair);
                            }
                            var engineer = new Engineer(id, firstName, lastName, salary, corps, repairs);
                            Console.WriteLine(engineer);
                        }
                        else if (type == "Commando")
                        {
                            var    missions = new List <Mission>();
                            string corps    = soldierArgs[5];

                            for (int i = 6; i < soldierArgs.Length; i += 2)
                            {
                                try
                                {
                                    string name    = soldierArgs[i];
                                    string state   = soldierArgs[i + 1];
                                    var    mission = new Mission(name, state);
                                    missions.Add(mission);
                                }
                                catch (Exception)
                                {
                                }
                            }
                            var commando = new Commando(id, firstName, lastName, salary, corps, missions);
                            Console.WriteLine(commando);
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
示例#28
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);
            }
        }
示例#29
0
        static void Main()
        {
            string command;
            var    soldiers = new List <ISoldier>();

            while ((command = Console.ReadLine()) != "End")
            {
                string[] entry = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                if (entry[0] == "Private")
                {
                    var newPrivate = new Private(entry[1], entry[2], entry[3], decimal.Parse(entry[4]));
                    soldiers.Add(newPrivate);
                }
                else if (entry[0] == "LieutenantGeneral")
                {
                    var newLtGen = new LieutenantGeneral(entry[1], entry[2], entry[3], decimal.Parse(entry[4]));

                    for (int i = 5; i < entry.Length; i++)
                    {
                        newLtGen.Privates.Add(soldiers.FirstOrDefault(x => x.Id == entry[i]));
                    }

                    soldiers.Add(newLtGen);
                }
                else if (entry[0] == "Engineer" && (entry[5] == "Marines" || entry[5] == "Airforces"))
                {
                    var newEngineer = new Engineer(entry[1], entry[2], entry[3], decimal.Parse(entry[4]), entry[5]);

                    for (int i = 6; i < entry.Length - 1; i += 2)
                    {
                        var newRepair = new Repair(entry[i], int.Parse(entry[i + 1]));
                        newEngineer.Repairs.Add(newRepair);
                    }

                    soldiers.Add(newEngineer);
                }
                else if (entry[0] == "Commando" && (entry[5] == "Marines" || entry[5] == "Airforces"))
                {
                    var newCommando = new Commando(entry[1], entry[2], entry[3], decimal.Parse(entry[4]), entry[5]);

                    for (int i = 6; i < entry.Length - 1; i += 2)
                    {
                        if (entry[i + 1] == "inProgress" || entry[i + 1] == "Finished")
                        {
                            var newMission = new Mission(entry[i], entry[i + 1]);
                            newCommando.Missions.Add(newMission);
                        }
                    }

                    soldiers.Add(newCommando);
                }
                else if (entry[0] == "Spy")
                {
                    var newSpy = new Spy(entry[1], entry[2], entry[3], int.Parse(entry[4]));
                    soldiers.Add(newSpy);
                }
            }

            Console.WriteLine(string.Join(Environment.NewLine, soldiers));
        }
示例#30
0
 public void AddRepair(Repair repairs)
 {
     Repairs.Add(repairs);
 }