using System.Xml; using System.Xml.Linq; public class Carrera { public Carrera() { this.ds = new List(); } public void Add(Deportista d) { this.ds.Add( d ); } public IEnumerable Cortados(int segundos) { return this.ds.Where( d => d.TiempoEnSegundos > segundos ); } public XElement ToXml() { var toret = new XElement("Carrera"); foreach(var d in this.ds) { toret.Add( d.ToXml() ); } return toret; } public void Save(string nf) { this.ToXml().Save( nf ); } public string ListadoPorDorsal() { this.ds.Sort( (d1, d2) => d1.Dorsal - d2.Dorsal ); return String.Join( "\n", this.ds ); } public override string ToString() { this.ds.Sort( (d1, d2) => d1.TiempoEnSegundos - d2.TiempoEnSegundos ); return string.Join( "\n", this.ds ); } public static Carrera Load(string nf) { var toret = new Carrera(); var doc = XElement.Load( nf ); var ds = doc.Elements( "Deportista" ); foreach(var d in ds) { int dorsal = ( (int?) d.Element("Dorsal") ) ?? throw new XmlException( "XML: falta el dorsal" ); int tiempo = ( (int?) d.Element("Tiempo") ) ?? throw new XmlException( "XML: falta el tiempo" ); toret.Add( new Deportista( dorsal, tiempo ) ); } return toret; } private List ds; } public class Deportista { public Deportista(int d, int t) { this.Dorsal = d; this.TiempoEnSegundos = t; } public Deportista(int d, int h, int m, int s) { this.Dorsal = d; this.TiempoEnSegundos = ( h * 3600 ) + ( m * 60 ) + s; } public int Dorsal { get; init; } public int TiempoEnSegundos { get; init; } public int Horas => this.TiempoEnSegundos / 3600; public int Minutos => ( this.TiempoEnSegundos - this.Horas * 3600 ) / 60; public int Segundos => this.TiempoEnSegundos % 60; public XElement ToXml() { return new XElement( "Deportista", new XElement( "Dorsal", this.Dorsal ), new XElement( "Tiempo", this.TiempoEnSegundos ) ); } public override string ToString() { return $"{this.Dorsal:00}/{this.Horas:00}:{this.Minutos:00}:{this.Segundos:00}"; } } class App { static void Lambdas() { // Lambdas son funciones compuestas // mediante expresiones Func doble = (x) => x * 2; Action echo = (s) => Console.WriteLine( s ); echo( Convert.ToString( doble( 21 ) ) ); } static void Main() { var d1 = new Deportista( 11, 0, 32, 42 ); var d2 = new Deportista( 12, 1, 34, 1 ); var d3 = new Deportista( 10, 1, 24, 6 ); var c1 = new Carrera(); c1.Add( d1 ); c1.Add( d2 ); c1.Add( d3 ); Console.WriteLine("\nListado por dorsal"); Console.WriteLine( c1.ListadoPorDorsal() ); Console.WriteLine("\nListado por tiempos"); Console.WriteLine( c1 ); Console.WriteLine("\nCortados"); foreach(var d in c1.Cortados( 5400 )) { Console.WriteLine( d ); } c1.Save( "carrera-1.xml" ); Console.WriteLine( "\nRecuperados" ); Console.WriteLine( Carrera.Load( "carrera-1.xml" ) ); } }