Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main()
- {
- try
- {
- Kitten testCat = new Kitten(10, "pesho", SexEnum.Female);
- TomCat maleCat = new TomCat(20, "misho", SexEnum.Male);
- Frog bigFrog = new Frog(50, "gabcho", SexEnum.Male);
- List<ISound> differentSounds = new List<ISound>();
- differentSounds.Add(new Kitten(10, "pesho", SexEnum.Female));
- differentSounds.Add(new TomCat(20, "misho", SexEnum.Male));
- differentSounds.Add(new Frog(50, "gabcho", SexEnum.Male));
- foreach (var item in differentSounds)
- {
- item.MakeSound();
- }
- List<Animals> differentAnimalsList = new List<Animals>();
- differentAnimalsList.Add(new Kitten(10, "pesho", SexEnum.Female));
- differentAnimalsList.Add(new TomCat(20, "misho", SexEnum.Male));
- differentAnimalsList.Add(new Frog(50, "gabcho", SexEnum.Male));
- differentAnimalsList.Add(new Frog(30, "gabcho2", SexEnum.Male));
- differentAnimalsList.Add(new Dog(25, "sharo", SexEnum.Male));
- differentAnimalsList.Add(new Kitten(5, "puhcho", SexEnum.Female));
- //one query
- var animalGroups =
- (from animal in differentAnimalsList
- group animal by animal.GetType().Name into groups
- select new
- {
- groupName = groups.Key,
- averageSum =
- (from anim in groups
- select anim.Age).Average()
- });
- foreach (var item in animalGroups)
- {
- Console.WriteLine(item);
- }
- //several queries
- Console.WriteLine("Average kitten age: ");
- var result =
- (from animal in differentAnimalsList
- where animal.GetType() == typeof(Kitten)
- select animal.Age).Average();
- Console.WriteLine(result);
- Console.WriteLine("Average tomcat age: ");
- result =
- (from animal in differentAnimalsList
- where animal.GetType() == typeof(TomCat)
- select animal.Age).Average();
- Console.WriteLine(result);
- Console.WriteLine("Average frog age: ");
- result =
- (from animal in differentAnimalsList
- where animal.GetType() == typeof(Frog)
- select animal.Age).Average();
- Console.WriteLine(result);
- Console.WriteLine("Average dog age: ");
- result =
- (from animal in differentAnimalsList
- where animal.GetType() == typeof(Dog)
- select animal.Age).Average();
- Console.WriteLine(result);
- }
- catch (ArgumentException err)
- {
- Console.WriteLine(err.Message);
- }
- }
- public abstract class Animals
- {
- private int age;
- private string name;
- private SexEnum sex;
- public int Age
- {
- get { return this.age; }
- private set
- {
- if (value <= 0 || value > 100)
- {
- throw new ArgumentException("Invalid age");
- }
- this.age = value;
- }
- }
- public string Name
- {
- get { return this.name; }
- private set
- {
- if (value.Length <= 2 || value.Length > 50)
- {
- throw new ArgumentException("Invalid name");
- }
- this.name = value;
- }
- }
- public SexEnum Sex
- {
- get { return this.sex; }
- private set { this.sex = value; }
- }
- protected Animals(int age, string name, SexEnum sex)
- {
- this.Age = age;
- this.Name = name;
- this.sex = sex;
- }
- }
- public abstract class Cat : Animals
- {
- public Cat(int age, string name, SexEnum sex)
- : base(age, name, sex)
- {
- }
- }
- public class Dog : Animals, ISound
- {
- public Dog(int age, string name, SexEnum sex)
- : base(age, name, sex)
- {
- }
- public void MakeSound()
- {
- Console.WriteLine("bau");
- }
- }
- public class Frog : Animals, ISound
- {
- public Frog(int age, string name, SexEnum sex) : base(age, name, sex)
- {
- }
- public void MakeSound()
- {
- Console.WriteLine("gaga");
- }
- }
- public interface ISound
- {
- void MakeSound();
- }
- public class Kitten : Cat, ISound
- {
- public Kitten(int age, string name, SexEnum sex)
- : base(age, name, sex)
- {
- if (sex != SexEnum.Female)
- {
- throw new ArgumentException("Kittens must be female");
- }
- }
- public void MakeSound()
- {
- Console.WriteLine("miau");
- }
- }
- public enum SexEnum
- {
- Male, Female
- }
- public class TomCat : Cat, ISound
- {
- public TomCat(int age, string name, SexEnum sex)
- : base(age, name, sex)
- {
- if (sex != SexEnum.Male)
- {
- throw new ArgumentException("TomCats must be male");
- }
- }
- public void MakeSound()
- {
- Console.WriteLine("murr");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment