sashomaga

Linq, list of different objects, average()

Mar 5th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. static void Main()
  2.     {
  3.         try
  4.         {
  5.             Kitten testCat = new Kitten(10, "pesho", SexEnum.Female);
  6.             TomCat maleCat = new TomCat(20, "misho", SexEnum.Male);
  7.             Frog bigFrog = new Frog(50, "gabcho", SexEnum.Male);
  8.  
  9.             List<ISound> differentSounds = new List<ISound>();
  10.             differentSounds.Add(new Kitten(10, "pesho", SexEnum.Female));
  11.             differentSounds.Add(new TomCat(20, "misho", SexEnum.Male));
  12.             differentSounds.Add(new Frog(50, "gabcho", SexEnum.Male));
  13.  
  14.             foreach (var item in differentSounds)
  15.             {
  16.                 item.MakeSound();
  17.             }
  18.  
  19.             List<Animals> differentAnimalsList = new List<Animals>();
  20.             differentAnimalsList.Add(new Kitten(10, "pesho", SexEnum.Female));
  21.             differentAnimalsList.Add(new TomCat(20, "misho", SexEnum.Male));
  22.             differentAnimalsList.Add(new Frog(50, "gabcho", SexEnum.Male));
  23.             differentAnimalsList.Add(new Frog(30, "gabcho2", SexEnum.Male));
  24.             differentAnimalsList.Add(new Dog(25, "sharo", SexEnum.Male));
  25.             differentAnimalsList.Add(new Kitten(5, "puhcho", SexEnum.Female));
  26.  
  27.             //one query
  28.             var animalGroups =
  29.                 (from animal in differentAnimalsList
  30.                  group animal by animal.GetType().Name into groups
  31.                  select new
  32.                  {
  33.                      groupName = groups.Key,
  34.                      averageSum =
  35.                          (from anim in groups
  36.                           select anim.Age).Average()
  37.                  });
  38.                    
  39.             foreach (var item in animalGroups)
  40.             {
  41.                 Console.WriteLine(item);
  42.             }
  43.             //several queries
  44.             Console.WriteLine("Average kitten age: ");
  45.             var result =
  46.                 (from animal in differentAnimalsList
  47.                  where animal.GetType() == typeof(Kitten)
  48.                  select animal.Age).Average();
  49.             Console.WriteLine(result);
  50.             Console.WriteLine("Average tomcat age: ");
  51.             result =
  52.                 (from animal in differentAnimalsList
  53.                  where animal.GetType() == typeof(TomCat)
  54.                  select animal.Age).Average();
  55.             Console.WriteLine(result);
  56.             Console.WriteLine("Average frog age: ");
  57.             result =
  58.                 (from animal in differentAnimalsList
  59.                  where animal.GetType() == typeof(Frog)
  60.                  select animal.Age).Average();
  61.             Console.WriteLine(result);
  62.             Console.WriteLine("Average dog age: ");
  63.             result =
  64.                 (from animal in differentAnimalsList
  65.                  where animal.GetType() == typeof(Dog)
  66.                  select animal.Age).Average();
  67.             Console.WriteLine(result);
  68.  
  69.            
  70.         }
  71.         catch (ArgumentException err)
  72.         {
  73.             Console.WriteLine(err.Message);
  74.         }
  75.        
  76.     }
  77.  
  78. public abstract class Animals
  79. {
  80.     private int age;
  81.     private string name;
  82.     private SexEnum sex;
  83.  
  84.     public int Age
  85.     {
  86.         get { return this.age; }
  87.         private set
  88.         {
  89.             if (value <= 0 || value > 100)
  90.             {
  91.                 throw new ArgumentException("Invalid age");
  92.             }
  93.             this.age = value;
  94.         }
  95.     }
  96.     public string Name
  97.     {
  98.         get { return this.name; }
  99.         private set
  100.         {
  101.             if (value.Length <= 2 || value.Length > 50)
  102.             {
  103.                 throw new ArgumentException("Invalid name");
  104.             }
  105.             this.name = value;
  106.         }
  107.     }
  108.     public SexEnum Sex
  109.     {
  110.         get { return this.sex; }
  111.         private set { this.sex = value; }
  112.     }
  113.  
  114.     protected Animals(int age, string name, SexEnum sex)
  115.     {
  116.         this.Age = age;
  117.         this.Name = name;
  118.         this.sex = sex;
  119.     }
  120. }
  121.  
  122. public abstract class Cat : Animals
  123. {
  124.     public Cat(int age, string name, SexEnum sex)
  125.         : base(age, name, sex)
  126.     {
  127.  
  128.     }
  129. }
  130.  
  131. public class Dog : Animals, ISound
  132. {
  133.     public Dog(int age, string name, SexEnum sex)
  134.         : base(age, name, sex)
  135.     {
  136.  
  137.     }
  138.  
  139.     public void MakeSound()
  140.     {
  141.         Console.WriteLine("bau");
  142.     }
  143. }
  144.  
  145. public class Frog : Animals, ISound
  146. {
  147.     public Frog(int age, string name, SexEnum sex) : base(age, name, sex)
  148.     {
  149.  
  150.     }
  151.  
  152.     public void MakeSound()
  153.     {
  154.         Console.WriteLine("gaga");
  155.     }
  156. }
  157.  
  158. public interface ISound
  159. {
  160.  
  161.     void MakeSound();
  162. }
  163.  
  164. public class Kitten : Cat, ISound
  165. {
  166.     public Kitten(int age, string name, SexEnum sex)
  167.         : base(age, name, sex)
  168.     {
  169.         if (sex != SexEnum.Female)
  170.         {
  171.             throw new ArgumentException("Kittens must be female");
  172.         }
  173.     }
  174.  
  175.     public void MakeSound()
  176.     {
  177.         Console.WriteLine("miau");
  178.     }
  179. }
  180.  
  181. public enum SexEnum
  182. {
  183.     Male, Female
  184. }
  185.  
  186. public class TomCat : Cat, ISound
  187. {
  188.     public TomCat(int age, string name, SexEnum sex)
  189.         : base(age, name, sex)
  190.     {
  191.         if (sex != SexEnum.Male)
  192.         {
  193.             throw new ArgumentException("TomCats must be male");
  194.         }
  195.     }
  196.  
  197.     public void MakeSound()
  198.     {
  199.         Console.WriteLine("murr");
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment