fork download
  1. using System.Xml;
  2. using System.Xml.Linq;
  3.  
  4.  
  5. public class Carrera {
  6. public Carrera()
  7. {
  8. this.ds = new List<Deportista>();
  9. }
  10.  
  11. public void Add(Deportista d)
  12. {
  13. this.ds.Add( d );
  14. }
  15.  
  16. public IEnumerable<Deportista> Cortados(int segundos)
  17. {
  18. return this.ds.Where(
  19. d => d.TiempoEnSegundos > segundos );
  20. }
  21.  
  22. public XElement ToXml()
  23. {
  24. var toret = new XElement("Carrera");
  25.  
  26. foreach(var d in this.ds) {
  27. toret.Add( d.ToXml() );
  28. }
  29.  
  30. return toret;
  31. }
  32.  
  33. public void Save(string nf)
  34. {
  35. this.ToXml().Save( nf );
  36. }
  37.  
  38. public string ListadoPorDorsal()
  39. {
  40. this.ds.Sort( (d1, d2) => d1.Dorsal - d2.Dorsal );
  41.  
  42. return String.Join( "\n", this.ds );
  43. }
  44.  
  45. public override string ToString()
  46. {
  47. this.ds.Sort( (d1, d2) => d1.TiempoEnSegundos - d2.TiempoEnSegundos );
  48.  
  49. return string.Join( "\n", this.ds );
  50. }
  51.  
  52. public static Carrera Load(string nf)
  53. {
  54. var toret = new Carrera();
  55. var doc = XElement.Load( nf );
  56. var ds = doc.Elements( "Deportista" );
  57.  
  58. foreach(var d in ds) {
  59. int dorsal = ( (int?) d.Element("Dorsal") )
  60. ?? throw new XmlException( "XML: falta el dorsal" );
  61. int tiempo = ( (int?) d.Element("Tiempo") )
  62. ?? throw new XmlException( "XML: falta el tiempo" );
  63.  
  64. toret.Add( new Deportista( dorsal, tiempo ) );
  65. }
  66.  
  67. return toret;
  68. }
  69.  
  70. private List<Deportista> ds;
  71. }
  72.  
  73.  
  74. public class Deportista {
  75. public Deportista(int d, int t)
  76. {
  77. this.Dorsal = d;
  78. this.TiempoEnSegundos = t;
  79. }
  80.  
  81. public Deportista(int d, int h, int m, int s)
  82. {
  83. this.Dorsal = d;
  84. this.TiempoEnSegundos = ( h * 3600 ) + ( m * 60 ) + s;
  85. }
  86.  
  87. public int Dorsal { get; init; }
  88. public int TiempoEnSegundos { get; init; }
  89.  
  90. public int Horas => this.TiempoEnSegundos / 3600;
  91. public int Minutos => ( this.TiempoEnSegundos - this.Horas * 3600 ) / 60;
  92. public int Segundos => this.TiempoEnSegundos % 60;
  93.  
  94. public XElement ToXml()
  95. {
  96. return new XElement( "Deportista",
  97. new XElement( "Dorsal", this.Dorsal ),
  98. new XElement( "Tiempo", this.TiempoEnSegundos ) );
  99. }
  100.  
  101. public override string ToString()
  102. {
  103. return $"{this.Dorsal:00}/{this.Horas:00}:{this.Minutos:00}:{this.Segundos:00}";
  104. }
  105. }
  106.  
  107.  
  108. class App {
  109. static void Lambdas()
  110. {
  111. // Lambdas son funciones compuestas
  112. // mediante expresiones
  113. Func<int, int> doble = (x) => x * 2;
  114. Action<string> echo = (s) => Console.WriteLine( s );
  115.  
  116. echo( Convert.ToString( doble( 21 ) ) );
  117. }
  118.  
  119. static void Main()
  120. {
  121. var d1 = new Deportista( 11, 0, 32, 42 );
  122. var d2 = new Deportista( 12, 1, 34, 1 );
  123. var d3 = new Deportista( 10, 1, 24, 6 );
  124. var c1 = new Carrera();
  125.  
  126. c1.Add( d1 );
  127. c1.Add( d2 );
  128. c1.Add( d3 );
  129. Console.WriteLine("\nListado por dorsal");
  130. Console.WriteLine( c1.ListadoPorDorsal() );
  131. Console.WriteLine("\nListado por tiempos");
  132. Console.WriteLine( c1 );
  133. Console.WriteLine("\nCortados");
  134. foreach(var d in c1.Cortados( 5400 )) {
  135. Console.WriteLine( d );
  136. }
  137.  
  138. c1.Save( "carrera-1.xml" );
  139.  
  140. Console.WriteLine( "\nRecuperados" );
  141. Console.WriteLine( Carrera.Load( "carrera-1.xml" ) );
  142. }
  143. }
  144.  
  145.  
Success #stdin #stdout 0.09s 38856KB
stdin
Standard input is empty
stdout
Listado por dorsal
10/01:24:06
11/00:32:42
12/01:34:01

Listado por tiempos
11/00:32:42
10/01:24:06
12/01:34:01

Cortados
12/01:34:01

Recuperados
11/00:32:42
10/01:24:06
12/01:34:01