private static void AddMissions(string[] soldierInfo, Commando commando) { for (int index = 6; index < soldierInfo.Length; index += 2) { var codeName = soldierInfo[index]; var state = soldierInfo[index + 1]; try { var mission = new Mission(codeName, state); commando.Missions.Add(mission); } catch (ArgumentException) { } } }
public static void Main() { string input = Console.ReadLine(); Army army = new Army(); while (input != "End") { string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string typeSoldier = parts[0]; int id = int.Parse(parts[1]); string firstName = parts[2]; string lastName = parts[3]; decimal salary; string corps; switch (typeSoldier) { case "Private": salary = decimal.Parse(parts[4]); Private currentPrivate = new Private(id, firstName, lastName, salary); army.AddSoldier(currentPrivate); break; case "LieutenantGeneral": salary = decimal.Parse(parts[4]); int[] ids = new int[parts.Length - 5]; for (int i = 0; i < ids.Length; i++) { ids[i] = int.Parse(parts[i + 5]); } HashSet <Private> privates = army.GetPrivates(ids); LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates); army.AddSoldier(lieutenantGeneral); break; case "Engineer": salary = decimal.Parse(parts[4]); corps = parts[5]; if (corps == "Marines" || corps == "Airforces") { HashSet <Repair> repairs = new HashSet <Repair>(); for (int i = 6; i < parts.Length; i += 2) { Repair repair = new Repair(parts[i], int.Parse(parts[i + 1])); if (repair != null) { repairs.Add(repair); } } Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs); army.AddSoldier(engineer); } break; case "Commando": salary = decimal.Parse(parts[4]); corps = parts[5]; if (corps == "Marines" || corps == "Airforces") { HashSet <Mission> missions = new HashSet <Mission>(); for (int i = 6; i < parts.Length; i += 2) { Mission mission = new Mission(parts[i], parts[i + 1]); if (mission != null) { missions.Add(mission); } } Commando commando = new Commando(id, firstName, lastName, salary, corps, missions); army.AddSoldier(commando); } break; case "Spy": int codeNumber = int.Parse(parts[4]); Spy spy = new Spy(id, firstName, lastName, codeNumber); army.AddSoldier(spy); break; } input = Console.ReadLine(); } Console.WriteLine(army); }
static void Main() { var soldiers = new List <ISoldier>(); while (true) { var soldierInfo = Console.ReadLine().Split(); var soldierType = soldierInfo[0]; if (soldierType == "End") { break; } var id = soldierInfo[1]; var firstName = soldierInfo[2]; var lastName = soldierInfo[3]; var salary = decimal.Parse(soldierInfo[4]); string corps; switch (soldierType) { case "Private": soldiers.Add(new Private(id, firstName, lastName, salary)); break; case "LeutenantGeneral": var privates = new List <ISoldier>(); AddPrivates(soldiers, soldierInfo, privates); soldiers.Add(new LeutenantGeneral(id, firstName, lastName, salary, privates)); break; case "Engineer": corps = soldierInfo[5]; try { var engineer = new Engineer(id, firstName, lastName, salary, corps); AddRepairs(soldierInfo, engineer); soldiers.Add(engineer); } catch (ArgumentException) { } break; case "Commando": corps = soldierInfo[5]; try { var commando = new Commando(id, firstName, lastName, salary, corps); AddMissions(soldierInfo, commando); soldiers.Add(commando); } catch (ArgumentException) { } break; case "Spy": var codeNumber = int.Parse(soldierInfo[4]); soldiers.Add(new Spy(id, firstName, lastName, codeNumber)); break; } } soldiers.ForEach(Console.WriteLine); }
public static void Main(string[] args) { var allSoldiers = new List <ISoldier>(); var privates = new List <IPrivate>(); string input = Console.ReadLine(); while (input != "End") { string[] data = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); string title = data[0]; string id = data[1]; string firstName = data[2]; string lastName = data[3]; decimal salary = decimal.Parse(data[4]); switch (title) { case "Private": var currentPrivate = new Private(id, firstName, lastName, salary); privates.Add(currentPrivate); allSoldiers.Add(currentPrivate); break; case "LieutenantGeneral": var currentLeutenant = new LieutenantGeneral(id, firstName, lastName, salary); for (int i = 5; i < data.Length; i++) { currentLeutenant.AddPrivate(privates, data[i]); } allSoldiers.Add(currentLeutenant); break; case "Engineer": string engCorps = data[5]; if (engCorps == "Airforces" || engCorps == "Marines") { var currentEngineer = new Engineer(id, firstName, lastName, salary, engCorps); for (int i = 6; i < data.Length; i += 2) { var repair = new Repair(data[i], int.Parse(data[i + 1])); currentEngineer.AddRepair(repair); } if (currentEngineer.Corps != null) { allSoldiers.Add(currentEngineer); } } break; case "Commando": string commandoCorps = data[5]; if (commandoCorps == "Airforces" || commandoCorps == "Marines") { var currentCommando = new Commando(id, firstName, lastName, salary, commandoCorps); for (int i = 6; i < data.Length; i += 2) { if (data[i + 1] == "inProgress" || data[i + 1] == "Finished") { var mission = new Mission(data[i], data[i + 1]); currentCommando.AddMission(mission); } } allSoldiers.Add(currentCommando); } break; case "Spy": var currentSpy = new Spy(id, firstName, lastName, (int)salary); allSoldiers.Add(currentSpy); break; default: break; } input = Console.ReadLine(); } foreach (var soldier in allSoldiers) { Console.WriteLine(soldier.ToString()); } }
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 (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]); 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) { var soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(); var soldierType = tokens[0]; int id = int.Parse(tokens[1]); var firstName = tokens[2]; var lastName = tokens[3]; var 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++) { var privateId = int.Parse(tokens[i]); ISoldier @private = soldiers.FirstOrDefault(p => p.Id == privateId); leutenant.AddPivate(@private); } soldier = leutenant; break; case "Engineer": var engineerCorps = tokens[5]; var enginner = new Engineer(id, firstName, lastName, salary, engineerCorps); for (int i = 6; i < tokens.Length; i++) { var partName = tokens[i]; var hoursWorked = int.Parse(tokens[++i]); IRepair repair = new Repair(partName, hoursWorked); enginner.AddRepair(repair); } soldier = enginner; break; case "Commando": var commandoCorps = tokens[5]; var commando = new Commando(id, firstName, lastName, salary, commandoCorps); for (int i = 6; i < tokens.Length; i++) { var codeName = tokens[i]; var 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 (Exception) { } } foreach (var s in soldiers) { Console.WriteLine(s); } }
static void Main(string[] args) { var input = Console.ReadLine(); var myArmy = new List <Soldier>(); var myPrivateArmy = new List <Private>(); while (input != "End") { var currentSoldier = input.Split(" ").ToArray(); var typeOfSoldier = currentSoldier[0]; var id = currentSoldier[1]; var firstName = currentSoldier[2]; var lastName = currentSoldier[3]; switch (typeOfSoldier) { case "Private": var salary = decimal.Parse(currentSoldier[4]); var currentPrivate = new Private(id, firstName, lastName, salary); myArmy.Add(currentPrivate); myPrivateArmy.Add(currentPrivate); break; case "LieutenantGeneral": salary = decimal.Parse(currentSoldier[4]); var general = new LieutenantGeneral(id, firstName, lastName, salary); if (currentSoldier.Length > 4) { for (int i = 5; i < currentSoldier.Length; i++) { var currentId = currentSoldier[i]; var foundPrivate = myPrivateArmy.FirstOrDefault(x => x.Id == currentId); if (foundPrivate != null) { general.Privates.Add(foundPrivate); } } } myArmy.Add(general); break; case "Spy": var codeName = int.Parse(currentSoldier[4]); var spy = new Spy(id, firstName, lastName, codeName); myArmy.Add(spy); break; case "Engineer": salary = decimal.Parse(currentSoldier[4]); var corps = currentSoldier[5]; try { var engineer = new Engineer(id, firstName, lastName, salary, corps); if (currentSoldier.Length > 7) { for (int i = 6; i < currentSoldier.Length; i += 2) { var currentRepair = new Repair(currentSoldier[i], int.Parse(currentSoldier[i + 1])); engineer.Repairs.Add(currentRepair); } } myArmy.Add(engineer); } catch (Exception ex) { input = Console.ReadLine(); continue; } break; case "Commando": salary = decimal.Parse(currentSoldier[4]); corps = currentSoldier[5]; try { var commando = new Commando(id, firstName, lastName, salary, corps); if (currentSoldier.Length > 7) { for (int i = 6; i < currentSoldier.Length; i += 2) { try { var currentMission = new Mission(currentSoldier[i], currentSoldier[i + 1]); commando.Missions.Add(currentMission); } catch (Exception) { continue; } } } myArmy.Add(commando); } catch (Exception) { input = Console.ReadLine(); continue; } break; } input = Console.ReadLine(); } foreach (var soldier in myArmy) { Console.WriteLine(soldier.ToString()); } }