Created
August 17, 2011 17:28
-
-
Save xenda/1152080 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Persona | |
{ | |
public string Nombre { get; set; } | |
public int Edad { get; set; } | |
private int anioNacimiento, mesNacimiento, diaNacimiento; | |
public Persona(string nombre, int anioNacimiento, int mesNacimiento, int diaNacimiento) | |
{ | |
Nombre = nombre; | |
this.anioNacimiento = anioNacimiento; | |
this.mesNacimiento = mesNacimiento; | |
this.diaNacimiento = diaNacimiento; | |
} | |
public void CalcularEdad() | |
{ | |
Edad = DateTime.Now.Year - anioNacimiento; | |
if (DateTime.Now.Month <= mesNacimiento && DateTime.Now.Day < diaNacimiento) | |
Edad--; | |
} | |
} | |
public class GestionPersonas | |
{ | |
private List<Persona> personas; | |
public GestionPersonas() | |
{ | |
personas = new List<Persona>(); | |
} | |
public void AnadirPersona(Persona persona) | |
{ | |
personas.Add(persona); | |
} | |
public void MostrarEdades() | |
{ | |
foreach (Persona persona in personas) | |
{ | |
persona.CalcularEdad(); | |
Console.WriteLine("{0}: {1} años", persona.Nombre, persona.Edad); | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
GestionPersonas gestionPersonas = new GestionPersonas(); | |
gestionPersonas.AnadirPersona(new Persona("Juan", 1980, 5, 20)); | |
gestionPersonas.AnadirPersona(new Persona("Paula", 1975, 12, 25)); | |
gestionPersonas.AnadirPersona(new Persona("Franco", 1996, 3, 14)); | |
gestionPersonas.AnadirPersona(new Persona("Alex", 1982, 6, 10)); | |
gestionPersonas.MostrarEdades(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_accessor :name, :birthdate | |
def initialize(name, birthdate) | |
name, birthdate = name, Date.parse(birthdate) | |
end | |
def age | |
(Time.now - birthdate).years | |
end | |
end | |
class PersonCollection | |
attr_accessor :people | |
def initialize | |
people = [] | |
end | |
def ages | |
people.map{|person| person.age} | |
end | |
end | |
collection = PersonCollection.new | |
collection.people << Person.new("Juan", "20/05/1980") | |
collection.people << Person.new("Paula", "25/12/1975") | |
collection.people << Person.new("Franco", "14/03/1996") | |
collection.people << Person.new("Alex", "10/06/1982") | |
puts collection.ages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment