private static void CreateEngineer(List <ISoldier> soldiers, List <string> splittedInput, int id, string firstName, string lastName) { var salary = ParseSalary(splittedInput); var corp = splittedInput[5]; if (!Enum.IsDefined(typeof(Corps), corp)) { return; } IEngineer engineer = new Engineer(id, firstName, lastName, salary, (Corps)Enum.Parse(typeof(Corps), corp)); splittedInput = Skipping(splittedInput, 6); for (int i = 0; i < splittedInput.Count; i += 2) { var partName = splittedInput[i]; var workedHours = int.Parse(splittedInput[i + 1]); IRepair repair = new Repair(partName, workedHours); engineer.AddRepair(repair); } soldiers.Add(engineer); }
private static void AddEngineer(string[] tokens) { try { Engineer engineer = new Engineer(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); for (int i = 6; i < tokens.Length; i += 2) { Repair repair = new Repair(tokens[i], int.Parse(tokens[i + 1])); engineer.AddRepair(repair); } soldiers.Add(engineer); } catch (ArgumentException) { } }
public IEngineer CreateEngineer(string[] args) { string id = args[1]; string firstName = args[2]; string lastName = args[3]; decimal salary = decimal.Parse(args[4]); string corpsStr = args[5]; string[] repairInfo = args.Skip(6).ToArray(); IEngineer engineer = new Engineer(id, firstName, lastName, salary, corpsStr); if (repairInfo.Length != 0) { for (int i = 0; i < repairInfo.Length; i += 2) { string partName = repairInfo[i]; int hrWorked = int.Parse(repairInfo[i + 1]); try { IRepair repair = new Repair(partName, hrWorked); engineer.AddRepair(repair); } catch (Exception) { } } } return(engineer); }
private ISoldier CreateEngineer(string[] arguments) { var id = int.Parse(arguments[0]); var firstName = arguments[1]; var lastName = arguments[2]; var salary = decimal.Parse(arguments[3]); var corps = arguments[4]; IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); var repairs = arguments .Skip(5) .ToList(); for (int i = 0; i < repairs.Count; i += 2) { var repairPartName = repairs[i]; var repairHours = int.Parse(repairs[i + 1]); IRepair repair = new Repair(repairPartName, repairHours); engineer.AddRepair(repair); } return(engineer); }
private void AddRepairs(Engineer engineer, string[] repairArgs) { for (int i = 0; i < repairArgs.Length; i += 2) { string name = repairArgs[i]; int hours = int.Parse(repairArgs[i + 1]); IRepair repair = this.repairFactory.MakeRepair(name, hours); engineer.AddRepair(repair); } }
private static void AddRepairs(string[] tokens, Engineer engineer) { for (int i = 6; i < tokens.Length; i += 2) { string partName = tokens[i]; int hoursWorked = int.Parse(tokens[i + 1]); Repair repair = new Repair(partName, hoursWorked); engineer.AddRepair(repair); } }
private static void GetRepairs(string[] tokens, Engineer engineer) { 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); } }
private static void AddRepairs(Engineer engineer, string[] repairArgs) { for (int i = 0; i < repairArgs.Length; i += 2) { string partName = repairArgs[i]; int hours = int.Parse(repairArgs[i + 1]); IRepair repair = new Repair(partName, hours); engineer.AddRepair(repair); } }
private static void GetAllRepairs(string[] cmdArg, Engineer engineer) { for (int i = 0; i < cmdArg.Length; i += 2) { var repairPart = cmdArg[i]; var repairHours = int.Parse(cmdArg[i + 1]); var repair = new Repair(repairPart, repairHours); engineer.AddRepair(repair); } }
private IEngineer CreateEngineer(string[] args, int iD, string firstName, string lastName, decimal salary, string corps) { IEngineer engineer = new Engineer(iD, firstName, lastName, salary, corps); string[] repairArg = args.Skip(6).ToArray(); for (int i = 0; i < repairArg.Length; i += 2) { IRepair repair = new Repair(repairArg[i], int.Parse(repairArg[i + 1])); engineer.AddRepair(repair); } return(engineer); }
private static void AddRepairsToEngeneer(string[] args, Engineer engineer) { string[] repairArgs = args.Skip(1).ToArray(); for (int i = 0; i < repairArgs.Length; i += 2) { string partName = repairArgs[i]; int hours = Int32.Parse(repairArgs[i + 1]); Repair repair = RepairFactory.CreateRepair(partName, hours); engineer.AddRepair(repair); } }
private static IEngineer CreateEngineer(string[] cmdArgs, int id, string firstName, string lastName, decimal salary, string corps) { IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); string[] repairArgs = cmdArgs.Skip(6).ToArray(); for (int i = 0; i < repairArgs.Length; i += 2) { string partName = repairArgs[i]; int hourWorked = int.Parse(repairArgs[i + 1]); IRepair repair = new Repair(partName, hourWorked); engineer.AddRepair(repair); } return(engineer); }
private static void TryCreateEnginner(List <ISoldier> soldiers, string[] inputParams) { Engineer engineer = new Engineer(inputParams[1], inputParams[2], inputParams[3], double.Parse(inputParams[4]), inputParams[5]); int numberOfRepairs = (inputParams.Length - 5) / 2; for (int i = 0; i < numberOfRepairs; i++) { Repair repair = new Repair(inputParams[6 + (i * 2)], int.Parse(inputParams[7 + (i * 2)])); engineer.AddRepair(repair); } soldiers.Add(engineer); }
private static void ParseEngineer(List <Soldier> soldiers, string[] tokens) { try { soldiers.Add(new Engineer(tokens[1], tokens[2], tokens[3], decimal.Parse(tokens[4]), tokens[5])); tokens = tokens.Skip(6).ToArray(); Engineer engineer = (Engineer)soldiers.Last(); for (int i = 0; i < tokens.Length; i += 2) { engineer.AddRepair(new Repair(tokens[i], int.Parse(tokens[i + 1]))); } } catch (ArgumentException ex) { } }
public void AddEnginees(int id, string firstName, string lastName, decimal salary, string corps, params string[] repairs) { var engineer = new Engineer(id, firstName, lastName, salary, corps); if (repairs.Length > 0) { for (int i = 0; i < repairs.Length; i += 2) { string partName = repairs[i]; int hoursWorked = int.Parse(repairs[i + 1]); engineer.AddRepair(partName, hoursWorked); } } this.soldiers.Add(engineer); }
private static ISoldier AddEngineer(string[] commandSplit, int id, string firstName, string lastName, decimal salary, string corps) { ISoldier soldier; Engineer engineer = new Engineer(id, firstName, lastName, salary, corps); string[] repairEntries = commandSplit.Skip(6).ToArray(); for (int i = 0; i < repairEntries.Length; i += 2) { string partName = commandSplit[i]; int hoursWorked = int.Parse(commandSplit[i + 1]); IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepair(repair); } soldier = engineer; return(soldier); }
private ISoldier GetEngineer(string id, string firstName, string lastName, decimal salary, Corps corps, string[] repairs) { IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); for (int i = 0; i < repairs.Length; i += 2) { IRepair repair = new Repair(repairs[i], int.Parse(repairs[i + 1])); if (repair == null) { continue; } engineer.AddRepair(repair); } return(engineer); }
private IEngineer CreateEngineer(string[] commandArgs, int id, string firstName, string lastName, decimal salary) { string corps = commandArgs[5]; IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); string[] repairsArgs = commandArgs .Skip(6) .ToArray(); for (int i = 0; i < repairsArgs.Length; i += 2) { string repiarPart = repairsArgs[i]; int repairHours = int.Parse(repairsArgs[i + 1]); IRepair repair = new Repair(repiarPart, repairHours); engineer.AddRepair(repair); } return(engineer); }
private ISoldier GenerateEngineer(int id, string firstName, string lastName, string[] args) { string corpsAsString = args[5]; if (!Enum.TryParse(corpsAsString, out Corps corps)) { return(null); } IEngineer engineer = new Engineer(firstName, lastName, id, decimal.Parse(args[4]), corps); for (int i = 6; i < args.Length; i += 2) { string partName = args[i]; int hoursWorked = int.Parse(args[i + 1]); IRepairs repair = new Repair(partName, hoursWorked); engineer.AddRepair(repair); } return(engineer); }
public void Run() { string command = Console.ReadLine(); while (command != "End") { string[] commandArgs = command.Split(); string type = commandArgs[0]; string id = commandArgs[1]; string firstName = commandArgs[2]; string lastName = commandArgs[3]; decimal salary = decimal.Parse(commandArgs[4]); if (type == "Private") { Private privateToAdd = new Private(id, firstName, lastName, salary); this.army.Add(privateToAdd); } else if (type == "LieutenantGeneral") { LieutenantGeneral lieutenantGeneralToAdd = new LieutenantGeneral(id, firstName, lastName, salary); string[] ids = commandArgs .Skip(5) .ToArray(); foreach (string privateId in ids) { ISoldier currentPrivate = this.army.First(p => p.Id == privateId); lieutenantGeneralToAdd.AddPrivate(currentPrivate); } this.army.Add(lieutenantGeneralToAdd); } else if (type == "Engineer") { string corps = commandArgs[5]; try { Engineer engineerToAdd = new Engineer(id, firstName, lastName, salary, corps); string[] repairs = commandArgs .Skip(6) .ToArray(); for (int i = 0; i < repairs.Length; i += 2) { string repairPart = repairs[i]; int repairHours = int.Parse(repairs[i + 1]); Repair repair = new Repair(repairPart, repairHours); engineerToAdd.AddRepair(repair); } this.army.Add(engineerToAdd); } catch (ArgumentException ae) { } } else if (type == "Commando") { string corps = commandArgs[5]; try { Commando commandoToAdd = new Commando(id, firstName, lastName, salary, corps); string[] missions = commandArgs .Skip(6) .ToArray(); for (int i = 0; i < missions.Length; i += 2) { string missionCodeName = missions[i]; string missionState = missions[i + 1]; try { Mission mission = new Mission(missionCodeName, missionState); commandoToAdd.AddMission(mission); } catch (ArgumentException ae) { } } this.army.Add(commandoToAdd); } catch (ArgumentException ae) { } } else if (type == "Spy") { int codeNumber = (int)salary; Spy spyToAdd = new Spy(id, firstName, lastName, codeNumber); this.army.Add(spyToAdd); } command = Console.ReadLine(); } foreach (var soldier in this.army) { //Type type = soldier.GetType(); //Object obj = Convert.ChangeType(soldier, type); Console.WriteLine(soldier.ToString()); } }
public static void Main() { IList <ISoldier> army; string input; army = new List <ISoldier>(); while ((input = Console.ReadLine()) != "End") { var args = input.Split(); string type = args[0]; string id = args[1]; switch (type) { case "Private": army.Add(new Private(id, args[2], args[3], double.Parse(args[4]))); break; case "Spy": army.Add(new Spy(id, args[2], args[3], int.Parse(args[4]))); break; case "LeutenantGeneral": LeutenantGeneral leutenantGeneral = new LeutenantGeneral(id, args[2], args[3], double.Parse(args[4])); for (int i = 5; i < args.Length; i++) { leutenantGeneral.AddPrivate(army.First(s => s.Id == args[i])); } army.Add(leutenantGeneral); break; case "Commando": Commando commando; try { commando = new Commando(id, args[2], args[3], double.Parse(args[4]), args[5]); } catch (Exception) { break; } for (int i = 6; i < args.Length - 1; i += 2) { Mission mission; try { mission = new Mission(args[i], args[i + 1]); } catch (Exception) { continue; } commando.AddMission(mission); } army.Add(commando); break; case "Engineer": Engineer engineer; try { engineer = new Engineer(id, args[2], args[3], double.Parse(args[4]), args[5]); } catch (Exception) { break; } for (int i = 6; i < args.Length - 1; i += 2) { engineer.AddRepair(new Repair(args[i], int.Parse(args[i + 1]))); } army.Add(engineer); break; } } foreach (var soldier in army) { Console.WriteLine(soldier); } }
public void Run() { string input; while ((input = Console.ReadLine()) != "End") { String[] commands = input.Split(); string command = commands[0]; if (command == "Private") { int id = int.Parse(commands[1]); string firstName = commands[2]; string lastName = commands[3]; decimal salary = decimal.Parse(commands[4]); Private soldier = new Private(id, firstName, lastName, salary); this.soldiers.Add(soldier); } else if (command == "LieutenantGeneral") { int id = int.Parse(commands[1]); string firstName = commands[2]; string lastName = commands[3]; decimal salary = decimal.Parse(commands[4]); LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary); foreach (string privateId in commands.Skip(5)) { IPrivate getId = (IPrivate)this.soldiers.SingleOrDefault(i => i.Id == int.Parse(privateId)); if (getId != null) { general.AddPrivate(getId); } } this.soldiers.Add(general); } else if (command == "Engineer") { int id = int.Parse(commands[1]); string firstName = commands[2]; string lastName = commands[3]; decimal salary = decimal.Parse(commands[4]); if (Enum.TryParse <Corps>(commands[5], out Corps corps)) { Engineer enginner = new Engineer(id, firstName, lastName, salary, corps); string[] repairParts = commands.Skip(6).ToArray(); for (int i = 0; i < repairParts.Length; i += 2) { string repairPart = repairParts[i]; int repairHour = int.Parse(repairParts[i + 1]); Repair repair = new Repair(repairPart, repairHour); enginner.AddRepair(repair); } this.soldiers.Add(enginner); } } else if (command == "Commando") { int id = int.Parse(commands[1]); string firstName = commands[2]; string lastName = commands[3]; decimal salary = decimal.Parse(commands[4]); if (Enum.TryParse <Corps>(commands[5], out Corps corps)) { Commando soldier = new Commando(id, firstName, lastName, salary, corps); string[] missions = commands.Skip(6).ToArray(); for (int i = 0; i < missions.Length; i += 2) { string missionName = missions[i]; string missionState = missions[i + 1]; if (Enum.TryParse(missionState, out State state)) { Mission mission = new Mission(missionName, state); soldier.AddMission(mission); } } this.soldiers.Add(soldier); } } else if (command == "Spy") { int id = int.Parse(commands[1]); string firstName = commands[2]; string lastName = commands[3]; int codeNumber = int.Parse(commands[4]); Spy soldier = new Spy(id, firstName, lastName, codeNumber); this.soldiers.Add(soldier); } } print(); }
public void Run() { string input; while ((input = this.reader.ReadLine()) != "End") { string[] cmdArgs = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string soldierType = cmdArgs[0]; string id = cmdArgs[1]; string firstName = cmdArgs[2]; string lastName = cmdArgs[3]; ISoldier soldier = null; switch (soldierType) { case "Private": IPrivate @private = new Private(id, firstName, lastName, decimal.Parse(cmdArgs[4])); soldier = @private; break; case "LieutenantGeneral": ILieutenantGeneral currentGeneral = new LieutenantGeneral(id, firstName, lastName, decimal.Parse(cmdArgs[4])); foreach (var pid in cmdArgs.Skip(5)) { ISoldier currentPrivate = (Private)soldiers.FirstOrDefault(s => s.Id == pid); if (currentPrivate != null) { currentGeneral.AddPrivate(currentPrivate); } } soldier = currentGeneral; break; case "Engineer": try { IEngineer currentEngineer = new Engineer(id, firstName, lastName, decimal.Parse(cmdArgs[4]), cmdArgs[5]); string[] repairArgs = cmdArgs.Skip(6).ToArray(); for (int i = 0; i < repairArgs.Length; i += 2) { IRepair currentRepair = new Repair(repairArgs[i], int.Parse(repairArgs[i + 1])); currentEngineer.AddRepair(currentRepair); } soldier = currentEngineer; } catch (InvalidCoprsException ice) { continue; } break; case "Commando": try { ICommando commando = new Commando(id, firstName, lastName, decimal.Parse(cmdArgs[4]), cmdArgs[5]); string[] missionArgs = cmdArgs.Skip(6).ToArray(); for (int i = 0; i < missionArgs.Length; i += 2) { try { Imission currentMission = new Mission(missionArgs[i], missionArgs[i + 1]); commando.AddMission(currentMission); } catch (InvalidStateException ise) { continue; } } soldier = commando; } catch (InvalidCoprsException ice) { continue; } break; case "Spy": soldier = new Spy(id, firstName, lastName, int.Parse(cmdArgs[4])); break; } if (soldier != null) { soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { this.writer.WriteLine(soldier.ToString()); } }
public static ISoldier CreateSoldier(string input) { string[] token = input.Split(); var soldierType = token[0]; var soldierId = int.Parse(token[1]); var soldierFirstName = token[2]; var soldierSecondName = token[3]; switch (soldierType) { case "Private": Private priv = new Private(soldierId, soldierFirstName, soldierSecondName, double.Parse(token[4])); Privates.Add(soldierId, priv); return(priv); case "LeutenantGeneral": LeutenantGeneral leutenant = new LeutenantGeneral( soldierId, soldierFirstName, soldierSecondName, double.Parse(token[4])); for (int i = 5; i < token.Length; i++) { int searchedID = int.Parse(token[i]); IPrivate p = Privates[searchedID]; leutenant.AddPrivate(p); } Privates.Add(soldierId, leutenant); return(leutenant); case "Engineer": Engineer engineer = new Engineer( soldierId, soldierFirstName, soldierSecondName, double.Parse(token[4]), token[5]); for (int i = 6; i < token.Length; i += 2) { IRepair r = new Repair(token[i], int.Parse(token[i + 1])); engineer.AddRepair(r); } Privates.Add(soldierId, engineer); return(engineer); case "Commando": Commando commando = new Commando( soldierId, soldierFirstName, soldierSecondName, double.Parse(token[4]), token[5]); for (int i = 6; i < token.Length; i += 2) { var missionName = token[i]; var missionState = token[i + 1]; try { Mission m = new Mission(missionName, missionState); commando.AddMission(m); } catch (Exception) { } } Privates.Add(soldierId, commando); return(commando); case "Spy": int soldeirCodeNumber = int.Parse(token[4]); Spy spy = new Spy(soldierId, soldierFirstName, soldierSecondName, soldeirCodeNumber); return(spy); default: return(null); } }
public static ISoldier ProduceSoldier(string input) { var command = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var solder = command[0]; var id = int.Parse(command[1]); var firstName = command[2]; var lastName = command[3]; switch (solder) { case "Private": var salary = double.Parse(command[4]); Private soldPrivate = new Private(id, firstName, lastName, salary); lstPrivate.Add(soldPrivate); return(soldPrivate); case "LeutenantGeneral": var salaryGeneral = double.Parse(command[4]); LeutenantGeneral LGeneral = new LeutenantGeneral(id, firstName, lastName, salaryGeneral); try { for (int i = 5; i < command.Length; i++) { int idPrivate = int.Parse(command[i]); var currPrivate = lstPrivate.First(x => x.Id == idPrivate); if (currPrivate != null) { LGeneral.Privates.Add(currPrivate); } } } catch (Exception) { } lstPrivate.Add(LGeneral); return(LGeneral); case "Engineer": var engSalary = double.Parse(command[4]); var engCorp = command[5]; Engineer Engineer = new Engineer(id, firstName, lastName, engSalary, engCorp); List <IRepair> repairs = new List <IRepair>(); for (int i = 6; i < command.Length - 1; i += 2) { string partName = command[i]; int workHours = int.Parse(command[i + 1]); Engineer.AddRepair(new Repair(partName, workHours)); } lstPrivate.Add(Engineer); return(Engineer); case "Commando": var commandoSalary = double.Parse(command[4]); var commandoCopr = command[5]; Commando Commando = new Commando(id, firstName, lastName, commandoSalary, commandoCopr); List <IMission> Missions = new List <IMission>(); for (int i = 6; i < command.Length - 1; i += 2) { string codeName = command[i]; string state = command[i + 1]; try { Commando.AddMission(new Mission(codeName, state)); } catch (Exception) { } } lstPrivate.Add(Commando); return(Commando); case "Spy": int codeNumber = int.Parse(command[3]); Spy Spy = new Spy(id, firstName, lastName, codeNumber); return(Spy); default: return(null); } }
public void Run() { while (true) { string[] input = Console.ReadLine().Split(); string rank = input[0].ToLower(); if (rank == "end") { break; } string id = input[1]; string firstName = input[2]; string lastName = input[3]; decimal salary = decimal.Parse(input[4]); switch (rank) { case "private": Private @private = new Private(id, firstName, lastName, salary); this.soldiers.Add(@private); this.privates.Add(@private); break; case "lieutenantgeneral": LieutenantGeneral lieutenant = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < input.Length; i++) { Private findedPrivate = this.privates.FirstOrDefault(p => p.Id == input[i]); lieutenant.AddPrivate(findedPrivate); } this.soldiers.Add(lieutenant); break; case "engineer": if (!Enum.TryParse(input[5], out Corps engineerCorps)) { continue; } Engineer engineer = new Engineer(id, firstName, lastName, salary, engineerCorps); for (int i = 6; i < input.Length; i += 2) { string partName = input[i]; int workedHours = int.Parse(input[i + 1]); Repair repair = new Repair(partName, workedHours); engineer.AddRepair(repair); } this.soldiers.Add(engineer); break; case "commando": if (!Enum.TryParse(input[5], out Corps commandoCorps)) { continue; } Commando commando = new Commando(id, firstName, lastName, salary, commandoCorps); for (int i = 6; i < input.Length; i += 2) { string codeName = input[i]; if (!Enum.TryParse(input[i + 1], out State state)) { continue; } Mission mission = new Mission(codeName, state); commando.AddMission(mission); } this.soldiers.Add(commando); break; case "spy": int codeNumber = int.Parse(input[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); this.soldiers.Add(spy); break; default: break; } } Print(); }
public ISoldier CreateSoldier(string[] soldierStrings) { var soldierType = soldierStrings[0]; var id = int.Parse(soldierStrings[1]); var firstName = soldierStrings[2]; var lastName = soldierStrings[3]; switch (soldierType) { case "Private": var privateSalary = double.Parse(soldierStrings[4]); return(new Private(id, firstName, lastName, privateSalary)); case "LeutenantGeneral": var ltSalary = double.Parse(soldierStrings[4]); var currentLt = new LeutenantGeneral(id, firstName, lastName, ltSalary); for (int i = 5; i < soldierStrings.Length; i++) { var privateId = int.Parse(soldierStrings[i]); if (DataManager.SoldierExists(privateId)) { currentLt.AddPrivate(DataManager.ReturnPrivateSoldier(privateId)); } } return(currentLt); case "Engineer": var engineerSalary = double.Parse(soldierStrings[4]); var corps = soldierStrings[5]; var currentEngineer = new Engineer(id, firstName, lastName, engineerSalary, corps); for (int i = 6; i < soldierStrings.Length; i += 2) { var part = soldierStrings[i]; var hours = int.Parse(soldierStrings[i + 1]); var repair = new Repair(part, hours); currentEngineer.AddRepair(repair); } return(currentEngineer); case "Commando": var commandoSalary = double.Parse(soldierStrings[4]); var commandoCorps = soldierStrings[5]; var currentCommando = new Commando(id, firstName, lastName, commandoSalary, commandoCorps); for (int i = 6; i < soldierStrings.Length; i += 2) { try { var codeName = soldierStrings[i]; var state = soldierStrings[i + 1]; var mission = new Mission(codeName, state); currentCommando.AddMission(mission); } catch (Exception e) { } } return(currentCommando); case "Spy": var codeNumber = int.Parse(soldierStrings[4]); return(new Spy(id, firstName, lastName, codeNumber)); default: return(null); } }
public void Run() { string command = Console.ReadLine(); while (command != "End") { string[] cmdArg = command.Split().ToArray(); var type = cmdArg[0]; var id = cmdArg[1]; var firstName = cmdArg[2]; var lastName = cmdArg[3]; var salary = decimal.Parse(cmdArg[4]); if (type == "Private") { Private soldier = new Private(id, firstName, lastName, salary); army.Add(soldier); } else if (type == "LieutenantGeneral") { LieutenantGeneral general = new LieutenantGeneral(id, firstName, lastName, salary); string[] privatesList = cmdArg.Skip(5).ToArray(); foreach (var privatesId in privatesList) { ISoldier currentPrivate = this.army.First(s => s.Id == privatesId); general.AddPrivate(currentPrivate); } this.army.Add(general); } else if (type == "Engineer") { try { string corps = cmdArg[5]; IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); string[] repairs = cmdArg.Skip(6).ToArray(); for (int i = 0; i < repairs.Length; i += 2) { string partName = repairs[i]; int hours = int.Parse(repairs[i + 1]); IRepair currentRepair = new Repair(partName, hours); engineer.AddRepair(currentRepair); } this.army.Add(engineer); } catch (InvalidCorpsException ice) { } } else if (type == "Commando") { try { string corps = cmdArg[5]; ICommando commando = new Commando(id, firstName, lastName, salary, corps); string[] missions = cmdArg.Skip(6).ToArray(); for (int i = 0; i < missions.Length; i += 2) { try { string codeName = missions[i]; string state = missions[i + 1]; IMission currentMission = new Mission(codeName, state); commando.AddMission(currentMission); } catch (InvalidStateException ise) { continue; } } army.Add(commando); } catch (InvalidCorpsException ice) { } } else if (type == "Spy") { int codeNumber = (int)salary; ISpy spy = new Spy(id, firstName, lastName, codeNumber); this.army.Add(spy); } command = Console.ReadLine(); } foreach (var soldier in this.army) { Type type = soldier.GetType(); Object actualType = Convert.ChangeType(soldier, type); Console.WriteLine(actualType.ToString()); } }
static void Main(string[] args) { var soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { string[] tokens = input.Split(); string soldierType = tokens[0]; int id = int.Parse(tokens[1]); string firstName = tokens[2]; string lastName = tokens[3]; decimal salary = decimal.Parse(tokens[4]); ISoldier soldier = null; try { switch (soldierType) { case "Private": soldier = new Private(id, firstName, lastName, salary); break; case "LeutenantGeneral": var leutenant = new LeutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < tokens.Length; i++) { int soldierId = int.Parse(tokens[i]); ISoldier @private = soldiers.First(p => p.Id == soldierId); leutenant.AddPrivate(@private); } soldier = leutenant; break; case "Engineer": string engineerCorps = tokens[5]; Engineer engineer = new Engineer(id, firstName, lastName, salary, engineerCorps); for (int i = 6; i < tokens.Length; i++) { string partName = tokens[i]; int hoursWorked = int.Parse(tokens[++i]); IRepair repair = new Repair(partName, hoursWorked); engineer.AddRepair(repair); } soldier = engineer; break; case "Commando": string commandCorps = tokens[5]; var commando = new Commando(id, firstName, lastName, salary, commandCorps); for (int i = 6; i < tokens.Length; i++) { string codeName = tokens[i]; string missionState = tokens[++i]; try { IMission mission = new Mission(codeName, missionState); commando.AddMission(mission); } catch { } } soldier = commando; break; case "Spy": int codeNumber = (int)salary; soldier = new Spy(id, firstName, lastName, codeNumber); break; default: throw new ArgumentException("Invalid soldier type!"); } soldiers.Add(soldier); } catch {} } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
public static ISoldier Soldier(string input) { var soldierInfo = input.Split(); var soldierType = soldierInfo[0]; var soldierId = soldierInfo[1]; var soldierFirstName = soldierInfo[2]; var soldierSecondName = soldierInfo[3]; switch (soldierType) { case "Private": { var soldierSalary = decimal.Parse(soldierInfo[4]); return(new Private(soldierId, soldierFirstName, soldierSecondName, soldierSalary)); } case "LeutenantGeneral": { var soldierSalary = decimal.Parse(soldierInfo[4]); var leutenant = new LeutenantGeneral(soldierId, soldierFirstName, soldierSecondName, soldierSalary); for (int i = 5; i < soldierInfo.Length; i++) { leutenant.AddPrivate(soldierInfo[i]); } return(leutenant); } case "Engineer": { var soldierSalary = decimal.Parse(soldierInfo[4]); var soldeirCorps = soldierInfo[5]; var engineer = new Engineer(soldierId, soldierFirstName, soldierSecondName, soldierSalary, soldeirCorps); for (int i = 6; i < soldierInfo.Length; i += 2) { var part = soldierInfo[i]; var hours = int.Parse(soldierInfo[i + 1]); var repair = new Repair(part, hours); engineer.AddRepair(repair); } return(engineer); } case "Commando": { var soldierSalary = decimal.Parse(soldierInfo[4]); var soldeirCorps = soldierInfo[5]; var commando = new Commando(soldierId, soldierFirstName, soldierSecondName, soldierSalary, soldeirCorps); for (int i = 6; i < soldierInfo.Length; i += 2) { var missionName = soldierInfo[i]; var missionState = soldierInfo[i + 1]; try { var mission = new Mission(missionName, missionState); commando.AddMission(mission); } catch (Exception) { } } return(commando); } case "Spy": { var soldeirCodeNumber = soldierInfo[4]; return(new Spy(soldierId, soldierFirstName, soldierSecondName, int.Parse(soldeirCodeNumber))); } } return(null); }