private static void AddLeutenantGeneral(List <ISoldier> allSoldiers, string[] tokens) { var lGeneral = new LeutenantGeneral(int.Parse(tokens[1]), tokens[2], tokens[3], double.Parse(tokens[4])); for (int i = 5; i < tokens.Length; i++) { Private currentPrivate = (Private)allSoldiers.First(a => a.Id == int.Parse(tokens[i])); lGeneral.Privates.Add(currentPrivate); } allSoldiers.Add(lGeneral); }
static void Main(string[] args) { Private p = new Private("1", "Pesho", "Peshev", 22.22); Private p1 = new Private("222", "Toncho", "Tonchev", 80.08); LeutenantGeneral l = new LeutenantGeneral("3", "Joro", "Jorev", 100); l.Privates.Add(p); l.Privates.Add(p1); Commando c = new Commando("13", "Stamat", "Stamov", 13.1, "Airforces"); Console.WriteLine(p); Console.Write(l); Console.WriteLine(c); c.Misions.Add(new Mision("1", "2")); }
private static List <ISoldier> ReadSoldiers() { List <ISoldier> soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { string[] soldierInfo = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (soldierInfo[0]) { case "Private": soldiers.Add(new Private(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]))); break; case "LeutenantGeneral": LeutenantGeneral general = new LeutenantGeneral(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4])); for (int index = 5; index < soldierInfo.Length; index++) { var privat = soldiers.First(s => s.Id == int.Parse(soldierInfo[index])); general.Privates.Add(privat); } soldiers.Add(general); break; case "Engineer": if (soldierInfo[5] != "Airforces" && soldierInfo[5] != "Marines") { continue; } Engineer engineer = new Engineer(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]), soldierInfo[5]); for (int index = 6; index < soldierInfo.Length; index += 2) { engineer.Repairs.Add(new Repair(soldierInfo[index], soldierInfo[index + 1])); } soldiers.Add(engineer); break; case "Commando": if (soldierInfo[5] != "Airforces" && soldierInfo[5] != "Marines") { continue; } Commando commando = new Commando(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], decimal.Parse(soldierInfo[4]), soldierInfo[5]); for (int index = 6; index < soldierInfo.Length; index += 2) { if (soldierInfo[index + 1] == "inProgress" || soldierInfo[index + 1] == "Finished") { commando.Missions.Add(new Mission(soldierInfo[index], soldierInfo[index + 1])); } } soldiers.Add(commando); break; case "Spy": Spy spy = new Spy(int.Parse(soldierInfo[1]), soldierInfo[2], soldierInfo[3], long.Parse(soldierInfo[4])); soldiers.Add(spy); break; default: break; } } return(soldiers); }
private static List <ISoldier> GetSoldiers() { var soldiers = new List <ISoldier>(); while (true) { var input = Console.ReadLine(); if (input == "End") { break; } var soldierTokens = ParseInput(input); var type = soldierTokens[0]; var id = soldierTokens[1]; var firstName = soldierTokens[2]; var lastName = soldierTokens[3]; double salary = 0; switch (type) { case "Private": salary = double.Parse(soldierTokens[4]); var privateSoldier = new Private(id, firstName, lastName, salary); soldiers.Add(privateSoldier); break; case "LeutenantGeneral": salary = double.Parse(soldierTokens[4]); var leutenantGeneral = new LeutenantGeneral(id, firstName, lastName, salary); var privatesIds = soldierTokens.Skip(5); foreach (var privateId in privatesIds) { var priv = (Private)soldiers.FirstOrDefault(s => s.Id == privateId); leutenantGeneral.Privates.Add(priv); } soldiers.Add(leutenantGeneral); break; case "Engineer": salary = double.Parse(soldierTokens[4]); var corps = soldierTokens[5]; try { var engineer = new Engineer(id, firstName, lastName, salary, corps); var repairs = soldierTokens.Skip(6).ToList(); for (int i = 0; i < repairs.Count; i += 2) { var repair = new Repair(repairs[i], int.Parse(repairs[i + 1])); engineer.Repairs.Add(repair); } soldiers.Add(engineer); } catch (Exception) { break; } break; case "Commando": salary = double.Parse(soldierTokens[4]); corps = soldierTokens[5]; try { var commando = new Commando(id, firstName, lastName, salary, corps); var missions = soldierTokens.Skip(6).ToList(); for (int i = 0; i < missions.Count; i += 2) { try { var mission = new Mission(missions[i], missions[i + 1]); commando.Missions.Add(mission); } catch (Exception) { continue; } } soldiers.Add(commando); } catch (Exception) { break; } break; case "Spy": var codeNumber = int.Parse(soldierTokens[4]); var spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); break; default: break; } } return(soldiers); }
public static void Main() { var input = Console.ReadLine(); IList <Soldier> soldiers = new List <Soldier>(); while (input != "End") { var command = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (command[0]) { case "Private": string id = command[1]; string firstName = command[2]; string lastName = command[3]; double salary = double.Parse(command[4]); var currentPrivate = new Private(id, firstName, lastName, salary); soldiers.Add(currentPrivate); break; case "LeutenantGeneral": id = command[1]; firstName = command[2]; lastName = command[3]; salary = double.Parse(command[4]); var privateNumbers = new List <Private>(); for (int i = 5; i < command.Length; i++) { string privateId = command[i]; var searchedPrivate = soldiers.SingleOrDefault(s => s.Id == privateId); if (searchedPrivate != null) { privateNumbers.Add(searchedPrivate as Private); } } var currentLeutenant = new LeutenantGeneral(id, firstName, lastName, salary, privateNumbers); soldiers.Add(currentLeutenant); break; case "Engineer": id = command[1]; firstName = command[2]; lastName = command[3]; salary = double.Parse(command[4]); string corps = command[5]; IList <Repair> repairs = new List <Repair>(); for (int i = 6; i < command.Length - 1; i += 2) { string repairName = command[i]; int repairHours = int.Parse(command[i + 1]); var currentRepair = new Repair(repairName, repairHours); repairs.Add(currentRepair); } try { var currentEngineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(currentEngineer); } catch (ArgumentException) { } break; case "Commando": id = command[1]; firstName = command[2]; lastName = command[3]; salary = double.Parse(command[4]); corps = command[5]; IList <Mission> missions = new List <Mission>(); for (int i = 6; i < command.Length - 1; i += 2) { string missionCodeName = command[i]; string missionState = command[i + 1]; try { var currentMission = new Mission(missionCodeName, missionState); missions.Add(currentMission); } catch (ArgumentException) { } } try { var currentCommando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(currentCommando); } catch (ArgumentException) { } break; case "Spy": id = command[1]; firstName = command[2]; lastName = command[3]; string codeNumbers = command[4]; var currentSpy = new Spy(id, firstName, lastName, codeNumbers); soldiers.Add(currentSpy); break; } input = Console.ReadLine(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier.ToString()); } }
static void Main(string[] args) { List <ISoldier> soldiers = new List <ISoldier>(); string input = ""; while ((input = Console.ReadLine()) != "End") { string[] splitInput = input.Split(); string soldierType = splitInput[0]; int id = int.Parse(splitInput[1]); string firstName = splitInput[2]; string lastNames = splitInput[3]; decimal salary = decimal.Parse(splitInput[4]); ISoldier soldier = null; try { switch (splitInput[0]) { case "Private": soldier = new Private(id, firstName, lastNames, salary); break; case "Spy": int codeNumber = (int)salary; soldier = new Spy(id, firstName, lastNames, salary, codeNumber); break; case "LeutenantGeneral": LeutenantGeneral leutenant = new LeutenantGeneral(id, firstName, lastNames, salary); for (int i = 5; i < splitInput.Length; i++) { int privateId = int.Parse(splitInput[i]); ISoldier @private = soldiers.First(x => x.Id == privateId); leutenant.AddPrivate(@private); } soldier = leutenant; break; case "Engineer": string engineerCorps = splitInput[5]; Engineer engineer = new Engineer(id, firstName, lastNames, salary, engineerCorps); for (int i = 6; i < splitInput.Length; i++) { string partName = splitInput[i]; int hoursWorked = int.Parse(splitInput[++i]); try { IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepairs(repair); } catch { } } soldier = engineer; break; case "Commando": string commandoCorps = splitInput[5]; Commando commando = new Commando(id, firstName, lastNames, salary, commandoCorps); for (int i = 6; i < splitInput.Length; i++) { string codeName = splitInput[i]; string missionState = splitInput[++i]; try { IMission mission = new Mission(codeName, missionState); commando.AddMissios(mission); } catch { } } soldier = commando; break; default: throw new ArgumentException("Invalid soldier type!"); } soldiers.Add(soldier); } catch { } } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
public static void Main() { var input = Console.ReadLine().Split(); var privates = new Dictionary <int, Soldier>(); List <Soldier> soldiers = new List <Soldier>(); while (!input[0].Equals("End")) { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; string corps; switch (input[0]) { case "Private": double salary = double.Parse(input[4]); Soldier soldier = new Private(id, firstName, lastName, salary); privates[id] = soldier; soldiers.Add(soldier); break; case "LeutenantGeneral": double generalSalary = double.Parse(input[4]); List <Private> generalPrivates = new List <Private>(); for (int i = 5; i < input.Length; i++) { int keyId = int.Parse(input[i]); Soldier currentSoldier = privates[keyId]; generalPrivates.Add((Private)currentSoldier); } Soldier leutenant = new LeutenantGeneral(id, firstName, lastName, generalSalary, generalPrivates); soldiers.Add(leutenant); break; case "Engineer": double engineerSalary = double.Parse(input[4]); corps = input[5]; List <Repairs> repairs = new List <Repairs>(); for (int i = 6; i < input.Length; i += 2) { var partName = input[i]; var workedHours = int.Parse(input[i + 1]); repairs.Add(new Repairs(partName, workedHours)); } try { Soldier engineer = new Engineer(id, firstName, lastName, engineerSalary, corps, repairs); soldiers.Add(engineer); } catch (ArgumentException arg) { } break; case "Commando": double commandoSalary = double.Parse(input[4]); corps = input[5]; List <Mission> missions = new List <Mission>(); for (int i = 6; i < input.Length; i += 2) { try { var codeName = input[i]; var state = input[i + 1]; missions.Add(new Mission(codeName, state)); } catch (ArgumentException arg) { } } try { Soldier commando = new Commando(id, firstName, lastName, commandoSalary, corps, missions); soldiers.Add(commando); } catch (ArgumentException arg) { } break; case "Spy": var codeNumber = input[4]; Soldier spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); break; } input = Console.ReadLine().Split(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier.ToString()); } }