Advertisement
desislava_topuzakova

клас Animal

May 8th, 2022
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Homework
  8. {
  9.     internal class Animal
  10.     {
  11.         //полета -> характеристики
  12.         private string name; //име -> от 3 до 18 символа
  13.         private string breed; //порода -> 3 до 15 символа
  14.         private string type; //вид -> 3 до 10 символа
  15.         private int age; //възраст -> >= 0 и <= 100
  16.         private int countLegs; //брой на крака -> >= 2
  17.  
  18.         //конструктор -> създаваме обекти
  19.         public Animal(string name, string breed, string type, int age, int countLegs)
  20.         {
  21.             //нов празен обект
  22.             Name = name;
  23.             Breed = breed;
  24.             Type = type;
  25.             Age = age;
  26.             CountLegs = countLegs;
  27.         }
  28.  
  29.         //getter и setter -> достъп до полета
  30.         public string Name
  31.         {
  32.             get { return name; }
  33.             set
  34.             {
  35.                 if (value.Length >= 3 && value.Length <= 18)
  36.                 {
  37.                     name = value;
  38.                 }
  39.                 else
  40.                 {
  41.                     throw new ArgumentException("Invalid name!");
  42.                 }
  43.                
  44.             }
  45.         }
  46.  
  47.         public string Breed
  48.         {
  49.             get { return breed; }
  50.             set
  51.             {
  52.                 if (value.Length >= 3 && value.Length <= 15)
  53.                 {
  54.                     breed = value;
  55.                 }
  56.                 else
  57.                 {
  58.                     throw new ArgumentException("Invalid breed!");
  59.                 }
  60.                
  61.             }
  62.         }
  63.         public string Type
  64.         {
  65.             get { return type; }
  66.             set
  67.             {
  68.                 if (value.Length >= 3 && value.Length <= 10)
  69.                 {
  70.                     type = value;
  71.                 }
  72.                 else
  73.                 {
  74.                     throw new ArgumentException("Invalid type");
  75.                 }
  76.                
  77.             }
  78.         }
  79.  
  80.         public int Age
  81.         {
  82.             get { return age; }
  83.             set
  84.             {
  85.                 if (value >= 0 && value <= 100)
  86.                 {
  87.                     age = value;
  88.                 }
  89.                 else
  90.                 {
  91.                     throw new ArgumentException("Invalid age!");
  92.                 }
  93.                
  94.             }
  95.         }
  96.         public int CountLegs
  97.         {
  98.             get { return countLegs; }
  99.             set
  100.             {   if (value >= 2)
  101.                 {
  102.                     countLegs = value;
  103.                 }
  104.                 else
  105.                 {
  106.                     throw new ArgumentException("Invalid count of legs!");
  107.                 }
  108.             }
  109.         }
  110.  
  111.         //методи -> действия / функционалности
  112.         public void PrintInfo ()
  113.         {
  114.             //име + " " + вид + " " + възраст
  115.             Console.WriteLine(name + " " + type + " " + age);
  116.         }
  117.  
  118.         public void PrintAgeAfter12 ()
  119.         {
  120.             Console.WriteLine(age + 12);
  121.         }
  122.  
  123.         public string GetAnimalType()
  124.         {
  125.             //young(0-5), average(6 - 10), old(над 10)
  126.             if (age <= 5)
  127.             {
  128.                 return "young";
  129.             }
  130.             else if (age >= 6 && age <= 10)
  131.             {
  132.                 return "average";
  133.             }
  134.             else if (age > 10)
  135.             {
  136.                 return "old";
  137.             }
  138.  
  139.             return "";
  140.  
  141.         }
  142.         //вграден метод ToString -> връща "Project.Class"
  143.         public override string ToString()
  144.         {
  145.             //override -> пренапиша (да работи по мой избор)
  146.             return "Animal " + name + " is " + age + " years old.";
  147.         }
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement