Advertisement
Vickyyy01

Dog Identify - my solution

Dec 14th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2.  
  3. public interface IDog
  4. {
  5.     string Name {get;}
  6.     string Breed {get;}
  7. }
  8.  
  9. public class Dog: IDog
  10. {
  11.         public string Name {get;}
  12.         public string Breed {get;}
  13.        
  14.         public Dog(string name, string breed)
  15.         {
  16.                 this.Name = name;
  17.                 this.Breed = breed;
  18.         }
  19.    
  20.         public Dog(string breed)
  21.          :this(null,breed)
  22.         {
  23.                 this.Breed = breed;
  24.         }
  25.        
  26.         public Dog()
  27.         {
  28.         }
  29.        
  30.         public override string ToString()
  31.         {
  32.                 string s;
  33.                
  34.                 if(this.Breed != null && this.Name != null)
  35.                 {
  36.                    s = string.Format("The {0} dog {1} said BARK!",this.Breed,this.Name);
  37.                 }
  38.                 else if(this.Name == null && this.Breed != null)
  39.         {
  40.              s = string.Format("The unnamed {0} dog said BARK!",this.Breed);
  41.         }
  42.             else
  43.                 {
  44.                      s = null;
  45.                 }
  46.                 return s;
  47.         }
  48. }
  49.                                        
  50. public class Program
  51. {
  52.         public static void Main()
  53.         {
  54.                
  55.                 Dog sharo = new Dog("Sharo","corgi");
  56.                
  57.                 Dog rex = new Dog("Rex","poodle");
  58.            
  59.                 Dog unnamed = new Dog(null,"hound");
  60.            
  61.         Dog[] dogs = new Dog[]{sharo, rex,unnamed};
  62.            
  63.            
  64.             foreach(var dog in dogs)
  65.             {
  66.                 Console.WriteLine(dog);
  67.             }
  68.         }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement