private static void GetPrivatesForLeutenantGeneral(LeutenantGeneral leutenantGeneral, List <Soldier> army, string[] soldierArgs) { for (int index = 5; index < soldierArgs.Length; index++) { Soldier privateSoldier = army.FirstOrDefault(ps => ps.Id == soldierArgs[index]); leutenantGeneral.Privates.Add((Private)privateSoldier); } }
private static void AddSoldier(List <Soldier> army, string inputLine) { var splitedInput = inputLine.Split(); var soldierType = splitedInput[0]; var id = splitedInput[1]; var firstName = splitedInput[2]; var lastName = splitedInput[3]; bool isPrivate = soldierType.Equals("Private", StringComparison.OrdinalIgnoreCase); bool isLeutenantGeneral = soldierType.Equals("LeutenantGeneral", StringComparison.OrdinalIgnoreCase); bool isEngineer = soldierType.Equals("Engineer", StringComparison.OrdinalIgnoreCase); bool isCommando = soldierType.Equals("Commando", StringComparison.OrdinalIgnoreCase); bool isSpy = soldierType.Equals("Spy", StringComparison.OrdinalIgnoreCase); if (isPrivate) { var salary = double.Parse(splitedInput[4]); var inputPrivate = new Private(firstName, lastName, id, salary); army.Add(inputPrivate); } else if (isLeutenantGeneral) { var salary = double.Parse(splitedInput[4]); List <ISoldier> privates = ExtractPrivates(splitedInput, army); var inputLeutenantGeneral = new LeutenantGeneral(firstName, lastName, id, salary, privates); army.Add(inputLeutenantGeneral); } else if (isEngineer) { var salary = double.Parse(splitedInput[4]); var corps = splitedInput[5]; List <IRepair> repairs = ExtractRepairs(splitedInput); var inputEngineer = new Engineer(firstName, lastName, id, salary, corps, repairs); army.Add(inputEngineer); } else if (isCommando) { var salary = double.Parse(splitedInput[4]); var corps = splitedInput[5]; List <IMission> missions = ExtractMissions(splitedInput); var inputCommando = new Commando(firstName, lastName, id, salary, corps, missions); army.Add(inputCommando); } else if (isSpy) { var code = int.Parse(splitedInput[4]); var inputSpy = new Spy(firstName, lastName, id, code); army.Add(inputSpy); } }
static void Main(string[] args) { List <Soldier> army = new List <Soldier>(); string input; while ((input = Console.ReadLine()) != "End") { string[] soldierArgs = input.Split(); string division = soldierArgs[0]; switch (division) { case "Private": Private privateSoldier = new Private(soldierArgs[1], soldierArgs[2], soldierArgs[3], decimal.Parse(soldierArgs[4])); army.Add(privateSoldier); break; case "LeutenantGeneral": LeutenantGeneral leutenantGeneral = new LeutenantGeneral(soldierArgs[1], soldierArgs[2], soldierArgs[3], decimal.Parse(soldierArgs[4])); army.Add(leutenantGeneral); GetPrivatesForLeutenantGeneral(leutenantGeneral, army, soldierArgs); break; case "Engineer": if (IsValidCorps(soldierArgs[5])) { Engineer engineer = new Engineer(soldierArgs[1], soldierArgs[2], soldierArgs[3], decimal.Parse(soldierArgs[4]), soldierArgs[5]); army.Add(engineer); GetRepairForEngineer(engineer, soldierArgs); } break; case "Commando": if (IsValidCorps(soldierArgs[5])) { Commando commando = new Commando(soldierArgs[1], soldierArgs[2], soldierArgs[3], decimal.Parse(soldierArgs[4]), soldierArgs[5]); army.Add(commando); GetMissionsForCommando(commando, soldierArgs); } break; case "Spy": Spy spy = new Spy(soldierArgs[1], soldierArgs[2], soldierArgs[3], int.Parse(soldierArgs[4])); army.Add(spy); break; default: break; } } army.ForEach(soldier => Console.WriteLine(soldier)); }
private static LeutenantGeneral CreateLeutenantGen(HashSet <Private> allPrivates, string[] inputArgs, int id, string firstName, string lastName) { var privateIds = inputArgs.Skip(5).Select(int.Parse).ToArray(); LeutenantGeneral leutenant = new LeutenantGeneral(id, firstName, lastName, decimal.Parse(inputArgs[4])); foreach (var privId in privateIds) { var currPriv = allPrivates.First(p => p.Id == privId); leutenant.Privates.Add(currPriv); } return(leutenant); }
private static void AddLeutenantGeneral(string[] tokens) { LeutenantGeneral general = new LeutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); for (int i = 5; i < tokens.Length; i++) { Private privat = (Private)soldiers.Single(s => s.Id == tokens[i]); general.AddPrivate(privat); } soldiers.Add(general); }
private static void SaveLeutenantGeneral(string[] inputParts) { List <int> ids = inputParts.Skip(5).Select(int.Parse).ToList(); List <ISoldier> privates = new List <ISoldier>(); foreach (int id in ids) { privates.Add(soldiers.FirstOrDefault(s => s.Id == id)); } ILieutenantGeneral leutenantGeneral = new LeutenantGeneral(int.Parse(inputParts[1]), inputParts[2], inputParts[3], double.Parse(inputParts[4]), privates); soldiers.Add(leutenantGeneral); }
private static void AddLeutenantGeneralToList(List <Soldier> soldiers, string[] soldierData) { GetIdNameAndSalary(soldierData, out int id, out string firstName, out string lastName, out double salary); var privates = new List <Private>(); for (int i = 5; i < soldierData.Length; i++) { int currentId = int.Parse(soldierData[i]); var currentPrivate = (Private)soldiers.SingleOrDefault(s => s.Id.Equals(currentId)); privates.Add(currentPrivate); } var leutenantGeneral = new LeutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(leutenantGeneral); }
static void CreateLeutenantGeneral(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); } LeutenantGeneral leutenant = new LeutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), privates); Print(leutenant); }
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(); }
static void Main(string[] args) { var privates = new Dictionary <int, Private>(); string input; while ((input = Console.ReadLine()) != "End") { var cmdArgs = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var soldierType = cmdArgs[0]; int id; string firstName; string lastName; double salary; Corps corps; switch (soldierType) { case "Private": id = int.Parse(cmdArgs[1]); firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = double.Parse(cmdArgs[4]); var privateSoldier = new Private(id, firstName, lastName, salary); privates.Add(id, privateSoldier); Console.WriteLine(privateSoldier); break; case "LeutenantGeneral": id = int.Parse(cmdArgs[1]); firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = double.Parse(cmdArgs[4]); var leutenantGeneral = new LeutenantGeneral(id, firstName, lastName, salary); if (cmdArgs.Length >= 5) { for (var i = 5; i < cmdArgs.Length; i++) { var privateId = int.Parse(cmdArgs[i]); privateSoldier = privates[privateId]; leutenantGeneral.SetOfSoldiers.Add(privateSoldier); } } Console.WriteLine(leutenantGeneral); break; case "Engineer": id = int.Parse(cmdArgs[1]); firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = double.Parse(cmdArgs[4]); if (Enum.TryParse(cmdArgs[5], out corps)) { var engineer = new Engineer(id, firstName, lastName, salary, corps); 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.SetOfRepairs.Add(repair); } } Console.WriteLine(engineer); } break; case "Commando": id = int.Parse(cmdArgs[1]); firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = double.Parse(cmdArgs[4]); if (Enum.TryParse(cmdArgs[5], out corps)) { var commando = new Commando(id, firstName, lastName, 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); } } break; case "Spy": id = int.Parse(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!"); } } }
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(string[] args) { var soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { try { var soldierInput = input.Split(); var id = int.Parse(soldierInput[1]); var firstName = soldierInput[2]; var lastName = soldierInput[3]; var sodierType = soldierInput[0]; var salary = decimal.Parse(soldierInput[4]); ISoldier sodier = null; switch (sodierType) { case "Private": sodier = new Private(id, firstName, lastName, salary); ; break; case "Spy": sodier = new Spy(id, firstName, lastName, int.Parse(soldierInput[4])); break; case "LeutenantGeneral": var general = new LeutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < soldierInput.Length; i++) { int iid = int.Parse(soldierInput[i]); ISoldier bbSoldier = soldiers.First(x => x.Id == iid); general.AddPrivate(bbSoldier); } sodier = general; break; case "Engineer": var eng = new Engineer(id, firstName, lastName, salary, soldierInput[5]); for (int i = 6; i < soldierInput.Length; i++) { try { string partName = soldierInput[i]; int hourWorked = int.Parse(soldierInput[++i]); IRepair repeir = new Repair(partName, hourWorked); eng.AddRepiar(repeir); } catch { } } sodier = eng; break; case "Commando": var commando = new Commando(id, firstName, lastName, salary, soldierInput[5]); for (int i = 6; i < soldierInput.Length; i++) { try { string codeName = soldierInput[i]; string state = soldierInput[++i]; IMission mission = new Mission(codeName, state); commando.AddMision(mission); } catch { } } sodier = commando; break; default: throw new ArgumentException(); } soldiers.Add(sodier); } catch { } } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
static void Main(string[] args) { var soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { var tokens = input .Split(' ', StringSplitOptions.RemoveEmptyEntries); var soldireType = tokens[0]; var id = int.Parse(tokens[1]); var firstName = tokens[2]; var lastName = tokens[3]; var sallary = double.Parse(tokens[4]); ISoldier soldier = null; try { switch (soldireType) { case "Private": soldier = new Private(id, firstName, lastName, sallary); break; case "LeutenantGeneral": var lueitenant = new LeutenantGeneral(id, firstName, lastName, sallary); for (int i = 5; i < tokens.Length; i++) { var privateId = int.Parse(tokens[i]); ISoldier @private = soldiers.FirstOrDefault(p => p.Id == privateId); lueitenant.AddPrivate(@private); } soldier = lueitenant; break; case "Engineer": var engineerCorps = tokens[5]; var engineer = new Engineer(id, firstName, lastName, sallary, engineerCorps); for (int i = 6; i < tokens.Length; i++) { var partName = tokens[i]; var hoursWorked = int.Parse(tokens[++i]); IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepair(repair); } soldier = engineer; break; case "Commando": var commandoCorps = tokens[5]; var commando = new Commando(id, firstName, lastName, sallary, commandoCorps); for (int i = 6; i < tokens.Length; i++) { var codeName = tokens[i]; var missionState = tokens[++i]; try { IMission mission = new Mission(codeName, missionState); commando.AddMission(mission); } catch { } } soldier = commando; break; case "Spy": int codeNumber = (int)sallary; soldier = new Spy(id, firstName, lastName, codeNumber); break; default: throw new ArgumentException("Invalid soldier type!"); } soldiers.Add(soldier); } catch { } } foreach (var s in soldiers) { Console.WriteLine(s); } }
static void Main(string[] args) { var input = Console.ReadLine().Split(" ").ToArray(); List <Soldier> soldiers = new List <Soldier>(); List <Private> privates = new List <Private>(); while (input[0] != "End") { switch (input[0]) { case "Private": { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; double salary = double.Parse(input[4]); Private soldier = new Private(id, firstName, lastName, salary); soldiers.Add(soldier); privates.Add(soldier); break; } case "Commando": { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; double salary = double.Parse(input[4]); string cops = input[5]; if (cops == "Airforces" || cops == "Marines") { Commando soldier = new Commando(id, firstName, lastName, salary, cops); for (int i = 6; i < input.Length; i += 2) { if (input[i + 1] == "inProgress" || input[i + 1] == "finished") { Mission mission = new Mission(input[i], input[i + 1]); soldier.Missions.Add(mission); } } soldiers.Add(soldier); } break; } case "Spy": { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; int codeNumber = int.Parse(input[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); break; } case "LieutenantGeneral": { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; double salary = double.Parse(input[4]); LeutenantGeneral soldier = new LeutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < input.Length; i++) { Private solderToAdd = privates.FirstOrDefault(s => s.Id == int.Parse(input[i])); soldier.Privates.Add(solderToAdd); } soldiers.Add(soldier); break; } case "Engineer": { int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; double salary = double.Parse(input[4]); string cops = input[5]; Engineer soldier = new Engineer(id, firstName, lastName, salary, cops); for (int i = 6; i < input.Length; i += 2) { Repear repear = new Repear(input[i], int.Parse(input[i + 1])); soldier.Repears.Add(repear); } soldiers.Add(soldier); break; } } input = Console.ReadLine().Split(" ").ToArray(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier.ToString()); } }