Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- namespace Лабы_по_проге
- {
- interface IRateAndCopy
- {
- double Rating { get; }
- object DeepCopy();
- }
- //Класс перечисления
- enum Frequency
- {
- Weekly,
- Monthly,
- Yearly
- }
- class Edition
- {
- protected string editionName;
- protected DateTime editionReleaseDate;
- protected int editionQuantity;
- public string EditionName { get; set; }
- public DateTime EditionRealeseDate { get; set; }
- public int EditionQuantity
- {
- get { return editionQuantity; }
- set
- {
- if(value < 0)
- {
- throw new Exception("Недопустимое значение тиража!");
- }
- else
- {
- editionQuantity = value;
- }
- }
- }
- public Edition(string name, DateTime time, int quantity)
- {
- this.editionName = name;
- this.editionReleaseDate = time;
- this.editionQuantity = quantity;
- }
- public Edition() : this("New", new DateTime(2000, 1, 1), 20) { }
- public virtual object DeepCopy()
- {
- Edition other = (Edition)MemberwiseClone();
- other.editionName = string.Copy(editionName);
- other.editionReleaseDate = new DateTime(editionReleaseDate.Year, editionReleaseDate.Month, editionReleaseDate.Day);
- other.editionQuantity = editionQuantity;
- return other;
- }
- public static bool operator ==(Edition p1, Edition p2)
- {
- return p1.EditionName == p2.EditionName &&
- p1.EditionRealeseDate == p2.EditionRealeseDate &&
- p1.EditionQuantity == p2.EditionQuantity;
- }
- public static bool operator !=(Edition p1, Edition p2)
- {
- return p1.EditionName != p2.EditionName &&
- p1.EditionRealeseDate != p2.EditionRealeseDate &&
- p1.EditionQuantity != p2.EditionQuantity;
- }
- public override bool Equals(object obj)
- {
- return (obj is Edition) &&
- ((Edition)obj).EditionName == EditionName &&
- ((Edition)obj).EditionRealeseDate == EditionRealeseDate &&
- ((Edition)obj).EditionQuantity == EditionQuantity;
- }
- public override int GetHashCode()
- {
- return EditionName.Length * EditionQuantity
- * (editionReleaseDate.Day + EditionRealeseDate.Month + EditionRealeseDate.Year);
- }
- public override string ToString()
- {
- return EditionName + ", " + EditionQuantity + ", " + EditionRealeseDate;
- }
- }
- class Article : IRateAndCopy
- {
- //Самостоятельные свойства
- public Person Person { get; set; }
- public string ArticleName { get; set; }
- public double Rating { get; set; }
- //Конструкторы класса
- public Article(Person person, string articleName, double rating)
- {
- this.Person = person;
- this.ArticleName = articleName;
- this.Rating = rating;
- }
- public Article() : this(new Person(), "Bbeppep", 10d) { }
- public virtual object DeepCopy()
- {
- Article other = (Article)MemberwiseClone();
- other.Person = (Person)Person.DeepCopy();
- other.ArticleName = string.Copy(ArticleName);
- other.Rating = Rating;
- return other;
- }
- public override string ToString()
- {
- return "Made by: " + Person + "\n" +
- "Article Name: " + ArticleName + "\n" +
- "Rating: " + Rating;
- }
- }
- class MagazineEnumerator: IEnumerator
- {
- List<Article> articles = new List<Article>();
- int position = -1;
- public MagazineEnumerator(List<Article> articles, List<Person> editors)
- {
- foreach(var article in articles)
- {
- if (!editors.Contains(article.Person))
- {
- this.articles.Add(article);
- }
- }
- }
- public bool MoveNext() // перемещение на одну позицию вперед в контейнере элементов
- {
- if (position < articles.Count - 1)
- {
- position++;
- return true;
- }
- else
- return false;
- }
- public object Current // текущий элемент в контейнере
- {
- get
- {
- if (position == -1 || position >= articles.Count)
- throw new InvalidOperationException();
- return articles[position];
- }
- }
- public void Reset() // перемещение в начало контейнера
- {
- position = -1;
- }
- }
- //Класс журнал
- class Magazine : Edition, IRateAndCopy, IEnumerable
- {
- /// <summary>Классс
- /// <para>Класс журнал</para>
- /// </summary>
- //Поля класса
- private string magazineName;
- private Frequency frequency;
- private DateTime releaseDate;
- private int quantity;
- private List<Person> editors = new List<Person>();
- private List<Article> setOfArticles = new List<Article>();
- public IEnumerator GetEnumerator()
- {
- return new MagazineEnumerator(setOfArticles, editors);
- }
- //Свойства класса
- public string MagazineName {
- get { return magazineName; }
- set { magazineName = value; }
- }
- public Frequency Frequency
- {
- get { return frequency; }
- set { frequency = value; }
- }
- public DateTime ReleaseDate
- {
- get { return releaseDate; }
- set { releaseDate = value; }
- }
- public int Quantity
- {
- get { return quantity; }
- set { quantity = value; }
- }
- public double Rating { get; set; }
- public List<Article> SetOfArticles
- {
- get { return setOfArticles; }
- set { setOfArticles = value; }
- }
- public List<Person> Autors
- {
- get { return editors; }
- set { editors = value; }
- }
- public Edition Edition
- {
- get { return new Edition(EditionName, EditionRealeseDate, EditionQuantity); }
- set
- {
- EditionName = value.EditionName;
- EditionRealeseDate = value.EditionRealeseDate;
- EditionQuantity = value.EditionQuantity;
- }
- }
- public IEnumerable<Article> ByRating(double Rating)
- {
- foreach (var a in SetOfArticles)
- {
- if (a.Rating >= Rating)
- yield return a;
- }
- }
- public IEnumerable<Article> ByNameSubstring(string SubString)
- {
- foreach (var a in SetOfArticles)
- {
- if (a.ArticleName.IndexOf(SubString) > -1)
- yield return a;
- }
- }
- public IEnumerable<Article> ByArticlePersonInEditor()
- {
- foreach (var article in setOfArticles)
- {
- if (editors.Contains(article.Person))
- {
- yield return article;
- }
- }
- }
- //Среднее значение рейтинга
- public double AverageRating
- {
- get
- {
- double rating = 0;
- for(int i = 0; i < setOfArticles.Count; i++)
- {
- rating += setOfArticles[i].Rating;
- }
- return rating / setOfArticles.Count;
- }
- }
- //Индексатор
- public bool this [Frequency frequency]
- {
- get { return this.frequency == frequency; }
- }
- //Конструкторы класса
- public Magazine(string name, Frequency frequency, DateTime date,
- int quantity, List<Article> setOfArticles)
- {
- this.magazineName = name;
- this.frequency = frequency;
- this.releaseDate = date;
- this.quantity = quantity;
- this.setOfArticles = setOfArticles;
- }
- public Magazine() : this("new", Frequency.Monthly, new DateTime(1970, 1, 1), 10, new List<Article>()) { }
- //Добавление статей
- public void AddArticles(params Article[] articles)
- {
- foreach(var i in articles)
- {
- setOfArticles.Add(i);
- }
- }
- public void AddEditors(params Person[] editors)
- {
- foreach(var i in editors)
- {
- this.editors.Add(i);
- }
- }
- public override object DeepCopy()
- {
- Magazine other = (Magazine)MemberwiseClone();
- other.magazineName = string.Copy(magazineName);
- other.frequency = Frequency;
- other.releaseDate = new DateTime(releaseDate.Year, releaseDate.Day, releaseDate.Month);
- other.quantity = quantity;
- other.setOfArticles = new List<Article>(setOfArticles);
- other.editors = new List<Person>(editors);
- return other;
- }
- public override string ToString()
- {
- string articles = "";
- foreach(var article in setOfArticles)
- {
- articles += article.ArticleName + ", ";
- }
- return "Magazin name: " + magazineName + "\n"+
- "Frequency: " + frequency + "\n" +
- "Release date: " + releaseDate + "\n" +
- "Quantity: " + quantity + "\n"+
- "Articles: " + articles;
- }
- public virtual string ToShortString()
- {
- return "Magazin name: " + magazineName + "\n" +
- "Frequency: " + frequency + "\n" +
- "Release date: " + releaseDate + "\n" +
- "Quantity: " + quantity + "\n" +
- "Average rating: " + AverageRating;
- }
- }
- class Person
- {
- private string firstName;
- private string lastName;
- private DateTime birthDate;
- public string FirstName {
- get { return firstName; }
- set { firstName = value; }
- }
- public string LastName
- {
- get { return lastName; }
- set { lastName = value; }
- }
- public DateTime BirthDate
- {
- get { return birthDate; }
- set { birthDate = value; }
- }
- public int GetBirthYear
- {
- get { return birthDate.Year; }
- set { birthDate = new DateTime(value, birthDate.Day, birthDate.Month); }
- }
- public Person() : this("Ivan", "Ivanov", new DateTime(1970, 1, 1)) { }
- public Person(string firstName, string lastName, DateTime birthDate)
- {
- this.firstName = firstName;
- this.lastName = lastName;
- this.birthDate = birthDate;
- }
- public virtual object DeepCopy()
- {
- Person other = (Person)MemberwiseClone();
- other.firstName = string.Copy(firstName);
- other.lastName = string.Copy(lastName);
- other.birthDate = new DateTime(birthDate.Year, birthDate.Day, birthDate.Month);
- return other;
- }
- public static bool operator ==(Person p1, Person p2)
- {
- return p1.FirstName == p2.FirstName &&
- p1.LastName == p2.LastName &&
- p1.BirthDate == p2.BirthDate;
- }
- public static bool operator !=(Person p1, Person p2)
- {
- return p1.FirstName != p2.FirstName &&
- p1.LastName != p2.LastName &&
- p1.BirthDate != p2.BirthDate;
- }
- public override bool Equals(object obj)
- {
- return (obj is Person) &&
- ((Person)obj).FirstName == FirstName &&
- ((Person)obj).LastName == LastName &&
- ((Person)obj).BirthDate == BirthDate;
- }
- public override int GetHashCode()
- {
- return FirstName.Length * LastName.Length
- * (BirthDate.Day + BirthDate.Month + BirthDate.Year);
- }
- public override string ToString()
- {
- return "First Name: " + firstName + "\n" +
- "Second Name: " + lastName + "\n" +
- "Birth Date: " + birthDate;
- }
- public virtual string ToShortString()
- {
- return "First Name: " + firstName + "\n" +
- "Second Name: " + lastName;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Edition e1 = new Edition();
- Edition e2 = new Edition();
- Console.WriteLine(e1.Equals(e2));
- //e2.EditionName = "Cool";
- Console.WriteLine(ReferenceEquals(e1, e2));
- Console.WriteLine("__________________");
- try
- {
- e1.EditionQuantity = -1;
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- Console.WriteLine("__________________");
- Console.WriteLine("MAGAZINE #1");
- Magazine m1 = new Magazine();
- m1.AddArticles(new Article(), new Article());
- m1.AddEditors(new Person(), new Person());
- Console.WriteLine(m1);
- Console.WriteLine("__________________");
- Console.WriteLine("EDITION #1");
- m1.Edition = new Edition("New", new DateTime(2000, 1, 1), 1000);
- Console.WriteLine(m1.Edition);
- Console.WriteLine("__________________");
- Console.WriteLine("MAGAZINE #2 COPY OF #1");
- Magazine m2 = new Magazine();
- m2 = (Magazine)m1.DeepCopy();
- Console.WriteLine(m2 == m1);
- Console.WriteLine("Adding new articles...\n");
- m1.AddArticles(new Article(new Person(), "Copy 1", 4), new Article(new Person(), "Copy 2", 5));
- Console.WriteLine(m2);
- Console.WriteLine("=============RATING CHECK===========");
- foreach(var i in m1.ByRating(10))
- {
- Console.WriteLine(i);
- }
- Console.WriteLine("=============SUBSTRING CHECK===========");
- foreach (var i in m1.ByNameSubstring("Bbeppep"))
- {
- Console.WriteLine(i);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment