Наследование - Определить понятие «Человек» - C#
Формулировка задачи:
Определить понятие «Человек». Состояние объекта определяется полями:
- номер паспорта (семизначное целое число);
- фамилия (строка до 20 символов).
Базируясь на этом понятии, определить понятие «Проживающий», дополнительно определив понятие «Человек» полями «Наименование отеля» (строка до 15 символов) и «Количество суток проживания» (целое число). Вывести фамилии всех лиц, проживающих в отеле «Загородный» наибольшее количество суток.
Буду очень признательна в помощи....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace k
{
class Program
{
static void Main(string[] args)
{
Person a = new Person();
Live b = new Live();
Console.WriteLine("Введите Фамилию");
a.lName = Console.ReadLine();
Console.WriteLine("Введите название отеля");
b.hotel = Console.ReadLine();
Console.WriteLine("Введите номер паспорта");
a.Pas = Console.Read();
Console.WriteLine("Введите количество ночей");
b.st = Console.Read();
Console.WriteLine(a.lName + " " + a.Pas + " " + b.st + " " + b.hotel);
if (b.Test(b))
{
Console.WriteLine("Проживает в отеле Загородный" + b.st + "% суток");
}
else
{
Console.WriteLine("не попадает");
}
Console.ReadLine();
}
}
class Person
{
string LName;
int pas;
public Person()
{ }
public string lName
{
set { this.LName = value; }
get { return this.LName; }
}
public int Pas
{
set { this.pas = value; }
get { return this.pas; }
}
}
class Live: Person
{
string LName;
int pas;
int St;
string Hotel;
public Live()
{
}
public string hotel
{
set { this.Hotel = value; }
get { return this.Hotel; }
}
public int st
{
set { this.St = value; }
get { return this.St; }
}
public bool Test (Live b)
{
if(b.Hotel=="Загородный")
{
return false;
}
return true;
}
}
}Решение задачи: «Наследование - Определить понятие «Человек»»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
metka : try
{
Console.WriteLine("Введите количество проживающих");
int count = int.Parse(Console.ReadLine());
Person[] people = new Person[count];
for (int i = 0; i < count; i++)
{
Console.WriteLine("Введите Фамилию");
string lName = Console.ReadLine();
Console.WriteLine("Введите название отеля");
string hotel = Console.ReadLine();
Console.WriteLine("Введите номер паспорта");
int Pas = int.Parse(Console.ReadLine());
Console.WriteLine("Введите количество ночей");
int st = int.Parse(Console.ReadLine());
people[i] = new Resident(hotel, st, lName, Pas);
}
foreach (var resident in GetPeopleInZagorodniy(people)) { Console.WriteLine(resident.LName + " - " + resident.Timestaying.ToString() + " суток"); }
Console.ReadLine();
}
catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Начать сначала? Напишите \"да\" чтобы попробовать еще раз"); if (Console.ReadLine() == "да") { goto metka; } }
}
private static List<Resident> GetPeopleInZagorodniy(Person[] people)
{
Console.WriteLine();
Console.WriteLine("Эти люди проживают в Загородом отеле, распределены по времени проживания");
List<Resident> inhotelzagorodniy = new List<Resident>();
foreach (var person in people)
{
if (typeof(Resident) == person.GetType())
{
if ((person as Resident).Hotel == "Загородный") { inhotelzagorodniy.Add(person as Resident); }
}
}
inhotelzagorodniy.Sort(comparetimestaying);
return inhotelzagorodniy;
}
private static int comparetimestaying(Resident x, Resident y)
{
if (x.Timestaying > y.Timestaying) { return -1; }
if (x.Timestaying == y.Timestaying) { return 0; }
if (x.Timestaying < y.Timestaying) { return 1; }
return 0;
}
}
class Person
{
protected string lName;
protected int pas;
public Person()
{ this.lName = "Anonimus"; this.pas = 6666666; }
public Person(string lName, int pas)
{
this.LName = lName;
this.Pas = pas;
}
public string LName
{
set { if (value.Length <= 20) { this.lName = value; } else { throw new ArgumentException("Имя должно быть не длиннее 20ти символов"); } }
get { return this.lName; }
}
public int Pas
{
set { if (value.ToString().Length == 7) { this.pas = value; } else { throw new ArgumentException("Номер паспорта должен состоять из 7 символов"); } }
get { return this.pas; }
}
}
class Resident : Person
{
private string hotel;
private int timestaying;
public Resident() : base()
{
this.timestaying = 1;
this.hotel = "HELL";
}
public Resident(string hotel,int timestaying,string LName, int pas) : base(LName,pas)
{
this.Hotel = hotel;
this.Timestaying = timestaying;
}
public int Timestaying
{
set { if (value > 0) { this.timestaying = value; } else { throw new ArgumentException("Время проживания не может быть отрицательным и меньше 1 суток"); } }
get { return this.timestaying; }
}
public string Hotel
{
set { if (value.Length <= 15) { this.hotel = value; } else { throw new ArgumentException("Название отеля должно быть не длиннее 15 символов"); } }
get { return this.hotel; }
}
}
}