.NET 4.x Class Person - C#

Узнай цену своей работы

Формулировка задачи:

Еще одна интересная задача попалась. Подскажите как сделать. Create class Person. Class Person should consists of a) two private fields: name and birthYear (the birthday year).(*As a type for this field you may use DataTime type.) b) two properties for access to these fields (only get) c) default constructor and constructor with 2 parameters d) methods: - Age() - to calculate the age of person -Input() - to input information about person -ChangeName() - to change the name of person -ToString() -Output() - to output information about person (call ToString()) - operator== (equal by name) In the method Main() create 6 objects of Person type and input information about them. Then calculate and write to console the name and Age of each person; Change the name of persons, which Age is less then 16, to "Very Young". Output information about all persons. Find and output information about Persons with the same names (use ==)

Решение задачи: «.NET 4.x Class Person»

textual
Листинг программы
  1. using System;
  2.  
  3. namespace HomeWork4
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Person[] persons = new Person[6];
  10.             Console.WriteLine("Enter information about person");
  11.  
  12.             for (int i = 0; i < 6; i++)
  13.             {
  14.                 Person p = new Person();
  15.                 persons[i] = p;
  16.                 Console.WriteLine("\n Person {0}.", (i + 1));
  17.                 persons[i] = persons[i].Input();
  18.             }
  19.  
  20.             Console.WriteLine("\n Information about age");
  21.             foreach (Person person in persons)
  22.             {
  23.                 int age;
  24.                 age = person.Age();
  25.                 Console.WriteLine("name - {0},\t age - {1}", person.Name, age);
  26.             }
  27.  
  28.             Console.WriteLine("\n Output");
  29.             foreach (Person person in persons)
  30.             {
  31.                 int age;
  32.                 age = person.Age();
  33.                 if (age < 16)
  34.                 {
  35.                     person.ChangeName();
  36.                 }
  37.                 person.Output();
  38.             }
  39.             Console.WriteLine("\n  Persons with the same names");
  40.             for (int i = 0; i < 6; i++)
  41.             {
  42.                 for (int j = i + 1; j < 6; j++)
  43.                 {
  44.                     if (persons[i] == persons[j])
  45.                     {
  46.                         Console.Write("Person {0}. - ", (i + 1));
  47.                         persons[i].Output();
  48.                         Console.Write("Person {0}. - ", j);
  49.                         persons[j].Output();
  50.                         Console.WriteLine();
  51.                     }
  52.                 }
  53.             }
  54.  
  55.         }
  56.     }
  57.     class Person
  58.     {                              
  59.         private string _name;
  60.         private int _birthYear;
  61.        
  62.         public string Name
  63.         {
  64.             get { return _name; }
  65.         }
  66.         public int BirthYear
  67.         {
  68.             get { return _birthYear; }
  69.         }
  70.        
  71.         public Person()
  72.         {
  73.  
  74.         }
  75.         public Person(string name, int birthYear)
  76.         {
  77.             _name = name;
  78.             _birthYear = birthYear;
  79.         }
  80.        
  81.         public int Age()
  82.         {
  83.             int age = 2017 - _birthYear;
  84.             return age;
  85.         }
  86.         public Person Input()
  87.         {
  88.             string name;
  89.             int birthYear;
  90.  
  91.             Console.Write(" name - ");
  92.             name = Console.ReadLine();
  93.             Console.Write(" year - ");
  94.             while (!Int32.TryParse(Console.ReadLine(), out birthYear))
  95.             {
  96.                 Console.Write("Error, year is not correct ");
  97.                 Console.Write("\n year - ");
  98.             }
  99.  
  100.             return new Person(name, birthYear);
  101.         }
  102.         public string ChangeName()
  103.         {
  104.             this._name = "Very Young";
  105.             return this._name;
  106.         }
  107.         public override string ToString()
  108.         {
  109.             return "name - " + _name + ",\t \t the birthday year - " + _birthYear.ToString();
  110.         }
  111.         public void Output()
  112.         {
  113.             Console.WriteLine(ToString());
  114.         }
  115.         public static bool operator ==(Person first, Person second)
  116.         {
  117.  
  118.             return first._name == second._name;
  119.         }
  120.         public static bool operator !=(Person first, Person second)
  121.         {
  122.             return !(first == second);
  123.  
  124.            
  125.         }
  126.     }
  127. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы