Sitisom

2 курс Прога лаба 2

Mar 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.43 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7.  
  8. namespace Лабы_по_проге
  9. {
  10.     interface IRateAndCopy
  11.     {
  12.         double Rating { get; }
  13.         object DeepCopy();
  14.     }
  15.     //Класс перечисления
  16.     enum Frequency
  17.     {
  18.         Weekly,
  19.         Monthly,
  20.         Yearly
  21.     }
  22.  
  23.     class Edition
  24.     {
  25.         protected string editionName;
  26.         protected DateTime editionReleaseDate;
  27.         protected int editionQuantity;
  28.  
  29.         public string EditionName { get; set; }
  30.         public DateTime EditionRealeseDate { get; set; }
  31.         public int EditionQuantity
  32.         {
  33.             get { return editionQuantity; }
  34.             set
  35.             {
  36.                 if(value < 0)
  37.                 {
  38.                     throw new Exception("Недопустимое значение тиража!");
  39.                 }
  40.                 else
  41.                 {
  42.                     editionQuantity = value;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         public Edition(string name, DateTime time, int quantity)
  48.         {
  49.             this.editionName = name;
  50.             this.editionReleaseDate = time;
  51.             this.editionQuantity = quantity;
  52.         }
  53.         public Edition() : this("New", new DateTime(2000, 1, 1), 20) { }
  54.  
  55.         public virtual object DeepCopy()
  56.         {
  57.             Edition other = (Edition)MemberwiseClone();
  58.             other.editionName = string.Copy(editionName);
  59.             other.editionReleaseDate = new DateTime(editionReleaseDate.Year, editionReleaseDate.Month, editionReleaseDate.Day);
  60.             other.editionQuantity = editionQuantity;
  61.             return other;
  62.         }
  63.  
  64.         public static bool operator ==(Edition p1, Edition p2)
  65.         {
  66.             return p1.EditionName == p2.EditionName &&
  67.                    p1.EditionRealeseDate == p2.EditionRealeseDate &&
  68.                    p1.EditionQuantity == p2.EditionQuantity;
  69.         }
  70.         public static bool operator !=(Edition p1, Edition p2)
  71.         {
  72.             return p1.EditionName != p2.EditionName &&
  73.                    p1.EditionRealeseDate != p2.EditionRealeseDate &&
  74.                    p1.EditionQuantity != p2.EditionQuantity;
  75.         }
  76.  
  77.         public override bool Equals(object obj)
  78.         {
  79.             return (obj is Edition) &&
  80.                    ((Edition)obj).EditionName == EditionName &&
  81.                    ((Edition)obj).EditionRealeseDate == EditionRealeseDate &&
  82.                    ((Edition)obj).EditionQuantity == EditionQuantity;
  83.         }
  84.  
  85.         public override int GetHashCode()
  86.         {
  87.             return EditionName.Length * EditionQuantity
  88.                 * (editionReleaseDate.Day + EditionRealeseDate.Month + EditionRealeseDate.Year);
  89.         }
  90.  
  91.         public override string ToString()
  92.         {
  93.             return EditionName + ", " + EditionQuantity + ", " + EditionRealeseDate;
  94.         }
  95.     }
  96.  
  97.     class Article : IRateAndCopy
  98.     {
  99.         //Самостоятельные свойства
  100.         public Person Person { get; set; }
  101.         public string ArticleName { get; set; }
  102.         public double Rating { get; set; }
  103.  
  104.         //Конструкторы класса
  105.         public Article(Person person, string articleName, double rating)
  106.         {
  107.             this.Person = person;
  108.             this.ArticleName = articleName;
  109.             this.Rating = rating;
  110.         }
  111.  
  112.         public Article() : this(new Person(), "Bbeppep", 10d) { }
  113.  
  114.         public virtual object DeepCopy()
  115.         {
  116.             Article other = (Article)MemberwiseClone();
  117.             other.Person = (Person)Person.DeepCopy();
  118.             other.ArticleName = string.Copy(ArticleName);
  119.             other.Rating = Rating;
  120.             return other;
  121.         }
  122.  
  123.         public override string ToString()
  124.         {
  125.             return "Made by: " + Person + "\n" +
  126.                    "Article Name: " + ArticleName + "\n" +
  127.                    "Rating: " + Rating;
  128.         }
  129.     }
  130.  
  131.     class MagazineEnumerator: IEnumerator
  132.     {
  133.         List<Article> articles = new List<Article>();
  134.         int position = -1;
  135.         public MagazineEnumerator(List<Article> articles, List<Person> editors)
  136.         {
  137.             foreach(var article in articles)
  138.             {
  139.                 if (!editors.Contains(article.Person))
  140.                 {
  141.                     this.articles.Add(article);
  142.                 }
  143.             }
  144.         }
  145.         public bool MoveNext() // перемещение на одну позицию вперед в контейнере элементов
  146.         {
  147.             if (position < articles.Count - 1)
  148.             {
  149.                 position++;
  150.                 return true;
  151.             }
  152.             else
  153.                 return false;
  154.         }
  155.         public object Current // текущий элемент в контейнере
  156.         {
  157.             get
  158.             {
  159.                 if (position == -1 || position >= articles.Count)
  160.                     throw new InvalidOperationException();
  161.                 return articles[position];
  162.             }
  163.         }  
  164.         public void Reset() // перемещение в начало контейнера
  165.         {
  166.             position = -1;
  167.         }  
  168.     }
  169.     //Класс журнал
  170.     class Magazine : Edition, IRateAndCopy, IEnumerable
  171.     {
  172.         /// <summary>Классс
  173.         /// <para>Класс журнал</para>
  174.         /// </summary>
  175.         //Поля класса
  176.         private string magazineName;
  177.         private Frequency frequency;
  178.         private DateTime releaseDate;
  179.         private int quantity;
  180.         private List<Person> editors = new List<Person>();
  181.         private List<Article> setOfArticles = new List<Article>();
  182.         public IEnumerator GetEnumerator()
  183.         {
  184.             return new MagazineEnumerator(setOfArticles, editors);
  185.         }
  186.  
  187.         //Свойства класса
  188.         public string MagazineName {
  189.             get { return magazineName; }
  190.             set { magazineName = value; }
  191.         }
  192.         public Frequency Frequency
  193.         {
  194.             get { return frequency; }
  195.             set { frequency = value; }
  196.         }
  197.         public DateTime ReleaseDate
  198.         {
  199.             get { return releaseDate; }
  200.             set { releaseDate = value; }
  201.         }
  202.         public int Quantity
  203.         {
  204.             get { return quantity; }
  205.             set { quantity = value; }
  206.         }
  207.         public double Rating { get; set; }
  208.         public List<Article> SetOfArticles
  209.         {
  210.             get { return setOfArticles; }
  211.             set { setOfArticles = value; }
  212.         }
  213.  
  214.         public List<Person> Autors
  215.         {
  216.             get { return editors; }
  217.             set { editors = value; }
  218.         }
  219.  
  220.         public Edition Edition
  221.         {
  222.             get { return new Edition(EditionName, EditionRealeseDate, EditionQuantity); }
  223.             set
  224.             {
  225.                 EditionName = value.EditionName;
  226.                 EditionRealeseDate = value.EditionRealeseDate;
  227.                 EditionQuantity = value.EditionQuantity;
  228.             }
  229.         }
  230.  
  231.         public IEnumerable<Article> ByRating(double Rating)
  232.         {
  233.             foreach (var a in SetOfArticles)
  234.             {
  235.                 if (a.Rating >= Rating)
  236.                     yield return a;
  237.             }
  238.         }
  239.  
  240.         public IEnumerable<Article> ByNameSubstring(string SubString)
  241.         {
  242.             foreach (var a in SetOfArticles)
  243.             {
  244.                 if (a.ArticleName.IndexOf(SubString) > -1)
  245.                     yield return a;
  246.             }
  247.         }
  248.  
  249.         public IEnumerable<Article> ByArticlePersonInEditor()
  250.         {
  251.             foreach (var article in setOfArticles)
  252.             {
  253.                 if (editors.Contains(article.Person))
  254.                 {
  255.                     yield return article;
  256.                 }
  257.             }
  258.         }
  259.  
  260.         //Среднее значение рейтинга
  261.         public double AverageRating
  262.         {
  263.             get
  264.             {
  265.                 double rating = 0;
  266.                 for(int i = 0; i < setOfArticles.Count; i++)
  267.                 {
  268.                     rating += setOfArticles[i].Rating;
  269.                 }
  270.                 return rating / setOfArticles.Count;
  271.             }
  272.         }
  273.  
  274.         //Индексатор
  275.         public bool this [Frequency frequency]
  276.         {
  277.             get { return this.frequency == frequency; }
  278.         }
  279.  
  280.         //Конструкторы класса
  281.         public Magazine(string name, Frequency frequency, DateTime date,
  282.             int quantity, List<Article> setOfArticles)
  283.         {
  284.             this.magazineName = name;
  285.             this.frequency = frequency;
  286.             this.releaseDate = date;
  287.             this.quantity = quantity;
  288.             this.setOfArticles = setOfArticles;
  289.         }
  290.  
  291.         public Magazine() : this("new", Frequency.Monthly, new DateTime(1970, 1, 1), 10, new List<Article>()) { }
  292.  
  293.         //Добавление статей
  294.         public void AddArticles(params Article[] articles)
  295.         {
  296.             foreach(var i in articles)
  297.             {
  298.                 setOfArticles.Add(i);
  299.             }
  300.         }
  301.  
  302.         public void AddEditors(params Person[] editors)
  303.         {
  304.             foreach(var i in editors)
  305.             {
  306.                 this.editors.Add(i);
  307.             }
  308.         }
  309.  
  310.  
  311.         public override object DeepCopy()
  312.         {
  313.             Magazine other = (Magazine)MemberwiseClone();
  314.             other.magazineName = string.Copy(magazineName);
  315.             other.frequency = Frequency;
  316.             other.releaseDate = new DateTime(releaseDate.Year, releaseDate.Day, releaseDate.Month);
  317.             other.quantity = quantity;
  318.             other.setOfArticles = new List<Article>(setOfArticles);
  319.             other.editors = new List<Person>(editors);
  320.             return other;
  321.         }
  322.  
  323.         public override string ToString()
  324.         {
  325.             string articles = "";
  326.             foreach(var article in setOfArticles)
  327.             {
  328.                 articles += article.ArticleName + ", ";
  329.             }
  330.             return "Magazin name: " + magazineName + "\n"+
  331.                    "Frequency: " + frequency + "\n" +
  332.                    "Release date: " + releaseDate + "\n" +
  333.                    "Quantity: " + quantity + "\n"+
  334.                    "Articles: " + articles;
  335.         }
  336.  
  337.         public virtual string ToShortString()
  338.         {
  339.             return "Magazin name: " + magazineName + "\n" +
  340.                    "Frequency: " + frequency + "\n" +
  341.                    "Release date: " + releaseDate + "\n" +
  342.                    "Quantity: " + quantity + "\n" +
  343.                    "Average rating: " + AverageRating;
  344.         }
  345.        
  346.     }
  347.  
  348.     class Person
  349.     {
  350.         private string firstName;
  351.         private string lastName;
  352.         private DateTime birthDate;
  353.  
  354.         public string FirstName {
  355.             get { return firstName; }
  356.             set { firstName = value; }
  357.         }
  358.         public string LastName
  359.         {
  360.             get { return lastName; }
  361.             set { lastName = value; }
  362.         }
  363.         public DateTime BirthDate
  364.         {
  365.             get { return birthDate; }
  366.             set { birthDate = value; }
  367.         }
  368.  
  369.         public int GetBirthYear
  370.         {
  371.             get { return birthDate.Year; }
  372.             set { birthDate = new DateTime(value, birthDate.Day, birthDate.Month); }
  373.         }
  374.  
  375.         public Person() : this("Ivan", "Ivanov", new DateTime(1970, 1, 1)) { }
  376.  
  377.         public Person(string firstName, string lastName, DateTime birthDate)
  378.         {
  379.             this.firstName = firstName;
  380.             this.lastName = lastName;
  381.             this.birthDate = birthDate;
  382.         }
  383.  
  384.         public virtual object DeepCopy()
  385.         {
  386.             Person other = (Person)MemberwiseClone();
  387.             other.firstName = string.Copy(firstName);
  388.             other.lastName = string.Copy(lastName);
  389.             other.birthDate = new DateTime(birthDate.Year, birthDate.Day, birthDate.Month);
  390.             return other;
  391.         }
  392.  
  393.  
  394.         public static bool operator ==(Person p1, Person p2)
  395.         {
  396.             return p1.FirstName == p2.FirstName &&
  397.                    p1.LastName == p2.LastName &&
  398.                    p1.BirthDate == p2.BirthDate;
  399.         }
  400.         public static bool operator !=(Person p1, Person p2)
  401.         {
  402.             return p1.FirstName != p2.FirstName &&
  403.                    p1.LastName != p2.LastName &&
  404.                    p1.BirthDate != p2.BirthDate;
  405.         }
  406.  
  407.         public override bool Equals(object obj)
  408.         {
  409.             return (obj is Person) &&
  410.                    ((Person)obj).FirstName == FirstName &&
  411.                    ((Person)obj).LastName == LastName &&
  412.                    ((Person)obj).BirthDate == BirthDate;
  413.         }
  414.  
  415.         public override int GetHashCode()
  416.         {
  417.             return FirstName.Length * LastName.Length
  418.                 * (BirthDate.Day + BirthDate.Month + BirthDate.Year);
  419.         }
  420.  
  421.         public override string ToString()
  422.         {
  423.             return "First Name: " + firstName + "\n" +
  424.                    "Second Name: " + lastName + "\n" +
  425.                    "Birth Date: " + birthDate;
  426.         }
  427.         public virtual string ToShortString()
  428.         {
  429.             return "First Name: " + firstName + "\n" +
  430.                    "Second Name: " + lastName;
  431.         }
  432.     }
  433.     class Program
  434.     {
  435.        
  436.         static void Main(string[] args)
  437.         {
  438.             Edition e1 = new Edition();
  439.             Edition e2 = new Edition();
  440.             Console.WriteLine(e1.Equals(e2));
  441.             //e2.EditionName = "Cool";
  442.             Console.WriteLine(ReferenceEquals(e1, e2));
  443.             Console.WriteLine("__________________");
  444.             try
  445.             {
  446.                 e1.EditionQuantity = -1;
  447.             }
  448.             catch (Exception e)
  449.             {
  450.                 Console.WriteLine(e);
  451.             }
  452.             Console.WriteLine("__________________");
  453.             Console.WriteLine("MAGAZINE #1");
  454.  
  455.             Magazine m1 = new Magazine();
  456.             m1.AddArticles(new Article(), new Article());
  457.             m1.AddEditors(new Person(), new Person());
  458.             Console.WriteLine(m1);
  459.  
  460.             Console.WriteLine("__________________");
  461.             Console.WriteLine("EDITION #1");
  462.             m1.Edition = new Edition("New", new DateTime(2000, 1, 1), 1000);
  463.             Console.WriteLine(m1.Edition);
  464.  
  465.             Console.WriteLine("__________________");
  466.             Console.WriteLine("MAGAZINE #2 COPY OF #1");
  467.  
  468.             Magazine m2 = new Magazine();
  469.             m2 = (Magazine)m1.DeepCopy();
  470.             Console.WriteLine(m2 == m1);
  471.             Console.WriteLine("Adding new articles...\n");
  472.             m1.AddArticles(new Article(new Person(), "Copy 1", 4), new Article(new Person(), "Copy 2", 5));
  473.             Console.WriteLine(m2);
  474.  
  475.             Console.WriteLine("=============RATING CHECK===========");
  476.             foreach(var i in m1.ByRating(10))
  477.             {
  478.                 Console.WriteLine(i);
  479.             }
  480.             Console.WriteLine("=============SUBSTRING CHECK===========");
  481.             foreach (var i in m1.ByNameSubstring("Bbeppep"))
  482.             {
  483.                 Console.WriteLine(i);
  484.             }
  485.         }
  486.     }
  487. }
Add Comment
Please, Sign In to add comment