Advertisement
Guest User

Animals

a guest
Jul 7th, 2019
3,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Animals
  6. {
  7.     public class Animal
  8.     {
  9.         private string name;
  10.         private int age;
  11.         private string gender;
  12.  
  13.         public Animal(string name, int age, string gender)
  14.         {
  15.             this.name = name;
  16.             this.Age = age;
  17.             this.gender = gender;
  18.         }
  19.  
  20.         public string Name
  21.         {
  22.             get => this.name;
  23.             private set
  24.             {
  25.                 if (value == null)
  26.                 {
  27.                     throw new ArgumentException("Invalid input!");
  28.                 }
  29.  
  30.                 this.name = value;
  31.             }
  32.         }
  33.  
  34.         public int Age
  35.         {
  36.             get => this.age;
  37.             private set
  38.             {
  39.                 if (value <= 0)
  40.                 {
  41.                     throw new ArgumentException("Invalid input!");
  42.                 }
  43.  
  44.                 this.age = value;
  45.             }
  46.         }
  47.  
  48.         public string Gender
  49.         {
  50.             get => this.gender;
  51.             private set
  52.             {
  53.                 if (value == null)
  54.                 {
  55.                     throw new ArgumentException("Invalid input!");
  56.                 }
  57.  
  58.                 this.gender = value;
  59.             }
  60.         }
  61.  
  62.         public virtual void ProduceSound()
  63.         {
  64.             Console.WriteLine("Produce sound");
  65.         }
  66.  
  67.         public override string ToString()
  68.         {
  69.             StringBuilder sb = new StringBuilder();
  70.  
  71.             sb.AppendLine($"{this.Name} {this.Age} {this.Gender}");
  72.            
  73.             return sb.ToString().TrimEnd();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement