Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Homework
- {
- internal class Animal
- {
- //полета -> характеристики
- private string name; //име -> от 3 до 18 символа
- private string breed; //порода -> 3 до 15 символа
- private string type; //вид -> 3 до 10 символа
- private int age; //възраст -> >= 0 и <= 100
- private int countLegs; //брой на крака -> >= 2
- //конструктор -> създаваме обекти
- public Animal(string name, string breed, string type, int age, int countLegs)
- {
- //нов празен обект
- Name = name;
- Breed = breed;
- Type = type;
- Age = age;
- CountLegs = countLegs;
- }
- //getter и setter -> достъп до полета
- public string Name
- {
- get { return name; }
- set
- {
- if (value.Length >= 3 && value.Length <= 18)
- {
- name = value;
- }
- else
- {
- throw new ArgumentException("Invalid name!");
- }
- }
- }
- public string Breed
- {
- get { return breed; }
- set
- {
- if (value.Length >= 3 && value.Length <= 15)
- {
- breed = value;
- }
- else
- {
- throw new ArgumentException("Invalid breed!");
- }
- }
- }
- public string Type
- {
- get { return type; }
- set
- {
- if (value.Length >= 3 && value.Length <= 10)
- {
- type = value;
- }
- else
- {
- throw new ArgumentException("Invalid type");
- }
- }
- }
- public int Age
- {
- get { return age; }
- set
- {
- if (value >= 0 && value <= 100)
- {
- age = value;
- }
- else
- {
- throw new ArgumentException("Invalid age!");
- }
- }
- }
- public int CountLegs
- {
- get { return countLegs; }
- set
- { if (value >= 2)
- {
- countLegs = value;
- }
- else
- {
- throw new ArgumentException("Invalid count of legs!");
- }
- }
- }
- //методи -> действия / функционалности
- public void PrintInfo ()
- {
- //име + " " + вид + " " + възраст
- Console.WriteLine(name + " " + type + " " + age);
- }
- public void PrintAgeAfter12 ()
- {
- Console.WriteLine(age + 12);
- }
- public string GetAnimalType()
- {
- //young(0-5), average(6 - 10), old(над 10)
- if (age <= 5)
- {
- return "young";
- }
- else if (age >= 6 && age <= 10)
- {
- return "average";
- }
- else if (age > 10)
- {
- return "old";
- }
- return "";
- }
- //вграден метод ToString -> връща "Project.Class"
- public override string ToString()
- {
- //override -> пренапиша (да работи по мой избор)
- return "Animal " + name + " is " + age + " years old.";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement