Further to Solution 1 you might want to maintain a list of pets rather than working on just one
public class Pet
{
public string Name { get; set; }
public int Hunger { get; private set; }
public bool IsAlive { get; private set; }
public Pet(string name)
{
Name = name;
Hunger = 100;
IsAlive = true;
}
public void SetHunger(int _hunger)
{
Hunger = Hunger - _hunger;
if (Hunger <= 0)
{
IsAlive = false;
}
}
}
private static List<Pet> pets = new List<Pet>();
static void Main(string[] args)
{
var myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 2000;
myTimer.Start();
string line;
while ((line = Console.ReadLine()) != "q")
{
Pet pet = CreatePet(line);
}
}
public static Pet CreatePet(string name)
{
Pet pet = new Pet (name);
pets.Add(pet);
return pet;
}
public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
foreach (Pet pet in pets)
{
pet.SetHunger(10);
if (pet.IsAlive)
{
Console.WriteLine("Pet:{0} Hunger:{1}", pet.Name, pet.Hunger);
}
else
{
Console.WriteLine("{0} died", pet.Name);
}
}
pets.RemoveAll(p => p.IsAlive == false);
}