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); } }
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); } } }