internal string RegisterLeutenantGeneral(string id, string firstName, string lastName, double salary, IEnumerable <string> privatesIds) { LeutenantGeneral leutenantGeneral = new LeutenantGeneral(firstName, lastName, id, salary); leutenantGeneral.Privates = privatesIds.Select(pId => this.privates.First(p => p.Id == pId)).ToList(); this.soldiers.Add(leutenantGeneral); return(leutenantGeneral.ToString()); }
public static void Main() { var input = string.Empty; var privates = new List <Private>(); while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(); var soldierType = tokens[0]; switch (soldierType) { case "Private": var privateSoldier = new Private(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); privates.Add(privateSoldier); Console.WriteLine(privateSoldier.ToString()); break; case "LeutenantGeneral": var soldiers = tokens.Skip(5).ToArray(); var leutenantGeneral = new LeutenantGeneral(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4])); leutenantGeneral.AddSoldiers(soldiers, privates); Console.WriteLine(leutenantGeneral.ToString()); break; case "Engineer": if (tokens[5] == "Airforces" || tokens[5] == "Marines") { var repairs = tokens.Skip(6).ToArray(); var engineer = new Engineer(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); engineer.AddRepairs(repairs); Console.WriteLine(engineer.ToString()); } break; case "Commando": if (tokens[5] == "Airforces" || tokens[5] == "Marines") { var missions = tokens.Skip(6).ToArray(); var commando = new Commando(tokens[1], tokens[2], tokens[3], double.Parse(tokens[4]), tokens[5]); commando.AddMissions(missions); Console.WriteLine(commando.ToString()); } break; case "Spy": var spy = new Spy(tokens[1], tokens[2], tokens[3], int.Parse(tokens[4])); Console.WriteLine(spy.ToString()); break; } } }
internal string RegisterLeutenantGeneral(string id, string firstName, string lastName, double salary, IEnumerable <string> privatesIds) { var leutenantGeneralPrivates = new List <IPrivate>(); foreach (var privateId in privatesIds) { var currentPrivate = this.privates.FirstOrDefault(p => p.Id == privateId); if (currentPrivate != null) { leutenantGeneralPrivates.Add(currentPrivate); } } var currentSolider = new LeutenantGeneral(id, firstName, lastName, salary, leutenantGeneralPrivates); this.soliders.Add(currentSolider); return(currentSolider.ToString()); }
private static void ExecuteLeutenantGeneral(List <string> inputLineArgs) { int id = int.Parse(inputLineArgs[0]); string firstName = inputLineArgs[1]; string lastName = inputLineArgs[2]; double salary = double.Parse(inputLineArgs[3]); ILeutenantGeneral leutenantGeneral = new LeutenantGeneral(id, firstName, lastName, salary); var targetPrivateIds = inputLineArgs.Skip(4).Select(int.Parse).ToList(); foreach (var privateId in targetPrivateIds) { if (privates.ContainsKey(privateId)) { var targetPrivate = privates[privateId]; leutenantGeneral.Privates.Add(targetPrivate); } } Console.WriteLine(leutenantGeneral.ToString().Trim()); }
static void Main(string[] args) { List <Private> privates = new List <Private>(); StringBuilder sb = new StringBuilder(); string command; while ((command = Console.ReadLine()) != "End") { string[] commTokens = command.Split(); string soldierType = commTokens[0]; int id = int.Parse(commTokens[1]); string firstName = commTokens[2]; string lastName = commTokens[3]; switch (soldierType) { case "Private": decimal privateSalary = decimal.Parse(commTokens[4]); Private newPrivate = new Private(id, firstName, lastName, privateSalary); sb.AppendLine(newPrivate.ToString()); privates.Add(newPrivate); break; case "LeutenantGeneral": decimal leutenantSalary = decimal.Parse(commTokens[4]); List <Private> tempPrivates = new List <Private>(); for (int i = 5; i < commTokens.Length; i++) { int currId = int.Parse(commTokens[i]); var currPrivate = privates.FirstOrDefault(x => x.Id == currId); if (currPrivate != null) { tempPrivates.Add(currPrivate); } } LeutenantGeneral newLeut = new LeutenantGeneral(id, firstName, lastName, leutenantSalary, tempPrivates); sb.AppendLine(newLeut.ToString()); break; case "Engineer": decimal engineerSalary = decimal.Parse(commTokens[4]); CorpsType currEngCorps; bool isEngCorpsValid = Enum.TryParse(commTokens[5], out currEngCorps); if (isEngCorpsValid) { List <Repair> repairs = new List <Repair>(); for (int i = 6; i < commTokens.Length; i += 2) { string partName = commTokens[i]; int workHours = int.Parse(commTokens[i + 1]); Repair newRepair = new Repair(partName, workHours); repairs.Add(newRepair); } Engineer newEngineer = new Engineer(id, firstName, lastName, engineerSalary, currEngCorps, repairs); sb.AppendLine(newEngineer.ToString()); } break; case "Commando": decimal commandoSalary = decimal.Parse(commTokens[4]); CorpsType currCorps; bool isCorpsValid = Enum.TryParse(commTokens[5], out currCorps); if (isCorpsValid) { List <Mission> missions = new List <Mission>(); for (int i = 6; i < commTokens.Length; i += 2) { string currCodeName = commTokens[i]; StateType missionState; bool isMissionValid = Enum.TryParse(commTokens[i + 1], out missionState); if (isMissionValid) { Mission addMission = new Mission(currCodeName, missionState); missions.Add(addMission); } } Commando newCommando = new Commando(id, firstName, lastName, commandoSalary, currCorps, missions); sb.AppendLine(newCommando.ToString()); } break; case "Spy": int codeNum = int.Parse(commTokens[4]); Spy newSpy = new Spy(id, firstName, lastName, codeNum); sb.AppendLine(newSpy.ToString()); break; default: break; } } string result = sb.ToString().TrimEnd(); Console.WriteLine(result); }
public void Run() { string cmd = Console.ReadLine(); List <Private> privateList = new List <Private>(); while (cmd != "End") { string[] info = cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string command = info[0]; if (command == "Private") { Private privates = new Private(info[1], info[2], info[3], decimal.Parse(info[4])); privateList.Add(privates); Console.WriteLine(privates.ToString()); } else if (command == "LieutenantGeneral") { LeutenantGeneral leutenant = new LeutenantGeneral(info[1], info[2], info[3], decimal.Parse(info[4])); Console.WriteLine(leutenant.ToString()); for (int i = 5; i < info.Length; i++) { var finalPrivate = privateList.Where(a => a.Id == info[i]); foreach (var p in finalPrivate) { Console.WriteLine($" Name: {p.FirstName} {p.LastName} Id: {p.Id} Salary: {p.Salary:f2}"); } } } else if (command == "Engineer") { if (info[5] == "Airforces" || info[5] == "Marines") { Engineer engineer = new Engineer(info[1], info[2], info[3], decimal.Parse(info[4]), Enum.Parse <Corps>(info[5])); Console.WriteLine(engineer.ToString()); for (int i = 6; i < info.Length; i += 2) { string name = info[i]; string hours = info[i + 1]; Repair repair = new Repair(name, hours); engineer.repairs.Add(repair); } foreach (var repair in engineer.repairs) { Console.WriteLine($" Part Name: {repair.Name} Hours Worked: {repair.HoursWorked}"); } } } else if (command == "Commando") { Commando commando = new Commando(info[1], info[2], info[3], decimal.Parse(info[4]), Enum.Parse <Corps>(info[5])); Console.WriteLine(commando.ToString()); for (int i = 6; i < info.Length; i += 2) { string code = info[i]; string state = info[i + 1]; if (state == "inProgress" || state == "Finished") { Mission mission = new Mission(code, Enum.Parse <States>(state)); commando.missions.Add(mission); } } foreach (var mission in commando.missions) { Console.WriteLine($" Code Name: {mission.CodeName} State: {mission.states}"); } } else if (command == "Spy") { Spy spy = new Spy(info[1], info[2], info[3], int.Parse(info[4])); Console.WriteLine(spy.ToString()); } cmd = Console.ReadLine(); } }