Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- namespace _15._03
- {
- enum Frequency { Weekly, Monthly, Yearly }//определяю тип Frequency
- public class Article : IRateAndCopy//класс, который имеет свойство с доступом для чтения и записи
- {
- public Article(string name, double rating, Person author)//конструктор с параметрами
- {
- Name = name;
- Rating = rating;
- Author = author;
- }
- public Article()//конструктор без параметров
- : this("Без названия", 0, new Person("Нет автора"))
- {
- }
- public string Name { get; set; }//свойства для чтения и записи
- public double Rating { get; set; }
- public Person Author { get; set; }
- public override string ToString()//перегруженная версия ToString
- => $"{Name} с рейтингом {Rating} от {Author}";
- public virtual object DeepCopy()
- {
- Article temp = (Article)this.MemberwiseClone();
- temp.Name = this.Name;
- temp.Rating = this.Rating;
- temp.Author = (Person)this.Author.DeepCopy();
- return temp;
- }
- double IRateAndCopy.Rating { get => Rating; }
- }
- interface IRateAndCopy
- {
- double Rating { get; }
- object DeepCopy();
- }
- public class Person
- {
- public Person(string name)
- {
- Name = name;
- }
- public string Name { get; set; }
- public override bool Equals(object? obj)
- {
- // если параметр метода представляет тип Person
- // то возвращаем true, если имена совпадают
- if (obj is Person person) return Name == person.Name;
- return false;
- }
- public virtual object DeepCopy()
- {
- Person temp = (Person)this.MemberwiseClone();
- temp.Name = this.Name;
- return temp;
- }
- // public override int GetHashCode() => Name.GetHashCode(); //другой вариант
- public override int GetHashCode()
- {
- return HashCode.Combine(Name);
- }
- public static bool operator ==(Person obj, Person obj1)
- {
- if (obj.Name == obj1.Name)
- return true;
- return false;
- }
- public static bool operator !=(Person obj, Person obj1)
- {
- if (obj.Name != obj1.Name)
- return true;
- return false;
- }
- public override string ToString()//перегруженная версия ToString
- => $"{Name}";
- }
- public class Edition
- {
- protected string Name { get; set; }
- protected DateTime Date_of_publish { get; set; }
- protected int circulation;
- public int _Circulation
- {
- get { return circulation; }
- set
- {
- if (value < 0)
- {
- throw new ArgumentOutOfRangeException("Circulation must be more or equal 0 ");
- }
- else
- {
- circulation = value;
- }
- }
- }
- public Edition(string name, DateTime date, int id)
- {
- Name = name;
- Date_of_publish = date;
- circulation = id;
- }
- public Edition()
- {
- }
- public Edition(Edition val)
- {
- this.Name = val.Name;
- this.Date_of_publish = val.Date_of_publish;
- this.circulation = val.circulation;
- }
- public virtual object DeepCopy()
- {
- Edition temp = (Edition)this.MemberwiseClone();
- temp.Name = this.Name;
- temp.Date_of_publish = this.Date_of_publish;
- temp.circulation = this.circulation;
- return temp;
- }
- public override bool Equals(object obj)
- {
- return obj is Edition edition &&
- Name == edition.Name &&
- Date_of_publish == edition.Date_of_publish &&
- circulation == edition.circulation;
- }
- public override int GetHashCode()
- {
- return HashCode.Combine(Name, Date_of_publish, circulation);
- }
- public static bool operator ==(Edition obj, Edition obj1)
- {
- if (obj.Name == obj1.Name && obj.circulation == obj1.circulation && obj.Date_of_publish == obj1.Date_of_publish)
- return true;
- return false;
- }
- public static bool operator !=(Edition obj, Edition obj1)
- {
- if (obj.Name != obj1.Name || obj.circulation != obj1.circulation || obj.Date_of_publish != obj1.Date_of_publish)
- return true;
- return false;
- }
- public override string ToString()//перегруженная версия для формирования строки со значениями всех полей класса
- {
- return string.Format("\nName: {0}\nCirculation: {1}\nPublishDate: {2}\n", Name, circulation, Date_of_publish);
- }
- public string name
- {
- get => Name;
- set => Name = value;
- }
- public DateTime PublishDate
- {
- get => Date_of_publish;
- set => Date_of_publish = value;
- }
- public int Circulation
- {
- get => circulation;
- set => circulation = value;
- }
- // public virtual string ToShortString()//виртуальный метод toShortString
- // => $"Name = {Name}"//для формирования строки без списка статей но со значением среднего рейтинга статей
- // + $"\nPublishDate = {PublishDate}";
- // + $"\nEdition = {Edition}";
- }
- class Magazine : Edition
- {
- //private string name;
- private Frequency frequency;
- //private DateTime publishDate;
- //private int edition;
- //private Person[] editors;
- //private Article[] articles;
- private System.Collections.ArrayList editors = new System.Collections.ArrayList();
- private System.Collections.ArrayList articles = new System.Collections.ArrayList();
- public System.Collections.ArrayList ListOfEditors { get { return editors; } set { editors = value; } }
- public System.Collections.ArrayList ListOfArticles { get { return articles; } set { articles = value; } }
- public Magazine(
- string name,
- Frequency frequency,
- DateTime publishDate,
- int circ)//конструктор для инициализации соотв-х полей класса
- {
- this.Name = name;
- this.frequency = frequency;
- this.Date_of_publish = publishDate;
- this.circulation = circ;
- }
- public Magazine()//конструткор без параметров ,иницал-й по умолчанию
- {
- }
- public void AddArticles(params Article[] NewArticles)
- {
- articles.AddRange(NewArticles);
- }
- public void AddEditors(params Person[] NewEditors)
- {
- editors.AddRange(NewEditors);
- }
- //public string Name//свойство типа string
- //{
- // get => Name;
- // set => Name = value;
- //}
- public Frequency Frequency
- {
- get => frequency;
- set => frequency = value;
- }
- public DateTime PublishDate
- {
- get => Date_of_publish;
- set => Date_of_publish = value;
- }
- //public int Edition
- //{
- // get => edition;
- // set => edition = value;
- //}
- //public Article[] Articles
- //{
- // get => articles;
- // set => articles = value;
- //}
- public double GetAvgRating
- {
- get
- {
- double result = 0;
- int count = 0;
- foreach (Article item in articles)
- {
- result += item.Rating;
- count++;
- }
- result /= count;
- if (count != 0) return result;
- else return 0;
- }
- }
- public bool this[Frequency frequency]//индексатор булевского типа,true если значение поля равно индексу
- {
- get => Frequency == frequency;
- }
- public override string ToString()
- {
- string stringListOfArticles = "";
- foreach (Article art in articles)
- {
- stringListOfArticles += art.ToString() + "\r\n";
- }
- string stringListOfEditors = "";
- foreach (Person pers in ListOfEditors)
- {
- stringListOfEditors += pers.ToString() + "\r\n";
- }
- return string.Format("\r\n Name: {0} \r\n Frequency: {1} \r\n PublishDate: {2}\r\nCirculation: {5} \r\n\n Editors: {3} \r\n Articles: {4}\r\n", Name, Frequency, Date_of_publish, stringListOfEditors, stringListOfArticles,circulation);
- }
- public string ToShortString()
- {
- return string.Format("\r\n Name: {0}, PublishDate: {1} \r\n ", Name, Date_of_publish);
- }
- public override object DeepCopy()
- {
- Magazine temp = (Magazine)this.MemberwiseClone();
- temp.Name = this.Name;
- temp.Date_of_publish = this.Date_of_publish;
- temp.circulation = this.circulation;
- temp.frequency = this.frequency;
- temp.ListOfArticles = this.ListOfArticles;
- temp.ListOfEditors = this.ListOfEditors;
- return temp;
- }
- public Edition GetEdition
- {
- get
- {
- return new Edition(Name, Date_of_publish, circulation);
- }
- set
- {
- this.Name = value.name;
- this.PublishDate = value.PublishDate;
- this.circulation = value.Circulation;
- }
- }
- public IEnumerable<Article> StrInName(string PartOfName)
- {
- foreach (Article art in articles)
- {
- if (art.Name.Contains(PartOfName))
- {
- yield return art;
- Console.WriteLine(art.ToString());
- }
- }
- }
- public IEnumerable<Article> HighRateArticles(double Rate)
- {
- for (int i = 0; i < articles.Count; i++)
- {
- if (((Article)articles[i]).Rating > Rate)
- {
- yield return (Article)articles[i];
- Console.WriteLine(((Article)articles[i]).ToString());
- }
- }
- }
- //public void AddArticles(params Article[] newArticles)//метод для добававления элементов в список статей в журнале
- //{
- // if (newArticles?.Length == 0)
- // {
- // return;
- // }
- // if (articles == null)
- // {
- // articles = Array.Empty<Article>();
- // }
- // int oldLength = articles.Length;
- // Array.Resize(ref articles, articles.Length + newArticles.Length);
- // Array.Copy(newArticles, 0, articles, oldLength, newArticles.Length);
- //}
- //public override string ToString()//перегруженная версия для формирования строки со значениями всех полей класса сос писокм статей
- //{
- // StringBuilder stroka = new StringBuilder();
- // foreach (var strk in Articles)
- // {
- // stroka.Append($"{strk}");
- // }
- // return string.Format("\nName: {0}\nFrequency: {1}\nPublishDate: {2, 7} \nEdition: {3, 6}\n\nArticles: {4}", name, Frequency, PublishDate, Edition, stroka);
- //}
- //public virtual string ToShortString()//виртуальный метод toShortString
- // => $"Name = {Name}"//для формирования строки без списка статей но со значением среднего рейтинга статей
- // + $"\nFrequency = {Frequency}"
- // + $"\nPublishDate = {PublishDate}"
- // + $"\nEdition = {Edition}";
- //// + $"\nAvg rating = {GetAvgRating()}";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement