private ISoldier GetCommandoType(int id, string firstName, string lastName, decimal salary, string[] newArgs) { ICommando commando = null; Corp corp; bool isCastToCorp = Enum.TryParse <Corp>(newArgs[0], out corp); if (isCastToCorp) { commando = new Commando(id, firstName, lastName, salary, corp); string codeName; string state; for (int i = 1; i < newArgs.Length; i += 2) { codeName = newArgs[i]; state = newArgs[i + 1]; //“Commando <id> <firstName> <lastName> <salary> <corps> //<mission1CodeName> <mission1state> … <missionNCodeName> <missionNstate>” //a missions cde name, description and state will always come together //bool validMissionName = Enum.TryParse<State>(missionName, out newMission.State) if (Enum.TryParse <State>(state, out State newState)) { IMission validMissionName = new Mission(codeName, newState); commando.AddMission(validMissionName); } } } return(commando); }
private static void CreateCommando(string[] soldierArgs, ICommando commando) { string[] missionArgs = soldierArgs .Skip(6) .ToArray(); for (int i = 0; i < missionArgs.Length; i += 2) { IMission mission = new Mission(missionArgs[i], missionArgs[i + 1]); commando.AddMission(mission); } }
private static void TakeMissions(ICommando commando, List <string> missions) { for (int i = 0; i < missions.Count; i += 2) { var missionName = missions[i]; var state = missions[i + 1]; if (state == "inProgress" || state == "Finished") { var mission = new Mission(missionName, state); commando.Missions.Add(mission); } } }
private static ISoldier GetCommando(ICommando commando, string[] missionArgs, int i) { ISoldier soldier; string missionCodeName = missionArgs[i]; string missionState = missionArgs[i + 1]; IMission mission = new Mission(missionCodeName, missionState); commando.AddMission(mission); soldier = commando; return(soldier); }
static void ShowHelpText(ICommando commando) { var helper = new Helper(); var parameters = helper.GetParameters(commando); var exeName = Assembly.GetEntryAssembly().GetName().Name + ".exe"; var parameterList = string.Join(" ", parameters.Where(p => p.Position > 0) .Select(p => string.Format("[{0}]", p.Position)) .ToArray()); var thereAreRequiredArguments = parameters.Any(p => p.Position > 0); var thereAreOptionalArguments = parameters.Any(p => p.Position == 0); Write("Usage:"); Write(); Write("\t{0} {1}{2}{3}", exeName, parameterList, parameterList.Length > 0 ? " " : "", thereAreOptionalArguments ? "[args]" : ""); if (thereAreRequiredArguments) { Write(); Write(); Write("Required arguments:"); foreach (var parameter in parameters.Where(p => p.Position > 0)) { Write(); WritePositionalParameter(parameter, ExampleOutputSettings.ShowExamples); } } if (thereAreOptionalArguments) { Write(); Write(); Write("Additional arguments:"); foreach (var parameter in parameters.Where(p => p.Position == 0)) { Write(); Write("\t/{0}\t{1}", parameter.Name, parameter.Description); PossibleWriteExamples(parameter); } } }
public ISoldier CreatSoldier(string[] soldierArgs, ICollection <ISoldier> privatesList) { string soldierType = soldierArgs[0]; int id = int.Parse(soldierArgs[1]); string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; ISoldier soldier = null; if (soldierType == "Private") { decimal salary = decimal.Parse(soldierArgs[4]); soldier = new Private(firstName, lastName, id, salary); } else if (soldierType == "LieutenantGeneral") { decimal salary = decimal.Parse(soldierArgs[4]); soldier = GreateGeneral(soldierArgs, id, firstName, lastName, salary, privatesList); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; IEngineer engineer = CreateEngineer(soldierArgs, id, firstName, lastName, salary, corps); soldier = engineer; } else if (soldierType == "Commando") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; ICommando commando = CreateComando(soldierArgs, id, firstName, lastName, salary, corps); soldier = commando; } else if (soldierType == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); soldier = new Spy(firstName, lastName, id, codeNumber); } return(soldier); }
public void Run() { while (true) { string[] command = reader.ReadLine().Split(); if (command[0] == "End") { break; } string soldierType = command[0]; int id = int.Parse(command[1]); string firstName = command[2]; string lastName = command[3]; ISoldier soldier = null; if (soldierType == "Private") { decimal salary = decimal.Parse(command[4]); soldier = new Private(id, firstName, lastName, salary); } else if (soldierType == "LieutenantGeneral") { soldier = AddGeneral(command, id, firstName, lastName); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(command[4]); string corps = command[5]; try { IEngineer engineer = CreateEngineer(command, id, firstName, lastName, salary, corps); soldier = engineer; } catch (InvalidCorpsException ice) { continue; } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(command[4]); string corps = command[5]; try { ICommando commando = GetCommando(command, id, firstName, lastName, salary, corps); soldier = commando; } catch (Exception) { throw; } } else if (soldierType == "Spy") { int codeNumber = int.Parse(command[4]); soldier = new Spy(id, firstName, lastName, codeNumber); } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { this.writter.WriteLine(soldier.ToString()); } }
public RunOptions(ICommando commando) { Command = commando.Command; Arguments = commando.Arguments; }
public static string Stringify(this ICommando soldier) => $"{Stringify((ISpecialisedSoldier) soldier)}\r\nMissions:\r\n {string.Join("\r\n ", soldier.Missions)}".TrimEnd();
static void Execute(ICommando instance) { instance.Run(); }
static BindingReport PopulateProperties(IEnumerable <CommandLineParameter> parameters, ICommando instance) { var binder = new Binder(); var report = binder.Bind(instance, parameters); return(report); }
public void Run() { string command; while ((command = this.reader.ReadLine()) != "End") { string[] commandSplit = command.Split().ToArray(); string soldierType = commandSplit[0]; int id = int.Parse(commandSplit[1]); string firstName = commandSplit[2]; string lastName = commandSplit[3]; ISoldier soldier = null; if (soldierType == "Private") { decimal salary = decimal.Parse(commandSplit[4]); soldier = AddPrivate(id, firstName, lastName, salary); } else if (soldierType == "LeutenantGeneral") { soldier = AddGeneral(commandSplit, id, firstName, lastName); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(commandSplit[4]); string corps = commandSplit[5]; try { soldier = AddEngineer(commandSplit, id, firstName, lastName, salary, corps); } catch (InvalidCorpsException invCorpEx) { continue; } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(commandSplit[4]); string corps = commandSplit[5]; try { ICommando commando = GetCommmando(commandSplit, id, firstName, lastName, salary, corps); soldier = commando; } catch (InvalidCorpsException invCorpEx) { continue; } } else if (soldierType == "Spy") { int codeNumber = int.Parse(commandSplit[4]); soldier = new Spy(id, firstName, lastName, codeNumber); } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in soldiers) { this.writer.WriteLine(soldier.ToString()); } }
static void Main(string[] args) { List <ISoldier> soldiers = new List <ISoldier>(); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] info = input.Split(); string type = info[0]; int id = int.Parse(info[1]); string firstName = info[2]; string lastName = info[3]; try { if (type == "Private") { decimal salary = decimal.Parse(info[4]); IPrivate @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (type == "LieutenantGeneral") { decimal salary = decimal.Parse(info[4]); ILieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < info.Length; i++) { int currentId = int.Parse(info[i]); IPrivate @private = (IPrivate)soldiers.First(p => p.Id == currentId); lieutenantGeneral.AddPrivate(@private); } soldiers.Add(lieutenantGeneral); } else if (type == "Engineer") { decimal salary = decimal.Parse(info[4]); IEngineer engineer = null; Corps corps; bool validCorps = Enum.TryParse <Corps>(info[5], out corps); if (validCorps) { engineer = new Engineer(id, firstName, lastName, salary, corps); } else { throw new InvalidCorpsException(); } for (int i = 6; i < info.Length; i += 2) { string repairPart = info[i]; int repairHours = int.Parse(info[i + 1]); IRepair repair = new Repair(repairPart, repairHours); engineer.AddRepair(repair); } soldiers.Add(engineer); } else if (type == "Commando") { decimal salary = decimal.Parse(info[4]); ICommando commando = null; Corps corps; bool validCorps = Enum.TryParse <Corps>(info[5], out corps); if (validCorps) { commando = new Commando(id, firstName, lastName, salary, corps); } else { throw new InvalidCorpsException(); } for (int i = 6; i < info.Length; i += 2) { try { string codeName = info[i]; MissionState state; bool validMission = Enum.TryParse <MissionState>(info[i + 1], out state); if (validMission) { IMission mission = new Mission(codeName, state); commando.AddMission(mission); } else { throw new InvalidMissionException(); } } catch (InvalidMissionException ime) { Console.WriteLine(); } } soldiers.Add(commando); } else if (type == "Spy") { int codeNumber = int.Parse(info[4]); ISpy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } } catch (InvalidCorpsException ice) { Console.WriteLine(); } } foreach (var soldier in soldiers) { Console.WriteLine(soldier.ToString()); } }
public void Run() { string command; while ((command = this.reader.ReadLine()) != "End") { string[] cmdArg = command .Split() .ToArray(); string soldierType = cmdArg[0]; int id = int.Parse(cmdArg[1]); string firstName = cmdArg[2]; string lastName = cmdArg[3]; decimal salary; ISoldier soldier = null; switch (soldierType) { case "Private": soldier = AddPrivate(cmdArg, id, firstName, lastName); break; case "LieutenantGeneral": soldier = AddGeneral(cmdArg, id, firstName, lastName, out soldier, out salary); break; case "Engineer": salary = decimal.Parse(cmdArg[4]); string corps = cmdArg[5]; try { soldier = CreateEngineer(cmdArg, id, firstName, lastName, salary, corps); } catch (InvalidCorpsException) { continue; } break; case "Commando": salary = decimal.Parse(cmdArg[4]); corps = cmdArg[5]; try { ICommando commando = CreateCommando(cmdArg, id, firstName, lastName, salary, corps); soldier = commando; } catch (InvalidCorpsException) { continue; } break; case "Spy": soldier = AddSpy(cmdArg, id, firstName, lastName); break; default: break; } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { this.writer.WriteLine(soldier.ToString()); } }
public void Run() { string cmd; while ((cmd = this.reader.ReadLine()) != END_INPUT) { string[] args = cmd.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray(); string soldierType = args[0]; int iD = int.Parse(args[1]); string firstName = args[2]; string lastName = args[3]; ISoldier soldier = null; switch (soldierType) { case "Private": decimal salary = decimal.Parse(args[4]); soldier = CreatePrivate(iD, firstName, lastName, salary); break; case "LieutenantGeneral": salary = decimal.Parse(args[4]); ILieutenantGeneral general = CreateLieutenant(args, iD, firstName, lastName, salary); soldier = general; break; case "Engineer": salary = decimal.Parse(args[4]); string corps = args[5]; try { IEngineer engineer = CreateEngineer(args, iD, firstName, lastName, salary, corps); soldier = engineer; } catch (InvalideCorpsException) { continue; } break; case "Commando": salary = decimal.Parse(args[4]); corps = args[5]; try { ICommando commando = CreateCommando(args, iD, firstName, lastName, salary, corps); soldier = commando; } catch (InvalideCorpsException) { continue; } break; case "Spy": int codeNumber = int.Parse(args[4]); soldier = new Spy(iD, firstName, lastName, codeNumber); break; } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { this.writer.Writeline(soldier.ToString()); } }
public void Run() { string command; while ((command = Console.ReadLine()) != "End") { ISoldier soldier = null; string[] cmdArgs = command.Split(); string type = cmdArgs[0]; int id = int.Parse(cmdArgs[1]); string firstName = cmdArgs[2]; string lastName = cmdArgs[3]; if (type == "Private") { decimal salary = decimal.Parse(cmdArgs[4]); soldier = new Private(id, firstName, lastName, salary); } else if (type == "LieutenantGeneral") { soldier = AddGeneral(cmdArgs, id, firstName, lastName); } else if (type == "Engineer") { decimal salary = decimal.Parse(cmdArgs[4]); string corps = cmdArgs[5]; try { IEngineer engineer = CreateEngineer(cmdArgs, id, firstName, lastName, salary, corps); soldier = engineer; } catch (InvalidCorpsException ice) { continue; } } else if (type == "Commando") { decimal salary = decimal.Parse(cmdArgs[4]); string corps = cmdArgs[5]; try { ICommando commando = GetCommando(cmdArgs, id, firstName, lastName, salary, corps); soldier = commando; } catch (InvalidCorpsException ice) { continue; } } else if (type == "Spy") { int codeNumber = int.Parse(cmdArgs[4]); soldier = new Spy(id, firstName, lastName, codeNumber); } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { Console.WriteLine(soldier); } }
public RunOptions(ICommando commando) { Command = commando.Command; Arguments = commando.Arguments; }
public void Run() { List <ISoldier> soldiers = new List <ISoldier>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { string[] elements = input .Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); string typeSoldier = elements[0]; string id = elements[1]; string firstName = elements[2]; string lastName = elements[3]; ISoldier soldier = null; if (typeSoldier == "Private") { decimal salary = decimal.Parse(elements[4]); soldier = new Private(id, firstName, lastName, salary); } else if (typeSoldier == "LieutenantGeneral") { decimal salary = decimal.Parse(elements[4]); soldier = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < elements.Length; i++) { string targetId = elements[i]; IPrivate @private = (IPrivate)soldiers .FirstOrDefault(x => x.Id == targetId); ILieutenantGeneral lieutenantGeneral = soldier as ILieutenantGeneral; lieutenantGeneral.AddPrivate(@private); } } else if (typeSoldier == "Engineer") { decimal salary = decimal.Parse(elements[4]); string targetCorp = elements[5]; bool isCorpValid = Enum.TryParse <Corp>(targetCorp, out Corp corp); if (!isCorpValid) { continue; } soldier = new Engineer(id, firstName, lastName, salary, corp); IEngineer engineer = soldier as IEngineer; for (int i = 6; i < elements.Length; i += 2) { string partName = elements[i]; int hours = int.Parse(elements[i + 1]); IRepair repair = new Repair(partName, hours); engineer.AddRepair(repair); } } else if (typeSoldier == "Commando") { decimal salary = decimal.Parse(elements[4]); string targetCorp = elements[5]; bool isCorpValid = Enum.TryParse <Corp>(targetCorp, out Corp corp); if (!isCorpValid) { continue; } soldier = new Commando(id, firstName, lastName, salary, corp); ICommando commando = soldier as ICommando; for (int i = 6; i < elements.Length; i += 2) { string codeName = elements[i]; string targetMissionState = elements[i + 1]; bool isMissionStateValid = Enum.TryParse <MissionState>(targetMissionState, out MissionState missionState); if (!isMissionStateValid) { continue; } IMission mission = new Mission(codeName, missionState); commando.AddMission(mission); } } else if (typeSoldier == "Spy") { int codeNumber = int.Parse(elements[4]); soldier = new Spy(id, firstName, lastName, codeNumber); } soldiers.Add(soldier); } foreach (ISoldier soldier in soldiers) { Console.WriteLine(soldier); } }
public void Run() { string command; while ((command = this.reader.ReadLine()) != "End") { string[] cmdArgs = command .Split(' ', StringSplitOptions.RemoveEmptyEntries) .ToArray(); string soldierType = cmdArgs[0]; int id = int.Parse(cmdArgs[1]); string firstName = cmdArgs[2]; string lastName = cmdArgs[3]; ISoldier soldier = null; if (soldierType == "Private") { soldier = AddPrivate(cmdArgs, id, firstName, lastName); } else if (soldierType == "LieutenantGeneral") { LieutenantGeneral general = AddGeneral(cmdArgs, id, firstName, lastName); soldier = general; } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(cmdArgs[4]); string corps = cmdArgs[5]; try { IEngineer engineer = AddSoldier(cmdArgs, id, firstName, lastName, salary, corps); soldier = engineer; } catch (InvalidCorpsExeption ice) { continue; } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(cmdArgs[4]); string corps = cmdArgs[5]; try { ICommando commando = GetCommando(cmdArgs, id, firstName, lastName, salary, corps); soldier = commando; } catch (InvalidCorpsExeption ice) { continue; } } else if (soldierType == "Spy") { int codeNumber = int.Parse(cmdArgs[4]); soldier = new Spy(id, firstName, lastName, codeNumber); } if (soldier != null) { this.soldiers.Add(soldier); } } foreach (var soldier in this.soldiers) { this.writer.WriteLine(soldier.ToString()); } }
public void Run() { string[] commandArgs = Console.ReadLine() .Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); string soldierType = commandArgs[0]; while (soldierType != "End") { ISoldier soldier = null; int id = int.Parse(commandArgs[1]); string firstName = commandArgs[2]; string lastName = commandArgs[3]; decimal.TryParse(commandArgs[4], out decimal salary); if (soldierType == "Private") { soldier = CreatePrivate(id, firstName, lastName, salary); } else if (soldierType == "LieutenantGeneral") { soldier = CreateGeneral(commandArgs, id, firstName, lastName, salary); } else if (soldierType == "Engineer") { try { IEngineer engineer = CreateEngineer(commandArgs, id, firstName, lastName, salary); soldier = engineer; } catch (InvalidCorpsException ice) { continue; } } else if (soldierType == "Commando") { try { ICommando commando = CreateCommando(commandArgs, id, firstName, lastName, salary); soldier = commando; } catch (InvalidCorpsException ice) { continue; } } else if (soldierType == "Spy") { soldier = CreateSpy(commandArgs, id, firstName, lastName); } if (soldier != null) { this.soldiers.Add(soldier); } commandArgs = Console.ReadLine() .Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); soldierType = commandArgs[0]; } foreach (var soldier in this.soldiers) { Console.WriteLine(soldier); } }