Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Prog_Tech_lab1
- {
- class HashCode
- {
- public static int GetRand()
- {
- Random rnd = new Random();
- return rnd.Next(100, 1000);
- }
- }
- interface INameAndCopy
- {
- string Name { get; set; }
- object DeepCopy();
- int GetHashCode();
- }
- class Person : INameAndCopy
- {
- private string name;
- private string sname;
- private System.DateTime birthD;
- public string Name { get => name; set => name = value; }
- public string Sname { get => sname; set => sname = value; }
- public Person(string n, string sn, DateTime bD)
- {
- name = n;
- sname = sn;
- birthD = bD;
- }
- public Person()
- {
- name = "*Person Name*";
- sname = "*Peron SName*";
- birthD = new DateTime(1, 1, 1);
- }
- public DateTime BirthD => birthD;
- public int BirthY
- {
- get => birthD.Year;
- set => birthD = new DateTime(value, birthD.Month, birthD.Day);
- }
- public override string ToString()
- {
- return string.Format("{0} {1} {2}", Name, Sname, birthD.Year);
- }
- public string ToShortString()
- {
- return Name + " " + Sname;
- }
- public override bool Equals(Object obj)
- {
- Person person = (Person)obj;
- if (Name == person.Name && Sname == person.Sname && BirthD == person.BirthD)
- return true;
- return false;
- }
- public static bool operator ==(Person p1, Person p2)
- {
- return p1.Equals(p2);
- }
- public static bool operator !=(Person p1, Person p2)
- {
- return p1.Equals(p2);
- }
- public override int GetHashCode()
- {
- return Name.Length + Sname.Length + BirthD.Day + BirthD.Month + BirthD.Year;
- }
- public object DeepCopy()
- {
- return new Person(Name, Sname, BirthD);
- }
- }
- class Team : INameAndCopy
- {
- protected string name;
- protected int regNum;
- public Team()
- {
- name = "*Def organisation Name*";
- regNum = -1;
- }
- public Team(string organisationName, int registrationNum)
- {
- name = organisationName;
- regNum = registrationNum;
- }
- public string Name { get => name; set { if (value.Length <= 48) name = value; } }
- public int RegNum
- {
- get => regNum;
- set
- {
- if (value >= 1 && value <= 999)
- regNum = value;
- else
- Console.WriteLine("Ошибка! Введеный регистрационный номер не входит в промежуток [1, 999]");
- }
- }
- public virtual object DeepCopy()
- {
- return new Team(name, regNum);
- }
- public override bool Equals(object obj)
- {
- Team team = (Team)obj;
- if (Name == team.Name && RegNum == team.RegNum)
- return true;
- return false;
- }
- public static bool operator ==(Team t1, Team t2)
- {
- return t1.Equals(t2);
- }
- public static bool operator !=(Team t1, Team t2)
- {
- return t1.Equals(t2);
- }
- public override int GetHashCode()
- {
- return Name.Length + RegNum;
- }
- public override string ToString()
- {
- return string.Format("Название группы: {0} \nРегистрационный номер: {1}", Name, RegNum);
- }
- }
- enum TimeFrame
- {
- Year = 1,
- TwoYear = 2,
- Long
- }
- class Paper
- {
- public string Name { get; set; }
- private Person author = new Person();
- public Person Author { get => author; set => author = value; }
- public System.DateTime PubDate { get; set; }
- public Paper()
- {
- Name = "*Name of Book*";
- Author.Name = "*Author Name*";
- Author.Sname = "*Author Sname*";
- Author.BirthY = -1;
- PubDate = new DateTime(1, 1, 1);
- }
- public Paper(string name, Person person, DateTime date)
- //требуется наличия Person существующего
- {
- Name = name;
- Author = person;
- PubDate = date;
- }
- public Paper(string name, string author_name, string author_sname, int author_birthY, DateTime publishing_date)
- // без создания отдельного Person
- {
- Name = name;
- Author.Name = author_name;
- Author.Sname = author_sname;
- Author.BirthY = author_birthY;
- PubDate = publishing_date;
- }
- public override string ToString()
- {
- return string.Format("Название книги: {0} \nАвтор Книги: {1} {2}, {3} года рождения \nГод побликации: {4}",
- Name, Author.Name, Author.Sname, Author.BirthY, PubDate);
- }
- public object DeepCopy()
- {
- return new Paper(Name, Author, PubDate);
- }
- }
- class ResearchTeam : Team, INameAndCopy
- {
- private string resName;
- private TimeFrame time;
- private List<Paper> pub = new List<Paper>();
- private List<Person> memb = new List<Person>();
- public string ResName { get => resName; set => resName = value; }
- public List<Paper> Pub { get => pub; set => pub = value; }
- public List<Person> Memb { get => memb; set => memb = value; }
- public TimeFrame Time
- {
- get => time;
- set
- {
- if (value >= TimeFrame.Year && value <= TimeFrame.Long)
- time = TimeFrame.Year;
- else
- Console.WriteLine("Ошибка");
- }
- }
- public ResearchTeam()
- {
- ResName = "*Research Name";
- Name = "*Organisation Name";
- RegNum = -1;
- Time = TimeFrame.Year; // Дефолтное значение
- Pub = new List<Paper>();
- Memb = new List<Person>();
- }
- public ResearchTeam(string researchName, Team team, TimeFrame researchTime)
- // с инициализированным объектом Team
- {
- ResName = researchName;
- Name = team.Name;
- RegNum = team.RegNum;
- Time = researchTime;
- Pub = new List<Paper>();
- Memb = new List<Person>();
- }
- public ResearchTeam(string researchName, string organisationName, int registrationNum, TimeFrame researchTime)
- // с инициализацией списка публикаций и участников автоматически
- {
- ResName = researchName;
- Name = organisationName;
- RegNum = registrationNum;
- Time = researchTime;
- Pub = new List<Paper>();
- Memb = new List<Person>();
- }
- public ResearchTeam(string researchName, string organisationName, int registrationNum, TimeFrame ResearchTime, List<Paper> publications, List<Person> members)
- // требуется инициализированный список публикаций и участников
- {
- ResName = researchName;
- Name = organisationName;
- regNum = registrationNum;
- time = ResearchTime;
- Pub = publications;
- Memb = members;
- }
- public Team Team
- {
- get { Team t = new Team { Name = this.Name, RegNum = this.RegNum }; return t; }
- }
- public Paper LastPubs
- {
- get
- {
- if (Pub != null)
- {
- Paper max = Pub[0];
- for (int i = 1; i < Pub.Count; i++)
- if (Pub[i].PubDate >= max.PubDate)
- max = Pub[i];
- return max;
- }
- return null;
- }
- }
- public bool this[TimeFrame index]
- {
- get
- {
- if (Time == index)
- return true;
- else
- return false;
- }
- }
- public IEnumerable GetPersonsWithNoPubs()
- {
- for (int i = 0; i < Memb.Count; i++)
- {
- bool E = false;
- for (int j = 0; j < Pub.Count && !E; j++)
- {
- if (Memb[i] == Pub[j].Author)
- E = true;
- }
- if (!E) yield return Memb[i];
- }
- }
- public IEnumerable GetLastNPaper(int n)
- {
- DateTime local_date = new DateTime(2019 - n, 1, 1);
- for (int i = 0; i < Pub.Count; i++)
- if (Pub[i].PubDate > local_date)
- yield return Pub[i];
- }
- public void AddPaper(Paper newPaper)
- {
- pub.Add(newPaper);
- }
- public void AddPerson(Person newPerson)
- {
- memb.Add(newPerson);
- }
- public override string ToString()
- {
- string local_papers_list = "";
- foreach (Paper i in pub)
- local_papers_list = local_papers_list + i.Name + ", ";
- string local_person_list = "";
- foreach (Person i in memb)
- local_person_list = local_person_list + i.Name + " " + i.Sname + ", ";
- return string.Format("{0} \n{1} \n{2} \n{3} \n{4} \n{5}", ResName, Name, RegNum, Time, local_papers_list, local_person_list);
- }
- public string ToShortString()
- {
- return ResName + ", " + Name + ", " + RegNum + ", " + Time;
- }
- public override object DeepCopy()
- {
- List<Paper> new_paper_list = Pub;
- List<Person> new_person_list = Memb;
- return new ResearchTeam(ResName, Name, RegNum, Time, new_paper_list, new_person_list);
- }
- }
- class MainClass
- {
- public static object GetCopy(INameAndCopy obj)
- {
- return obj.DeepCopy();
- }
- public static object GetName(INameAndCopy obj)
- {
- return obj.Name;
- }
- public static int GetCode(INameAndCopy obj)
- {
- return obj.GetHashCode() + HashCode.GetRand();
- }
- public static void Main(string[] args)
- {
- Team T1 = new Team("Team1", 333);
- Team T2 = new Team("Team1", 333);
- Console.WriteLine("T1: " + GetCode(T1) + ", T2: " + GetCode(T2));
- Console.WriteLine("_____________________");
- //
- int regTest = int.Parse(Console.ReadLine());
- Team TTest = new Team("TeamTest", 10);
- TTest.RegNum = regTest;
- Console.WriteLine("_____________________");
- //
- DateTime default_Date = new DateTime(1890, 1, 1);
- Person M = new Person();
- Person M1 = new Person("M1", "_", default_Date);
- Person M2 = new Person("M2", "_", default_Date);
- Person M3 = new Person("M3", "_", default_Date);
- Person M4 = new Person("M4", "_", default_Date);
- Paper P1 = new Paper("Book1", M1, new DateTime(1930, 1, 1));
- Paper P2 = new Paper("Book2", M2, new DateTime(1930, 1, 1));
- Paper P3 = new Paper("Book3", M2, new DateTime(1935, 1, 1));
- Paper P4 = new Paper("Book4", M3, new DateTime(1910, 1, 1));
- Paper P5 = new Paper("Book5", M4, new DateTime(1915, 1, 1));
- ResearchTeam Rt = new ResearchTeam("Research1", T1, TimeFrame.Year);
- Rt.AddPerson(M);
- Rt.AddPerson(M1);
- Rt.AddPerson(M2);
- Rt.AddPerson(M3);
- Rt.AddPerson(M4);
- Rt.AddPaper(P1);
- Rt.AddPaper(P2);
- Rt.AddPaper(P3);
- Rt.AddPaper(P4);
- Rt.AddPaper(P5);
- Console.WriteLine(Rt.ToString());
- Console.WriteLine("_____________________");
- //
- Team TLocal = Rt.Team;
- Console.WriteLine(TLocal.ToString());
- Console.WriteLine("_____________________");
- //
- ResearchTeam Rt_Copy = (ResearchTeam)GetCopy(Rt);
- Rt.Time = TimeFrame.TwoYear;
- Console.WriteLine("Копия: \n" + Rt_Copy.ToString() + "\n \nОригинал измененный: \n" + Rt.ToString());
- Console.WriteLine("_____________________");
- //
- Console.WriteLine("Не имеют публикаций: \n");
- foreach(Person i in Rt.GetPersonsWithNoPubs())
- {
- Console.WriteLine(i + " ");
- }
- Console.WriteLine("_____________________");
- //
- Console.WriteLine("За последние 100 лет: \n");
- foreach(Paper i in Rt.GetLastNPaper(100))
- {
- Console.WriteLine(i + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement