private void DistributeCommands() { string input; while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); var type = tokens[0]; tokens.RemoveAt(0); string id = tokens[0]; string firstName = tokens[1]; string lastName = tokens[2]; switch (type) { case "Spy": soldiers.Add(new Spy(id, firstName, lastName, int.Parse(tokens[3]))); break; case "Private": soldiers.Add(new Private(id, firstName, lastName, double.Parse(tokens[3]))); break; case "LeutenantGeneral": LeutenantGeneral general = new LeutenantGeneral(id, firstName, lastName, double.Parse(tokens[3])); tokens.Skip(4).ToList().ForEach(p => general.Privates.Add(soldiers.First(priv => priv.Id == p) as IPrivate)); soldiers.Add(general); break; case "Engineer": Corps corps = tokens[4] == "Airforces" ? Corps.Airforces : Corps.Marines; Engineer engineer = new Engineer(id, firstName, lastName, double.Parse(tokens[3]), corps); tokens = tokens.Skip(5).ToList(); for (int i = 0; i < tokens.Count; i += 2) { string part = tokens[i]; int workedHours = int.Parse(tokens[i + 1]); IRepair repair = new Repair(part, workedHours); engineer.Repairs.Add(repair); } soldiers.Add(engineer); break; case "Commando": corps = tokens[4] == "Airforces" ? Corps.Airforces : Corps.Marines; Commando commando = new Commando(id, firstName, lastName, double.Parse(tokens[3]), corps); tokens = tokens.Skip(5).ToList(); for (int i = 0; i < tokens.Count; i += 2) { string missionCodeName = tokens[i]; State missionState = State.inProgress; if (Enum.TryParse(tokens[i + 1], out missionState)) { IMission mission = new Mission(missionCodeName, missionState); commando.Missions.Add(mission); } } soldiers.Add(commando); break; default: break; } } }
public static void Main() { var allSoldiers = new Dictionary <string, ISoldier>(); string inputLine; while ((inputLine = Console.ReadLine()) != "End") { var tokens = inputLine.Split(' '); var soldierType = tokens[0]; var id = tokens[1]; var firstName = tokens[2]; var lastName = tokens[3]; switch (soldierType) { case "Private": { var salary = double.Parse(tokens[4]); allSoldiers.Add(id, new Private(id, firstName, lastName, salary)); break; } case "LeutenantGeneral": { double salary = double.Parse(tokens[4]); List <ISoldier> privates = new List <ISoldier>(); for (int i = 5; i < tokens.Length; i++) { var privateId = tokens[i]; privates.Add(allSoldiers[privateId]); } allSoldiers.Add(id, new LeutenantGeneral(id, firstName, lastName, salary, privates)); break; } case "Engineer": { double salary = double.Parse(tokens[4]); string corps = tokens[5]; List <IRepair> repairs = new List <IRepair>(); for (int i = 6; i < tokens.Length; i = i + 2) { string partName = tokens[i]; int hoursWorked = int.Parse(tokens[i + 1]); IRepair repair = new Repair(partName, hoursWorked); repairs.Add(repair); } try { allSoldiers.Add(id, new Engineer(id, firstName, lastName, salary, corps, repairs)); } catch (ArgumentException) { } break; } case "Commando": { double salary = double.Parse(tokens[4]); string corps = tokens[5]; List <IMission> missions = new List <IMission>(); for (int i = 6; i < tokens.Length; i = i + 2) { string codeName = tokens[i]; string state = tokens[i + 1]; try { IMission mission = new Mission(codeName, state); missions.Add(mission); } catch (ArgumentException) { } } try { allSoldiers.Add(id, new Commando(id, firstName, lastName, salary, corps, missions)); } catch (Exception) { } break; } case "Spy": { var codeNumber = tokens[4]; allSoldiers.Add(id, new Spy(id, firstName, lastName, int.Parse(codeNumber))); break; } } } foreach (var soldier in allSoldiers) { Console.WriteLine(soldier.Value); } }