static void CreateLieutenantGeneral(string[] tokens) { List <IPrivate> privates = new List <IPrivate>(); for (int i = 5; i < tokens.Length; i++) { var priv = allPrivates.FirstOrDefault(p => p.Id == tokens[i]); privates?.Add(priv); } LieutenantGeneral lieutenant = new LieutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), privates); Print(lieutenant); }
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); }
static void Main(string[] args) { HashSet <Soldier> soldiers = new HashSet <Soldier>(); while (true) { string[] inputArgs = Console.ReadLine().Split(); string type = inputArgs[0]; if (type == "End") { break; } string id = inputArgs[1]; string firstName = inputArgs[2]; string lastName = inputArgs[3]; if (type == "Private") { decimal salary = decimal.Parse(inputArgs[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (type == "Spy") { int code = int.Parse(inputArgs[4]); Spy spy = new Spy(id, firstName, lastName, code); soldiers.Add(spy); } else if (type == "LieutenantGeneral") { decimal salary = decimal.Parse(inputArgs[4]); HashSet <string> ids = inputArgs.Skip(5).ToHashSet(); HashSet <Private> privates = new HashSet <Private>(); GetSoldiers(ids, soldiers, privates); LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(general); } else if (type == "Engineer") { decimal salary = decimal.Parse(inputArgs[4]); string corps = inputArgs[5]; List <string> repairArgs = inputArgs.Skip(6).ToList(); var repairs = new HashSet <Repair>(); GetRepairs(repairArgs, repairs); try { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(engineer); } catch (Exception) { } } else if (type == "Commando") { decimal salary = decimal.Parse(inputArgs[4]); string corps = inputArgs[5]; List <string> mission = inputArgs.Skip(6).ToList(); var missions = new HashSet <Mission>(); GetMissions(mission, missions); try { Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(commando); } catch (Exception) { } } } foreach (var item in soldiers) { Console.WriteLine(item); } }