.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
Листинг программы
using System;
namespace HomeWork4
{
class Program
{
static void Main(string[] args)
{
Person[] persons = new Person[6];
Console.WriteLine("Enter information about person");
for (int i = 0; i < 6; i++)
{
Person p = new Person();
persons[i] = p;
Console.WriteLine("\n Person {0}.", (i + 1));
persons[i] = persons[i].Input();
}
Console.WriteLine("\n Information about age");
foreach (Person person in persons)
{
int age;
age = person.Age();
Console.WriteLine("name - {0},\t age - {1}", person.Name, age);
}
Console.WriteLine("\n Output");
foreach (Person person in persons)
{
int age;
age = person.Age();
if (age < 16)
{
person.ChangeName();
}
person.Output();
}
Console.WriteLine("\n Persons with the same names");
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 6; j++)
{
if (persons[i] == persons[j])
{
Console.Write("Person {0}. - ", (i + 1));
persons[i].Output();
Console.Write("Person {0}. - ", j);
persons[j].Output();
Console.WriteLine();
}
}
}
}
}
class Person
{
private string _name;
private int _birthYear;
public string Name
{
get { return _name; }
}
public int BirthYear
{
get { return _birthYear; }
}
public Person()
{
}
public Person(string name, int birthYear)
{
_name = name;
_birthYear = birthYear;
}
public int Age()
{
int age = 2017 - _birthYear;
return age;
}
public Person Input()
{
string name;
int birthYear;
Console.Write(" name - ");
name = Console.ReadLine();
Console.Write(" year - ");
while (!Int32.TryParse(Console.ReadLine(), out birthYear))
{
Console.Write("Error, year is not correct ");
Console.Write("\n year - ");
}
return new Person(name, birthYear);
}
public string ChangeName()
{
this._name = "Very Young";
return this._name;
}
public override string ToString()
{
return "name - " + _name + ",\t \t the birthday year - " + _birthYear.ToString();
}
public void Output()
{
Console.WriteLine(ToString());
}
public static bool operator ==(Person first, Person second)
{
return first._name == second._name;
}
public static bool operator !=(Person first, Person second)
{
return !(first == second);
}
}
}