I had a test before and had to do this task:
Create classes:
Cat
,Dog
andPetshop
. TypesCat
andDog
have fieldsName
andBreed
and methodIntroduce()
which prints text “I’m (Name) of (Breed). I’m a cat (or dog)”.The class
Petshop
collects different pets in its container.
We may add new pet to container using methodAddPet()
, and we may display information about all pets by calling methodIntroduceAll()
.Which hierarchy of classes is the best solution of this problem?
Write short code to demonstrate your solution. Your code should include class (interface) aggregation, inheritance, should use .NET BCL collections or generics, and should implement exception handling.
My answer:
using System; using System.Collections.Generic; namespace ConsoleApplication1 { interface IPet { string MyName { get; set; } string MyBreed { get; set; } void Introduce(); }
Cat
class Cat : IPet { string Name; string Breed; public Cat() { Name = ""; Breed = ""; } public string MyName { get { return Name; } set { Name = value; } } public string MyBreed { get { return Breed; } set { Breed = value; } } public void Introduce() { Console.WriteLine("I'm " + Name + " of " + Breed + " I'm a cat"); } }
Dog
class Dog : IPet { public string Name; public string Breed; public Dog() { Name = ""; Breed = ""; } public string MyName { get { return Name; } set { Name = value; } } public string MyBreed { get { return Breed; } set { Breed = value; } } public void Introduce() { Console.WriteLine("I'm " + Name + " of " + Breed + " I'm a dog"); } }
PetShop
class PetShop { List<Object> objList = new List<object>(); public void AddPet(Object obj) { objList.Add(obj); } public void IntroduceAll() { foreach (object element in objList) { Console.WriteLine("One more"); Type mytype = element.GetType(); if (mytype == typeof(Cat)) { var el = (Cat)element; el.Introduce(); } else if (mytype == typeof(Dog)) { var el = (Dog)element; el.Introduce(); } else { throw new NotFoundException(); } } } }
Program
class Program { static void Main(string[] args) { Cat pussy = new Cat(); pussy.MyName = "Murlon"; pussy.MyBreed = "Supernatural"; Dog doggy = new Dog(); doggy.MyName = "Oscar"; doggy.MyBreed = "Blonde"; Cat pussy1 = new Cat(); pussy.MyName = "Murlon1"; pussy.MyBreed = "Supernatural1"; Dog doggy1 = new Dog(); doggy.MyName = "Oscar1"; doggy.MyBreed = "Blonde1"; //Console.WriteLine(pussy.MyBreed); //pussy.Introduce(); PetShop ps = new PetShop(); Exception ex = new Exception(); ps.AddPet(pussy); ps.AddPet(doggy); ps.AddPet(pussy1); ps.AddPet(doggy1); ps.AddPet(ex); try { ps.IntroduceAll(); } catch (NotFoundException) { Console.WriteLine("Exeption"); } Console.ReadKey(); } }
NotFoundException
class NotFoundException : Exception { /* Implement all of the Exception constructors. Notice that the constructors simply execute the base class constructor. Because NotFoundException adds nothing to Exception, there is no need for any further actions. */ public NotFoundException() : base() { } public NotFoundException(string message) : base(message) { } public NotFoundException(string message, Exception innerException) : base(message, innerException) { } protected NotFoundException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } }
I scored 7.8 out of 10. Test was auditing remotely and I haven’t had an opportunity to get explanations about the mark.
What could I have done to improve on my answer to get a better rating?
Answer
One mistake could be using List<Object>
in your class PetShop
List<Object> objList = new List<object>();
instead you could have a List<IPet>
and then your method AddPet
should expect a parameter of type IPet
, add Cat
and Dog
objects to that list. Later in your method IntroduceAll
you could have done:
public void IntroduceAll()
{
foreach (IPet element in objList)
{
element.Introduce();
}
}
instead of comparing types and then calling Introduce
method, this would show the polymorphic behaviour.
Other thing could be having a parameterized constructor, since you are instantiating the objects for Cat
and Dog
and then setting properties.
Attribution
Source : Link , Question Author : AndriiGro , Answer Author : Habib