private static void AddCommando(List <ISoldier> allSoldiers, string[] tokens) { var commando = new Commando(int.Parse(tokens[1]), tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); if (commando.Corps != null) { for (int i = 6; i < tokens.Length; i += 2) { var missions = new Mission(tokens[i], tokens[i + 1]); commando.AddMission(missions); } allSoldiers.Add(commando); } }
public void Run() { string command = Console.ReadLine(); while (command != "End") { string[] commandArgs = command.Split(); string type = commandArgs[0]; int id = int.Parse(commandArgs[1]); string firstName = commandArgs[2]; string lastName = commandArgs[3]; decimal salary = decimal.Parse(commandArgs[4]); if (type == "Private") { Private @private = new Private(id, firstName, lastName, salary); this.army.Add(@private); } else if (type == "LieutenantGeneral") { int[] privatesId = commandArgs .Skip(5) .Select(int.Parse) .ToArray(); LieutenantGeneral lieutenant = new LieutenantGeneral(id, firstName, lastName, salary); foreach (var pid in privatesId) { ISoldier soldierToAdd = this.army .First(s => s.Id == pid); lieutenant.AddPrivate((Private)soldierToAdd); } this.army.Add(lieutenant); } else if (type == "Engineer") { try { string corps = commandArgs[5]; string[] repairArgs = commandArgs .Skip(6) .ToArray(); Engineer engineer = new Engineer(id, firstName, lastName, salary, corps); 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); } this.army.Add(engineer); } catch (InvalidCorpsExceptions ice) { } } else if (type == "Commando") { try { string corps = commandArgs[5]; ICommando comando = new Commando(id, firstName, lastName, salary, corps); string[] missionsArgs = commandArgs .Skip(6) .ToArray(); for (int i = 0; i < missionsArgs.Length; i += 2) { try { string codeName = missionsArgs[i]; string state = missionsArgs[i + 1]; IMission mission = new Mission(codeName, state); comando.AddMission(mission); } catch (InvalidStatesExceptions ise) { } } this.army.Add(comando); } catch (InvalidCorpsExceptions ice) { continue; } } else if (type == "Spy") { int codeNumber = (int)salary; ISpy spy = new Spy(id, firstName, lastName, codeNumber); this.army.Add(spy); } command = Console.ReadLine(); } foreach (var soldier in this.army) { Type type = soldier.GetType(); Object actual = Convert.ChangeType(soldier, type); Console.WriteLine(soldier.ToString()); } }
static void Main() { var somting = Assembly.GetExecutingAssembly(); var soldiersList = new List <Soldier>(); string soldierInfo = Console.ReadLine(); while (soldierInfo != "End") { var info = soldierInfo.Split(" ", StringSplitOptions.RemoveEmptyEntries); string id = info[1]; string firstName = info[2]; string lastName = info[3]; decimal salary = 0; string corps = string.Empty; switch (info[0]) { case "Private": salary = decimal.Parse(info[4]); Private @private = new Private(id, firstName, lastName, salary); soldiersList.Add(@private); break; case "LieutenantGeneral": salary = decimal.Parse(info[4]); LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary); var targetId = info.Skip(5); foreach (var currentID in targetId) { var currentSoldier = soldiersList.Where(x => x.Id == currentID).FirstOrDefault(); if (currentSoldier != null) { var type = currentSoldier.GetType().Name; if (type == "Private") { general.AddSoldier((Private)currentSoldier); } } } soldiersList.Add(general); break; case "Engineer": salary = decimal.Parse(info[4]); corps = info[5]; if (corps == "Airforces" || corps == "Marines") { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps); var repaiers = info.Skip(6).ToArray(); while (repaiers.Length != 0) { var repaierInfo = repaiers.Take(2).ToArray(); Repairs repairs = new Repairs(repaierInfo[0], int.Parse(repaierInfo[1])); engineer.AddRepairs(repairs); repaiers = repaiers.Skip(2).ToArray(); } soldiersList.Add(engineer); } break; case "Commando": salary = decimal.Parse(info[4]); corps = info[5]; if (corps == "Airforces" || corps == "Marines") { Commando commando = new Commando(id, firstName, lastName, salary, corps); var missions = info.Skip(6).ToArray(); while (missions.Length != 0) { var missionInfo = missions.Take(2).ToArray(); Missions missions1 = new Missions(missionInfo[0], missionInfo[1]); if (missions1.State == "Finished" || missions1.State == "inProgress") { commando.AddMission(missions1); } missions = missions.Skip(2).ToArray(); } soldiersList.Add(commando); } break; case "Spy": string codeNumber = info[4]; Spy spy = new Spy(id, firstName, lastName, int.Parse(codeNumber)); soldiersList.Add(spy); break; default: break; } soldierInfo = Console.ReadLine(); } soldiersList.ForEach(Console.WriteLine); }