public override string ToString() { if (Head == null || Torso == null || Arms.Count < 2 || Legs.Count < 2) { return("We need more parts!"); } long totalEnergy = 0; totalEnergy += Head.EnergyCons; totalEnergy += Torso.EnergyCons; totalEnergy += Arms.Select(x => long.Parse(x.EnergyCons.ToString())).Sum(); totalEnergy += Legs.Select(x => long.Parse(x.EnergyCons.ToString())).Sum(); if (totalEnergy > Energy) { return("We need more power!"); } StringBuilder sb = new StringBuilder(); sb.Append("Jarvis:\r\n"); sb.Append(Head.ToString()); sb.Append(Torso.ToString()); foreach (var arm in Arms.OrderBy(x => x.EnergyCons)) { sb.Append(arm.ToString()); } foreach (var leg in Legs.OrderBy(x => x.EnergyCons)) { sb.Append(leg.ToString()); } return(sb.ToString()); }
public void Assemble() { if (!HasEnoughPart()) { Console.WriteLine("We need more parts!"); } else if (!HasEnoughEnergy()) { Console.WriteLine("We need more power!"); } else { Console.WriteLine("Jarvis:"); Head currentHead = head.OrderBy(x => x.EnergyConsumption).Take(1).First(); Console.WriteLine(currentHead.ToString()); Torso currentTorso = torso.OrderBy(x => x.EnergyConsumption).Take(1).First(); Console.WriteLine(currentTorso.ToString()); Arm[] armsArray = arms.OrderBy(x => x.EnergyConsumption).Take(2).ToArray(); Console.WriteLine(armsArray[0].ToString()); Console.WriteLine(armsArray[1].ToString()); Leg[] legArray = legs.OrderBy(x => x.EnergyConsumption).Take(2).ToArray(); Console.WriteLine(legArray[0].ToString()); Console.WriteLine(legArray[1].ToString()); } }