private static void AddRepairs(string[] tokens, Engineer engineer) { for (int i = 6; i < tokens.Length; i += 2) { var repairPart = tokens[i]; var repairHours = int.Parse(tokens[i + 1]); var newRepair = new Repair(repairPart, repairHours); engineer.AddRepairs(newRepair); } }
public static void Main() { var input = string.Empty; var privates = new List <Private>(); while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(); var soldierType = tokens[0]; switch (soldierType) { case "Private": var privateSoldier = new Private(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); privates.Add(privateSoldier); Console.WriteLine(privateSoldier.ToString()); break; case "LeutenantGeneral": var soldiers = tokens.Skip(5).ToArray(); var leutenantGeneral = new LeutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); leutenantGeneral.AddSoldiers(soldiers, privates); Console.WriteLine(leutenantGeneral.ToString()); break; case "Engineer": if (tokens[5] == "Airforces" || tokens[5] == "Marines") { var repairs = tokens.Skip(6).ToArray(); var engineer = new Engineer(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); engineer.AddRepairs(repairs); Console.WriteLine(engineer.ToString()); } break; case "Commando": if (tokens[5] == "Airforces" || tokens[5] == "Marines") { var missions = tokens.Skip(6).ToArray(); var commando = new Commando(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); commando.AddMissions(missions); Console.WriteLine(commando.ToString()); } break; case "Spy": var spy = new Spy(tokens[1], tokens[2], tokens[3], int.Parse(tokens[4])); Console.WriteLine(spy.ToString()); break; } } }
private ISoldier GetEngineerType(int id, string firstName, string lastName, decimal salary, string[] newArgs) { IEngineer engineer = null; Corp corp; bool isCastToCorp = Enum.TryParse <Corp>(newArgs[0], out corp); if (isCastToCorp) { engineer = new Engineer(id, firstName, lastName, salary, corp); string partName; int hoursWorked; for (int i = 1; i < newArgs.Length; i += 2) { partName = newArgs[i]; hoursWorked = int.Parse(newArgs[i + 1]); IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepairs(repair); } } return(engineer); }
public void Run() { List <ISoldier> soldiers = new List <ISoldier>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { ISoldier soldier = null; string[] data = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string type = data[0]; int id = int.Parse(data[1]); string firstName = data[2]; string lastName = data[3]; if (type == "Private") { decimal salary = decimal.Parse(data[4]); soldier = new Private(firstName, lastName, id, salary); } else if (type == "LieutenantGeneral") { decimal salary = decimal.Parse(data[4]); ILieutenantGeneral general = new LieutenantGeneral(firstName, lastName, id, salary); foreach (var item in data.Skip(5)) { ISoldier privateToAdd = soldiers.First(x => x.Id == int.Parse(item)); general.AddPrivates(privateToAdd); } soldier = general; } else if (type == "Engineer") { decimal salary = decimal.Parse(data[4]); string corps = data[5]; try { IEngineer engineer = new Engineer(firstName, lastName, id, salary, corps); string[] repairsData = data .Skip(6) .ToArray(); for (int i = 0; i < repairsData.Length; i += 2) { string partName = repairsData[i]; int hoursWorked = int.Parse(repairsData[i + 1]); IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepairs(repair); } soldier = engineer; } catch (ArgumentException) { continue; } } else if (type == "Commando") { decimal salary = decimal.Parse(data[4]); string corps = data[5]; try { ICommando commando = new Commando(firstName, lastName, id, salary, corps); string[] missionsData = data .Skip(6) .ToArray(); for (int i = 0; i < missionsData.Length; i += 2) { try { string codeName = missionsData[i]; string state = missionsData[i + 1]; IMission mission = new Mission(codeName, state); commando.AddMissions(mission); } catch (ArgumentException) { continue; } } soldier = commando; } catch (ArgumentException ex) { continue; } } else if (type == "Spy") { int codeNumber = int.Parse(data[4]); soldier = new Spy(firstName, lastName, id, codeNumber); } if (soldier != null) { soldiers.Add(soldier); } } foreach (var item in soldiers) { Console.WriteLine(item); } }