Advertisement
JouJoy

Class1.cs

Mar 26th, 2022
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.14 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace _15._03
  7. {
  8.     enum Frequency { Weekly, Monthly, Yearly }//определяю тип Frequency
  9.  
  10.     public class Article : IRateAndCopy//класс, который имеет свойство с доступом для чтения и записи
  11.     {
  12.         public Article(string name, double rating, Person author)//конструктор с параметрами
  13.         {
  14.             Name = name;
  15.             Rating = rating;
  16.             Author = author;
  17.         }
  18.  
  19.         public Article()//конструктор без параметров
  20.             : this("Без названия", 0, new Person("Нет автора"))
  21.         {
  22.         }
  23.  
  24.         public string Name { get; set; }//свойства для чтения и записи
  25.         public double Rating { get; set; }
  26.         public Person Author { get; set; }
  27.  
  28.         public override string ToString()//перегруженная версия ToString
  29.             => $"{Name} с рейтингом {Rating} от {Author}";
  30.  
  31.         public virtual object DeepCopy()
  32.         {
  33.             Article temp = (Article)this.MemberwiseClone();
  34.             temp.Name = this.Name;
  35.             temp.Rating = this.Rating;
  36.             temp.Author = (Person)this.Author.DeepCopy();
  37.             return temp;
  38.         }
  39.         double IRateAndCopy.Rating { get => Rating; }
  40.     }
  41.     interface IRateAndCopy
  42.     {
  43.         double Rating { get; }
  44.         object DeepCopy();
  45.     }
  46.  
  47.     public class Person
  48.     {
  49.         public Person(string name)
  50.         {
  51.             Name = name;
  52.         }
  53.  
  54.         public string Name { get; set; }
  55.         public override bool Equals(object? obj)
  56.         {
  57.             // если параметр метода представляет тип Person
  58.             // то возвращаем true, если имена совпадают
  59.             if (obj is Person person) return Name == person.Name;
  60.             return false;
  61.         }
  62.  
  63.  
  64.         public virtual object DeepCopy()
  65.         {
  66.             Person temp = (Person)this.MemberwiseClone();
  67.             temp.Name = this.Name;
  68.             return temp;
  69.         }
  70.         // public override int GetHashCode() => Name.GetHashCode(); //другой вариант
  71.         public override int GetHashCode()
  72.         {
  73.             return HashCode.Combine(Name);
  74.         }
  75.  
  76.         public static bool operator ==(Person obj, Person obj1)
  77.         {
  78.             if (obj.Name == obj1.Name)
  79.                 return true;
  80.             return false;
  81.         }
  82.         public static bool operator !=(Person obj, Person obj1)
  83.         {
  84.             if (obj.Name != obj1.Name)
  85.                 return true;
  86.             return false;
  87.         }
  88.         public override string ToString()//перегруженная версия ToString
  89.             => $"{Name}";
  90.  
  91.     }
  92.     public class Edition
  93.     {
  94.         protected string Name { get; set; }
  95.         protected DateTime Date_of_publish { get; set; }
  96.         protected int circulation;
  97.        
  98.         public int _Circulation
  99.         {
  100.             get { return circulation; }
  101.             set
  102.             {
  103.                 if (value < 0)
  104.                 {
  105.                     throw new ArgumentOutOfRangeException("Circulation must be more or equal 0 ");
  106.                 }
  107.                 else
  108.                 {
  109.                     circulation = value;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         public Edition(string name, DateTime date, int id)
  115.         {
  116.             Name = name;
  117.             Date_of_publish = date;
  118.             circulation = id;
  119.         }
  120.         public Edition()
  121.         {
  122.  
  123.         }
  124.         public Edition(Edition val)
  125.         {
  126.             this.Name = val.Name;
  127.             this.Date_of_publish = val.Date_of_publish;
  128.             this.circulation = val.circulation;
  129.         }
  130.         public virtual object DeepCopy()
  131.         {
  132.             Edition temp = (Edition)this.MemberwiseClone();
  133.             temp.Name = this.Name;
  134.             temp.Date_of_publish = this.Date_of_publish;
  135.             temp.circulation = this.circulation;
  136.             return temp;
  137.         }
  138.  
  139.         public override bool Equals(object obj)
  140.         {
  141.             return obj is Edition edition &&
  142.                    Name == edition.Name &&
  143.                    Date_of_publish == edition.Date_of_publish &&
  144.                    circulation == edition.circulation;
  145.         }
  146.  
  147.         public override int GetHashCode()
  148.         {
  149.             return HashCode.Combine(Name, Date_of_publish, circulation);
  150.         }
  151.  
  152.         public static bool operator ==(Edition obj, Edition obj1)
  153.         {
  154.             if (obj.Name == obj1.Name && obj.circulation == obj1.circulation && obj.Date_of_publish == obj1.Date_of_publish)
  155.                 return true;
  156.             return false;
  157.         }
  158.         public static bool operator !=(Edition obj, Edition obj1)
  159.         {
  160.             if (obj.Name != obj1.Name || obj.circulation != obj1.circulation || obj.Date_of_publish != obj1.Date_of_publish)
  161.                 return true;
  162.             return false;
  163.         }
  164.  
  165.         public override string ToString()//перегруженная версия для формирования строки со значениями всех полей класса
  166.         {
  167.             return string.Format("\nName: {0}\nCirculation: {1}\nPublishDate: {2}\n", Name, circulation, Date_of_publish);
  168.         }
  169.         public string name
  170.         {
  171.             get => Name;
  172.             set => Name = value;
  173.         }
  174.         public DateTime PublishDate
  175.         {
  176.             get => Date_of_publish;
  177.             set => Date_of_publish = value;
  178.         }
  179.         public int Circulation
  180.         {
  181.             get => circulation;
  182.             set => circulation = value;
  183.         }
  184.         //  public virtual string ToShortString()//виртуальный метод toShortString
  185.         //   => $"Name = {Name}"//для формирования строки без списка статей но  со значением среднего рейтинга статей
  186.         // + $"\nPublishDate = {PublishDate}";
  187.         // + $"\nEdition = {Edition}";
  188.     }
  189.     class Magazine : Edition
  190.     {
  191.         //private string name;
  192.         private Frequency frequency;
  193.         //private DateTime publishDate;
  194.         //private int edition;
  195.         //private Person[] editors;
  196.         //private Article[] articles;
  197.         private System.Collections.ArrayList editors = new System.Collections.ArrayList();
  198.         private System.Collections.ArrayList articles = new System.Collections.ArrayList();
  199.  
  200.         public System.Collections.ArrayList ListOfEditors { get { return editors; } set { editors = value; } }
  201.         public System.Collections.ArrayList ListOfArticles { get { return articles; } set { articles = value; } }
  202.        
  203.         public Magazine(
  204.             string name,
  205.             Frequency frequency,
  206.             DateTime publishDate,
  207.             int circ)//конструктор для инициализации соотв-х полей класса
  208.         {
  209.  
  210.             this.Name = name;
  211.             this.frequency = frequency;
  212.             this.Date_of_publish = publishDate;
  213.             this.circulation = circ;
  214.         }
  215.  
  216.         public Magazine()//конструткор без параметров ,иницал-й по умолчанию
  217.         {
  218.         }
  219.         public void AddArticles(params Article[] NewArticles)
  220.         {
  221.             articles.AddRange(NewArticles);
  222.         }
  223.         public void AddEditors(params Person[] NewEditors)
  224.         {
  225.             editors.AddRange(NewEditors);
  226.         }
  227.         //public string Name//свойство типа string
  228.         //{
  229.         //    get => Name;
  230.         //    set => Name = value;
  231.         //}
  232.  
  233.         public Frequency Frequency
  234.         {
  235.             get => frequency;
  236.             set => frequency = value;
  237.         }
  238.  
  239.         public DateTime PublishDate
  240.         {
  241.             get => Date_of_publish;
  242.             set => Date_of_publish = value;
  243.         }
  244.  
  245.         //public int Edition
  246.         //{
  247.         //    get => edition;
  248.         //    set => edition = value;
  249.         //}
  250.  
  251.         //public Article[] Articles
  252.         //{
  253.         //    get => articles;
  254.         //    set => articles = value;
  255.         //}
  256.  
  257.         public double GetAvgRating
  258.         {
  259.             get
  260.             {
  261.                 double result = 0;
  262.                 int count = 0;
  263.                 foreach (Article item in articles)
  264.                 {
  265.                     result += item.Rating;
  266.                     count++;
  267.                 }
  268.                 result /= count;
  269.                 if (count != 0) return result;
  270.                 else return 0;
  271.             }
  272.         }
  273.         public bool this[Frequency frequency]//индексатор булевского типа,true если значение поля равно индексу
  274.         {
  275.             get => Frequency == frequency;
  276.         }
  277.         public override string ToString()
  278.         {
  279.             string stringListOfArticles = "";
  280.             foreach (Article art in articles)
  281.             {
  282.                 stringListOfArticles += art.ToString() + "\r\n";
  283.             }
  284.             string stringListOfEditors = "";
  285.             foreach (Person pers in ListOfEditors)
  286.             {
  287.                 stringListOfEditors += pers.ToString() + "\r\n";
  288.             }
  289.             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);
  290.         }
  291.         public string ToShortString()
  292.         {
  293.             return string.Format("\r\n Name: {0}, PublishDate: {1} \r\n ", Name, Date_of_publish);
  294.         }
  295.         public override object DeepCopy()
  296.         {
  297.             Magazine temp = (Magazine)this.MemberwiseClone();
  298.             temp.Name = this.Name;
  299.             temp.Date_of_publish = this.Date_of_publish;
  300.             temp.circulation = this.circulation;
  301.             temp.frequency = this.frequency;
  302.  
  303.             temp.ListOfArticles = this.ListOfArticles;
  304.             temp.ListOfEditors = this.ListOfEditors;
  305.             return temp;
  306.  
  307.         }
  308.         public Edition GetEdition
  309.         {
  310.             get
  311.             {
  312.                 return new Edition(Name, Date_of_publish, circulation);
  313.             }
  314.             set
  315.             {
  316.                 this.Name = value.name;
  317.                 this.PublishDate = value.PublishDate;
  318.                 this.circulation = value.Circulation;
  319.             }
  320.         }
  321.  
  322.         public IEnumerable<Article> StrInName(string PartOfName)
  323.         {
  324.             foreach (Article art in articles)
  325.             {
  326.                 if (art.Name.Contains(PartOfName))
  327.                 {
  328.                     yield return art;
  329.                     Console.WriteLine(art.ToString());
  330.                 }
  331.             }
  332.         }
  333.         public IEnumerable<Article> HighRateArticles(double Rate)
  334.         {
  335.             for (int i = 0; i < articles.Count; i++)
  336.             {
  337.                 if (((Article)articles[i]).Rating > Rate)
  338.                 {
  339.                     yield return (Article)articles[i];
  340.                     Console.WriteLine(((Article)articles[i]).ToString());
  341.                 }
  342.             }
  343.         }
  344.  
  345.         //public void AddArticles(params Article[] newArticles)//метод для добававления элементов в список статей в журнале
  346.         //{
  347.         //    if (newArticles?.Length == 0)
  348.         //    {
  349.         //        return;
  350.         //    }
  351.  
  352.         //    if (articles == null)
  353.         //    {
  354.         //        articles = Array.Empty<Article>();
  355.         //    }
  356.  
  357.         //    int oldLength = articles.Length;
  358.         //    Array.Resize(ref articles, articles.Length + newArticles.Length);
  359.         //    Array.Copy(newArticles, 0, articles, oldLength, newArticles.Length);
  360.         //}
  361.  
  362.         //public override string ToString()//перегруженная версия для формирования строки со значениями всех полей класса сос писокм статей
  363.         //{
  364.         //    StringBuilder stroka = new StringBuilder();
  365.         //    foreach (var strk in Articles)
  366.         //    {
  367.         //        stroka.Append($"{strk}");
  368.         //    }
  369.         //    return string.Format("\nName: {0}\nFrequency: {1}\nPublishDate: {2, 7} \nEdition: {3, 6}\n\nArticles: {4}", name, Frequency, PublishDate, Edition, stroka);
  370.         //}
  371.         //public virtual string ToShortString()//виртуальный метод toShortString
  372.         //    => $"Name = {Name}"//для формирования строки без списка статей но  со значением среднего рейтинга статей
  373.         //    + $"\nFrequency = {Frequency}"
  374.         //    + $"\nPublishDate = {PublishDate}"
  375.         //    + $"\nEdition = {Edition}";
  376.         //// + $"\nAvg rating = {GetAvgRating()}";
  377.     }
  378. }
  379.  
  380.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement