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); }
static void CreateEngineer(string[] tokens) { List <IRepair> repairs = new List <IRepair>(); for (int i = 6; i < tokens.Length; i += 2) { repairs.Add(new Repair(tokens[i], int.Parse(tokens[i + 1]))); } try { Engineer eng = new Engineer(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5], repairs); Print(eng); } catch (Exception) { } }
private Engineer CreateEngineer(string[] soldierParams) { //Engineer<id> < firstName > < lastName > < salary > < corps > < repair1Part > < repair1Hours > … < repairNPart > < repairNHours > 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); } Engineer engineer = new Engineer(id, firstName, lastName, salary, corps); for (int i = 5; i < soldierParams.Length; i += 2) { string repairName = soldierParams[i]; int workerHours = int.Parse(soldierParams[i + 1]); engineer.AddRepair(repairName, workerHours); } return(engineer); }
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)); }
public static void Main(string[] args) { var input = Console.ReadLine(); var list = new List <ISoldier>(); var privates = new List <Private>(); // var general = new List<LieutenantGeneral>(); while (input != "End") { var line = input.Split(); if (line[0] == "Private") { list.Add(new Private(line[1], line[2], line[3], decimal.Parse(line[4]))); privates.Add(new Private(line[1], line[2], line[3], decimal.Parse(line[4]))); } if (line[0] == "Spy") { list.Add(new Spy(line[1], line[2], line[3], int.Parse(line[4]))); } if (line[0] == "LieutenantGeneral") { var listPrivateIds = new List <string>(); var leit = new LieutenantGeneral(line[1], line[2], line[3], decimal.Parse(line[4])); if (line.Length > 4) { for (int i = 5; i < line.Length; i++) { foreach (var item in privates) { if (item.Id == line[i]) { leit.Privates.Add(item); } } } leit.ListPrivateIds.AddRange(listPrivateIds); } list.Add(leit); } if (line[0] == "Engineer") { var eng = new Engineer(line[1], line[2], line[3], decimal.Parse(line[4]), line[5]); var repaits = new List <Repair>(); for (int i = 6; i < line.Length; i = i + 2) { repaits.Add(new Repair(line[i], int.Parse(line[i + 1]))); } eng.Repairs.AddRange(repaits); if (line[5] == "Airforces" || line[5] == "Marines") { list.Add(eng); } } if (line[0] == "Commando") { var comm = new Commando(line[1], line[2], line[3], decimal.Parse(line[4]), line[5]); var currList = new List <Mission>(); for (int i = 6; i < line.Length; i = i + 2) { if (line[i + 1] == "inProgress" || line[i + 1] == "Finished") { currList.Add(new Mission(line[i], line[i + 1])); } } comm.Mission.AddRange(currList); if (line[5] == "Airforces" || line[5] == "Marines") { list.Add(comm); } } input = Console.ReadLine(); } foreach (var item in list) { Console.WriteLine(item); } }
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) { } } }
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); } }
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)); }
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); } }
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); } } }
static void Main(string[] args) { List <ISoldier> soldiers = new List <ISoldier>(); string[] input = Console.ReadLine().Split(); while (input[0].ToLower() != "end") { string title = input[0].ToLower(); int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; if (title == "private") { decimal salary = decimal.Parse(input[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (title == "lieutenantgeneral") { decimal salary = decimal.Parse(input[4]); List <IPrivate> privates = GetLieutenantGeneralPrivates(input.Skip(5).ToArray(), soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (title == "engineer") { decimal salary = decimal.Parse(input[4]); string corps = input[5]; List <IRepair> repairs = GetEngineerRepairs(input.Skip(6).ToArray()); if (corps == "Airforces") { Engineer engineer = new Engineer(id, firstName, lastName, salary, SoldiersCorpsEnum.Airforces, repairs); soldiers.Add(engineer); } else if (corps == "Marines") { Engineer engineer = new Engineer(id, firstName, lastName, salary, SoldiersCorpsEnum.Marines, repairs); soldiers.Add(engineer); } } else if (title == "commando") { decimal salary = decimal.Parse(input[4]); string corps = input[5]; List <IMission> missions = GetCommandoMissions(input.Skip(6).ToArray()); if (corps == "Airforces") { Commando commando = new Commando(id, firstName, lastName, salary, SoldiersCorpsEnum.Airforces, missions); soldiers.Add(commando); } else if (corps == "Marines") { Commando commando = new Commando(id, firstName, lastName, salary, SoldiersCorpsEnum.Marines, missions); soldiers.Add(commando); } } else if (title == "spy") { int codeNumber = int.Parse(input[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } input = Console.ReadLine().Split(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
static void Main(string[] args) { List <Soldier> soldiers = new List <Soldier>(); string soldier = Console.ReadLine(); while (soldier != "End") { string[] soldierArgs = soldier.Split(); string soldierType = soldierArgs[0]; string id = soldierArgs[1]; string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; if (soldierType == "Private") { decimal salary = decimal.Parse(soldierArgs[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (soldier == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } else if (soldierType == "LieutenantGeneral") { decimal salary = decimal.Parse(soldierArgs[4]); List <string> ids = soldierArgs.Skip(5).ToList(); List <Private> privates = GetPrivates(ids, soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> repairArgs = soldierArgs.Skip(6).ToList(); List <Repair> repairs = GetRepairs(repairArgs); try { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(engineer); } catch (Exception) { } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> missionArgs = soldierArgs.Skip(6).ToList(); List <Mission> missions = GetMissions(missionArgs); try { Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(commando); } catch (Exception) { } } soldier = Console.ReadLine(); } foreach (var soldierObject in soldiers) { Console.WriteLine(soldierObject); } }
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); } }
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); } }
private static void FillList(List <ISoldier> soldiers, string[] input) { switch (input[0]) { case "Private": Private newPrivate = new Private(double.Parse(input[4]), int.Parse(input[1]), input[2], input[3]); soldiers.Add(newPrivate); break; case "Spy": Spy spy = new Spy(int.Parse(input[4]), int.Parse(input[1]), input[2], input[3]); soldiers.Add(spy); break; case "LieutenantGeneral": LuitenantGeneral luitenantGeneral = new LuitenantGeneral(new List <Private>(), double.Parse(input[4]), int.Parse(input[1]), input[2], input[3]); if (input.Length >= 5) { for (int i = 5; i < input.Length; i++) { luitenantGeneral.Privates.Add(soldiers.Find(x => x.Id == int.Parse(input[i])) as Private); } } soldiers.Add(luitenantGeneral); break; case "Engineer": Engineer engineer = new Engineer(new List <Repairs>(), input[5], double.Parse(input[4]), int.Parse(input[1]), input[2], input[3]); if (input.Length >= 6) { for (int i = 6; i < input.Length - 1; i += 2) { Repairs repair = new Repairs(); repair.HoursWorked = int.Parse(input[i + 1]); repair.PartName = input[i]; engineer.Repairs.Add(repair); } } soldiers.Add(engineer); break; case "Commando": Commando commando = new Commando(new List <Mission>(), input[5], double.Parse(input[4]), int.Parse(input[1]) , input[2], input[3]); if (input.Length >= 6) { for (int i = 6; i < input.Length - 1; i += 2) { Mission mission = new Mission(); mission.CodeName = input[i]; if (input[i + 1] == "Finished") { mission.State = true; commando.Missions.Add(mission); } if (input[i + 1] == "inProgress") { mission.State = false; commando.Missions.Add(mission); } } } soldiers.Add(commando); break; } }
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); } }
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); } } }
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); } }
public static void Main() { List <ISoldier> soldiers = new List <ISoldier>(); string command = Console.ReadLine(); while (command != "End") { string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries); string id = tokens[1]; string firstName = tokens[2]; string lastName = tokens[3]; if (tokens[0] == "Private") { decimal salary = decimal.Parse(tokens[4]); Private current = new Private(firstName, lastName, id, salary); soldiers.Add(current); } else if (tokens[0] == "Commando") { decimal salary = decimal.Parse(tokens[4]); string corps = tokens[5]; Commando current = new Commando(firstName, lastName, id, salary, corps, tokens.Skip(6).ToArray()); if (current.Corps != null) { soldiers.Add(current); } } else if (tokens[0] == "LieutenantGeneral") { decimal salary = decimal.Parse(tokens[4]); List <Private> privates = new List <Private>(); for (int i = 5; i < tokens.Length; i++) { Private soldier = (Private)soldiers.First(s => s.Id == tokens[i]); privates.Add(soldier); } LieutenantGeneral current = new LieutenantGeneral(firstName, lastName, id, salary, privates); soldiers.Add(current); } else if (tokens[0] == "Engineer") { decimal salary = decimal.Parse(tokens[4]); string corps = tokens[5]; Engineer current = new Engineer(firstName, lastName, id, salary, corps, tokens.Skip(6).ToArray()); if (current.Corps != null) { soldiers.Add(current); } } else if (tokens[0] == "Spy") { int codeNumber = int.Parse(tokens[4]); Spy current = new Spy(firstName, lastName, id, codeNumber); soldiers.Add(current); } command = Console.ReadLine(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
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()); } }
public static void Main(string[] args) { List <Soldier> soldiers = new List <Soldier>(); while (true) { string soldier = Console.ReadLine(); if (soldier == "End") { break; } string[] soldierArgs = soldier.Split(); string soldierType = soldierArgs[0]; string id = soldierArgs[1]; string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; if (soldierType == "Private") { decimal salary = decimal.Parse(soldierArgs[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (soldierType == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } else if (soldierType == "LieutenantGeneral") { decimal salary = decimal.Parse(soldierArgs[4]); // Turning the last elements of the input to List of Ids List <string> ids = soldierArgs.Skip(5).ToList(); // Method that accepts List<string> ids and List<soldiers> soldiers => the one we created in the begging // to hold all the types of soldiers HashSet <Private> privates = GetPrivates(ids, soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> repairArgs = soldierArgs.Skip(6).ToList(); List <Repair> repairs = GetRepairs(repairArgs); try { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(engineer); } catch (Exception) { continue; } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> missionArgs = soldierArgs.Skip(6).ToList(); List <Mission> missions = GetMissions(missionArgs); try { Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(commando); } catch (Exception) { continue; } } } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
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); } }
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!"); } } }
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 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); } } }
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(); }
public static void Main() { List <Soldier> soldiers = new List <Soldier>(); string userInput = Console.ReadLine(); while (userInput != "End") { string[] soldierInfo = userInput.Split(" "); string soldierType = soldierInfo[0]; int id = int.Parse(soldierInfo[1]); string firstName = soldierInfo[2]; string lastName = soldierInfo[3]; decimal salary; bool isSalary = decimal.TryParse(soldierInfo[4], out salary); Soldier newSoldier; switch (soldierType) { case "Private": newSoldier = new Private(id, firstName, lastName, salary); soldiers.Add(newSoldier); break; case "LieutenantGeneral": int[] privatesToAddIds = soldierInfo.Skip(5).Select(int.Parse).ToArray(); List <Private> privates = GetPrivates(privatesToAddIds, soldiers); newSoldier = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(newSoldier); break; case "Spy": int codeNumber = int.Parse(soldierInfo[4]); Spy newSpy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(newSpy); break; case "Engineer": string corps = soldierInfo[5]; List <Repair> repairs = GetRepairs(soldierInfo); try { Engineer newEngineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(newEngineer); } catch { userInput = Console.ReadLine(); continue; } break; case "Commando": string commandoCorps = soldierInfo[5]; List <Mission> missions = GetMissions(soldierInfo); Commando newCommando = new Commando(id, firstName, lastName, salary, commandoCorps, missions); soldiers.Add(newCommando); break; } userInput = Console.ReadLine(); } Console.WriteLine($"{string.Join(Environment.NewLine, soldiers)}"); }
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) { 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); } }
static void Main(string[] args) { List <ISoldier> allSoldiers = new List <ISoldier>(); HashSet <Private> privates = new HashSet <Private>(); //string[] input = Console.ReadLine().Split(); //while (input[0] != "End") //{ // switch (input[0]) // { // case "Private": // Private priv = new Private(int.Parse(input[1]), input[2], input[3], decimal.Parse(input[4])); // allSoldiers.Add(priv); // privates.Add(priv); // break; // case "LeutenantGeneral": // LieutenantGeneral lieutenantGeneral = new LieutenantGeneral // (int.Parse(input[1]), input[2], input[3], decimal.Parse(input[4])); // input = input.Skip(5).ToArray(); // for (int i = 0; i < input.Length; i++) // { // Private privateToAdd = privates.First(p => p.Id == int.Parse(input[i])); // lieutenantGeneral.Privates.Add(privateToAdd); // } // allSoldiers.Add(lieutenantGeneral); // break; // case "Engineer": // Engineer engineer = new Engineer(int.Parse(input[1]), input[2], input[3], decimal.Parse(input[4]), input[5]); // if (engineer.Corps == "Airforces" || engineer.Corps == "Marines") // { // input = input.Skip(6).ToArray(); // for (int i = 0; i < input.Length; i += 2) // { // Repair repair = new Repair(input[i], int.Parse(input[i + 1])); // engineer.Repairs.Add(repair); // } // allSoldiers.Add(engineer); // } // break; // case "Commando": // Commando commando = new Commando(int.Parse(input[1]), input[2], input[3], decimal.Parse(input[4]), input[5]); // if (commando.Corps == "Airforces" || commando.Corps == "Marines") // { // input = input.Skip(6).ToArray(); // for (int i = 0; i < input.Length; i++) // { // Mission mission = new Mission(input[i], input[i + 1]); // if (mission.State == "inProgress" || mission.State == "Finished") // { // commando.Missions.Add(mission); // } // } // allSoldiers.Add(commando); // } // break; // case "Spy": // Spy spy = new Spy(int.Parse(input[1]), input[2], input[3], int.Parse(input[4])); // allSoldiers.Add(spy); // break; // } // input = Console.ReadLine().Split(); //} //foreach (var soldier in allSoldiers) //{ // Console.WriteLine(soldier.ToString()); //} while (true) { string[] data = Console.ReadLine().Split(); string soldierType = data[0]; data = data.Skip(1).ToArray(); if (soldierType == "End") { break; } switch (soldierType) { case "Private": Private priv = new Private(int.Parse(data[0]), data[1], data[2], decimal.Parse(data[3])); allSoldiers.Add(priv); privates.Add(priv); break; case "LieutenantGeneral": LieutenantGeneral lieutenant = new LieutenantGeneral(int.Parse(data[0]), data[1], data[2], decimal.Parse(data[3])); data = data.Skip(4).ToArray(); for (int i = 0; i < data.Length; i++) { Private privToAdd = privates.First(p => p.Id == int.Parse(data[i])); lieutenant.Privates.Add(privToAdd); } allSoldiers.Add(lieutenant); break; case "Engineer": Engineer engineer = new Engineer(int.Parse(data[0]), data[1], data[2], decimal.Parse(data[3]), data[4]); if (engineer.Corps == "Airforces" || engineer.Corps == "Marines") { data = data.Skip(5).ToArray(); for (int i = 0; i < data.Length; i += 2) { Repair repair = new Repair(data[i], int.Parse(data[i + 1])); engineer.Repairs.Add(repair); } allSoldiers.Add(engineer); } break; case "Commando": Commando commando = new Commando(int.Parse(data[0]), data[1], data[2], decimal.Parse(data[3]), data[4]); if (commando.Corps == "Airforces" || commando.Corps == "Marines") { data = data.Skip(5).ToArray(); for (int i = 0; i < data.Length; i += 2) { Mission mission = new Mission(data[i], data[i + 1]); if (mission.State == "Finished" || mission.State == "inProgress") { commando.Missions.Add(mission); } } allSoldiers.Add(commando); } break; case "Spy": Spy spy = new Spy(int.Parse(data[0]), data[1], data[2], int.Parse(data[3])); allSoldiers.Add(spy); break; } GC.Collect(); } foreach (var soldier in allSoldiers) { Console.WriteLine(soldier.ToString()); } }