public void Run() { string soldierInput = this.reader.ReadLine(); while (soldierInput.ToLower() != "end") { string[] soldierArgs = soldierInput.Split(' ').ToArray(); string soldierType = soldierArgs[0]; string[] soldierParams = soldierArgs.Skip(1).ToArray(); try { if (soldierType == "Private") { Private newPrivate = CreatePrivate(soldierParams); soldiers.Add(newPrivate); } else if (soldierType == "LieutenantGeneral") { LieutenantGeneral lieutenantGeneral = CreateLieutenantGeneral(soldierParams); soldiers.Add(lieutenantGeneral); } else if (soldierType == "Engineer") { Engineer engineer = CreateEngineer(soldierParams); soldiers.Add(engineer); } else if (soldierType == "Commando") { Commando commando = CreateCommando(soldierParams); soldiers.Add(commando); } else if (soldierType == "Spy") { Spy spy = CreateSpy(soldierParams); soldiers.Add(spy); } } catch (InvalidCastException) { } soldierInput = this.reader.ReadLine(); } foreach (Soldier soldier in soldiers) { this.writer.WriteLine(soldier.ToString()); } }
private static void CreateLeutenantGeneral(List <Private> allPrivates, string[] tokens, int id, string firstName, string lastName, decimal salary) { List <IPrivate> currentLeutenantPrivatesLIst = new List <IPrivate>(); for (int i = 5; i < tokens.Length; i++) { int currentId = int.Parse(tokens[i]); Private currentPrivate = allPrivates.FirstOrDefault(p => p.ID == currentId); currentLeutenantPrivatesLIst.Add(currentPrivate); } LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, currentLeutenantPrivatesLIst); Console.WriteLine(lieutenantGeneral.ToString()); }
private LieutenantGeneral CreateLieutenantGeneral(string[] soldierParams) { // LieutenantGeneral<id> < firstName > < lastName > < salary > < private1Id > < private2Id > … < privateNId > string id = soldierParams[0]; string firstName = soldierParams[1]; string lastName = soldierParams[2]; decimal salary = decimal.Parse(soldierParams[3]); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 4; i < soldierParams.Length; i++) { string newPrivateId = soldierParams[i]; Private newPrivate = (Private)this.soldiers.Where(s => s.Id == newPrivateId).FirstOrDefault(); lieutenantGeneral.AddPrivate(newPrivate); } return(lieutenantGeneral); }
private static List <Private> GetPrivates(List <string> ids, HashSet <Soldier> soldiers) { List <Private> privates = new List <Private>(); List <Soldier> filterSoldiers = soldiers .Where(x => x.GetType().Name == nameof(Private)) .ToList(); foreach (var id in ids) { if (soldiers.Select(x => x.Id).Contains(id)) { Private @private = (Private)soldiers .FirstOrDefault(x => x.Id == id); privates.Add(@private); } } return(privates); }
static void Main(string[] args) { string command; var allSoldiers = new List <Soldier>(); while ((command = Console.ReadLine()) != "End") { var cmdArgs = command.Split(); var soldierType = cmdArgs[0]; switch (soldierType) { case "Private": var privateS = new Private(cmdArgs[1], cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4])); allSoldiers.Add(privateS); break; case "LieutenantGeneral": var lieutenantGeneral = new LieutenantGeneral(cmdArgs[1], cmdArgs[2], cmdArgs[3], decimal.Parse(cmdArgs[4])); lieutenantGeneral.GetPrivates(cmdArgs, allSoldiers); allSoldiers.Add(lieutenantGeneral); break; case "Engineer": var engineer = new Engineer(); engineer.ReadEngineer(cmdArgs, allSoldiers); break; case "Commando": var commando = new Commando(); commando.ReadCommando(cmdArgs, allSoldiers); break; case "Spy": var spy = new Spy(cmdArgs[1], cmdArgs[2], cmdArgs[3], int.Parse(cmdArgs[4])); allSoldiers.Add(spy); break; default: break; } } Console.WriteLine(String.Join(Environment.NewLine, allSoldiers)); }
private void LieutenantGeneralCreator(string[] inputArr) { string id = inputArr[1]; string firstName = inputArr[2]; string lastName = inputArr[3]; decimal salary = decimal.Parse(inputArr[4]); var newLieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < inputArr.Length; i++) { if (soldiersList.Any(x => x.Id == inputArr[i])) { Private toAdd = (Private)soldiersList.FirstOrDefault(x => x.Id == inputArr[i]); newLieutenantGeneral.AddPrivate(toAdd); } } soldiersList.Add(newLieutenantGeneral); }
public void AddPrivate(Private newPrivate) { privates.Add(newPrivate); }
public static void Main(string[] args) { Dictionary <string, ISoldier> soldiersById = new Dictionary <string, ISoldier>(); while (true) { string line = Console.ReadLine(); if (line == "End") { break; } string[] parts = line.Split(); string type = parts[0]; string id = parts[1]; string firstName = parts[2]; string lastName = parts[3]; if (type == nameof(Private)) { decimal salary = decimal.Parse(parts[4]); soldiersById[id] = new Private(id, firstName, lastName, salary); } else if (type == nameof(LieutenantGeneral)) { decimal salary = decimal.Parse(parts[4]); ILieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < parts.Length; i++) { string privateId = parts[i]; if (!soldiersById.ContainsKey(privateId)) { continue; } lieutenantGeneral.AddPrivate((IPrivate)soldiersById[privateId]); } soldiersById[id] = lieutenantGeneral; } else if (type == nameof(Engineer)) { decimal salary = decimal.Parse(parts[4]); bool isCorpsValid = Enum.TryParse(parts[5], out Corps corps); if (!isCorpsValid) { continue; } IEngineer engineer = new Engineer(id, firstName, lastName, salary, corps); for (int i = 6; i < parts.Length; i += 2) { string part = parts[i]; int hoursWorked = int.Parse(parts[i + 1]); IRepair repair = new Repair(part, hoursWorked); engineer.AddRepair(repair); } soldiersById[id] = engineer; } else if (type == nameof(Commando)) { decimal salary = decimal.Parse(parts[4]); bool isCorpsValid = Enum.TryParse(parts[5], out Corps corps); if (!isCorpsValid) { continue; } ICommando commando = new Commando(id, firstName, lastName, salary, corps); for (int i = 6; i < parts.Length; i += 2) { string codeName = parts[i]; string state = parts[i + 1]; bool isMissionValid = Enum.TryParse(state, out MissionState missionState); if (!isMissionValid) { continue; } IMission mission = new Mission(codeName, missionState); commando.AddMission(mission); } soldiersById[id] = commando; } else if (type == nameof(Spy)) { int codeNumber = int.Parse(parts[4]); ISpy spy = new Spy(id, firstName, lastName, codeNumber); soldiersById[id] = spy; } } foreach (var soldier in soldiersById.Values) { Console.WriteLine(soldier); } }
static void Main(string[] args) { List <ISoldier> soldiers = new List <ISoldier>(); string[] input = Console.ReadLine().Split(); while (input[0].ToLower() != "end") { string title = input[0].ToLower(); int id = int.Parse(input[1]); string firstName = input[2]; string lastName = input[3]; if (title == "private") { decimal salary = decimal.Parse(input[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (title == "lieutenantgeneral") { decimal salary = decimal.Parse(input[4]); List <IPrivate> privates = GetLieutenantGeneralPrivates(input.Skip(5).ToArray(), soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (title == "engineer") { decimal salary = decimal.Parse(input[4]); string corps = input[5]; List <IRepair> repairs = GetEngineerRepairs(input.Skip(6).ToArray()); if (corps == "Airforces") { Engineer engineer = new Engineer(id, firstName, lastName, salary, SoldiersCorpsEnum.Airforces, repairs); soldiers.Add(engineer); } else if (corps == "Marines") { Engineer engineer = new Engineer(id, firstName, lastName, salary, SoldiersCorpsEnum.Marines, repairs); soldiers.Add(engineer); } } else if (title == "commando") { decimal salary = decimal.Parse(input[4]); string corps = input[5]; List <IMission> missions = GetCommandoMissions(input.Skip(6).ToArray()); if (corps == "Airforces") { Commando commando = new Commando(id, firstName, lastName, salary, SoldiersCorpsEnum.Airforces, missions); soldiers.Add(commando); } else if (corps == "Marines") { Commando commando = new Commando(id, firstName, lastName, salary, SoldiersCorpsEnum.Marines, missions); soldiers.Add(commando); } } else if (title == "spy") { int codeNumber = int.Parse(input[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } input = Console.ReadLine().Split(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
public static void Main() { List <ISoldier> soldiers = new List <ISoldier>(); string command = Console.ReadLine(); while (command != "End") { string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries); string id = tokens[1]; string firstName = tokens[2]; string lastName = tokens[3]; if (tokens[0] == "Private") { decimal salary = decimal.Parse(tokens[4]); Private current = new Private(firstName, lastName, id, salary); soldiers.Add(current); } else if (tokens[0] == "Commando") { decimal salary = decimal.Parse(tokens[4]); string corps = tokens[5]; Commando current = new Commando(firstName, lastName, id, salary, corps, tokens.Skip(6).ToArray()); if (current.Corps != null) { soldiers.Add(current); } } else if (tokens[0] == "LieutenantGeneral") { decimal salary = decimal.Parse(tokens[4]); List <Private> privates = new List <Private>(); for (int i = 5; i < tokens.Length; i++) { Private soldier = (Private)soldiers.First(s => s.Id == tokens[i]); privates.Add(soldier); } LieutenantGeneral current = new LieutenantGeneral(firstName, lastName, id, salary, privates); soldiers.Add(current); } else if (tokens[0] == "Engineer") { decimal salary = decimal.Parse(tokens[4]); string corps = tokens[5]; Engineer current = new Engineer(firstName, lastName, id, salary, corps, tokens.Skip(6).ToArray()); if (current.Corps != null) { soldiers.Add(current); } } else if (tokens[0] == "Spy") { int codeNumber = int.Parse(tokens[4]); Spy current = new Spy(firstName, lastName, id, codeNumber); soldiers.Add(current); } command = Console.ReadLine(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
private static void SavePrivate(string[] inputParts) { IPrivate newPrivate = new Private(int.Parse(inputParts[1]), inputParts[2], inputParts[3], double.Parse(inputParts[4])); soldiers.Add(newPrivate); }
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); } } }
public void Run() { string line = Console.ReadLine(); while (line != "End") { string[] args = line.Split(" ", StringSplitOptions.RemoveEmptyEntries); string type = args[0]; try { switch (type) { case "Private": Private privateSoldier = this.privateFactory.MakePrivateSoldier(args); this.soldiers.Add(privateSoldier); break; case "LieutenantGeneral": LieutenantGeneral lieutenantGeneral = this.lieutenantGeneralFactory.MakeLieutenantGeneral(args); for (int i = 5; i < args.Length; i++) { Private currentPrivate = (Private)this.soldiers.FirstOrDefault(p => p.Id == int.Parse(args[i])); lieutenantGeneral.AddPrivate(currentPrivate); } this.soldiers.Add(lieutenantGeneral); break; case "Engineer": Engineer engineer = this.engineerFactory.MakeEngineer(args); for (int i = 6; i < args.Length; i += 2) { string name = args[i]; int hours = int.Parse(args[i + 1]); Repair repair = this.repairFactory.MakeRepair(name, hours); engineer.AddRepair(repair); } this.soldiers.Add(engineer); break; case "Commando": Commando commando = this.commandoFactory.MakeCommando(args); for (int i = 6; i < args.Length; i += 2) { string name = args[i]; string state = args[i + 1]; Mission mission = this.missionFactory.MakeMission(name, state); commando.AddMission(mission); } this.soldiers.Add(commando); break; case "Spy": Spy spy = this.spyFactory.MakeSpy(args); this.soldiers.Add(spy); break; default: break; } } catch (Exception) { } line = Console.ReadLine(); } foreach (var soldier in this.soldiers) { Console.WriteLine(soldier.ToString()); } }
static void Main(string[] args) { List <ISoldier> soldiers = new List <ISoldier>(); List <Private> privates = new List <Private>(); string text = Console.ReadLine(); while (text != "End") { string[] split = text.Split(); string type = split[0]; string id = split[1]; string firstName = split[2]; string lastName = split[3]; decimal salary = decimal.Parse(split[4]); switch (type) { case "Private": Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); privates.Add(@private); break; case "LieutenantGeneral": LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < split.Length; i++) { if (privates.Any(s => s.Id == split[i])) { lieutenantGeneral.AddPrivate(privates.FirstOrDefault(s => s.Id == split[i])); } } soldiers.Add(lieutenantGeneral); break; case "Engineer": string corp = split[5]; if (Enum.IsDefined(typeof(Enums.Corp), corp)) { Engineer engineer = new Engineer(id, firstName, lastName, salary, Enum.Parse <Enums.Corp>(corp)); for (int i = 6; i < split.Length; i += 2) { engineer.AddRepair(new Repair(split[i], int.Parse(split[i + 1]))); } soldiers.Add(engineer); } break; case "Commando": corp = split[5]; if (Enum.IsDefined(typeof(Enums.Corp), corp)) { Commando commando = new Commando(id, firstName, lastName, salary, Enum.Parse <Enums.Corp>(corp)); for (int i = 6; i < split.Length; i += 2) { if (Enum.IsDefined(typeof(Enums.State), split[i + 1])) { commando.AddMission(new Mission(split[i], Enum.Parse <Enums.State>(split[i + 1]))); } } soldiers.Add(commando); } break; case "Spy": int codeNumber = (int)salary; soldiers.Add(new Spy(id, firstName, lastName, codeNumber)); break; } text = Console.ReadLine(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
static void Main(string[] args) { List <Soldier> army = new List <Soldier>(); string command; while ((command = Console.ReadLine()) != "End") { string[] curCommand = command.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray(); string soldierType = curCommand[0]; int Id = int.Parse(curCommand[1]); string firstName = curCommand[2]; string lastName = curCommand[3]; switch (soldierType) { case "Private": Private curPrivate = new Private(Id, firstName, lastName, decimal.Parse(curCommand[4])); army.Add(curPrivate); break; case "LieutenantGeneral": LieutenantGeneral curLieutenantGeneral = new LieutenantGeneral(Id, firstName, lastName, decimal.Parse(curCommand[4])); for (int i = 5; i < curCommand.Length; i++) { Soldier searchPrivate = army.First(el => el.Id == int.Parse(curCommand[i]) && el.GetType().Name == "Private"); curLieutenantGeneral.Add((Private)searchPrivate); } army.Add(curLieutenantGeneral); break; case "Engineer": try { Engineer curEngineer = new Engineer(Id, firstName, lastName, decimal.Parse(curCommand[4]), curCommand[5]); string[] repairs = curCommand.Skip(6).ToArray(); for (int i = 0; i < repairs.Length; i += 2) { Repair curRepair = new Repair(repairs[i], int.Parse(repairs[i + 1])); curEngineer.Add(curRepair); } army.Add(curEngineer); } catch (Exception) { continue; } break; case "Commando": try { Commando curCommando = new Commando(Id, firstName, lastName, decimal.Parse(curCommand[4]), curCommand[5]); string[] missions = curCommand.Skip(6).ToArray(); for (int i = 0; i < missions.Length; i += 2) { try { Mission curMission = new Mission(missions[i], missions[i + 1]); curCommando.Add(curMission); } catch (Exception) { continue; } } army.Add(curCommando); } catch (Exception) { continue; } break; case "Spy": Spy curSpy = new Spy(Id, firstName, lastName, int.Parse(curCommand[4])); army.Add(curSpy); break; } } foreach (var item in army) { Console.WriteLine(item); } }
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(); }
public static void Main() { List <Private> privates = new List <Private>(); Corps corps; while (true) { string[] token = Console.ReadLine().Split(); if (token[0] == "End") { break; } if (token[0] == "Private") { Private privatee = new Private(token[1], token[2], token[3], decimal.Parse(token[4])); privates.Add(privatee); Console.WriteLine(privatee.ToString()); } else if (token[0] == "LieutenantGeneral") { LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(token[1], token[2], token[3], decimal.Parse(token[4])); if (token.Length >= 5) { for (int i = 5; i < token.Length; i++) { string privateId = token[i]; Private privatee = privates.FirstOrDefault(x => x.Id == privateId); lieutenantGeneral.Privates.Add(privatee); } } Console.WriteLine(lieutenantGeneral.ToString()); } else if (token[0] == "Engineer") { if (Enum.TryParse(token[5], out corps)) { Engineer engineer = new Engineer(token[1], token[2], token[3], decimal.Parse(token[4]), corps); if (token.Length >= 6) { for (int i = 6; i < token.Length; i += 2) { string repairPartName = token[i]; int repairPartHours = int.Parse(token[i + 1]); Repair repair = new Repair(repairPartName, repairPartHours); engineer.Repairs.Add(repair); } } Console.WriteLine(engineer.ToString()); } } else if (token[0] == "Commando") { if (Enum.TryParse(token[5], out corps)) { Commando commando = new Commando(token[1], token[2], token[3], decimal.Parse(token[4]), corps); if (token.Length >= 6) { for (int i = 6; i < token.Length; i += 2) { string missionName = token[i]; string missionProgress = token[i + 1]; if (Enum.TryParse(missionProgress, out MissionState missionState)) { Mission mission = new Mission(missionName, missionState); commando.Missions.Add(mission); } } Console.WriteLine(commando.ToString()); } } } else if (token[0] == "Spy") { Spy spy = new Spy(token[1], token[2], token[3], int.Parse(token[4])); Console.WriteLine(spy); } else { throw new ArgumentException("Invalid Type of Soldier!"); } } }
public static void Main(string[] args) { List <Soldier> soldiers = new List <Soldier>(); while (true) { string soldier = Console.ReadLine(); if (soldier == "End") { break; } string[] soldierArgs = soldier.Split(); string soldierType = soldierArgs[0]; string id = soldierArgs[1]; string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; if (soldierType == "Private") { decimal salary = decimal.Parse(soldierArgs[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (soldierType == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } else if (soldierType == "LieutenantGeneral") { decimal salary = decimal.Parse(soldierArgs[4]); // Turning the last elements of the input to List of Ids List <string> ids = soldierArgs.Skip(5).ToList(); // Method that accepts List<string> ids and List<soldiers> soldiers => the one we created in the begging // to hold all the types of soldiers HashSet <Private> privates = GetPrivates(ids, soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> repairArgs = soldierArgs.Skip(6).ToList(); List <Repair> repairs = GetRepairs(repairArgs); try { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(engineer); } catch (Exception) { continue; } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> missionArgs = soldierArgs.Skip(6).ToList(); List <Mission> missions = GetMissions(missionArgs); try { Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(commando); } catch (Exception) { continue; } } } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
public void AddPrivate(Private soldier) { privates.Add(soldier); }
public static void Main() { List <Soldier> soldiers = new List <Soldier>(); string userInput = Console.ReadLine(); while (userInput != "End") { string[] soldierInfo = userInput.Split(" "); string soldierType = soldierInfo[0]; int id = int.Parse(soldierInfo[1]); string firstName = soldierInfo[2]; string lastName = soldierInfo[3]; decimal salary; bool isSalary = decimal.TryParse(soldierInfo[4], out salary); Soldier newSoldier; switch (soldierType) { case "Private": newSoldier = new Private(id, firstName, lastName, salary); soldiers.Add(newSoldier); break; case "LieutenantGeneral": int[] privatesToAddIds = soldierInfo.Skip(5).Select(int.Parse).ToArray(); List <Private> privates = GetPrivates(privatesToAddIds, soldiers); newSoldier = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(newSoldier); break; case "Spy": int codeNumber = int.Parse(soldierInfo[4]); Spy newSpy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(newSpy); break; case "Engineer": string corps = soldierInfo[5]; List <Repair> repairs = GetRepairs(soldierInfo); try { Engineer newEngineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(newEngineer); } catch { userInput = Console.ReadLine(); continue; } break; case "Commando": string commandoCorps = soldierInfo[5]; List <Mission> missions = GetMissions(soldierInfo); Commando newCommando = new Commando(id, firstName, lastName, salary, commandoCorps, missions); soldiers.Add(newCommando); break; } userInput = Console.ReadLine(); } Console.WriteLine($"{string.Join(Environment.NewLine, soldiers)}"); }
static void Main() { var privates = new Dictionary <string, Private>(); string input; while ((input = Console.ReadLine()) != "End") { var cmdArgs = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var soldierType = cmdArgs[0]; string id; string firstName; string lastName; decimal salary; Corps corps; switch (soldierType) { case "Private": id = cmdArgs[1]; firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = decimal.Parse(cmdArgs[4]); var privateSoldier = new Private(firstName, lastName, id, salary); privates.Add(id, privateSoldier); Console.WriteLine(privateSoldier); break; case "LieutenantGeneral": id = cmdArgs[1]; firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = decimal.Parse(cmdArgs[4]); var leutenantGeneral = new LieutenantGeneral(firstName, lastName, id, salary); if (cmdArgs.Length >= 5) { for (var i = 5; i < cmdArgs.Length; i++) { string privateId = cmdArgs[i]; privateSoldier = privates[privateId]; leutenantGeneral.Privates.Add(privateSoldier); } } Console.WriteLine(leutenantGeneral); break; case "Engineer": id = cmdArgs[1]; firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = decimal.Parse(cmdArgs[4]); if (Enum.TryParse(cmdArgs[5], out corps)) { var engineer = new Engineer(firstName, lastName, id); 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.Repairs.Add(repair); } } Console.WriteLine(engineer); } break; case "Commando": id = cmdArgs[1]; firstName = cmdArgs[2]; lastName = cmdArgs[3]; salary = decimal.Parse(cmdArgs[4]); if (Enum.TryParse(cmdArgs[5], out corps)) { Commando commando = new Commando(firstName, lastName, id, 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.ToString()); } } break; case "Spy": id = 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) { List <Private> privates = new List <Private>(); while (true) { try { string[] soldierArgs = Console.ReadLine().Split(); string type = soldierArgs[0]; if (type == "End") { break; } string id = soldierArgs[1]; string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; if (type == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); var spy = new Spy(id, firstName, lastName, codeNumber); Console.WriteLine(spy); } else { decimal salary = decimal.Parse(soldierArgs[4]); if (type == "Private") { var @private = new Private(id, firstName, lastName, salary); privates.Add(@private); Console.WriteLine(@private); } else if (type == "LieutenantGeneral") { var privatesGeneral = new List <Private>(); for (int i = 5; i < soldierArgs.Length; i++) { var currentPrivate = privates.FirstOrDefault(x => x.Id == soldierArgs[i]); privatesGeneral.Add(currentPrivate); } var leutenant = new LieutenantGeneral(id, firstName, lastName, salary, privatesGeneral); Console.WriteLine(leutenant); } else if (type == "Engineer") { var repairs = new List <Repair>(); string corps = soldierArgs[5]; for (int i = 6; i < soldierArgs.Length; i += 2) { string name = soldierArgs[i]; int hours = int.Parse(soldierArgs[i + 1]); var repair = new Repair(name, hours); repairs.Add(repair); } var engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); Console.WriteLine(engineer); } else if (type == "Commando") { var missions = new List <Mission>(); string corps = soldierArgs[5]; for (int i = 6; i < soldierArgs.Length; i += 2) { try { string name = soldierArgs[i]; string state = soldierArgs[i + 1]; var mission = new Mission(name, state); missions.Add(mission); } catch (Exception) { } } var commando = new Commando(id, firstName, lastName, salary, corps, missions); Console.WriteLine(commando); } } } catch (Exception) { } } }
private static void AddPrivate(string[] tokens) { Private privat = new Private(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); soldiers.Add(privat); }
static void Main(string[] args) { List <Soldier> soldiers = new List <Soldier>(); string soldier = Console.ReadLine(); while (soldier != "End") { string[] soldierArgs = soldier.Split(); string soldierType = soldierArgs[0]; string id = soldierArgs[1]; string firstName = soldierArgs[2]; string lastName = soldierArgs[3]; if (soldierType == "Private") { decimal salary = decimal.Parse(soldierArgs[4]); Private @private = new Private(id, firstName, lastName, salary); soldiers.Add(@private); } else if (soldier == "Spy") { int codeNumber = int.Parse(soldierArgs[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); soldiers.Add(spy); } else if (soldierType == "LieutenantGeneral") { decimal salary = decimal.Parse(soldierArgs[4]); List <string> ids = soldierArgs.Skip(5).ToList(); List <Private> privates = GetPrivates(ids, soldiers); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); soldiers.Add(lieutenantGeneral); } else if (soldierType == "Engineer") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> repairArgs = soldierArgs.Skip(6).ToList(); List <Repair> repairs = GetRepairs(repairArgs); try { Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); soldiers.Add(engineer); } catch (Exception) { } } else if (soldierType == "Commando") { decimal salary = decimal.Parse(soldierArgs[4]); string corps = soldierArgs[5]; List <string> missionArgs = soldierArgs.Skip(6).ToList(); List <Mission> missions = GetMissions(missionArgs); try { Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); soldiers.Add(commando); } catch (Exception) { } } soldier = Console.ReadLine(); } foreach (var soldierObject in soldiers) { Console.WriteLine(soldierObject); } }
static void Main(string[] args) { string input = Console.ReadLine(); while (input != "End") { string[] splitted = input.Split(); string soldierType = splitted[0]; int id = int.Parse(splitted[1]); string firstName = splitted[2]; string lastName = splitted[3]; decimal salary = decimal.Parse(splitted[4]); ISoldier soldier = null; try { switch (soldierType) { case "Private": soldier = new Private(id, firstName, lastName, salary); break; case "Spy": soldier = new Spy(id, firstName, lastName, int.Parse(splitted[4])); break; case "LieutenantGeneral": LieutenantGeneral lieutenant = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < splitted.Length; i++) { int privateId = int.Parse(splitted[i]); ISoldier currentPrivate = soldiers .FirstOrDefault(s => s.Id == privateId); lieutenant.AddPrivate(currentPrivate); } soldier = lieutenant; break; case "Engineer": string engineerCorps = splitted[5]; Engineer engineer = new Engineer(id, firstName, lastName, salary, engineerCorps); for (int i = 6; i < splitted.Length; i += 2) { string repairPart = splitted[i]; int repairHours = int.Parse(splitted[i + 1]); IRepair repair = new Repair(repairPart, repairHours); engineer.AddRepair(repair); } soldier = engineer; break; case "Commando": string commandoCorps = splitted[5]; Commando commando = new Commando(id, firstName, lastName, salary, commandoCorps); for (int i = 6; i < splitted.Length; i += 2) { string missionCodeName = splitted[i]; string missionState = splitted[i + 1]; try { IMission mission = new Mission(missionCodeName, missionState); commando.AddMission(mission); } catch (ArgumentException) { } } soldier = commando; break; default: throw new ArgumentException("Invalid soldier type!"); } soldiers.Add(soldier); } catch (ArgumentException) { } input = Console.ReadLine(); } foreach (var soldier in soldiers) { Console.WriteLine(soldier); } }
static void Main(string[] args) { var privateList = new List <Private>(); while (true) { string[] input = Console.ReadLine().Split(); if (input[0] == "End") { break; } else if (input[0] == "Private") { id = int.Parse(input[1]); firstName = input[2]; lastName = input[3]; salary = decimal.Parse(input[4]); var privateSoldie = new Private(id, firstName, lastName, salary); privateList.Add(privateSoldie); Console.WriteLine(privateSoldie); } else if (input[0] == "Spy") { id = int.Parse(input[1]); firstName = input[2]; lastName = input[3]; int code = int.Parse(input[4]); var spy = new Spy(id, firstName, lastName, code); Console.WriteLine(spy); } else if (input[0] == "Engineer") { id = int.Parse(input[1]); firstName = input[2]; lastName = input[3]; salary = decimal.Parse(input[4]); corp = input[5]; try { var engineer = new Engineer(id, firstName, lastName, salary, corp); //string[] result = input.Skip(6).ToArray(); for (int i = 6; i < input.Length; i += 2) { string partname = input[i]; int hours = int.Parse(input[i + 1]); var repair = new Repair(partname, hours); engineer.Add(repair); } Console.WriteLine(engineer); } catch (ArgumentException) { //continue; } } else if (input[0] == "Commando") { id = int.Parse(input[1]); firstName = input[2]; lastName = input[3]; salary = decimal.Parse(input[4]); corp = input[5]; try { var commando = new Commando(id, firstName, lastName, salary, corp); //string[] result = input.Skip(6).ToArray(); for (int i = 6; i < input.Length; i += 2) { string partname = input[i]; string state = input[i + 1]; try { var repair = new Mission(partname, state); commando.Add(repair); } catch (ArgumentException) { //continue; } } Console.WriteLine(commando); } catch (ArgumentException) { //continue; } } else if (input[0] == "LieutenantGeneral") { id = int.Parse(input[1]); firstName = input[2]; lastName = input[3]; salary = decimal.Parse(input[4]); var leutenant = new LieutenantGeneral(id, firstName, lastName, salary); //string[] result = input.Skip(5).ToArray(); for (int i = 5; i < input.Length; i++) { int num = int.Parse(input[i]); var currentPrivateSoldier = privateList.FirstOrDefault(x => x.Id == num); leutenant.Add(currentPrivateSoldier); } Console.WriteLine(leutenant); } } }
public void AddPrivate(Private @private) { Privates.Add(@private); }
static void Main() { string command; var soldiers = new List <ISoldier>(); while ((command = Console.ReadLine()) != "End") { string[] entry = command.Split(" ", StringSplitOptions.RemoveEmptyEntries); if (entry[0] == "Private") { var newPrivate = new Private(entry[1], entry[2], entry[3], decimal.Parse(entry[4])); soldiers.Add(newPrivate); } else if (entry[0] == "LieutenantGeneral") { var newLtGen = new LieutenantGeneral(entry[1], entry[2], entry[3], decimal.Parse(entry[4])); for (int i = 5; i < entry.Length; i++) { newLtGen.Privates.Add(soldiers.FirstOrDefault(x => x.Id == entry[i])); } soldiers.Add(newLtGen); } else if (entry[0] == "Engineer" && (entry[5] == "Marines" || entry[5] == "Airforces")) { var newEngineer = new Engineer(entry[1], entry[2], entry[3], decimal.Parse(entry[4]), entry[5]); for (int i = 6; i < entry.Length - 1; i += 2) { var newRepair = new Repair(entry[i], int.Parse(entry[i + 1])); newEngineer.Repairs.Add(newRepair); } soldiers.Add(newEngineer); } else if (entry[0] == "Commando" && (entry[5] == "Marines" || entry[5] == "Airforces")) { var newCommando = new Commando(entry[1], entry[2], entry[3], decimal.Parse(entry[4]), entry[5]); for (int i = 6; i < entry.Length - 1; i += 2) { if (entry[i + 1] == "inProgress" || entry[i + 1] == "Finished") { var newMission = new Mission(entry[i], entry[i + 1]); newCommando.Missions.Add(newMission); } } soldiers.Add(newCommando); } else if (entry[0] == "Spy") { var newSpy = new Spy(entry[1], entry[2], entry[3], int.Parse(entry[4])); soldiers.Add(newSpy); } } Console.WriteLine(string.Join(Environment.NewLine, soldiers)); }
static void Main(string[] args) { List <ISoldier> list = new List <ISoldier>(); List <Private> privates = new List <Private>(); string[] input = Console.ReadLine().Split(); while (input[0] != "End") { string soldier = input[0]; string id = input[1]; string firstName = input[2]; string lastName = input[3]; switch (soldier) { case "Private": decimal salary = decimal.Parse(input[4]); Private privat = new Private(id, firstName, lastName, salary); privates.Add(privat); list.Add(privat); break; case "LieutenantGeneral": decimal salaryLieut = decimal.Parse(input[4]); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salaryLieut); string[] privatesIds = input.Skip(5).ToArray(); for (int i = 0; i < privatesIds.Length; i++) { foreach (var privateSoldier in privates) { if (privateSoldier.Id == privatesIds[i]) { lieutenantGeneral.AddPrivate(privateSoldier); } } } list.Add(lieutenantGeneral); break; case "Engineer": string corps = input[5]; if (corps == "Airforces" || corps == "Marines") { decimal salaryEngi = decimal.Parse(input[4]); Engineer engineer = new Engineer(id, firstName, lastName, salaryEngi, corps); string[] repairs = input.Skip(6).ToArray(); for (int i = 0; i < repairs.Length; i += 2) { string partName = repairs[i]; int workedHours = int.Parse(repairs[i + 1]); Repair repair = new Repair(partName, workedHours); engineer.AddRepair(repair); } list.Add(engineer); } break; case "Commando": string corpsComm = input[5]; if (corpsComm == "Airforces" || corpsComm == "Marines") { decimal salaryComm = decimal.Parse(input[4]); Commando commando = new Commando(id, firstName, lastName, salaryComm, corpsComm); string[] missions = input.Skip(6).ToArray(); for (int i = 0; i < missions.Length; i += 2) { string codeName = missions[i]; string state = missions[i + 1]; if (state == "inProgress" || state == "Finished") { Mission mission = new Mission(codeName, state); commando.Missions.Add(mission); } } list.Add(commando); } break; case "Spy": int codeNumber = int.Parse(input[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); list.Add(spy); break; } input = Console.ReadLine().Split(); } foreach (var soldier in list) { Console.WriteLine(soldier); } }
static void Main(string[] args) { var privates = new List <Private>(); while (true) { string[] command = Console.ReadLine().Split(); string member = command[0]; if (member == "End") { break; } string id = command[1]; string firstName = command[2]; string lastName = command[3]; if (member == "Private") { decimal salary = decimal.Parse(command[4]); var privateMember = new Private(id, firstName, lastName, salary); Console.WriteLine(privateMember); privates.Add(privateMember); } else if (member == "LieutenantGeneral") { var privateIds = new List <string>(); decimal salary = decimal.Parse(command[4]); foreach (var privateId in command.Skip(4)) { privateIds.Add(privateId); } var lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privateIds, privates); Console.WriteLine(lieutenantGeneral); } else if (member == "Engineer") { var repairs = new List <Repair>(); decimal salary = decimal.Parse(command[4]); string corps = command[5]; if (corps != "Airforces" && corps != "Marines") { continue; } for (int i = 6; i < command.Length - 1; i += 2) { var repair = new Repair(command[i], int.Parse(command[i + 1])); repairs.Add(repair); } var engeener = new Engineer(id, firstName, lastName, salary, corps, repairs); Console.WriteLine(engeener); } else if (member == "Commando") { var missions = new List <Mission>(); decimal salary = decimal.Parse(command[4]); string corps = command[5]; if (corps != "Airforces" && corps != "Marines") { continue; } for (int i = 6; i < command.Length - 1; i += 2) { string codeName = command[i]; string state = command[i + 1]; if (state == "Finished" || state == "inProgress") { var mission = new Mission(codeName, state); missions.Add(mission); } } var commando = new Commando(id, firstName, lastName, salary, corps, missions); Console.WriteLine(commando); } else if (member == "Spy") { int codeNumber = int.Parse(command[4]); var spy = new Spy(id, firstName, lastName, codeNumber); Console.WriteLine(spy); } } }