Advertisement
ivan_yosifov

Pilot.cs

Feb 24th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. namespace WarMachines.Machines
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Text;
  5.     using System.Linq;
  6.     using WarMachines.Interfaces;
  7.  
  8.     public class Pilot: IPilot
  9.     {
  10.         private HashSet<IMachine> machines =
  11.             new HashSet<IMachine>();
  12.  
  13.         public Pilot(string name)
  14.         {
  15.             this.Name = name;
  16.         }
  17.  
  18.         public string Name { get; set; }
  19.  
  20.         public void AddMachine(IMachine machine)
  21.         {
  22.             this.machines.Add(machine);
  23.         }
  24.  
  25.         public string Report()
  26.         {
  27.             StringBuilder sb = new StringBuilder();
  28.  
  29.             if (this.machines.Count == 1)
  30.             {
  31.                 sb.Append(" - 1 machine");                
  32.             }
  33.             else if (this.machines.Count > 1)
  34.             {
  35.                 sb.Append(" - " + this.machines.Count + " machines");
  36.             }
  37.             else
  38.             {
  39.                 sb.Append(" - no machines");
  40.             }
  41.  
  42.             sb.Append(System.Environment.NewLine);
  43.  
  44.             foreach (var machine in machines.OrderBy
  45.                 (machine => machine.HealthPoints)
  46.                 .ThenBy(machine => machine.Name))
  47.             {
  48.                 sb.Append("- " + machine.Name);
  49.                 sb.Append(System.Environment.NewLine);
  50.                 sb.Append(machine.ToString());
  51.  
  52.             }
  53.  
  54.             return base.ToString() + sb.ToString().TrimEnd();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement