Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1.     class Elephant
  2.     {
  3.         public string name { get; private set; }
  4.         public uint age { get; private set; }
  5.         public uint hunger { get; private set; }
  6.  
  7.         public Elephant(string name, uint age)
  8.         {
  9.             this.name = name;
  10.             this.age = age;
  11.             this.hunger = 0;
  12.         }
  13.  
  14.         public void Eat(uint foodCount)
  15.         {
  16.             if (foodCount > hunger) hunger = 0;
  17.             else hunger -= foodCount;
  18.             Console.WriteLine("We fed {0} with {1} brickets of food [hunger {2}]", name, foodCount, hunger);
  19.         }
  20.  
  21.         public void Shit()
  22.         {
  23.             hunger += 50;
  24.             Console.WriteLine("{0} shits well [hunger {1}]", name, hunger);
  25.         }
  26.  
  27.         public override string ToString()
  28.         {
  29.             string temp = "satisfied";
  30.  
  31.             if (hunger > 20) temp = "hungry";
  32.             if (hunger > 50) temp = "starving";
  33.             if (hunger > 100) temp = "starved to death";
  34.  
  35.             return string.Format("{0}, {1} years old. Elephant is {2}", name, age, temp);
  36.         }
  37.     }
  38.  
  39.     class ElephantFarm : List<Elephant>
  40.     {
  41.         public void AddElephant(string name, uint age)
  42.         {
  43.             this.Add(new Elephant(name, age));
  44.         }
  45.  
  46.         public void FindSmallestElephant()
  47.         {
  48.             uint minimalAge = this.Min(o => o.age);
  49.             int smallestElephantIndex = this.FindIndex(o => o.age == minimalAge);
  50.  
  51.             Console.WriteLine("We found smallest elephant, {0} y.o.", minimalAge);
  52.  
  53.             this[smallestElephantIndex].Shit();
  54.             this[smallestElephantIndex].Eat(minimalAge * 5);
  55.  
  56.             Console.WriteLine("Smallest elephant {0}", this[smallestElephantIndex].ToString());
  57.         }
  58.     }
  59.  
  60.     class Program
  61.     {
  62.         static void Main(string[] args)
  63.         {
  64.             ElephantFarm farm = new ElephantFarm();
  65.             farm.AddElephant("Rickey", 18);
  66.             farm.AddElephant("Conroy", 3);
  67.             farm.AddElephant("Jackson", 21);
  68.             farm.AddElephant("Piwi", 5);
  69.             farm.AddElephant("Dave", 11);
  70.  
  71.             farm.FindSmallestElephant();
  72.  
  73.             Console.ReadKey(true);
  74.         }
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement